gpt4 book ai didi

java - 如何调用返回 tuple[] 的函数

转载 作者:行者123 更新时间:2023-12-05 02:39:41 24 4
gpt4 key购买 nike

我对使用 Java 与智能合约进行交互还比较陌生,在尝试检索智能合约函数返回的元组 [] 时遇到了问题。这是我要调用的函数的 ABI 定义:

{
"inputs":[{"internalType":"address","name":"account","type":"address"}],
"name":"claimableRewards",
"outputs":
[{"components":
[
{"internalType":"address","name":"token","type":"address"},
{"internalType":"uint256","name":"amount","type":"uint256"}
],
"internalType":"struct MultiFeeDistribution.RewardData[]",
"name":"rewards",
"type":"tuple[]"
}],
"stateMutability":"view",
"type":"function"
}

这是智能合约代码的链接: https://polygonscan.com/address/0x920f22e1e5da04504b765f8110ab96a20e6408bd#code

下面是我为调用该函数而编写的 Java 代码(我删除了错误检查以使代码更易于阅读):

List<Type> claimableRewardsParams = Arrays.<Type>asList(new Address(credentials.getAddress()));
List<TypeReference<?>> claimableRewardsReturnTypes = Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<DynamicStruct>>() {});

final Function claimableRewardsFunction = new Function(
"claimableRewards",
claimableRewardsParams,
claimableRewardsReturnTypes);

String claimableRewardsEncodedFunction = FunctionEncoder
.encode(claimableRewardsFunction);

EthCall claimableRewardsResponse = web3.ethCall(
Transaction.createEthCallTransaction(walletAddress, adamantRewardsContractAddress, claimableRewardsEncodedFunction),
DefaultBlockParameterName.LATEST)
.sendAsync().get();

List<Type> claimableRewardsSomeTypes = FunctionReturnDecoder.decode(
claimableRewardsResponse.getValue(), claimableRewardsFunction.getOutputParameters());

当我运行程序时出现以下异常:

Exception in thread "main" java.lang.RuntimeException: TypeReferencedstruct must contain a constructor with types that extend Type

我已经为 claimableRewardsReturnTypes 尝试了其他几个定义,但我无法让它工作。有人可以帮帮我吗?

最佳答案

您需要定义一个扩展 DynamicStruct 的类

public class RewardData extends DynamicStruct {
public RewardData(Address token, Uint256 amount) {
super(new Type[]{token, amount});
}
}

然后用RewardData替换DynamicStruct

List<TypeReference<?>> claimableRewardsReturnTypes = Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<RewardData>>() {});

关于java - 如何调用返回 tuple[] 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68977302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com