Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Change all internal value handling to wei (not eth) #185

Open
wanderingstan opened this issue May 16, 2018 · 13 comments
Open

Change all internal value handling to wei (not eth) #185

wanderingstan opened this issue May 16, 2018 · 13 comments
Labels
enhancement New feature or request good first issue Good for newcomers javascript P3 Users are not significantly affected, minor cosmetic issue

Comments

@wanderingstan
Copy link
Collaborator

wanderingstan commented May 16, 2018

Currently, we store listing prices in ETH throughout the DApp (and in origin-js). This is bad practice for monetary values, as rounding problems can occur when dealing with real values instead of ints. (And it's my fault for making this bad design choice last year. 🤦‍♀️)

We should refactor to always representing prices in Wei (or fundamental unit of tokens, when that comes) and only convert to ETH at render time.

This will also prepare us for handling ERC20 tokens with arbitrary number of decimals.

@wanderingstan wanderingstan added enhancement New feature or request good first issue Good for newcomers javascript labels May 16, 2018
@micahalcorn micahalcorn added this to Backlog in Origin Project Sprints via automation May 16, 2018
@DanielVF
Copy link
Member

I'm thinking we'll be passing and returning these numbers as strings from the origin.js API. This lets us use a json compatible type. It's easy to create a BigNumber from a string, which you would be doing anyway to use the value.

@wanderingstan
Copy link
Collaborator Author

@DanielVF is that because BigNumbers are so hard to deal with, and always display strangely?

I'm with you in spirit, but worry about introducing a third representation format. (js Number, BigNumber, string of number)

It seems the "right" way would be to use BigNumber in the DApp, but I agree it's a pain. Anyone else have opinions on this?

@nick
Copy link
Member

nick commented May 16, 2018

Agree we should represent everything in wei and pass strings back from origin-js. web3.utils.fromWei doesn't work with numbers, it will throw Please pass numbers as strings or BigNumber objects to avoid precision errors.

@wanderingstan
Copy link
Collaborator Author

If web3.utils accepts strings (along with BigNumbers), then thats's good enough for me! I retract any hesitation about using them for origin-js/DApp.

@gaboesquivel
Copy link

gaboesquivel commented May 16, 2018

I can help on this one. Yes, Math with floating point in JS is totally broken, you want to pass the value in the smallest unit and the use UI filters to display the value in floating point to end user. You want to use number instead of string to avoid parsing to make math operations.

Stripe API is a good reference
https://stripe.com/docs/currencies#zero-decimal

Dinero.js is a great lib for the UI
https://sarahdayan.github.io/dinero.js

To be more accurate :), in JSON/JS there's no BigNumber only number primitive type
http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2.1

BigNumber could be enforced with Flow. This could be handy too https://github.com/MikeMcl/bignumber.js/

@micahalcorn micahalcorn moved this from Backlog to 🏗Prioritized 5/30/18-6/12/18 in Origin Project Sprints May 30, 2018
@micahalcorn micahalcorn moved this from 🏗Prioritized 5/30/18-6/12/18 to 📅Prioritized 6/13/18-6/27/18 in Origin Project Sprints Jun 15, 2018
@micahalcorn micahalcorn moved this from 📅Prioritized 6/13/18-6/26/18 to 💬Prioritized 6/27/18-7/10/18 in Origin Project Sprints Jun 28, 2018
@micahalcorn micahalcorn moved this from 💬Prioritized 6/27/18-7/10/18 to ? Prioritized 7/11/18-7/24/18 in Origin Project Sprints Jul 11, 2018
@micahalcorn micahalcorn moved this from ⚽️ Prioritized 7/11/18-7/24/18 to 🚢Prioritized 7/25/18-8/7/18 in Origin Project Sprints Jul 25, 2018
@gustavoguimaraes
Copy link

Hi all,

Is this issue still applicable? I see some commits that changes the prices from wei to eth for rendering purposes only in the codebase. This issue shows as a good first issue but I cannot tell if it is still relevant given that it is from May.

@wanderingstan
Copy link
Collaborator Author

Hi @gustavoguimaraes thanks for bringing this up. I believe things have migrated a bit in the codebase and now we actually are storing things in raw eth in many places. This is a discussion we need to re-visit.

So yes, don't do any work on this until we figure out what path we want to take.

@franckc
Copy link
Collaborator

franckc commented Jan 4, 2019

I don't think this would require any change to the JSON schemas (since there is no validation at the schema level of the monetary unit being used). But logic code in origin-js would be impacted. Since with the GraphQL rewrite we plan to replace origin-js with new code, this could be a good opportunity to clean this up ?

I could imagine having a "Money" class at the business logic layer that gets constructed off data from IPFS. That constructor should be smart enough to handle legacy IPFS data for old listings/offers that were not using natural units. Then the Money object would be what gets used throughout the DApp. And it would provide helper methods like basic math operations, getting token symbol, unit / natural unit conversion and representation, etc...

Also, we had discussed storing the ERC20 address rather than its symbol (and using a special address like 0x0 for ETH) in the IPFS data. The address could go in the existing "currency" string field of the JSON schema or it could be defined as a new field "address".

@micahalcorn
Copy link
Member

Just leaving a note here that @crazybuster and I are having trouble dealing with BigNumbers 😕

say-what

According to the docs...

toBN
Will safely convert any given value (including BigNumber.js instances) into a BN.js instance, for handling big numbers in JavaScript.

isBN
Checks if a given value is a BN.js instance.

@DanielVF
Copy link
Member

DanielVF commented Feb 1, 2019

@micahalcorn Ever find out what was the deal with isBN?

@micahalcorn
Copy link
Member

Nope 😕

@DanielVF
Copy link
Member

DanielVF commented Feb 1, 2019

Well, this is fun. It works fine in dapp2.

image

The web3 code for utils.isBN hasn't changed in a year, nor has the code for web3 code toBN. The BN dependencies used by web.utils have not have their version numbers changed in two years.

It looks like the difference is in what is returned by "toBN". I wonder if there's some kind of webpack weirdness going on with DAPP1.

image

Sadly, I've got to run for now before chasing this all the way down to the ground.

@micahalcorn micahalcorn added the P3 Users are not significantly affected, minor cosmetic issue label Jun 12, 2019
@micahalcorn
Copy link
Member

Still relevant per @nick 🙂

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers javascript P3 Users are not significantly affected, minor cosmetic issue
Projects
None yet
Development

No branches or pull requests

7 participants