@@ -4,44 +4,58 @@ import { PRICE_FACTOR } from "../../config/constants"
44import type { EntityManager } from "fasset-indexer-core/orm"
55
66
7- export function fassetDecimals ( fasset : FAssetType ) : number {
8- if ( fasset == FAssetType . FXRP ) {
9- return 6
10- } else if ( fasset == FAssetType . FBTC ) {
11- return 8
12- } else if ( fasset == FAssetType . FDOGE ) {
13- return 8
14- } else if ( fasset == FAssetType . FLTC ) {
15- return 8
16- } else if ( fasset == FAssetType . FALG ) {
17- return 6
18- } else {
19- throw new Error ( `Decimals not known for fasset ${ FAssetType [ fasset ] } ` )
7+ export class FAssetPriceLoader {
8+ private cache : Map < FAssetType , [ bigint , bigint ] > = new Map ( )
9+
10+ async getFAssetToUsdPrice ( em : EntityManager , fasset : FAssetType ) : Promise < [ mul : bigint , div : bigint ] > {
11+ if ( ! this . cache . has ( fasset ) ) {
12+ const price = await this . fassetToUsdPrice ( em , fasset )
13+ this . cache . set ( fasset , price )
14+ }
15+ return this . cache . get ( fasset )
16+ }
17+
18+ // doesn't need caching yet
19+ async fassetToUsd ( em : EntityManager , fasset : FAssetType , amount : bigint ) : Promise < bigint > {
20+ const [ mul , div ] = await this . fassetToUsdPrice ( em , fasset )
21+ return PRICE_FACTOR * amount * mul / div
22+ }
23+
24+ // doens't need caching yet
25+ async tokenToUsd ( em : EntityManager , address : string , amount : bigint ) : Promise < bigint > {
26+ const [ mul , div ] = await this . tokenToUsdPrice ( em , address )
27+ return PRICE_FACTOR * amount * mul / div
28+ }
29+
30+ protected fassetDecimals ( fasset : FAssetType ) : number {
31+ if ( fasset == FAssetType . FXRP ) {
32+ return 6
33+ } else if ( fasset == FAssetType . FBTC ) {
34+ return 8
35+ } else if ( fasset == FAssetType . FDOGE ) {
36+ return 8
37+ } else if ( fasset == FAssetType . FLTC ) {
38+ return 8
39+ } else if ( fasset == FAssetType . FALG ) {
40+ return 6
41+ } else {
42+ throw new Error ( `Decimals not known for fasset ${ FAssetType [ fasset ] } ` )
43+ }
44+ }
45+
46+ protected async fassetToUsdPrice ( em : EntityManager , fasset : FAssetType ) : Promise < [ mul : bigint , div : bigint ] > {
47+ if ( fasset == FAssetType . FSIMCOINX || fasset == FAssetType . FLTC || fasset == FAssetType . FALG ) {
48+ return [ BigInt ( 0 ) , BigInt ( 1 ) ]
49+ }
50+ const fassetToken = await em . findOneOrFail ( CollateralTypeAdded , { fasset } )
51+ const fassetPrice = await em . findOneOrFail ( FtsoPrice , { symbol : fassetToken . assetFtsoSymbol } )
52+ const fassetTokenDecimals = this . fassetDecimals ( fasset )
53+ return [ fassetPrice . price , BigInt ( 10 ) ** BigInt ( fassetPrice . decimals + fassetTokenDecimals ) ]
2054 }
21- }
2255
23- export async function fassetToUsdPrice ( em : EntityManager , fasset : FAssetType ) : Promise < [ mul : bigint , div : bigint ] > {
24- if ( fasset == FAssetType . FSIMCOINX || fasset == FAssetType . FLTC || fasset == FAssetType . FALG ) {
25- return [ BigInt ( 0 ) , BigInt ( 1 ) ]
56+ protected async tokenToUsdPrice ( em : EntityManager , address : string ) : Promise < [ mul : bigint , div : bigint ] > {
57+ const token = await em . findOneOrFail ( CollateralTypeAdded , { address : { hex : address } } )
58+ const price = await em . findOneOrFail ( FtsoPrice , { symbol : token . tokenFtsoSymbol } )
59+ return [ price . price , BigInt ( 10 ) ** BigInt ( price . decimals + token . decimals ) ]
2660 }
27- const fassetToken = await em . findOneOrFail ( CollateralTypeAdded , { fasset } )
28- const fassetPrice = await em . findOneOrFail ( FtsoPrice , { symbol : fassetToken . assetFtsoSymbol } )
29- const fassetTokenDecimals = fassetDecimals ( fasset )
30- return [ fassetPrice . price , BigInt ( 10 ) ** BigInt ( fassetPrice . decimals + fassetTokenDecimals ) ]
31- }
32-
33- export async function tokenToUsdPrice ( em : EntityManager , address : string ) : Promise < [ mul : bigint , div : bigint ] > {
34- const token = await em . findOneOrFail ( CollateralTypeAdded , { address : { hex : address } } )
35- const price = await em . findOneOrFail ( FtsoPrice , { symbol : token . tokenFtsoSymbol } )
36- return [ price . price , BigInt ( 10 ) ** BigInt ( price . decimals + token . decimals ) ]
37- }
38-
39- export async function fassetToUsd ( em : EntityManager , fasset : FAssetType , amount : bigint ) : Promise < bigint > {
40- const [ mul , div ] = await fassetToUsdPrice ( em , fasset )
41- return PRICE_FACTOR * amount * mul / div
42- }
43-
44- export async function tokenToUsd ( em : EntityManager , address : string , amount : bigint ) : Promise < bigint > {
45- const [ mul , div ] = await tokenToUsdPrice ( em , address )
46- return PRICE_FACTOR * amount * mul / div
4761}
0 commit comments