8

Ether has precision of 10^18 which makes it impossible to use BIGINT type in database to store values in weis: DECIMAL type should be used instead.

  1. Is it worth to store values in Gwei to stay with BIGINT at expenses of losing some precision? What may be negative side-effects?
  2. What are the industry best practices for exchanges, custodian wallets? From API docs it seems Binance uses 8 digits after the decimal point.
3
  • 1
    The answer depends on your product requirements. Oct 24, 2020 at 21:01
  • Would like to know potential trade-offs to get better understanding of requirements. In general curious about approaches used for exchanges and custodian wallets.
    – origaminal
    Oct 24, 2020 at 21:06
  • If you ever have to store something less than 1gwei you of course won't be able to reflect that in the database without some ugly work arounds. This seems more like a database optimization question than an ETH question. Oct 30, 2020 at 1:48

2 Answers 2

4
+50

As the commenters have correctly pointed out, it depends entirely on what you are trying to achieve.

You can, and probably should store full-precision strings so you have all significant digits, i.e. accurate information. When discarding accuracy you should generally have a good reason why you do not need it and never will need it.

Discarding precision can and does lead to the propagation of errors if you plan to perform any off-chain operations such as adding, multiplying, dividing - you will find that there is no way to accurately do such things off-chain and you must rely on the on-chain contract to discover the truth.

Treating numbers as strings in your database probably means inconvenience or the impossibility of performing math operations at the DB level but that is probably not an option in any case if you are considering inaccurate information, to begin with. If you have accurate information, as strings, you can rely on a BigNumber library to interpret and work with the stored values without loss of precision.

In the end, it depends on what you want to use the database for. As a simple example, if the user wants to empty their account and they rely on a number that was sourced from your database, then they won't have all significant digits and will likely leave "dust" in their account. Rounding can produce a number that is too high and a failing transaction. The solution to that problem is a complete, accurate number.

Hope it helps.

0

I have an alternative for this same requirement which completely relies on blockchain network.

Here we are trying to store a value which is publicly available, readable at any point of time from any time in history of Ethereum.

Instead of storing a balance what I would do is

Approaces:

  1. Store Transaction hash

Use that transaction hash to get the block no

Fetch the balance of an Account in that Block.

  1. Store date time for which you need balance

Use that datetime value to fetch the balance of an account..

Refer to this link...

https://etherscan.io/balancecheck-tool

Your Answer

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

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