我正在使用@solana/web3.js库,并已测试创建新帐户和获取余额。现在我要将合同(Raydium Liquidity PoolV4(与web3进行集成。
我在谷歌上搜索过,找不到好的学习材料。你能帮我如何使用web3在solana中集成合同吗?谢谢
如果你想与智能合约集成,你必须知道智能合约的来源。该程序将为您提供有关如何创建交易说明的信息。
让我们看看Raydium。它们是封闭源代码程序端,但来自客户端代码。
我们可以看到dataLayout
是:
const dataLayout = struct([u8('instruction'), nu64('amountIn'), nu64('minAmountOut')])
这告诉我们交换指令接收u64
amountIn
和u64
minAmountOut
。CCD_ 6是程序侧的CCD_。
可能很像spl交换交换指令
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)