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.
Asked
Active
Viewed 2,473 times
1

Sloopaska
- 11
- 2
1 Answers
2
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:
- token id / mint:
Crh6XxeJoKGVbCKaCXzD57pkUv5vYwgW3acnKWP91bWV
- source account:
AqCV7nMqWY8TnN9VqNrdvGNcchHjkbZfjSoLXSTeE9Fb
- destination account:
vLswbP3WbNRyE4FBaj7GdQj3hG5CKgJGw9fEGaA165z
- source account owner / authority:
5j22en4YDzDNzmGm7WWVxxYGDQ3Y873p7joVbKncZ1Ke
- amount: 1
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

Jon C
- 7,019
- 10
- 17
-
2While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/30974266) – Artem Feb 02 '22 at 21:47
-
2Noted, edited to add a full example – Jon C Feb 03 '22 at 11:33
-
I have updated the answer, This answer is no longer valid. Its a great starting point though. https://stackoverflow.com/questions/72665347/solana-transaction-using-python/73264835#73264835 – Koops Aug 07 '22 at 04:13