1

I'm trying to swap wsol against usdc and then in second transaction wsol against the usdc it's sort of arbitrage script. Now that I don't have wsol balance but the Jupiter aggregator uses sol from my wallet to wrap them into wsol. But when I read the wsol balance it return zero.. So my question is does the Jupiter will also give me sol out instead of wsol in second swap because below is a function that read the balance of token for other token it has no issue but for the wsol it always return 0. Is it due to

same machenism of wrapping and unwrapping.

  private async getTokenBalance(
    wallet: PublicKey,
    tokenMint: PublicKey
  ): Promise<number> {
    try {
      let tokenAccountPublicKey: PublicKey;
  
      if (tokenMint.equals(new PublicKey("So11111111111111111111111111111111111111112"))) {
        // For WSOL, fetch the associated token account for the wallet
        tokenAccountPublicKey = await getAssociatedTokenAddress(
          tokenMint, // The mint address for WSOL
          wallet, // Wallet public key
          false // Specify if the account should be writable (false by default)
        );
      } else {
        // For other SPL tokens, get the associated token account
        const tokenAccounts = await this.solanaConnection.getTokenAccountsByOwner(wallet, {
          mint: tokenMint,
        });
  
        if (tokenAccounts.value.length === 0) return 0; // No associated token account found
  
        tokenAccountPublicKey = tokenAccounts.value[0].pubkey;
      }
  
      // Fetch the account information for the token account
      const tokenAccountInfo = await this.solanaConnection.getParsedAccountInfo(tokenAccountPublicKey);
      const tokenBalance = (tokenAccountInfo.value?.data as any)?.parsed?.info?.tokenAmount?.uiAmount;
  
      return tokenBalance || 0; // Return the balance, or 0 if not found
    } catch (error) {
      console.error("Error fetching token balance:", error);
      return 0; // Return 0 in case of an error
    }
  } 

1 Answer 1

1

Perhaps you didn't set the wrapAndUnwrapSol option to false and swap tx automatically wraps and unwraps the SOL.

// get serialized transactions for the swap
const { swapTransaction } = await (
  await fetch('https://quote-api.jup.ag/v6/swap', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },

    body: JSON.stringify({
       quoteResponse,
       userPublicKey: wallet.publicKey.toString(),
      // auto wrap and unwrap SOL. default is true
      wrapAndUnwrapSol: false, // ! set to false
    })
  })
).json();

1
  • yeah that was the case actually when i debugged the trx at the end there was instruction invoked to close the wsol associated accounts
    – Anwar
    Commented Dec 12 at 6:18

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.