繁体   English   中英

使用 Java Web3J 获取 ERC1155 钱包余额

[英]Getting ERC1155 Wallet Balance Using Java Web3J

由于 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());

来自org.web3j.protocol.core.methods.response.EthCallresponse对象执行 JSON-RPC 调用“eth_call”,它只从区块链中检索数据。

我相信这相当于在 web3js 中执行以下操作:


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM