Reputation: 21
I need to automatically transfer NFT from one wallet to another. I'm using python and couldn't find information anywhere on how to do this. Here is an example transaction https://solscan.io/tx/4JaLzHB44rBA74YiFV2kjDeVu79qWdvcwDiuDdwML9XtysXHchwWwMN4KuGuJph83m8JH56cgXEMMCC2u3cuxqPM . The only thing I could find is this How to transfer custom token by '@solana/web3.js' but I'm very far from JS. As I understand it, I need to create something like splToken.Token.createTransferInstruction
but I could not find an analogue for python. And I also don’t understand what token they are transferring, because I don’t see the token id anywhere. If someone can help - I will be very grateful. thanks in advance.
Upvotes: 2
Views: 6086
Reputation: 8462
In python, you can use the SPL token client included in the solana-py
repo at https://github.com/michaelhly/solana-py/tree/master/src/spl/token
To do the transfer, based on the example transaction that you linked, you use:
Crh6XxeJoKGVbCKaCXzD57pkUv5vYwgW3acnKWP91bWV
AqCV7nMqWY8TnN9VqNrdvGNcchHjkbZfjSoLXSTeE9Fb
vLswbP3WbNRyE4FBaj7GdQj3hG5CKgJGw9fEGaA165z
5j22en4YDzDNzmGm7WWVxxYGDQ3Y873p7joVbKncZ1Ke
This gives you:
from spl.token.constants import TOKEN_PROGRAM_ID
from spl.token.instructions import transfer_checked, TransferCheckedParams
from solana.rpc.commitment import Confirmed
from solana.rpc.api import Client
from solana.rpc.types import TxOpts
from solana.keypair import Keypair
from solana.publickey import PublicKey
from solana.transaction import Transaction
transaction = Transaction()
transaction.add(
transfer_checked(
TransferCheckedParams(
program_id=TOKEN_PROGRAM_ID,
source=new PublicKey("AqCV7nMqWY8TnN9VqNrdvGNcchHjkbZfjSoLXSTeE9Fb"),
mint=new PublicKey("Crh6XxeJoKGVbCKaCXzD57pkUv5vYwgW3acnKWP91bWV"),
dest=new PublicKey("vLswbP3WbNRyE4FBaj7GdQj3hG5CKgJGw9fEGaA165z"),
owner=new PublicKey("5j22en4YDzDNzmGm7WWVxxYGDQ3Y873p7joVbKncZ1Ke"),
amount=1,
decimals=0,
signers=[]
)
)
)
client = Client(endpoint="https://api.mainnet-beta.solana.com", commitment=Confirmed)
owner = Keypair() # <-- need the keypair for the token owner here! 5j22en4YDzDNzmGm7WWVxxYGDQ3Y873p7joVbKncZ1Ke
client.send_transaction(
transaction, owner, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed))
Here's an integration test using transfer_checked
, same as the SolScan transaction that you linked: https://github.com/michaelhly/solana-py/blob/f41f020938d1fb257142f18608bcb884adb54479/tests/integration/test_token_client.py#L231
Upvotes: 2