2

I would like to get information about token. For example total supply and so on. For this I need the contract. However what is the way to actually load it/access it from Java?

Checking examples in languages such as python, it seems trivial:

contract_address = 'CONTRACT_ADDRESS' 

abi = json.loads(abiJson)

contract = web3.eth.contract(address=contract_address, abi=abi)

totalSupply = contract.functions.totalSupply().call()

However how to actually do this using Java? Do I need to generate .abi file and then compile it to java? This way, wouldn't I need to recompile the project everytime I wanted to find info about another contract?

Thanks for help!

2 Answers 2

0

Web3j provides you with the ability to generate Java wrappers from ABI files for Solidity smart contracts. You can do so in different ways, mainly:

For your case, you can use the web3j-codegen module to generate the wrappers. Then, load the generated class dynamically.

Since you only need a set of predefined methods that follow ERC20 standard or similar. This might work for you.

0
  • Run task(generateContractWrappers), wrapper contracts will be generated under build/generated/sources/web3j/ERC20.java

  • Generate contract instance using above wrapper

    ERC20 contractInstance=ERC20.load(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider)
   //contract gas provider can be created from DefaultGasProvider or StaticGasProvider
   //Credentials credential=Credentials.create(privatekey);
  • Call contract functions
    BigInteger totalSupply=contractInstance.totalSupply().send();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.