UNPKG

@atomiqlabs/sdk

Version:

atomiq labs SDK for cross-chain swaps between smart chains and bitcoin

47 lines (38 loc) 1.54 kB
import {CoinType, CtorCoinTypes} from "../abstract/IPriceProvider"; import {HttpPriceProvider} from "./abstract/HttpPriceProvider"; import {MultiChain} from "../../swapper/Swapper"; import {httpGet} from "../../http/HttpUtils"; export type CoinGeckoResponse<Currency extends string> = { [coinId: string]: {[c in Currency]: number} }; /** * Price provider using CoinGecko API * @category Pricing */ export class CoinGeckoPriceProvider<T extends MultiChain> extends HttpPriceProvider<T> { constructor(coinsMap: CtorCoinTypes<T>, url: string = "https://api.coingecko.com/api/v3", httpRequestTimeout?: number) { super(coinsMap, url, httpRequestTimeout); } /** * @inheritDoc */ protected async fetchPrice(token: CoinType, abortSignal?: AbortSignal): Promise<bigint> { let response = await httpGet<CoinGeckoResponse<"sats">>( this.url+"/simple/price?ids="+token.coinId+"&vs_currencies=sats&precision=6", this.httpRequestTimeout, abortSignal ); return BigInt(response[token.coinId].sats*1000000); } /** * @inheritDoc */ protected async fetchUsdPrice(abortSignal?: AbortSignal): Promise<number> { let response = await httpGet<CoinGeckoResponse<"usd">>( this.url+"/simple/price?ids=bitcoin&vs_currencies=usd&precision=9", this.httpRequestTimeout, abortSignal ); return response["bitcoin"].usd/100000000; } }