2

I'm trying to close token accounts using the wallet adapter,I'm facing the following issues

  const closeAcc = async (tokenpubkey: PublicKey) => {
    if (wallet.publicKey) {
      let tx = new Transaction().add(
        createCloseAccountInstruction(
          tokenpubkey, // token account which you want to close
          wallet.publicKey, // destination
          wallet.publicKey // owner of token account
        )
      );

      if (wallet && wallet.signTransaction) {
        let blockhash = (await connection.getLatestBlockhash('finalized')).blockhash;
        tx.recentBlockhash = blockhash;
        tx.feePayer = wallet.publicKey;
        let signature = await wallet.sendTransaction(tx,connection);
        await connection.confirmTransaction(signature, "processed");
      }
    }
  };

Issue 1) On mainnet it works fine, but on devnet I get this error "Transaction expired. You waited too long to approve the transaction and it expire" and "Error processing instruction 0: INvalid account data for instruction"

Issue 2) connection.confirmTransaction shows up as deprecated, what is the right thing to use now?

2 Answers 2

2

Okay, here is how I resolved these

  1. Did not realise I have to first switch networks inside of phantom wallet if I want to work with devnet apps
  2. This is the proper way
    if (wallet.publicKey) {
      const latestBlockhash = await connection.getLatestBlockhash();
      let tx = new Transaction(latestBlockhash).add(
        createCloseAccountInstruction(
          tokenpubkey, // token account which you want to close
          wallet.publicKey, // destination
          wallet.publicKey // owner of token account
        )
      );

      if (wallet && wallet.signTransaction) {
        const signature = await wallet.sendTransaction(tx, connection);
        await connection.confirmTransaction({
          ...latestBlockhash,
          signature,
        });
      }
    }
    ```

 
1

For issue 1, be sure that tokenpubkey also exists on devnet. Most likely you're mixing devnet and mainnet connections.

For issue 2, the deprecation notice at https://github.com/solana-labs/solana-web3.js/blob/6524b01189cd4917da62fe78a33fef58bd692986/packages/library-legacy/src/connection.ts#L3711 says exactly what to do:

  /** @deprecated Instead, call `confirmTransaction` and pass in {@link TransactionConfirmationStrategy} */
0

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.