1

I am able to get the balance of my custom token with web3 javascript.The code is shown below.

However, when i switch to web3j, I don't find equivalent method like web3.eth.contract(contractABI).at(contractAddress).

Can anyone please advise how can i do the same with web3j?

var web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/<APIKEY>"));

address = "0x0e2e75240c69495d2b9e768b548db381de2142b9" //From Etherscan
contractAddress = "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" //OMG
contractABI = human_standard_token_abi

tokenContract = web3.eth.contract(contractABI).at(contractAddress)

console.log(tokenContract.balanceOf(address).toNumber())
2
  • What exactly is the error? Which version of web3 are you using? Jan 30, 2018 at 10:33
  • there is no error.web3j works fine. But i just dont know how to do the same with web3j.
    – vaj oja
    Jan 30, 2018 at 10:35

2 Answers 2

1

I guess what you are looking for is:

YourSmartContract contract = YourSmartContract.load(
        "0x<address>|<ensName>", web3j, credentials, GAS_PRICE, GAS_LIMIT);

And then interact with contract using:

TransactionReceipt transactionReceipt = contract.someMethod(
             <param1>,
             ...).send();

Edit
Working with web3j is little tedious as compared to web3.js. Like in web3.js you unlock an account by:

web3.personal.unlockAccount("someAddress", "somePassword")

whereas in web3.j you need

Credentials credentials = WalletUtils.loadCredentials("somePassword", "path_to_keystore_file");
2
  • can u please explain how can i get credentials?
    – vaj oja
    Jan 30, 2018 at 10:59
  • a credential is nothing but an unlocked wallet. You don't need it if you are only reading from the smart contract. You can consider this as an unlocked ethereum account. Jan 30, 2018 at 11:15
0

As @Prashant mentioned, the private key should not be required just to get the balance. How then the credentials object should be created in Web3j? I am able to get the balance from a tocken, but I have to give the privat key, which should not be needed.

1
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – 0xSanson
    Oct 19, 2022 at 22:43

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.