2

I am still new to this and have been trying to figure this out for some time now so any help you can offer would really be greatly appreciated.

I see quite a bit of literature and tutorials on how to transfer Sol using Solana.py (which no longer works) and I have been combing through the Solders docs trying to put it together but can't get it.

Does anyone have an idea of how a function to transfer Sol from one wallet to another using Solders would look?

https://kevinheavey.github.io/solders/api_reference/transaction.html

Thank you for all your help!

3 Answers 3

1

Here's an example from the solana-py docs (lightly edited):

from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solana.rpc.api import Client
from solders.system_program import TransferParams, transfer
from solana.transaction import Transaction
sender = Keypair.from_bytes(b"") # replace with a real keypair
receiver = Keypair().pubkey()
transfer_ix = transfer(TransferParams(from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1_000_000))
txn = Transaction().add(transfer_ix)
solana_client = Client("http://localhost:8899") # replace with a real RPC
solana_client.send_transaction(txn, sender)
1
  • Thank you so much. After playing with this for awhile I finally got it to work. I really appreciate your help. Commented Mar 21, 2023 at 8:28
1

I will be changing this message as I can not delete accepted answers

4
  • Thank you so much. I really appreciate your help. Commented Mar 17, 2023 at 4:22
  • Is VSCode giving you trouble with the Solders types or something?
    – KevinH
    Commented Mar 17, 2023 at 13:44
  • not vscode, mypy
    – gattytto
    Commented Mar 17, 2023 at 14:18
  • Yeah, I get "TypeError: Transaction.__new__() missing 3 required positional arguments: 'from_keypairs', 'message', and 'recent_blockhash'" when I run your code. Commented Mar 19, 2023 at 10:09
-1

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.

1
  • Ok, thanks @kevinh for the clarification of the usage.
    – chalda
    Commented Mar 21, 2023 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.