1

i'd like to add a raydium swap function to the flutter. So, i making a function. ( i'm not familiar with dart language. )

class RaydiumProgram {
  static final Ed25519HDPublicKey id = Ed25519HDPublicKey.fromBase58('675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8');

  static final swapInstructionIndex = ByteArray([9]);
}
  class RaydiumInstruction extends Instruction {
  RaydiumInstruction._({
  required List<AccountMeta> accounts,
  required ByteArray data,
  }) : super(
    programId: RaydiumProgram.id,
    accounts: accounts,
    data: data,
  );

  static List<Instruction> swapUsingRaydium({
    required Ed25519HDPublicKey ammId,
    required Ed25519HDPublicKey ammAuthority,
    required Ed25519HDPublicKey ammOpenOrders,
    required Ed25519HDPublicKey ammTargetOrders,
    required Ed25519HDPublicKey poolCoinTokenAccount,
    required Ed25519HDPublicKey poolPcTokenAccount,
    required Ed25519HDPublicKey serumProgramId,
    required Ed25519HDPublicKey serumMarket,
    required Ed25519HDPublicKey serumBids,
    required Ed25519HDPublicKey serumAsks,
    required Ed25519HDPublicKey serumEventQueue,
    required Ed25519HDPublicKey serumCoinVaultAccount,
    required Ed25519HDPublicKey serumPcVaultAccount,
    required Ed25519HDPublicKey serumVaultSigner,
    required Ed25519HDPublicKey userSourceTokenAccount,
    required Ed25519HDPublicKey userDestTokenAccount,
    required Ed25519HDPublicKey userOwner,
    required int amountIn,
    required int minAmountOut,
  }) =>
      [
        RaydiumInstruction._(
        accounts: [
          AccountMeta.writeable(pubKey: ammId, isSigner: false),
          AccountMeta.readonly(pubKey: ammAuthority, isSigner: false),
          AccountMeta.writeable(pubKey: ammOpenOrders, isSigner: false),
          AccountMeta.writeable(pubKey: ammTargetOrders, isSigner: false),
          AccountMeta.writeable(pubKey: poolCoinTokenAccount, isSigner: false),
          AccountMeta.writeable(pubKey: poolPcTokenAccount, isSigner: false),
          AccountMeta.readonly(pubKey: serumProgramId, isSigner: false),
          AccountMeta.writeable(pubKey: serumMarket, isSigner: false),
          AccountMeta.writeable(pubKey: serumBids, isSigner: false),
          AccountMeta.writeable(pubKey: serumAsks, isSigner: false),
          AccountMeta.writeable(pubKey: serumEventQueue, isSigner: false),
          AccountMeta.writeable(pubKey: serumCoinVaultAccount, isSigner: false),
          AccountMeta.writeable(pubKey: serumPcVaultAccount, isSigner: false),
          AccountMeta.readonly(pubKey: serumVaultSigner, isSigner: false),
          AccountMeta.writeable(pubKey: userSourceTokenAccount, isSigner: false),
          AccountMeta.writeable(pubKey: userDestTokenAccount, isSigner: false),
          AccountMeta.writeable(pubKey: userOwner, isSigner: true),
        ],
        data: ByteArray.merge(
        [
          RaydiumProgram.swapInstructionIndex,
          ByteArray.u64(amountIn),
          ByteArray.u64(minAmountOut),
        ],
        ),
        )
      ];
}

i get the above information from the raydium token pair.

    List<Ed25519HDKeyPair> ListOwner = [];
    ListOwner.add(m.wallet.keyPair);

    final instructions = RaydiumInstruction.swapUsingRaydium(
      ammId: Ed25519HDPublicKey.fromBase58(poolInfo.id),
      ammAuthority: Ed25519HDPublicKey.fromBase58(poolInfo.authority),
      ammOpenOrders: Ed25519HDPublicKey.fromBase58(poolInfo.openOrders),
      ammTargetOrders: Ed25519HDPublicKey.fromBase58(poolInfo.targetOrders),
      poolCoinTokenAccount: Ed25519HDPublicKey.fromBase58(poolInfo.baseVault),
      poolPcTokenAccount: Ed25519HDPublicKey.fromBase58(poolInfo.quoteVault),
      serumProgramId: Ed25519HDPublicKey.fromBase58(poolInfo.marketProgramId),
      serumMarket: Ed25519HDPublicKey.fromBase58(poolInfo.marketId),
      serumBids: Ed25519HDPublicKey.fromBase58(poolInfo.marketBids),
      serumAsks: Ed25519HDPublicKey.fromBase58(poolInfo.marketAsks),
      serumEventQueue: Ed25519HDPublicKey.fromBase58(poolInfo.marketEventQueue),
      serumCoinVaultAccount: Ed25519HDPublicKey.fromBase58(poolInfo.marketBaseVault),
      serumPcVaultAccount: Ed25519HDPublicKey.fromBase58(poolInfo.marketQuoteVault),
      serumVaultSigner: Ed25519HDPublicKey.fromBase58(poolInfo.marketAuthority),
      userSourceTokenAccount: userSource,
      userDestTokenAccount: userDest,
      userOwner: m.wallet.keyPair.publicKey,
      amountIn: _amountIn,
      minAmountOut: _minAmountOut
    );

    try {
      TransactionId signature = await _solanaClient.sendAndConfirmTransaction(
          message: new Message(instructions: instructions),
          signers: ListOwner,
          commitment: commitment);
      print('signature: ' + signature);
    } catch(e) {
      print(e);
    }


i used it like this, but an error occurs.

0 = "Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 invoke [1]"
1 = "Program log: Error: InvalidOwner"
2 = "Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 consumed 5401 of 200000 compute units"
3 = "Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 failed: custom program error: 0x12"

i saw another transaction and implemented it in my own way. There are so many things I don't know.

1
  • I'm sure you have an error in the order of the accounts transferred. Check the Raydium instructions you created
    – Rizz
    Jun 1, 2023 at 13:43

1 Answer 1

1

https://github.com/raydium-io/raydium-sdk-V1-demo/blob/master/src/swapOnlyAmm.ts This is the typescript implementation for the swap function. You can look into how to do the same using dart

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.