我是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代码下面是我写的调用函数的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());
当我运行程序时,我得到以下异常:
线程"main"异常java.lang.RuntimeException: TypeReferenced结构必须包含类型扩展为Type
的构造函数。
我已经尝试了几个其他的定义claimableerewardsreturntypes,但我不能让它工作。有人能帮帮我吗?
你需要定义一个类来扩展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>>() {});
{
"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"
}