如何使用web3j将元组编码为函数输入参数

4

我正在尝试调用一个类似于以下内容的solidity函数:

function fillOrder(
    Order memory order,
    uint256 takerAssetFillAmount,
    bytes memory signature
)

使用web3j,我会创建类似以下函数的函数,但我不太确定如何表示在Solidity中表示为结构体的订单。
List<Type> inputParams = Arrays.asList(???, new 
Uint256(takerAssetFillAmount), new Bytes32(signture));
new Function("fillOrder", inputParams, Collections.emptyList());

有关如何表示结构体的任何指针吗?
谢谢。

@RaghavSood 不是,那篇文章是关于如何在参数有元组时计算函数选择器的。 - DiveInto
1
@dneo,你找到任何解决方案了吗?如果有的话,请更新为答案。谢谢。 - vanillascotch
3个回答

1
您可以使用方括号来包装参数。
例如,假设我有一个合同:

contract Test {
    struct Foo {
        uint a;
        string b;
        address c;
    }

    function bar (Foo memory foo) public {
        c = foo.c;
    }
}

我可以通过web3.js这样调用bar函数:

contract.methods.foo([123, "123", "0xABC...."]).send({ from: '0x...' })

0

这是合约地址 https://goerli.etherscan.io/address/0xd5999bf0ce31a1d9d6a6de2bf03feaff1913cee5#writeContract

在写函数中,createSwapOrder正在请求嵌套元组。以下是Solidity代码以显示元组的结构:

struct Side {
    address user;
    bytes signedRequiredOutput;
    ERC20Component[] erc20s;
    ERC721Component[] erc721s;
    ERC1155Component[] erc1155s;
}

struct ERC20Component {
    uint256 amount;
    address underlying;

    // A signed approval transaction giving `amount` transfer rights
    // of token `underlying` to address(this).
    // bytes signedApproval;
}

struct ERC721Component {
    uint256 tokenId;
    address collection;

    // A signed approval transaction giving `tokenId` tranfer rights
    // of token `collection` to address(this).
    // bytes signedApproval;
}

struct ERC1155Component {
    uint256 tokenId;
    uint256 amount;
    address collection;

    // A signed approval transaction giving `tokenId` tranfer rights
    // of token `collection` to address(this).
    // bytes signedApproval;
}

struct Order {
    Side side0;
    Side side1;
    uint256 expiry;
    bytes32 hashlock;
    bytes32 preimage;
    bool completed;
}

event OrderCreated(address indexed user, bytes32 orderId);

uint256 public totalOrders;
mapping(bytes32 => Order) public orders;

function createSwapOrder(
    Side calldata side0,
    bytes32 hashlock,
    uint256 timelock
) public {

...

在第一个参数中,side0正在请求一个嵌套元组,这个元组的格式应该像这样:["0x5B38Da6a701c568545dCfcB03FcB875f56beddC4","0x00",[["32","0x5B38Da6a701c568545dCfcB03FcB875f56beddC4"]],[["32","0x5B38Da6a701c568545dCfcB03FcB875f56beddC4"]],[["32","32","0x5B38Da6a701c568545dCfcB03FcB875f56beddC4"]]]。

我希望你能理解提供的结构!!并且确保它可以正常工作。


0

Web3j 提供了 StaticStructDynamicStruct 等类,您可以通过基本类型定义结构对象。以下是我项目中的示例:

class Value: DynamicStruct {

    private lateinit var offer: String
    private lateinit var availableSince: BigInteger
    private lateinit var availabilityEnd: BigInteger
    private var isConsumed: Boolean = false
    private lateinit var lockedUntil: BigInteger

    constructor(
        offer: String,
        availableSince: BigInteger,
        availabilityEnd: BigInteger,
        isConsumed: Boolean,
        lockedUntil: BigInteger
    ) : super(
        Utf8String(offer), Uint256(availableSince),
        Uint256(availabilityEnd), Bool(isConsumed),
        Uint256(lockedUntil)
    ) {
        this.offer = offer
        this.availableSince = availableSince
        this.availabilityEnd = availabilityEnd
        this.isConsumed = isConsumed
        this.lockedUntil = lockedUntil
    }

    constructor(
        offer: Utf8String,
        availableSince: Uint256,
        availabilityEnd: Uint256,
        isConsumed: Bool,
        lockedUntil: Uint256
    ) : super(offer, availableSince, availabilityEnd, isConsumed, lockedUntil) {
        this.offer = offer.value
        this.availableSince = availableSince.value
        this.availabilityEnd = availabilityEnd.value
        this.isConsumed = isConsumed.value
        this.lockedUntil = lockedUntil.value
    }

}

理想情况下,您只需要将此结构实例作为参数传递给合同方法即可,其中合同是通过$web3j solidity generate -b /path/to/<smart-contract>.bin -a /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name命令自动生成的

然而,就个人而言,我遇到了使此控制台命令工作的问题,必须自己实现所需的逻辑。这正在进行中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接