-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix finance USD conversion mechanism #1177
Changes from 1 commit
932c6e4
a9f9313
94d4be6
122d497
d36ec33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
import BN from 'bn.js' | ||
|
||
const CONVERT_API_RETRY_DELAY = 2 * 1000 | ||
const CONVERT_API_RETRY_DELAY_MAX = 60 * 1000 | ||
|
||
const USD_DECIMALS = new BN('2') | ||
|
||
function convertRatesUrl(symbolsQuery) { | ||
return `https://min-api.cryptocompare.com/data/price?fsym=USD&tsyms=${symbolsQuery}` | ||
} | ||
|
||
function formatConvertRate(convertRate, decimals) { | ||
const [whole = '', dec = ''] = convertRate.split('.') | ||
const parsedWhole = whole.replace(/^0*/, '') | ||
const parsedDec = dec.replace(/0*$/, '') | ||
// parsedWhole could be empty, | ||
// so in this case, we wanna remove leading zeros. | ||
const fullyParsedDec = parsedWhole ? parsedDec : parsedDec.replace(/^0*/, '') | ||
|
||
// Even if we remove leading zeroes from the decimal | ||
// part, we want to count as if we "shifted" them | ||
const decimalsToShift = decimals.sub(new BN(parsedDec.length.toString())) | ||
// Apart from always considering the USD decimals (2), | ||
// if there's the strange case that the above is negative, | ||
// we take it as a carry as we know we already shifted to far, | ||
// and will compensate by shifting the token amount by this much | ||
const carryAmount = | ||
decimalsToShift.toNumber() < 0 | ||
? decimalsToShift.add(USD_DECIMALS) | ||
: USD_DECIMALS | ||
// The remaining total amount to shift through bn.js to avoid overflow. | ||
const amountToShift = new BN('10').pow( | ||
decimalsToShift.toNumber() > 0 ? decimalsToShift : new BN('0') | ||
) | ||
|
||
// Finish shifting the whole number through BN.js to avoid overflow, | ||
return [ | ||
new BN(`${parsedWhole}${fullyParsedDec}`).mul(amountToShift), | ||
carryAmount, | ||
] | ||
} | ||
|
||
export function getConvertedAmount(amount, convertRate, decimals) { | ||
const [formattedConvertRate, carryAmount] = formatConvertRate( | ||
convertRate.toString(), | ||
decimals | ||
) | ||
|
||
// Get the actual precision we need to re-add when calculations are over | ||
const precisionTarget = new BN('10').pow(decimals.sub(USD_DECIMALS)) | ||
const convertedAmount = amount | ||
// Shift the amount to take into account the USD decimals | ||
// + any leftover | ||
.mul(new BN('10').pow(carryAmount)) | ||
// Actually convert to an USD rate | ||
.div(formattedConvertRate) | ||
// Return it to its original precision | ||
// Note that we don't have to subtract the "extra carry" | ||
// as it's undone during the division | ||
.mul(precisionTarget) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we need to consider any USD decimals here, as our "USD" converted amount is still in the original token's decimals after the conversion. Wondering if we can simplify everything down to:
As we don't need to use the decimals at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could completely cut the string manipulations by multiplying the two values and relying on PR: #1180 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only worry with the above is overflow issues on many tokens due to the ceiling for safe numbers in Javascript ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the loss of precision with I am wondering if it would be easier to implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, and the solutions are not pretty :( In general these conversions are a headache when maintaining them in BigInts.
I think it could be good! it's also pretty high leverage as doing these conversions is both hard to get 100% right, and at the same time extremely common to do. Should we wait for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking that we could add it to I added the commit with the Jest configuration. What would you think of:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good to me! Let's do that. |
||
|
||
return convertedAmount | ||
} | ||
|
||
export function useConvertRates(symbols) { | ||
Evalir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const [rates, setRates] = useState({}) | ||
const retryDelay = useRef(CONVERT_API_RETRY_DELAY) | ||
|
||
const symbolsQuery = symbols.join(',') | ||
|
||
useEffect(() => { | ||
let cancelled = false | ||
let retryTimer = null | ||
|
||
const update = async () => { | ||
if (!symbolsQuery) { | ||
setRates({}) | ||
return | ||
} | ||
|
||
try { | ||
const response = await fetch(convertRatesUrl(symbolsQuery)) | ||
const rates = await response.json() | ||
if (!cancelled) { | ||
setRates(rates) | ||
retryDelay.current = CONVERT_API_RETRY_DELAY | ||
} | ||
} catch (err) { | ||
// The !cancelled check is needed in case: | ||
// 1. The fetch() request is ongoing. | ||
// 2. The component gets unmounted. | ||
// 3. An error gets thrown. | ||
// | ||
// Assuming the fetch() request keeps throwing, it would create new | ||
// requests even though the useEffect() got cancelled. | ||
if (!cancelled) { | ||
// Add more delay after every failed attempt | ||
retryDelay.current = Math.min( | ||
CONVERT_API_RETRY_DELAY_MAX, | ||
retryDelay.current * 1.2 | ||
) | ||
retryTimer = setTimeout(update, retryDelay.current) | ||
} | ||
} | ||
} | ||
update() | ||
|
||
return () => { | ||
cancelled = true | ||
clearTimeout(retryTimer) | ||
retryDelay.current = CONVERT_API_RETRY_DELAY | ||
} | ||
}, [symbolsQuery]) | ||
|
||
return rates | ||
} |
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.
I'll admit I haven't found practical examples, but I'm just making sure! 😃