【发布时间】:2022-09-23 05:35:57
【问题描述】:
我有这段代码使用 spl-token 0.2.x 传输令牌。
如何在 0.1.8 中使用相同的代码?根据我对文档的理解,两者之间没有重大变化,但旧版本使用 Token 类,但我不确定如何为 getOrCreateAssociatedTokenAccount 和 transfer 函数调用它。
async function transferToken(endpoint: string, fromWallet: Keypair, address_to: string, token_id: string)
{
const connection = new Connection(endpoint);
const toWalletPublicKey = new PublicKey(address_to);
const mint_key = new PublicKey(token_id);
// From
const from = [connection, fromWallet, mint_key, fromWallet.publicKey];
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(...from);
// To
const to = [connection, fromWallet, mint_key, toWalletPublicKey];
const toTokenAccount = await getOrCreateAssociatedTokenAccount(...to);
// Transfer
const transferParams = [connection, fromWallet, fromTokenAccount.address, toTokenAccount.address, fromWallet.publicKey, 1, []];
return await transfer(...transferParams);
}
这就是我传递从十六进制字符串加载的fromWallet KeyPair 的方式。
const fromWallet = Keypair.fromSecretKey(Uint8Array.from(Buffer.from(private_key, \'hex\')));
标签: javascript typescript solana