通过Metaplex(糖果机)转移NFT

发布于 2025-01-31 23:49:22 字数 1355 浏览 4 评论 0原文

来自Metaplex(Candy Machine)的铸造NFT会自动生成令牌帐户(因为我打算通过JavaScript将其传输给其他所有者)。另外,我正在使用函数getorCreateAssciatedTokenAccount来创建一个令牌帐户,但是我的问题是需要Keypair,我不知道如果我只有一个钱包地址,我不知道在哪里可以找到?这是我的代码:

   // wallet address of the owner of NFT but I want to get the keypair here since it is the one that is required for getOrCreateAssociatedTokenAccount
    const fromWallet = owner_wallet;
    // Public Key of buyer
    const toWallet = buyer_wallet;
   // NFT mint address
   let mint = mint_address;
   //connection
   const connection = use_connection;

   // Get the token account of the toWallet address, and if it does not exist, create it
        const toTokenAccount = await getOrCreateAssociatedTokenAccount(connection, fromWallet, mint, toWallet);

  // Get the token account of the fromWallet address, and if it does not exist, create it (my problem here is 2nd parameter is looking for keypair but i can only provide a wallet address
  const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
        connection,
        fromWallet,
        mint,
        fromWallet.publicKey
    );

  const signature = await transfer(
        connection,
        fromWallet,
        fromTokenAccount.address,
        toTokenAccount.address,
        fromWallet.publicKey,
        0
    );
    console.log(`finished transfer with ${signature}`);

Does the minted NFT from Metaplex(candy machine) automatically generate the Token Account (as I am planning to transfer it to other owner via Javascript). Also, i am using the function getOrCreateAssociatedTokenAccount to create a token account but my problem is it needs keypair and I dont know where to locate if I a only have a wallet address? Here is my code:

   // wallet address of the owner of NFT but I want to get the keypair here since it is the one that is required for getOrCreateAssociatedTokenAccount
    const fromWallet = owner_wallet;
    // Public Key of buyer
    const toWallet = buyer_wallet;
   // NFT mint address
   let mint = mint_address;
   //connection
   const connection = use_connection;

   // Get the token account of the toWallet address, and if it does not exist, create it
        const toTokenAccount = await getOrCreateAssociatedTokenAccount(connection, fromWallet, mint, toWallet);

  // Get the token account of the fromWallet address, and if it does not exist, create it (my problem here is 2nd parameter is looking for keypair but i can only provide a wallet address
  const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
        connection,
        fromWallet,
        mint,
        fromWallet.publicKey
    );

  const signature = await transfer(
        connection,
        fromWallet,
        fromTokenAccount.address,
        toTokenAccount.address,
        fromWallet.publicKey,
        0
    );
    console.log(`finished transfer with ${signature}`);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2025-02-07 23:49:22
  • Metaplex(Candy Machine)的铸造NFT会自动生成令牌帐户吗?是
  • (因为我打算通过JavaScript将其转移给其他所有者) - >您需要 /应该创建一个新的令牌帐户,而不仅仅是传输令牌帐户的所有权
  • ,我还使用函数getorCreateAssssciatedTokenAccount来创建一个令牌帐户,但我的问题是它需要Keypair,我不知道在哪里可以找到是否只找到有钱包地址? - >您需要使用NFT当前持有人的钱包的钱包 /关键。

这个问题与糖果机无关,而与转移NFT有关。在这里看看如何转移令牌: https ://solanacookbook.com/references/basic-transactions.html#how-to-to-send-spl-tokens

  • Does the minted NFT from Metaplex(candy machine) automatically generate the Token Account --> Yes
  • (as I am planning to transfer it to other owner via Javascript) --> you need / should create a new token account and not just transfer the ownership of the token account
  • Also, i am using the function getOrCreateAssociatedTokenAccount to create a token account but my problem is it needs keypair and I dont know where to locate if I a only have a wallet address? --> You need to use the wallet / keypair of the wallet that is the current holder of the NFT.

This question is not related to candy machine but to transferring NFTs. Have a look here to learn how to transfer tokens: https://solanacookbook.com/references/basic-transactions.html#how-to-send-spl-tokens

伴我心暖 2025-02-07 23:49:22

我不知道回答是否为时已晚,但是如果有人需要它们,我只是把它放在这里。
我假设您正在从前端使用连接的钱包。因此,您需要做的是:

TOKEN_PUBLIC_KEY = new anchor.web3.PublicKey(TOKEN_PUBLIC_KEY); // NFT Mint Address
RECEIVER_PUBLIC_KEY = new anchor.web3.PublicKey(OWNER_PUBLIC_KEY); // Address of the receiver of the NFT 
const receiverTokenAccount = getAssociatedTokenAddressSync(
  TOKEN_PUBLIC_KEY,
  RECEIVER_PUBLIC_KEY
);
const oAccountInfo = await connection.getParsedAccountInfo(ATA);
// This is a check if the receiver already has an associated account for storing the token if not it will create one first.
if (oAccountInfo.value == null) {
  const tx = new anchor.web3.Transaction().add(
    createAssociatedTokenAccountInstruction(
      wallet.publicKey,
      receiverTokenAccount,
      wallet.publicKey,
      TOKEN_PUBLIC_KEY
    )
  );
  await sProvider.sendAndConfirm(tx); // This will send the transaction with your connected wallet as signer
}

getorCreateAsssciedTokenAccount函数替换它,您不需要键盘,但是您需要一个连接的钱包。

另外,您还必须使用说明来转移Sph-Token。

I don't know if it is too late to answer but I will just put it here in case anyone needs them.
I assume you are using a connected wallet for this from the frontend. So what you need to do is:

TOKEN_PUBLIC_KEY = new anchor.web3.PublicKey(TOKEN_PUBLIC_KEY); // NFT Mint Address
RECEIVER_PUBLIC_KEY = new anchor.web3.PublicKey(OWNER_PUBLIC_KEY); // Address of the receiver of the NFT 
const receiverTokenAccount = getAssociatedTokenAddressSync(
  TOKEN_PUBLIC_KEY,
  RECEIVER_PUBLIC_KEY
);
const oAccountInfo = await connection.getParsedAccountInfo(ATA);
// This is a check if the receiver already has an associated account for storing the token if not it will create one first.
if (oAccountInfo.value == null) {
  const tx = new anchor.web3.Transaction().add(
    createAssociatedTokenAccountInstruction(
      wallet.publicKey,
      receiverTokenAccount,
      wallet.publicKey,
      TOKEN_PUBLIC_KEY
    )
  );
  await sProvider.sendAndConfirm(tx); // This will send the transaction with your connected wallet as signer
}

Replace this with your getOrCreateAssociatedTokenAccount function you won't need a keypair but ya you need a connected wallet.

Also you have to use instructions for transfer of SPL-token.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文