1+ import { ExchangeRate , FixedPoint , Money , currencies } from "@thesis-co/cent"
12import {
3+ AnyAsset ,
24 AnyAssetAmount ,
5+ AssetMetadata ,
6+ CoinGeckoAsset ,
7+ DisplayCurrency ,
8+ FiatCurrency ,
9+ FungibleAsset ,
10+ PricePoint ,
11+ SmartContractFungibleAsset ,
12+ UnitPricePoint ,
313 assetAmountToDesiredDecimals ,
414 convertAssetAmountViaPricePoint ,
5- unitPricePointForPricePoint ,
615 isFungibleAssetAmount ,
7- PricePoint ,
8- FungibleAsset ,
9- UnitPricePoint ,
10- AnyAsset ,
11- CoinGeckoAsset ,
1216 isSmartContractFungibleAsset ,
13- SmartContractFungibleAsset ,
14- AssetMetadata ,
15- DisplayCurrency ,
17+ unitPricePointForPricePoint ,
1618} from "../../assets"
1719import {
1820 BUILT_IN_NETWORK_BASE_ASSETS ,
1921 OPTIMISM ,
2022 POLYGON ,
21- USD ,
2223} from "../../constants"
2324import { fromFixedPointNumber } from "../../lib/fixed-point"
2425import { sameEVMAddress } from "../../lib/utils"
@@ -203,7 +204,7 @@ export function formatCurrencyAmount(
203204export function convertUSDPricePointToCurrency (
204205 pricePoint : PricePoint ,
205206 currency : DisplayCurrency ,
206- ) : PricePoint {
207+ ) {
207208 const { pair, amounts, time } = pricePoint
208209 const idx = pricePoint . pair . findIndex ( ( asset ) => asset . symbol === "USD" )
209210
@@ -213,23 +214,35 @@ export function convertUSDPricePointToCurrency(
213214 time,
214215 }
215216
216- newPricePoint . pair [ idx ] = currency
217- newPricePoint . amounts [ idx ] *= currency . rate
217+ const { name, code } = currencies [ currency . code ]
218218
219- return newPricePoint
220- }
221-
222- export function convertUSDAmountToCurrency (
223- assetAmount : AnyAssetAmount ,
224- currency : DisplayCurrency ,
225- ) {
226- const pricePoint : PricePoint = {
227- pair : [ USD , currency ] ,
228- amounts : [ 1n * 10n ** BigInt ( USD . decimals ) , currency . rate ] ,
229- time : Date . now ( ) ,
219+ const asset : FiatCurrency = {
220+ name,
221+ symbol : code ,
222+ decimals : 10 ,
230223 }
231224
232- return convertAssetAmountViaPricePoint ( assetAmount , pricePoint )
225+ const rate = new ExchangeRate ( {
226+ // Keeping 10 decimals ensures we don't lose precision during conversion
227+ baseCurrency : { ...currencies [ currency . code ] , decimals : 10n } ,
228+ quoteCurrency : currencies . USD ,
229+ rate : FixedPoint ( currency . rate ) ,
230+ } )
231+
232+ newPricePoint . pair [ idx ] = asset
233+ newPricePoint . amounts [ idx ] = rate
234+ . convert (
235+ Money ( {
236+ asset : currencies . USD ,
237+ amount : FixedPoint ( {
238+ amount : newPricePoint . amounts [ idx ] ,
239+ decimals : 10n ,
240+ } ) ,
241+ } ) ,
242+ )
243+ . concretize ( ) [ 0 ] . balance . amount . amount
244+
245+ return newPricePoint
233246}
234247
235248/**
@@ -269,47 +282,49 @@ export function enrichAssetAmountWithMainCurrencyValues<
269282 desiredDecimals : number ,
270283 displayCurrency : DisplayCurrency ,
271284) : T & AssetMainCurrencyAmount {
272- // Converts to USD as price points are in USD
273- const assetAmountInUSD = convertAssetAmountViaPricePoint (
274- assetAmount ,
275- assetPricePoint ,
276- )
277- const { unitPrice } = unitPricePointForPricePoint ( assetPricePoint ) ?? {
278- unitPrice : undefined ,
279- }
280-
281- if ( typeof assetAmountInUSD !== "undefined" ) {
282- const assetAmountInFiatCurrency = convertUSDAmountToCurrency (
283- assetAmountInUSD ,
285+ if ( assetPricePoint ) {
286+ // Create a temporary pricepoint in the target user currency
287+ const currencyPricePoint = convertUSDPricePointToCurrency (
288+ assetPricePoint ,
284289 displayCurrency ,
285- ) !
290+ )
286291
287- const convertedDecimalValue = assetAmountToDesiredDecimals (
288- assetAmountInFiatCurrency ,
289- desiredDecimals ,
292+ const assetAmountInCurrency = convertAssetAmountViaPricePoint (
293+ assetAmount ,
294+ currencyPricePoint ,
290295 )
291- const unitPriceDecimalValue =
292- typeof unitPrice === "undefined"
293- ? undefined
294- : assetAmountToDesiredDecimals ( unitPrice , desiredDecimals )
295-
296- return {
297- ...assetAmount ,
298- mainCurrencyAmount : convertedDecimalValue ,
299- localizedMainCurrencyAmount : formatCurrencyAmount (
300- displayCurrency . symbol ,
301- convertedDecimalValue ,
296+ const { unitPrice } = unitPricePointForPricePoint ( currencyPricePoint ) ?? {
297+ unitPrice : undefined ,
298+ }
299+
300+ if ( typeof assetAmountInCurrency !== "undefined" ) {
301+ const convertedDecimalValue = assetAmountToDesiredDecimals (
302+ assetAmountInCurrency ,
302303 desiredDecimals ,
303- ) ,
304- unitPrice : unitPriceDecimalValue ,
305- localizedUnitPrice :
306- typeof unitPriceDecimalValue === "undefined"
304+ )
305+ const unitPriceDecimalValue =
306+ typeof unitPrice === "undefined"
307307 ? undefined
308- : formatCurrencyAmount (
309- assetAmountInUSD . asset . symbol ,
310- unitPriceDecimalValue ,
311- desiredDecimals ,
312- ) ,
308+ : assetAmountToDesiredDecimals ( unitPrice , desiredDecimals )
309+
310+ return {
311+ ...assetAmount ,
312+ mainCurrencyAmount : convertedDecimalValue ,
313+ localizedMainCurrencyAmount : formatCurrencyAmount (
314+ displayCurrency . code ,
315+ convertedDecimalValue ,
316+ desiredDecimals ,
317+ ) ,
318+ unitPrice : unitPriceDecimalValue ,
319+ localizedUnitPrice :
320+ typeof unitPriceDecimalValue === "undefined"
321+ ? undefined
322+ : formatCurrencyAmount (
323+ assetAmountInCurrency . asset . symbol ,
324+ unitPriceDecimalValue ,
325+ desiredDecimals ,
326+ ) ,
327+ }
313328 }
314329 }
315330
0 commit comments