From c02b8ab1bd662cb510b445e0fe29b854d44e01af Mon Sep 17 00:00:00 2001 From: Richard Nguyen Date: Thu, 22 Aug 2024 14:23:38 +0700 Subject: [PATCH] add pool v2 price function (#35) --- src/adapter.ts | 36 ++++++++++++++++++++++++++++++++++++ src/dex-v2.ts | 44 ++++++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/src/adapter.ts b/src/adapter.ts index ba95b49..c5780cb 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -43,6 +43,12 @@ export type GetPoolPriceParams = { decimalsB?: number; }; +export type GetV2PoolPriceParams = { + pool: PoolV2.State; + decimalsA?: number; + decimalsB?: number; +}; + export type GetPoolHistoryParams = PaginationOptions & { id: string; }; @@ -326,6 +332,36 @@ export class BlockfrostAdapter { ); } + /** + * Get pool price. + * @param {Object} params - The parameters to calculate pool price. + * @param {string} params.pool - The pool we want to get price. + * @param {string} [params.decimalsA] - The decimals of assetA in pool, if undefined then query from Blockfrost. + * @param {string} [params.decimalsB] - The decimals of assetB in pool, if undefined then query from Blockfrost. + * @returns {[string, string]} - Returns a pair of asset A/B price and B/A price, adjusted to decimals. + */ + public async getV2PoolPrice({ + pool, + decimalsA, + decimalsB, + }: GetV2PoolPriceParams): Promise<[Big, Big]> { + if (decimalsA === undefined) { + decimalsA = await this.getAssetDecimals(pool.assetA); + } + if (decimalsB === undefined) { + decimalsB = await this.getAssetDecimals(pool.assetB); + } + const adjustedReserveA = Big(pool.reserveA.toString()).div( + Big(10).pow(decimalsA) + ); + const adjustedReserveB = Big(pool.reserveB.toString()).div( + Big(10).pow(decimalsB) + ); + const priceAB = adjustedReserveA.div(adjustedReserveB); + const priceBA = adjustedReserveB.div(adjustedReserveA); + return [priceAB, priceBA]; + } + public async getAllStablePools(): Promise<{ pools: StablePool.State[]; errors: unknown[]; diff --git a/src/dex-v2.ts b/src/dex-v2.ts index fdd681b..5f9b9b4 100644 --- a/src/dex-v2.ts +++ b/src/dex-v2.ts @@ -54,7 +54,7 @@ export type BulkOrdersOption = { expiredOptions?: OrderV2.ExpirySetting; availableUtxos: UTxO[]; composeTx?: Tx; - authorizationMethodType? : OrderV2.AuthorizationMethodType; + authorizationMethodType?: OrderV2.AuthorizationMethodType; }; export type OrderV2SwapRouting = { @@ -167,8 +167,8 @@ export type OrderOptions = ( export type CancelBulkOrdersOptions = { orderOutRefs: OutRef[]; - composeTx? : Tx, - AuthorizationMethodType? : OrderV2.AuthorizationMethodType; + composeTx?: Tx; + AuthorizationMethodType?: OrderV2.AuthorizationMethodType; }; export class DexV2 { @@ -737,8 +737,8 @@ export class DexV2 { expiredOptions, availableUtxos, composeTx, - authorizationMethodType - }: BulkOrdersOption): Promise { + authorizationMethodType, + }: BulkOrdersOption): Promise { // calculate total order value const totalOrderAssets: Record = {}; for (const option of orderOptions) { @@ -780,16 +780,22 @@ export class DexV2 { orderAssets["lovelace"] = totalBatcherFee; } - const senderPaymentCred = this.lucid.utils.getAddressDetails(sender).paymentCredential; - invariant( senderPaymentCred, "sender address payment credentials not found"); + const senderPaymentCred = + this.lucid.utils.getAddressDetails(sender).paymentCredential; + invariant( + senderPaymentCred, + "sender address payment credentials not found" + ); - const canceller = authorizationMethodType ? { - type: authorizationMethodType, - hash: senderPaymentCred.hash, - } : { - type: OrderV2.AuthorizationMethodType.SIGNATURE, - hash: senderPaymentCred.hash, - }; + const canceller = authorizationMethodType + ? { + type: authorizationMethodType, + hash: senderPaymentCred.hash, + } + : { + type: OrderV2.AuthorizationMethodType.SIGNATURE, + hash: senderPaymentCred.hash, + }; const orderDatum: OrderV2.Datum = { canceller: canceller, refundReceiver: sender, @@ -834,17 +840,15 @@ export class DexV2 { limitOrders: limitOrderMessage, }); lucidTx.payToAddress(sender, reductionAssets); - if (composeTx){ + if (composeTx) { lucidTx.compose(composeTx); } return lucidTx.complete(); - } async cancelOrder({ orderOutRefs, composeTx, - }: CancelBulkOrdersOptions): Promise { const v2OrderScriptHash = this.getOrderScriptHash(); const orderUtxos = await this.lucid.utxosByOutRef(orderOutRefs); @@ -890,8 +894,8 @@ export class DexV2 { ); } - if(datum.canceller.type === OrderV2.AuthorizationMethodType.SIGNATURE) - requiredPubKeyHashSet.add(datum.canceller.hash); + if (datum.canceller.type === OrderV2.AuthorizationMethodType.SIGNATURE) + requiredPubKeyHashSet.add(datum.canceller.hash); } const redeemer = Data.to( new Constr(OrderV2.Redeemer.CANCEL_ORDER_BY_OWNER, []) @@ -904,7 +908,7 @@ export class DexV2 { lucidTx.attachMetadata(674, { msg: [MetadataMessage.CANCEL_ORDER], }); - if (composeTx){ + if (composeTx) { lucidTx.compose(composeTx); } return lucidTx.complete();