如何在 Solana 中将合约(程序)与 web3 集成?

How to integrate contract (program) with web3 in Solana?

我正在使用@solana/web3.js 库并测试过创建新帐户和获取余额。 现在我要把合约(Raydium Liqudity PoolV4)和web3整合起来。

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

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

再看看瑞鼎。他们是闭源 program-side,但来自他们的 client-side code.

我们可以看到dataLayout是:

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

这告诉我们交换指令接收 u64 amountInu64 minAmountOutnu64 在 program-side 上是 u64

可能很像 spl-swap swap instruction

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 的 front end 中看到他们列出了交易的所有帐户:

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)