1

Problem with solana-wallet-adapter

I am developing a Dapp with a token I created, I use the React Wallet Adapter provided by solana (solana-wallet-adapter). I am using javascript, but when I use the getOrCreateTokenAccount() method, I get an error. It is worth mentioning that I am sending the wallet address from the wallet adapter, but that is not web3.keypair but WalletContextState. I tried to send the transaction but it fails.

Snippets of my code

Creating the token account.

async function createTokenAccount(){
    const minted = await getMint(connection,tokenContract)
    const tx = await getOrCreateAssociatedTokenAccount(
        connection,
        wallet,
        minted,
        wallet.publicKey);
    console.log(tx)
}

Resulting error:

Uncaught (in promise) TypeError: mint.toBuffer is not a function
getAssociatedTokenAddress mint.ts:123
getOrCreateAssociatedTokenAccount getOrCreateAssociatedTokenAccount.ts:46
createTokenAccount conectWallet.js:78
React 23
js index.js:29
factory react refresh:6
Webpack 3

Changes with the new code (Edit 1)

I made these changes with the suggestion. But now it gives the following error.

New code:

const mint = await getMint(connection, tokenContract);
const tx = await getOrCreateAssociatedTokenAccount(
  connection,
  wallet,
  mint.address,
  wallet.publicKey
);

Resulting error:

Uncaught (in promise) TokenAccountNotFoundError: 
    TokenError errors.ts:4
    TokenAccountNotFoundError bundle.js:134630
    getAccount account.ts:93
5
  • 1
    Could you show the errors
    – C.OG
    Commented Jul 30, 2022 at 10:59
  • This is the error Uncaught (in promise) TypeError: mint.toBuffer is not a function getAssociatedTokenAddress mint.ts:123 getOrCreateAssociatedTokenAccount getOrCreateAssociatedTokenAccount.ts:46 createTokenAccount conectWallet.js:78 React 23 js index.js:29 factory react refresh:6 Webpack 3
    – KinimoX
    Commented Jul 30, 2022 at 23:09
  • 1
    You might consider deleting the Rust code in this question and posting it as a separate question so that this doesn’t get closed as ‘lacking focus.’ Commented Jul 31, 2022 at 6:07
  • please update the post rather than post the error message as a comment. it's unreadable this way
    – trent.sol
    Commented Aug 2, 2022 at 2:24
  • Well there I put that I had edited (edit 1), the previous error is because of the comment that I was asked for the output. If you tell me what is illegible, I will gladly correct it.
    – KinimoX
    Commented Aug 2, 2022 at 4:52

2 Answers 2

1

The return type of getMint is a Mint. The third parameter of getOrCreateAssociatedTokenAccount is a PublicKey.

Change your invocation to this:

const mint = await getMint(connection, tokenContract);
const tx = await getOrCreateAssociatedTokenAccount(
  connection,
  wallet,
  mint.address,
  wallet.publicKey
);
3
  • Make sure that you enable the Typescript typechecker in your editor. It will point out errors like this one without you having to run the code. Commented Jul 31, 2022 at 6:05
  • In fact, I don’t think you need to getMint at all, because isn’t tokenContract the same as mint.address here? Commented Jul 31, 2022 at 6:34
  • Look at line 93 of account.ts: github.com/jordansexton/spl-token-js/blob/… Matching up your input arguments, that implies that wallet does not exist. Either: 1. It does not exist, or 2. Your connection is pointing to the wrong cluster (eg. mainnet-beta, when in fact your account is on devnet). Commented Aug 2, 2022 at 4:41
0

You need to get the account assigned to the token, in the wallet. For this use the following code:

const associatedAccount = await getAssociatedTokenAddress(
            mint.address,
            wallet.publicKey
        )

Once, the account is stored. I had to create and send the transaction instruction.

 const createToken = new Transaction().add(
            createAssociatedTokenAccountInstruction(
                wallet.publicKey,
                associatedAccount,
                wallet.publicKey,
                mint.address
            )
        );

And that's it, all that's left is to approve the transaction in the wallet. Done, we have successfully added the token.

One small note, depending on your wallet, it will show the token. In Phantom, it doesn't show it until you already have some amount in it, but solflare does show the token address.

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.