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

1 Answer 1

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.

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.