0

I have an app that is pulling all erc20 tokens for each address. Now I want to store these values already calculated to the right amount into my postgres db. Right now, I am using the column type of plain numerical.

Could this be any issue later on? Do I need to set some sort of precision and scale for the numeric column?

What is the best approach overall to store these balances which I am formatting like this

 try {
  const divisor = new BigNumber(10).pow(token.decimals);
  let balance = new BigNumber(result);
  balance = balance.div(divisor);
  if (balance.isGreaterThan(0)) {
    const balanceObj = await this.balanceService.create({
      address: wallet,
      amount: balance.toNumber(),
      token: token,
    });
  }
3
  • This question might be better suited for StackOverflow. It is fundamentally a question about best practices on designing Postgres databases. Nov 2, 2021 at 20:56
  • there are not many people arround having worked with erc20 standard and persisting token data tho. I tried already Nov 2, 2021 at 20:59
  • Do you know about The Graph? Nov 2, 2021 at 21:48

2 Answers 2

0

What is the problem you're trying to solve with this storage? Is it sorting, lookup, any aggregation, etc.?

First, I'd normalize all token amounts to 18 decimals, and store all the values in WEI - since not all tokens follow a standard of 18 decimals. Otherwise you'd have to keep a separate field to keep the number of decimals.

As for the data type, I'd go with decimal or numeric. The other types, including bigint do not seem to satisfy the constraints of the BigNumber data type used in the EVM. These types will allow you to perform most operations you need, like indexes, aggregation etc.

0

How about storing it as NUMERIC(78,0)? postgres

Max unsigned 256-bit integer is 115792089237316195423570985008687907853269984665640564039457584007913129639935, which is exactly 78 digits long.

There cannot be a value larger than that on ETH network, I suppose, so a 78-digit integer should do the trick.

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.