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), + } +}