Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token swap error #1542

Closed
sansaywm opened this issue Aug 5, 2024 · 0 comments
Closed

Token swap error #1542

sansaywm opened this issue Aug 5, 2024 · 0 comments
Labels

Comments

@sansaywm
Copy link

sansaywm commented Aug 5, 2024

Has anyone implemented a token exchange method? I'm new to blockchain and can't figure out what could be wrong. I looked at many implementations in different languages, but I can’t find what’s wrong. Now I have the following error jsonrpc-2.0 error (-32002): Transaction simulation failed: Error processing Instruction 4: custom program error: 0x13


const String TOKEN_PROGRAM_ID = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
const String authority = "5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1";
const String MAINNET_PROGRAM_ID =  "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8"
Future<void> _buyToken(
      RawPool poolkeys,
      RawMarket rawMarket,
      Ed25519HDKeyPair wallet,
      Ed25519HDPublicKey poolId,
      AccountResult poolinfo) async {
    Ed25519HDPublicKey? wSolAssociatedToken;
    try {
      wSolAssociatedToken = await findAssociatedTokenAddress(
          owner: wallet.publicKey, mint: poolkeys.quoteMint);
    } catch (e) {
      debugPrint("error: $e");
    }

    if (wSolAssociatedToken == null) return;

    Ed25519HDPublicKey? outputAssociatedToken;
    try {
      outputAssociatedToken = await findAssociatedTokenAddress(
          owner: wallet.publicKey, mint: poolkeys.baseMint);
    } catch (e) {
      debugPrint("error: $e");
    }

    if (outputAssociatedToken == null) return;

    final instruction = makeSwapFixedInInstruction(
        poolKeys: poolkeys,
        rawMarket: rawMarket,
        poolId: poolId,
        wallet: wallet.publicKey,
        wsolAccountAddress: wSolAssociatedToken,
        outputTokenAccountAddress: outputAssociatedToken,
        amountIn: BigInt.from((0.01 * LAMPORTS_PER_SOL)),
       minAmountOut: BigInt.from(0),       
       version: 4,
       poolinfo: poolinfo);

    final message = Message(instructions: [
      ComputeBudgetInstruction.setComputeUnitLimit(units: 101337),
      ComputeBudgetInstruction.setComputeUnitPrice(microLamports: 421197),
    
  AssociatedTokenAccountInstruction.createAccount(
          funder: wallet.publicKey,
          address: wSolAssociatedToken,
          owner: wallet.publicKey,
          mint: poolkeys.quoteMint),
    
  SystemInstruction.transfer(
          fundingAccount: wallet.publicKey,
          recipientAccount: wSolAssociatedToken,
          lamports: (0.01 * LAMPORTS_PER_SOL).toInt()),

      TokenInstruction.syncNative(nativeTokenAccount: wSolAssociatedToken),

      AssociatedTokenAccountInstruction.createAccount(
          funder: wallet.publicKey,
          address: outputAssociatedToken,
          owner: wallet.publicKey,
          mint: poolkeys.baseMint),
      instruction,
      TokenInstruction.closeAccount(
          accountToClose: wSolAssociatedToken,
          destination: wallet.publicKey,
          owner: wallet.publicKey)
    ]);

    try {
      var signature = await _solanaClient.rpcClient
          .signAndSendTransaction(message, [wallet], onSigned: (signature) {
        debugPrint("signature::  signature ${base58encode(signature.bytes)}");
      });
      _solanaClient.waitForSignatureStatus(signature,
          status: Commitment.finalized,
          pingInterval: const Duration(seconds: 1));
    } catch (e) {
      debugPrint("error $e");
    }
  }

  encoder.Instruction makeSwapFixedInInstruction(
      {required RawPool poolKeys,
      required RawMarket rawMarket,
      required Ed25519HDPublicKey poolId,
      required Ed25519HDPublicKey wallet,
      required Ed25519HDPublicKey wsolAccountAddress,
      required Ed25519HDPublicKey outputTokenAccountAddress,
      required BigInt amountIn,
      required BigInt minAmountOut,
      required int version,
      required AccountResult poolinfo}) {
    final data = RawAmount(
        discriminator: 9, amountIn: amountIn, minAmountOut: minAmountOut);

    final keys = [
      encoder.AccountMeta(
          pubKey: Ed25519HDPublicKey.fromBase58(TOKEN_PROGRAM_ID),
          isSigner: false,
          isWriteable: false),
     encoder.AccountMeta(pubKey: poolId, isSigner: false, isWriteable: true),

      encoder.AccountMeta(
          pubKey: Ed25519HDPublicKey.fromBase58(authority),
          isSigner: false,
          isWriteable: false),
      encoder.AccountMeta(
          pubKey: poolKeys.openOrders, isSigner: false, isWriteable: true)
    ];

    if (version == 4) {
      keys.add(encoder.AccountMeta(
          pubKey: poolKeys.targetOrders, isSigner: false, isWriteable: true));
    }

    keys.addAll([
      encoder.AccountMeta(
          pubKey: poolKeys.baseVault, isSigner: false, isWriteable: true),
      encoder.AccountMeta(
          pubKey: poolKeys.quoteVault, isSigner: false, isWriteable: true),
    ]);
    keys.addAll([
     encoder.AccountMeta(
          pubKey: poolKeys.marketProgramId,
          isSigner: false,
          isWriteable: false),
      encoder.AccountMeta(
          pubKey: poolKeys.marketId, isSigner: false, isWriteable: true),
      encoder.AccountMeta(
          pubKey: rawMarket.bids, isSigner: false, isWriteable: true),
      encoder.AccountMeta(
          pubKey: rawMarket.asks, isSigner: false, isWriteable: true),
      encoder.AccountMeta(
          pubKey: rawMarket.eventQueue, isSigner: false, isWriteable: true),
      encoder.AccountMeta(
          pubKey: rawMarket.baseVault, isSigner: false, isWriteable: true),
      encoder.AccountMeta(
          pubKey: rawMarket.quoteVault, isSigner: false, isWriteable: true),
      encoder.AccountMeta(
          pubKey: rawMarket.ownAddress, isSigner: false, isWriteable: false),
      encoder.AccountMeta(
          pubKey: wsolAccountAddress, isSigner: false, isWriteable: true),
      encoder.AccountMeta(
          pubKey: outputTokenAccountAddress,
          isSigner: false,
          isWriteable: true),
      encoder.AccountMeta(pubKey: wallet, isSigner: true, isWriteable: true),
    ]);

    return encoder.Instruction(
        programId: Ed25519HDPublicKey.fromBase58(MAINNET_PROGRAM_ID),
        accounts: keys,
        data: encoder.ByteArray(data.toBorsh()));
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant