使用Java Web3J获取ERC1155钱包余额



由于Web3J目前不支持ERC1155,是否有办法获得钱包的余额?我的猜测是使用一个函数,但我似乎不知道如何让它工作。

Function function = new Function(
"balancedOf",
Arrays.asList(new Address(ethAddress), new Uint256(1)),
Arrays.asList(new org.web3j.abi.TypeReference<Bool>() {}));
String data = FunctionEncoder.encode(function);

然后创建一个事务吗?或者我使用ethSendRawTransaction?balanceOf只有2个输入,所以我希望必须从智能合约中调用它,但我没有看到这样做的方法。

从阅读web3j文档来看,您似乎可以做以下事情:

Function function = new Function<>(
"functionName",
Arrays.asList(new Type(value)),  // Solidity Types in smart contract functions
Arrays.asList(new TypeReference<Type>() {}, ...));
String encodedFunction = FunctionEncoder.encode(function)
org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
Transaction.createEthCallTransaction(<from>, contractAddress, encodedFunction),
DefaultBlockParameterName.LATEST)
.sendAsync().get();
List<Type> someTypes = FunctionReturnDecoder.decode(
response.getValue(), function.getOutputParameters());

response对象,从org.web3j.protocol.core.methods.response.EthCall做JSON-RPC调用& eth_call"它只从区块链中检索数据。

我相信这相当于在web3js中这样做:


let contract = new web3.eth.Contract(<ABI>, <Contract Address>);
const res = await contract.functionName(<params>);

最新更新