我需要自动将NFT从一个钱包转移到另一个钱包。我正在使用python,并且在任何地方都找不到关于如何做到这一点的信息。下面是一个事务https://solscan.io/tx/4JaLzHB44rBA74YiFV2kjDeVu79qWdvcwDiuDdwML9XtysXHchwWwMN4KuGuJph83m8JH56cgXEMMCC2u3cuxqPM示例。我唯一能找到的就是这个How to transfer custom token by '@solana/web3.js',但我离JS很远。据我所知,我需要创建类似于splToken.Token.createTransferInstruction的东西,但我无法为python找到类似的东西。我也不明白他们在传递什么令牌,因为我在任何地方都看不到令牌id。如果有人能帮忙-我会非常感激的。提前谢谢。
发布于 2022-02-01 20:41:21
在python中,可以使用solana-py回购( at https://github.com/michaelhly/solana-py/tree/master/src/spl/token )中包含的SPL令牌客户端。
若要根据链接的示例事务执行传输,请使用:
Crh6XxeJoKGVbCKaCXzD57pkUv5vYwgW3acnKWP91bWV
AqCV7nMqWY8TnN9VqNrdvGNcchHjkbZfjSoLXSTeE9Fb
vLswbP3WbNRyE4FBaj7GdQj3hG5CKgJGw9fEGaA165z
5j22en4YDzDNzmGm7WWVxxYGDQ3Y873p7joVbKncZ1Ke
这给了你:
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))下面是一个使用transfer_checked的集成测试,与您链接的SolScan事务相同:https://github.com/michaelhly/solana-py/blob/f41f020938d1fb257142f18608bcb884adb54479/tests/integration/test_token_client.py#L231
https://stackoverflow.com/questions/70923875
复制相似问题