Skip to content

Commit

Permalink
feat: update totalFee calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito committed Jan 15, 2024
1 parent 755a01b commit c8d4071
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
59 changes: 59 additions & 0 deletions src/order-book/transformOrder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Order, OrderClass, OrderKind, OrderStatus, SigningScheme } from './generated'
import { transformOrder } from './transformOrder'

const ORDER: Order = {
sellToken: '0x6810e776880c02933d47db1b9fc05908e5386b96',
buyToken: '0x6810e776880c02933d47db1b9fc05908e5386b96',
sellAmount: '1234567890',
buyAmount: '1234567890',
validTo: 0,
appData: '0x0000000000000000000000000000000000000000000000000000000000000000',
partiallyFillable: true,
kind: OrderKind.BUY,
class: OrderClass.MARKET,
feeAmount: '1234567890',
signature:
'0x4d306ce7c770d22005bcfc00223f8d9aaa04e8a20099cc986cb9ccf60c7e876b777ceafb1e03f359ebc6d3dc84245d111a3df584212b5679cb5f9e6717b69b031b',
signingScheme: SigningScheme.EIP1271,
creationDate: '2020-12-03T18:35:18.814523Z',
owner: '0x6810e776880c02933d47db1b9fc05908e5386b96',
uid: '0x59920c85de0162e9e55df8d396e75f3b6b7c2dfdb535f03e5c807731c31585eaff714b8b0e2700303ec912bd40496c3997ceea2b616d6710',
executedSellAmount: '1234567890',
executedSellAmountBeforeFees: '1234567890',
executedBuyAmount: '1234567890',
executedFeeAmount: '1234567890',
invalidated: true,
status: OrderStatus.FULFILLED,
}

describe('transformOrder', () => {
describe('addTotalFeeToOrder', () => {
test('should use executedFeeAmount when executedSurplusFee is 0', () => {
const rawOrder = { ...ORDER, executedFeeAmount: '1', executedSurplusFee: '0' }
const transformedOrder = transformOrder(rawOrder)

expect(transformedOrder.totalFee).toEqual(rawOrder.executedFeeAmount)
})

test('should use executedSurplusFee when executedFeeAmount is 0', () => {
const rawOrder = { ...ORDER, executedFeeAmount: '0', executedSurplusFee: '1' }
const transformedOrder = transformOrder(rawOrder)

expect(transformedOrder.totalFee).toEqual(rawOrder.executedSurplusFee)
})

test('should use sum of executedFeeAmount and executedSurplusFee', () => {
const rawOrder = { ...ORDER, executedFeeAmount: '1', executedSurplusFee: '1' }
const transformedOrder = transformOrder(rawOrder)

expect(transformedOrder.totalFee).toEqual('2')
})

test('should not fail when executedSurplusFee is falsy', () => {
const rawOrder = { ...ORDER, executedSurplusFee: null }
const transformedOrder = transformOrder(rawOrder)

expect(transformedOrder.totalFee).toEqual(rawOrder.executedFeeAmount)
})
})
})
17 changes: 12 additions & 5 deletions src/order-book/transformOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ export function transformOrder(order: Order): EnrichedOrder {
/**
* Add the total fee to the order.
*
* The `executedSurplusFee` represents exactly the fee that was charged (regardless of the fee
* signed with the order). So, while the protocol currently does not allow placing a limit order
* with any other fee than 0 - the backend is designed to support these kinds of orders for the
* future.
* The total fee of the order will be represented by the `totalFee` field, which is the sum of `executedSurplusFee`
* and `executedFeeAmount`.
*
* Note that either `executedSurplusFee` or `executedFeeAmount` may be `0`, or both might have a non `0` value.
*
* See https://cowservices.slack.com/archives/C036G0J90BU/p1705322037866779?thread_ts=1705083817.684659&cid=C036G0J90BU
*
* @param dto The order to add the total fee to.
* @returns The order with the total fee added.
*/
function addTotalFeeToOrder(dto: Order): EnrichedOrder {
const { executedFeeAmount, executedSurplusFee } = dto
const totalFee = executedSurplusFee ?? executedFeeAmount

const _executedFeeAmount = BigInt(executedFeeAmount || '0')
const _executedSurplusFee = BigInt(executedSurplusFee || '0')

const totalFee = String(_executedFeeAmount + _executedSurplusFee)

return {
...dto,
Expand Down

0 comments on commit c8d4071

Please sign in to comment.