0

我正在使用@solana/web3.js 库并已测试创建新帐户并获得余额。现在我要将合约(Raydium Liqudity PoolV4)与 web3 集成。

我用谷歌搜索,找不到好的学习材料。你能帮我如何使用 web3 将合同集成到 solana 中吗?谢谢。

4

1 回答 1

0

如果你想与智能合约集成,你必须知道智能合约的来源。该程序将为您提供有关如何为您的交易创建指令的信息。

让我们看看镭。它们是闭源程序端,但来自它们的客户端代码

我们可以看到dataLayout是:

const dataLayout = struct([u8('instruction'), nu64('amountIn'), nu64('minAmountOut')])

这告诉我们交换指令接受 au64 amountInu64 minAmountOutnu64u64程序方面。

可能很像spl-swap 交换指令

pub struct Swap {
    /// SOURCE amount to transfer, output to DESTINATION is based on the exchange rate
    pub amount_in: u64,
    /// Minimum amount of DESTINATION token to output, prevents excessive slippage
    pub minimum_amount_out: u64,
}

然后我们可以看到数据是用 编码的instruction: 9,这意味着该swap指令是程序中的第 9 条指令。

const data = Buffer.alloc(dataLayout.span)
dataLayout.encode(
  {
    instruction: 9,
    amountIn,
    minAmountOut
  },
  data
)

上面所做的是分配数据缓冲区并使用您的参数对其进行编码,以便事务执行正确的事务。

所以 Rust 程序看起来像这样:

pub enum RaydiumInstructions {
    /** 0 **/Instruction0 (/**/),
    /** 1 **/Instruction1 (/**/),
    /** 2 **/Instruction2 (/**/),
    /** 3 **/Instruction3 (/**/),
    /** 4 **/Instruction4 (/**/),
    /** 5 **/Instruction5 (/**/),
    /** 6 **/Instruction6 (/**/),
    /** 7 **/Instruction7 (/**/),
    /** 8 **/Instruction8 (/**/),
    /** 9 **/Swap (/**/)
}

我们需要知道该指令要求的账户才能与交易一起发送。如果您选中spl-swap,它会准确列出哪些帐户、顺序以及它们是签名者还是可写者。11 个帐户。

///   0. `[]` Token-swap
///   1. `[]` swap authority
///   2. `[]` user transfer authority
///   3. `[writable]` token_(A|B) SOURCE Account, amount is transferable by user transfer authority,
///   4. `[writable]` token_(A|B) Base Account to swap INTO.  Must be the SOURCE token.
///   5. `[writable]` token_(A|B) Base Account to swap FROM.  Must be the DESTINATION token.
///   6. `[writable]` token_(A|B) DESTINATION Account assigned to USER as the owner.
///   7. `[writable]` Pool token mint, to generate trading fees
///   8. `[writable]` Fee account, to receive trading fees
///   9. `[]` Token program id
///   10. `[optional, writable]` Host fee account to receive additional trading fees
    Swap(Swap),

你可以在 Raydium 的前端看到他们列出了交易的所有账户:

const keys = [
  // spl token
  { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  // amm
  { pubkey: ammId, isSigner: false, isWritable: true },
  { pubkey: ammAuthority, isSigner: false, isWritable: false },
  { pubkey: ammOpenOrders, isSigner: false, isWritable: true },
  { pubkey: ammTargetOrders, isSigner: false, isWritable: true },
  { pubkey: poolCoinTokenAccount, isSigner: false, isWritable: true },
  { pubkey: poolPcTokenAccount, isSigner: false, isWritable: true },
  // serum
  { pubkey: serumProgramId, isSigner: false, isWritable: false },
  { pubkey: serumMarket, isSigner: false, isWritable: true },
  { pubkey: serumBids, isSigner: false, isWritable: true },
  { pubkey: serumAsks, isSigner: false, isWritable: true },
  { pubkey: serumEventQueue, isSigner: false, isWritable: true },
  { pubkey: serumCoinVaultAccount, isSigner: false, isWritable: true },
  { pubkey: serumPcVaultAccount, isSigner: false, isWritable: true },
  { pubkey: serumVaultSigner, isSigner: false, isWritable: false },
  { pubkey: userSourceTokenAccount, isSigner: false, isWritable: true },
  { pubkey: userDestTokenAccount, isSigner: false, isWritable: true },
  { pubkey: userOwner, isSigner: true, isWritable: false }
]

所以下一步是追踪所有这些账户以通过交易发送。

最后,我们将这些添加到交易对象并广播!

let transaction = new Transaction().add(new TransactionInstruction({
    keys,
    programId,
    data
  })
await sendTransaction(connection, wallet, transaction)
于 2022-03-04T04:55:00.540 回答