0

I'm currently creating my first dApp, and have some trouble understanding the storage of data of smart contract on the Ethereum blockchain. I've altered the react basic code by adding a constructor. As can be seen in the first image. Now by my understanding when interacting with the smart contract every variable that is public will be stored on the blockchain. Now I try testing it in truffle but cannot find the value in question. Example of the transaction can be found in image #2. Can someone please explain to me where the data is actually stored on the blockchain or is it encoded?

P.S. I do know that storing large amount on the blockchain is fairly costly.

code and transactions

1

The transactions in image 2 show you the transactions that were sent. Literally setValue(25), from/to, UID (transaction Hash), block number where it was mined, etc. - but not the effect. The answer you seek is not there.

Up above, where you did app.getValue() you got the value at the time. That is how you inspect the contract state via the function in the contract.

What is throwing you off is the format of the response. The contract uses a 256-bit integer and JavaScript has no Type for that. Instead, what you get is cast as a BigNumber using the BigNumber.js library (rolled up into Web3) that organizes the significant digits in a special kind of object and gives methods for math operations.

This is the case with all uint responses.

You can go:

$ let x = await app.getValue()
$ x
BN: { <bigNumber object> }
$ x.toString(10)
`25`

This prints the value of x, a BigNumber as human-readable string interpreting it with the base-10 number system.

A best practice is to get used to casting all of your "big numbers" this way and performing your math operations with the BigNumber methods. This way, you won't lose any precision. Then present the human-readable format through the UI. For example:

let x = await app.getValue();   // returns a BN
let y = web3.utils.toBN(`1`);   // cast `1` as a BN
let z = x.add(y);               // add two BN returns another BN
let zReadable = z.toString(10); // human-readable string, not recommended for math
console.log("The next number in the sequence is", zReadable); 

Hope it helps.

0

You should run

app.getValue().call()

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.