The solders code seems a bit raw currently. There seems to be instructions only for SystemProgram
.
I'm not sure what are the other design ideas being but Solana clients For transferring SOL you need to create an instruction of the SystemProgram
, place it to a transaction, sign it (if needed, signature of the wallet could be added automatically, not sure though how it is here) and send it to network.
Based on the documentation I tried like this
import json
import requests
import json
from solana.rpc.api import Client
from solders.message import Message # type: ignore
from solders.transaction import Transaction # type: ignore
from solders.system_program import transfer, TransferParams
from solders.pubkey import Pubkey as PublicKey # type: ignore
from solders.keypair import Keypair # type: ignore
from solders.hash import Hash # type: ignore
from solders.commitment_config import CommitmentLevel # type: ignore
from solders.rpc.config import RpcSendTransactionConfig # type: ignore
from solders.rpc.requests import SendLegacyTransaction # type: ignore
SOL_TO_LAMPORTS = 1000000000
to = PublicKey.from_string("7xUpLb33Bp3yGKVsARWzAQiYanXL1ujx3136qoJCLWXN")
instruction = transfer(
TransferParams(
from_pubkey = loaded_keypair.pubkey(),
to_pubkey = PublicKey.from_string("<some_pubkey_base58"),
lamports = SOL_TO_LAMPORTS
)
)
# solana api to get recent blockhash
solana_client = Client(args.rpc, timeout=30)
recent_blockhash = solana_client.get_recent_blockhash()
blockhash = Hash.from_string(recent_blockhash['result']['value']['blockhash'])
message = Message([instruction], loaded_keypair.pubkey())
tx = Transaction([loaded_keypair], message, blockhash)
commitment = CommitmentLevel.Confirmed
config = RpcSendTransactionConfig(preflight_commitment=commitment)
req = SendLegacyTransaction(tx, config)
as_json = req.to_json() # as_json returns a string not a json
print('sending', SOL_TO_LAMPORTS, 'to', to)
res = requests.post(args.rpc, json=json.loads(as_json))
print(res.json())
For sending there seems not being a direct integration to solana.rpc.api.Client
and the transaction can be get as a JSON to be sent via HTTP request.