How to get value from solidity contract using java

0 votes

My solidity contract is this:

contract SimpleStorage {
uint storedData;

function set(uint x) {
    storedData = x;
}

function get() constant returns (uint retVal) {
    return storedData;
}}

and generate the abi is following:

[ { "constant": false, "inputs": [ { "name": "x", "type": "uint256" } ], "name": "set", "outputs": [], "type": "function" }, { "constant": true, "inputs": [], "name": "get", "outputs": [ { "name": "retVal", "type": "uint256", "value": "0" } ], "type": "function" } ]

and referenced by https://github.com/ethereum/wiki/wiki/JSON-RPC,

How to invoke get funtion and get the value by using java (not js)?

Sep 28, 2018 in Blockchain by digger
• 26,740 points
2,024 views

1 answer to this question.

0 votes

web3j caters for this very use case. It generates Smart Contract wrappers in Java from a Solidity compiled binary and ABI file.

Once you've generated the wrapper code with web3j, you will be able to deploy, then call the methods on the above contract example as follows:

SimpleStorage simpleStorage = SimpleStorage.deploy(
    <web3j>, <credentials>, GAS_PRICE, GAS_LIMIT,
    BigInteger.ZERO);  // ether value of contract

TransactionReceipt transactionReceipt = simpleStorage.set(
        new Uint256(BigInteger.valueOf(1000))),
        .get();

Uint256 result = simpleStorage.get()
        .get();

See the docs for further information.

answered Sep 28, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

0 votes
1 answer

Ethereum solidity: Unable to get value from another contract

I have faced similar issues when compiling ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
1,663 views
0 votes
1 answer

How to get all address and send ethers in solidity using a loop?

I found a similar code somewhere: contract  Holders{ uint ...READ MORE

answered Jul 31, 2018 in Blockchain by digger
• 26,740 points
2,757 views
0 votes
1 answer

How to get a value from SQLite to convert it?

Server side: b = BtcConverter(force_decimal=True) p1btc = b.get_latest_price('USD') p1btcr = ...READ MORE

answered Sep 6, 2018 in Blockchain by digger
• 26,740 points
751 views
0 votes
1 answer

Solidity: How to return id from hash value?

You can do it by storing the hash ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 69,220 points
2,369 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer
0 votes
1 answer

How to get return value from solidity contract?

Once you call a constant function, you ...READ MORE

answered Aug 16, 2018 in Blockchain by slayer
• 29,370 points
5,041 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP