20 在solana网络上,如何识别不同的action? action跟instruction 有什么区别?

请先 登录 后评论

最佳答案 2024-06-09 16:16

没什么大的区别,action就是把instruction的函数名给大写了。 识别不同的action,就是通过解析tx里面的instruction的函数调用,不同的函数对应不同的action,解析合约的函数调用,就是通过合约的idl描述文件,这个和evm上的abi文件基本上差不多,只是具体细节上差异很大。

在tx里面,Instruction Details里面 一条Instruction Instruction Data Raw ,前面8个字节仍然是函数签名。 不是anchor生成的解析方法得看合约的源码。

你这个例子中的,raydium就是一个没有使用anchor的特例,参考源码 https://github.com/raydium-io/raydium-amm/blob/master/program/src/instruction.rs 这个不是使用的函数签名 这里第一个字节 09表示 swapBaseIn。

impl AmmInstruction {
    /// Unpacks a byte buffer into a [AmmInstruction](enum.AmmInstruction.html).
    pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> {
        let (&tag, rest) = input
            .split_first()
            .ok_or(ProgramError::InvalidInstructionData)?;
        Ok(match tag {
            9 => {
                let (amount_in, rest) = Self::unpack_u64(rest)?;
                let (minimum_amount_out, _rest) = Self::unpack_u64(rest)?;
                Self::SwapBaseIn(SwapInstructionBaseIn {
                    amount_in,
                    minimum_amount_out,
                })
            }

有解析不了的,联系我,开不开源,无所谓。 补充一个,Jupiter v6 的tx识别 tx如下: https://solana.fm/tx/5YTgCGQpXjkwXLnEMgVt4A4XvXxJzBpGzni5wMQQQcWR9AL28VrH1FJeNM2g9yeb12C6rXT1yRvTSV7wh4CytjEM?cluster=mainnet-alpha 函数原始Instruction Data Raw image.png 是一个标准的anchor接口的api 贴一个算法: 由idl中的定义到,Instruction Data Raw 中的函数签名: image.png

请先 登录 后评论

其它 0 个回答

  • 1 关注
  • 0 收藏,1348 浏览
  • rebase 提出于 2024-06-08 23:33