This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
Fix USDT payment #4306
Closed
Closed
Fix USDT payment #4306
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e337862
Fix USDT payment
shahthepro b52ed8f
Prettify
shahthepro ecd4bd2
Fixes
shahthepro 73188f4
Merge branch 'master' into shahthepro/fix-usdt
8f24e37
Fix tests
shahthepro 6ef3c1a
Merge branch 'shahthepro/fix-usdt' of github.com:OriginProtocol/origi…
shahthepro 03c332c
Prettify
shahthepro 890441e
Fix allowance
shahthepro 44f925a
Fix make offer mutation
shahthepro 1d7d259
Merge branch 'master' into shahthepro/fix-usdt
shahthepro d45eb8b
Make things simpler
shahthepro 5bc6a64
Merge branch 'master' into shahthepro/fix-usdt
shahthepro e2e8301
Merge branch 'master' into shahthepro/fix-usdt
shahthepro 8397823
Lint
shahthepro 0c96062
Exclude fiat
shahthepro e69100b
Update return type
shahthepro 598e8df
Fix tests
shahthepro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import memoize from 'lodash/memoize' | ||
import contracts from '../contracts' | ||
import currencies from './currencies' | ||
|
||
/** | ||
* Get the ether unit that has a certain number of decimals | ||
* | ||
* Ref: https://web3js.readthedocs.io/en/v1.2.0/web3-utils.html#id88 | ||
* | ||
* @param {Number} decimals number of decimals | ||
* | ||
* @returns {String} the ether unit that has `decimals` decimals | ||
*/ | ||
const getUnitWithDecimals = memoize(decimals => { | ||
const web3 = contracts.web3 | ||
|
||
const unit = Object.keys(web3.utils.unitMap).find(unit => { | ||
const valueInWei = web3.utils.unitMap[unit] | ||
|
||
const zeroCount = valueInWei.length - valueInWei.replace(/0/g, '').length | ||
|
||
return zeroCount === decimals | ||
}) | ||
|
||
return unit | ||
}) | ||
|
||
/** | ||
* Truncates the big numbers upto `n` decimals | ||
* | ||
* @param {String} numStr The big number as string | ||
* @param {Number} decimals Number of decimals to truncate to | ||
* | ||
* @return {String} truncated number as string | ||
*/ | ||
function removeExtraDecimals(numStr, decimals) { | ||
return numStr.replace( | ||
new RegExp(`^([0-9]+\\.[0-9]{${decimals || 18}}).*`), | ||
'$1' | ||
) | ||
} | ||
|
||
/** | ||
* Takes in a token value with decimals and returns the token's value | ||
* with the smallest unit possible without any decimals | ||
* | ||
* @param {String} amount The token value with decimals | ||
* @param {String|Number} token The token ID or the number of decimals | ||
* | ||
* @returns {String} `amount` represented in smallest unit of `token` | ||
*/ | ||
function getUnitTokenValue(amount, tokenOrDecimals) { | ||
const web3 = contracts.web3 | ||
|
||
let tokenDecimals = tokenOrDecimals | ||
|
||
if (typeof tokenOrDecimals === 'string') { | ||
const tokenObj = currencies.data[tokenOrDecimals] | ||
|
||
if (!tokenObj) { | ||
// Fallback | ||
return web3.utils.toWei(removeExtraDecimals(amount, 18), 'ether') | ||
} | ||
|
||
tokenDecimals = tokenObj.decimals | ||
} | ||
|
||
const targetWei = removeExtraDecimals(amount, tokenDecimals) | ||
|
||
const targetUnit = getUnitWithDecimals(tokenDecimals) | ||
|
||
const targetValue = web3.utils.toBN(web3.utils.toWei(targetWei, targetUnit)) | ||
|
||
return targetValue | ||
} | ||
|
||
export default getUnitTokenValue |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT web3 units for each zero count don't exist. Sometimes there are 3 zeros between the 2 closest units. So the above function might miss the correct unit in case its' decimal count is between 2 units. (for example between
szabo
-> 12 zeros andnano
-> 9 zeros)Maybe one suggestion as how to amend the above implementation:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this @sparrowDom, Will fix this