diff --git a/src/abacus-ts/calculators/orders.ts b/src/abacus-ts/calculators/orders.ts index 0bf838d7d..c1dd7807f 100644 --- a/src/abacus-ts/calculators/orders.ts +++ b/src/abacus-ts/calculators/orders.ts @@ -8,12 +8,11 @@ import { assertNever } from '@/lib/assertNever'; import { MustBigNumber } from '@/lib/numbers'; import { Loadable } from '../lib/loadable'; +import { mapLoadableData } from '../lib/mapLoadable'; +import { mergeObjects } from '../lib/mergeObjects'; import { OrdersData } from '../rawTypes'; import { OrderStatus } from '../summaryTypes'; -const BN_0 = MustBigNumber(0); -const BN_1 = MustBigNumber(1); - // todo these are calculating the same thing twice pasically function calculateOpenOrders(liveOrders: Loadable, restOrders: Loadable) { const getOpenOrders = (data: Loadable) => @@ -59,6 +58,7 @@ function getSimpleOrderStatus(status: OrderStatus) { return OrderStatus.Filled; default: assertNever(status); + // should never happen since we made OrderStatus manually return OrderStatus.Open; } } @@ -128,32 +128,3 @@ function calculateMergedOrders(liveOrders: Loadable, restOrders: Loa maxBy([a, b], (o) => MustBigNumber(o.updatedAtHeight ?? o.createdAtHeight).toNumber())! ); } - -function mapLoadableData(load: Loadable, map: (obj: T) => R): Loadable { - return { - ...load, - data: load.data != null ? map(load.data) : undefined, - } as Loadable; -} - -type SimpleMap = { [key: string]: T }; -function mergeObjects(one: SimpleMap, two: SimpleMap, merge: (a: T, b: T) => T) { - const finalObj: SimpleMap = {}; - - [...Object.keys(one), ...Object.keys(two)].forEach((key) => { - if (finalObj[key] != null) { - return; - } - const obj = one[key]; - const otherObj = two[key]; - if (obj != null && otherObj != null) { - finalObj[key] = merge(obj, otherObj); - } else if (obj == null && otherObj == null) { - // do nothing - } else { - // we know one of them is non-null - finalObj[key] = (obj ?? otherObj)!; - } - }); - return finalObj; -}