6

I am trying to create ATA of a user using my own account and send custom tokens to that ATA. Using the following code:


  const recieverATA = await getAssociatedTokenAddress(
    tokenMintAddress,
    recieverPublicAddress
  );

  let createRecATA = new Transaction().add(
    createAssociatedTokenAccountInstruction(
      wallet.pubkey,
      recieverATA,
      recieverPublicAddress,
      tokenMintAddress
    )
  );


 const trx = await sendTransaction(createRecATA,connection);

I am getting the following error:

 Cannot read properties of undefined (reading 'toString')
    at PhantomWalletAdapter.sendTransaction (adapter.ts:233:1)
    at async WalletProvider.tsx:283:1
    at async awardCustomToken (utils.js:725:1)
    at async _awardCustomToken (App.js:317:1)

Is there any way to resolve this?

3 Answers 3

1

The issue in the code was that I was using wallet.pubkey instead of wallet.publicKey.

0

How about to check ATA already exist?

export async function accountExists(
    provider: Provider,
    account: PublicKey,
    programId: PublicKey = TOKEN_PROGRAM_ID): Promise<boolean> {
    const info = await provider.connection.getAccountInfo(account);
    return !(info === null || !info.owner.equals(programId));
}

....

const tx = new web3.Transaction();
if(!(await accountExists(provider, recieverATA)) {
  tx.add(createAssociatedTokenAccountInstruction(...));
}

...
const trx = await sendTransaction(createRecATA,connection);
1
  • I have checked this. the ATA is not created for the account
    – fahad
    Sep 5, 2022 at 9:33
0

Change the last line to:

const trx = await anchor.web3.sendAndConfirmTransaction(
   createRecATA, connection, [`wallet`])

The third argument for this transaction are its signers. Here we add wallet as a signer because it pays for and signs the creation of the ATA.

1
  • Got the following error TypeError: connection.sendTransaction is not a function at Module.sendAndConfirmTransaction (send-and-confirm-transaction.ts:31:1) at _awardCustomToken (App.js:506:1)
    – fahad
    Sep 5, 2022 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.