UNPKG

@atomiqlabs/sdk

Version:

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

195 lines (179 loc) 7.44 kB
import {ISwapPrice} from "./abstract/ISwapPrice"; import {ChainIds, MultiChain} from "../swapper/Swapper"; import {Token} from "../types/Token"; import {PriceInfoType} from "../types/PriceInfoType"; /** * Chain-specific wrapper for swap pricing * * @category Pricing */ export class SwapPriceWithChain<T extends MultiChain, ChainIdentifier extends ChainIds<T>> { swapPrice: ISwapPrice<T>; chainIdentifier: ChainIdentifier; maxAllowedFeeDifferencePPM: bigint; constructor(swapPrice: ISwapPrice<T>, chainIdentifier: ChainIdentifier) { this.swapPrice = swapPrice; this.chainIdentifier = chainIdentifier; this.maxAllowedFeeDifferencePPM = swapPrice.maxAllowedFeeDifferencePPM; } /** * Checks whether the swap amounts are valid given the current market rate for a given pair * * @param amountSats Amount of sats (BTC) to be received from the swap * @param satsBaseFee Base fee in sats (BTC) as reported by the intermediary * @param feePPM PPM fee rate as reported by the intermediary * @param paidToken Amount of token to be paid to the swap * @param tokenAddress Token address to be paid * @param abortSignal Abort signal * @param preFetchedPrice An optional price pre-fetched with {@link preFetchPrice} */ public async isValidAmountSend( amountSats: bigint, satsBaseFee: bigint, feePPM: bigint, paidToken: bigint, tokenAddress: string, abortSignal?: AbortSignal, preFetchedPrice?: bigint ): Promise<PriceInfoType> { return this.swapPrice.isValidAmountSend<ChainIdentifier>( this.chainIdentifier, amountSats, satsBaseFee, feePPM, paidToken, tokenAddress, abortSignal, preFetchedPrice ); } /** * Checks whether the swap amounts are valid given the current market rate for a given pair * * @param amountSats Amount of sats (BTC) to be paid to the swap * @param satsBaseFee Base fee in sats (BTC) as reported by the intermediary * @param feePPM PPM fee rate as reported by the intermediary * @param receiveToken Amount of token to be received from the swap * @param tokenAddress Token address to be received * @param abortSignal Abort signal * @param preFetchedPrice An optional price pre-fetched with {@link preFetchPrice} */ public async isValidAmountReceive( amountSats: bigint, satsBaseFee: bigint, feePPM: bigint, receiveToken: bigint, tokenAddress: string, abortSignal?: AbortSignal, preFetchedPrice?: bigint ): Promise<PriceInfoType> { return this.swapPrice.isValidAmountReceive<ChainIdentifier>( this.chainIdentifier, amountSats, satsBaseFee, feePPM, receiveToken, tokenAddress, abortSignal, preFetchedPrice ); } /** * Pre-fetches the pricing data for a given token, such that further calls to {@link isValidAmountReceive} or * {@link isValidAmountSend} are quicker and don't need to wait for the price fetch * * @param tokenAddress Token address * @param abortSignal Abort signal */ public preFetchPrice(tokenAddress: string, abortSignal?: AbortSignal): Promise<bigint> { return this.swapPrice.preFetchPrice<ChainIdentifier>(this.chainIdentifier, tokenAddress, abortSignal); } /** * Pre-fetches the Bitcoin USD price data, such that further calls to {@link getBtcUsdValue}, * {@link getTokenUsdValue} or {@link getUsdValue} are quicker and don't need to wait for the price fetch * * @param abortSignal */ public preFetchUsdPrice(abortSignal?: AbortSignal): Promise<number> { return this.swapPrice.preFetchUsdPrice(abortSignal); } /** * Returns amount of `toToken` that is equivalent to `fromAmount` satoshis * * @param fromAmount Amount of satoshis * @param toToken Token address * @param abortSignal * @param preFetchedPrice An optional price pre-fetched with {@link preFetchPrice} * @throws {Error} when token is not found */ public async getFromBtcSwapAmount( fromAmount: bigint, toToken: string, abortSignal?: AbortSignal, preFetchedPrice?: bigint ): Promise<bigint> { return this.swapPrice.getFromBtcSwapAmount<ChainIdentifier>( this.chainIdentifier, fromAmount, toToken, abortSignal, preFetchedPrice ); } /** * Returns amount of satoshis that are equivalent to `fromAmount` of `fromToken` * * @param fromAmount Amount of the token * @param fromToken Token address * @param abortSignal * @param preFetchedPrice An optional price pre-fetched with {@link preFetchPrice} * @throws {Error} when token is not found */ public async getToBtcSwapAmount( fromAmount: bigint, fromToken: string, abortSignal?: AbortSignal, preFetchedPrice?: bigint ): Promise<bigint> { return this.swapPrice.getToBtcSwapAmount<ChainIdentifier>( this.chainIdentifier, fromAmount, fromToken, abortSignal, preFetchedPrice ); } /** * Returns whether the token should be ignored and pricing for it not calculated * * @param tokenAddress Token address * @throws {Error} if token is not found */ public shouldIgnore(tokenAddress: string): boolean { return this.swapPrice.shouldIgnore<ChainIdentifier>(this.chainIdentifier, tokenAddress); } /** * Returns the USD value of the bitcoin amount * * @param btcSats Bitcoin amount in satoshis * @param abortSignal * @param preFetchedUsdPrice An optional price pre-fetched with {@link preFetchUsdPrice} */ public async getBtcUsdValue( btcSats: bigint, abortSignal?: AbortSignal, preFetchedUsdPrice?: number ): Promise<number> { return this.swapPrice.getBtcUsdValue(btcSats, abortSignal, preFetchedUsdPrice); } /** * Returns the USD value of the smart chain token amount * * @param tokenAmount Amount of the token in base units * @param tokenAddress Token address * @param abortSignal * @param preFetchedUsdPrice An optional price pre-fetched with {@link preFetchUsdPrice} */ public async getTokenUsdValue( tokenAmount: bigint, tokenAddress: string, abortSignal?: AbortSignal, preFetchedUsdPrice?: number ): Promise<number> { return this.swapPrice.getTokenUsdValue(this.chainIdentifier, tokenAmount, tokenAddress, abortSignal, preFetchedUsdPrice); } /** * Returns the USD value of the token amount * * @param amount Amount in base units of the token * @param token Token to fetch the usd price for * @param abortSignal * @param preFetchedUsdPrice An optional price pre-fetched with {@link preFetchUsdPrice} */ public getUsdValue( amount: bigint, token: Token<ChainIdentifier>, abortSignal?: AbortSignal, preFetchedUsdPrice?: number ): Promise<number> { return this.swapPrice.getUsdValue(amount, token, abortSignal, preFetchedUsdPrice); } }