0

So I'm trying to create a transaction in a React App that bundles multiple instructions. The fromWallet is captured using the wallet adapter and the useAnchorWallet hook. The toWallet is specified with a web3 PublicKey that I'm specifying. My example will show attempting to send two nfts at once. The end goal would be sending 3 nfts and a certain amount of a fungible token.

So far, I have a working transaction with either a fungible token (for which I know the token account of the receiver) and with an NFT for which I derive the ata. But only one at a time, not together.

const transaction = new Transaction().add(
      
      spltoken.createAssociatedTokenAccountInstruction(
        fromWallet.publicKey, // payer
        ataNft1, // ata
        toWallet, // owner
        nft1, // mint
        spltoken.TOKEN_PROGRAM_ID,
        spltoken.ASSOCIATED_TOKEN_PROGRAM_ID
      ),
      spltoken.createTransferInstruction(
        nftAccount1.value[0].pubkey,
        ataNft1,
        fromWallet.publicKey,
        1,
        [],
        spltoken.TOKEN_PROGRAM_ID
      ),

But when I go to add another nft to the bundle, I get a failed to simulate transaction. So here is an example of the failure.

const transaction = new Transaction().add(
      
      spltoken.createAssociatedTokenAccountInstruction(
        fromWallet.publicKey, // payer
        ataNft1, // ata
        toWallet, // owner
        nft1, // mint
        spltoken.TOKEN_PROGRAM_ID,
        spltoken.ASSOCIATED_TOKEN_PROGRAM_ID
      ),
      spltoken.createTransferInstruction(
        nftAccount1.value[0].pubkey,
        ataNft1,
        fromWallet.publicKey,
        1,
        [],
        spltoken.TOKEN_PROGRAM_ID
      ),
      spltoken.createAssociatedTokenAccountInstruction(
        fromWallet.publicKey, // payer
        ataNft2, // ata
        toWallet, // owner
        nft2, // mint
        spltoken.TOKEN_PROGRAM_ID,
        spltoken.ASSOCIATED_TOKEN_PROGRAM_ID
      ),
       spltoken.createTransferInstruction(
         nftAccount2.value[0].pubkey,
         ataNft2,
         fromWallet.publicKey,
         1,
         [],
         spltoken.TOKEN_PROGRAM_ID
       ),

I've also tried creating all the associated token account instructions first, then the transfer instructions like shown below.

const transaction = new Transaction().add(
      
      spltoken.createAssociatedTokenAccountInstruction(
        fromWallet.publicKey, // payer
        ataNft1, // ata
        toWallet, // owner
        nft1, // mint
        spltoken.TOKEN_PROGRAM_ID,
        spltoken.ASSOCIATED_TOKEN_PROGRAM_ID
      ),
       spltoken.createAssociatedTokenAccountInstruction(
        fromWallet.publicKey, // payer
        ataNft2, // ata
        toWallet, // owner
        nft2, // mint
        spltoken.TOKEN_PROGRAM_ID,
        spltoken.ASSOCIATED_TOKEN_PROGRAM_ID
      ),
      spltoken.createTransferInstruction(
        nftAccount1.value[0].pubkey,
        ataNft1,
        fromWallet.publicKey,
        1,
        [],
        spltoken.TOKEN_PROGRAM_ID
      ),
       spltoken.createTransferInstruction(
         nftAccount2.value[0].pubkey,
         ataNft2,
         fromWallet.publicKey,
         1,
         [],
         spltoken.TOKEN_PROGRAM_ID
       ),
    );

I'm still learning everyday both react and working with the solana/web3js and spl-token libraries. Any nudge in the correct direction would be very helpful! I'm sure there is a better way to accomplish what I'm trying to accomplish. Thank you!

UPDATE: I have solved my issue. When getting the ATA from the mint/wallet, I didn't specify allowOffCurve. This solved my issue.

0 Answers0