简体   繁体   中英

Getting ERC1155 Wallet Balance Using Java Web3J

Since Web3J doesn't currently support ERC1155, is there a way to get the balance for a wallet? My guess is to use a function for this, but I can't seem to figure out how to get it to work.

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);

Do I then create a transaction? Or do I use ethSendRawTransaction? balanceOf only has 2 input so I would expect to have to invoke it from a smartcontract, but I don't see a way to do it.

From reading the web3j docs, It seems that you can do the following:

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

The response object, from org.web3j.protocol.core.methods.response.EthCall does the JSON-RPC call "eth_call" which only retrieves data form the blockchain.

I believe this is the equivalent of doing in web3js the following:


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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