omgnetwork / optimism

Monorepo implementing the Optimistic Ethereum protocol
https://optimism.io
MIT License
16 stars 6 forks source link

Gateway - code refactoring to use consistent approach to BN, Wei_String, Floats, BigNumbers, and ethers.utils.BN #516

Open CAPtheorem opened 3 years ago

CAPtheorem commented 3 years ago

In the gateway, we use five different ways to represent numbers. As numbers, as strings, and BN's, as BigNumbers etc. Although things work, it makes maintaining and understanding the gateway difficult.

Here is an example of the complexity:

Screen Shot 2021-09-22 at 10 18 20 AM

Values entered by the user are typically floats, but values returned from contracts are BigInts, and those in turn are handled either as strings or as BigNumbers. The difficulty lies in making sure that user inputs (e.g. 22.0) and precise values from the contracts do not clash. For example, when a user clicks USE ALL, they expect their wallet balance afterwards to be precisely zero, and therefore, the values provided to the contracts need to be uncorrupted BigInts. In parts of the code, the approach that is being used is to have two sets of numbers, for example, value (typeof = float) and value_Wei_String (typeof = string). For USE ALL operations, the system must only ever use value_Wei_String as inputs. The code above accomplishes this by maintaining value and value_Wei_String.

By contrast, here is an example of a bad way to do this:

Screen Shot 2021-09-22 at 10 18 05 AM

Where is the problem? handleStakeValue takes value inputs, and converts those into value_Wei_String. That's wrong, since the actual value of the value_Wei_String will get corrupted by value_Wei_String->value->maxValue->value->value_Wei_String.

ToDo - Develop a better way to handle values overall, and then, implement and test that system across the gateway.