Skip to content

Commit

Permalink
chore: add mapQuoteAmountsAndCosts util
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Nov 26, 2024
1 parent 54e279b commit 365a604
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/trading/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export { getEthFlowTransaction } from './getEthFlowTransaction'

export * from './appDataUtils'
export * from './calculateUniqueOrderId'
export { swapParamsToLimitOrderParams } from './utils'
export { swapParamsToLimitOrderParams, mapQuoteAmountsAndCosts } from './utils'
36 changes: 36 additions & 0 deletions src/trading/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, R>(
value: QuoteAmountsAndCosts<T>,
mapper: (value: T) => R
): QuoteAmountsAndCosts<R> {
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),
}
}

0 comments on commit 365a604

Please sign in to comment.