简体   繁体   English

Solana,如何使用 javascript/wallets 将 NFT(spl 代币)发送到另一个钱包

[英]Solana, how to send a NFT (spl token) to another wallet using javascript/wallets

I'm trying to figure out how to send an NFT and display it on a website.我试图弄清楚如何发送 NFT 并将其显示在网站上。 Ideally the NFT can be dropped on the account linked to the website and therefore "belongs" to the website at least for a while, this is important for my project because I need to be able to burn the nft or send it back to the user.理想情况下,NFT 可以放在链接到网站的帐户上,因此至少在一段时间内“属于”网站,这对我的项目很重要,因为我需要能够烧掉 nft 或将其发回给用户. Actually im using @project-serum/anchor and @solana/web3.js but I can't even get a transfer between the user's wallet and another wallet.实际上我正在使用@project-serum/anchor 和@solana/web3.js,但我什至无法在用户的钱包和另一个钱包之间进行转账。

I've spent all day trying to figure out how to do it but I can't get a result.我花了一整天的时间试图弄清楚如何做到这一点,但我无法得到结果。 Ideally I would have a button that opens the wallet and shows the different NFT that can be deposited, the user chooses one that is sent to the wallet linked to the site.理想情况下,我会有一个按钮来打开钱包并显示可以存入的不同 NFT,用户选择一个发送到链接到该站点的钱包的 NFT。

I thank you in advance我提前谢谢你

Try this code:试试这个代码:

// This transaction is sending the tokens
var transaction = new web3.Transaction().add(
  splToken.Token.createTransferInstruction(
    splToken.TOKEN_PROGRAM_ID,
    fromTokenAccount.address,
    toTokenAccount.address,
    fromWallet.publicKey,
    [],
    1000000, // This is transferring 1 token, not 1000000 tokens
  ),
);

var signature = await web3.sendAndConfirmTransaction(
  connection,
  transaction,
  [fromWallet],
  {commitment: 'confirmed'},
);

console.log("SIGNATURE: ", signature);
let tokenBalance = await toTokenAccount.amount;
console.log("token balance: ", tokenBalance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM