5 使用java web3j对该solidity函数结构体参数进行编码

solidity中的代码为

  struct ExactInputParams {
    bytes path;
    address recipient;
    uint256 amountIn;
    uint256 amountOutMinimum;
}

function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut)

使用java web3j对该函数进行编码: 我的尝试:

//拼装路径;tokenA fee tokenB fee tokenC;
    String path = "0x55d398326f99059ff775485246999027b3197955000064e9e7cea3dedca5984780bafc599bd69add087d560000649840652dc04fb9db2c43853633f0f62be6f00f98";
    //exactInput((bytes,address,uint256,uint256))方法编码

    List<Type> inputParameters1 = new LinkedList<>();
    byte[] bytesParam = Numeric.hexStringToByteArray(path);
    DynamicBytes bytesParam1 = new DynamicBytes(bytesParam);

    inputParameters1.add(bytesParam1);
    inputParameters1.add(new Address("0x71836e418915B34D05bE74995c1eC77EEB246E22"));
    inputParameters1.add(new Uint256(new BigInteger("11137595489375510586")));
    inputParameters1.add(new Uint256(new BigInteger("112745856625915642687")));
    Function function = new Function(
            "exactInput",
            inputParameters1,
            Arrays.asList(new TypeReference<Type>() {
            })
    );

    String swapCode = FunctionEncoder.encode(function);

    System.out.println(swapCode);

得到的结果: 0x11b69c46000000000000000000000000000000000000000000000000000000000000008000000000000000000000000071836e418915b34d05be74995c1ec77eeb246e220000000000000000000000000000000000000000000000009a90b0168c38603a0000000000000000000000000000000000000000000000061ca9bf3f84d2c33f000000000000000000000000000000000000000000000000000000000000004255d398326f99059ff775485246999027b3197955000064e9e7cea3dedca5984780bafc599bd69add087d560000649840652dc04fb9db2c43853633f0f62be6f00f98000000000000000000000000000000000000000000000000000000000000

期望的结果:

0xb858183f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000071836e418915b34d05be74995c1ec77eeb246e220000000000000000000000000000000000000000000000009a90b0168c38603a0000000000000000000000000000000000000000000000061ca9bf3f84d2c33f000000000000000000000000000000000000000000000000000000000000004255d398326f99059ff775485246999027b3197955000064e9e7cea3dedca5984780bafc599bd69add087d560000649840652dc04fb9db2c43853633f0f62be6f00f98000000000000000000000000000000000000000000000000000000000000

验证:

image.png

请先 登录 后评论

2 个回答

用户_15137
请先 登录 后评论
Meta - 风是自由的,你也是
请先 登录 后评论
  • 2 关注
  • 0 收藏,2188 浏览
  • 用户_15137 提出于 2023-06-20 16:01