From 365a6042c848c5d6c6eb9cbd049e741456ae89fd Mon Sep 17 00:00:00 2001 From: shoom3301 Date: Tue, 26 Nov 2024 18:36:17 +0500 Subject: [PATCH] chore: add mapQuoteAmountsAndCosts util --- package.json | 2 +- src/trading/index.ts | 2 +- src/trading/utils.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c787084a..3c27ac2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cowprotocol/cow-sdk", - "version": "5.8.0-RC.6", + "version": "5.8.0-RC.7", "license": "(MIT OR Apache-2.0)", "files": [ "/dist" diff --git a/src/trading/index.ts b/src/trading/index.ts index 837d4d3c..e316e43b 100644 --- a/src/trading/index.ts +++ b/src/trading/index.ts @@ -18,4 +18,4 @@ export { getEthFlowTransaction } from './getEthFlowTransaction' export * from './appDataUtils' export * from './calculateUniqueOrderId' -export { swapParamsToLimitOrderParams } from './utils' +export { swapParamsToLimitOrderParams, mapQuoteAmountsAndCosts } from './utils' diff --git a/src/trading/utils.ts b/src/trading/utils.ts index 67965efb..45303a91 100644 --- a/src/trading/utils.ts +++ b/src/trading/utils.ts @@ -36,3 +36,39 @@ export function getSigner(signer: Signer | ExternalProvider | PrivateKey): Signe export function isAccountAddress(address: any): address is AccountAddress { return typeof address === 'string' && /^0x[0-9a-fA-F]{40}$/.test(address) } + +export function mapQuoteAmountsAndCosts( + value: QuoteAmountsAndCosts, + mapper: (value: T) => R +): QuoteAmountsAndCosts { + const { + costs: { networkFee, partnerFee }, + } = value + + function serializeAmounts(value: { sellAmount: T; buyAmount: T }): { sellAmount: R; buyAmount: R } { + return { + sellAmount: mapper(value.sellAmount), + buyAmount: mapper(value.buyAmount), + } + } + + return { + ...value, + costs: { + ...value.costs, + networkFee: { + ...networkFee, + amountInSellCurrency: mapper(networkFee.amountInSellCurrency), + amountInBuyCurrency: mapper(networkFee.amountInBuyCurrency), + }, + partnerFee: { + ...partnerFee, + amount: mapper(partnerFee.amount), + }, + }, + beforeNetworkCosts: serializeAmounts(value.beforeNetworkCosts), + afterNetworkCosts: serializeAmounts(value.afterNetworkCosts), + afterPartnerFees: serializeAmounts(value.afterPartnerFees), + afterSlippage: serializeAmounts(value.afterSlippage), + } +}