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())
  • What exactly is the error? Which version of web3 are you using? – Prashant Prabhakar Singh Jan 30 '18 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 '18 at 10:35
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");
  • can u please explain how can i get credentials? – vaj oja Jan 30 '18 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. – Prashant Prabhakar Singh Jan 30 '18 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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