3

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.

juminoz
  • 3,168
  • 7
  • 35
  • 52
  • [How to connect to Ethereum network using Java / Web3j](https://www.quicknode.com/guides/web3-sdks/how-to-connect-to-ethereum-network-using-java-web3j) may be helpful – Eskandar Abedini Jun 26 '22 at 14:22
  • Unfortunately, that isn't helpful. Web3J is good for basic stuff, but not when it comes to a newer standard or more complex tasks. – juminoz Jun 27 '22 at 12:30
  • Do you want to get the balance of all ERC1155 an address holds, or do you want the balance of an Specific ERC1155? Because if you want to get the balance of an specific ERC1155, you just need the ABI, the address of the ERC1155 and then create a `Contract` object with them. It will have a method to check balance of specific tokens within the ERC1155. – Pedro Henrique Bufulin Jun 28 '22 at 04:50
  • @PedroHenriqueBufulin Just need it for a specific address. I know how to do this using Javascript. I'm looking for an example using Java. – juminoz Jun 29 '22 at 15:47

1 Answers1

0

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