-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3612 from osmosis-labs/fabryscript/feat-add-v3-de…
…signs [Limit Orders]: Feat add v3 designs
- Loading branch information
Showing
62 changed files
with
4,192 additions
and
2,268 deletions.
There are no files selected for viewing
131 changes: 128 additions & 3 deletions
131
packages/server/src/queries/complex/orderbooks/active-orders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,157 @@ | ||
import { Chain } from "@osmosis-labs/types"; | ||
import { Dec, Int } from "@keplr-wallet/unit"; | ||
import { tickToPrice } from "@osmosis-labs/math"; | ||
import { AssetList, Chain } from "@osmosis-labs/types"; | ||
import { getAssetFromAssetList } from "@osmosis-labs/utils"; | ||
import cachified, { CacheEntry } from "cachified"; | ||
import dayjs from "dayjs"; | ||
import { LRUCache } from "lru-cache"; | ||
|
||
import { DEFAULT_LRU_OPTIONS } from "../../../utils/cache"; | ||
import { LimitOrder, queryOrderbookActiveOrders } from "../../osmosis"; | ||
import { getOrderbookDenoms } from "./denoms"; | ||
import { | ||
getOrderbookTickState, | ||
getOrderbookTickUnrealizedCancels, | ||
} from "./tick-state"; | ||
import type { MappedLimitOrder, OrderStatus } from "./types"; | ||
|
||
const activeOrdersCache = new LRUCache<string, CacheEntry>(DEFAULT_LRU_OPTIONS); | ||
|
||
export function getOrderbookActiveOrders({ | ||
orderbookAddress, | ||
userOsmoAddress, | ||
chainList, | ||
assetLists, | ||
}: { | ||
orderbookAddress: string; | ||
userOsmoAddress: string; | ||
chainList: Chain[]; | ||
assetLists: AssetList[]; | ||
}) { | ||
return cachified({ | ||
cache: activeOrdersCache, | ||
key: `orderbookActiveOrders-${orderbookAddress}-${userOsmoAddress}`, | ||
ttl: 1000 * 3, // 3 seconds | ||
ttl: 500, // 2 seconds | ||
getFreshValue: () => | ||
queryOrderbookActiveOrders({ | ||
orderbookAddress, | ||
userAddress: userOsmoAddress, | ||
chainList, | ||
}).then( | ||
({ data }: { data: { count: number; orders: LimitOrder[] } }) => data | ||
async ({ data }: { data: { count: number; orders: LimitOrder[] } }) => { | ||
const { quoteAsset, baseAsset } = await getOrderbookDenoms({ | ||
orderbookAddress, | ||
chainList, | ||
assetLists, | ||
}); | ||
|
||
return getTickInfoAndTransformOrders( | ||
orderbookAddress, | ||
data.orders, | ||
chainList, | ||
quoteAsset, | ||
baseAsset | ||
); | ||
} | ||
), | ||
}); | ||
} | ||
|
||
function mapOrderStatus(order: LimitOrder, percentFilled: Dec): OrderStatus { | ||
const quantInt = parseInt(order.quantity); | ||
if (quantInt === 0 || percentFilled.equals(new Dec(1))) return "filled"; | ||
if (percentFilled.isZero()) return "open"; | ||
if (percentFilled.lt(new Dec(1))) return "partiallyFilled"; | ||
|
||
return "open"; | ||
} | ||
|
||
async function getTickInfoAndTransformOrders( | ||
orderbookAddress: string, | ||
orders: LimitOrder[], | ||
chainList: Chain[], | ||
quoteAsset: ReturnType<typeof getAssetFromAssetList>, | ||
baseAsset: ReturnType<typeof getAssetFromAssetList> | ||
): Promise<MappedLimitOrder[]> { | ||
const tickIds = [...new Set(orders.map((o) => o.tick_id))]; | ||
const tickStates = await getOrderbookTickState({ | ||
orderbookAddress, | ||
chainList, | ||
tickIds, | ||
}); | ||
const unrealizedTickCancels = await getOrderbookTickUnrealizedCancels({ | ||
orderbookAddress, | ||
chainList, | ||
tickIds, | ||
}); | ||
|
||
const fullTickState = tickStates.map(({ tick_id, tick_state }) => ({ | ||
tickId: tick_id, | ||
tickState: tick_state, | ||
unrealizedCancels: unrealizedTickCancels.find((c) => c.tick_id === tick_id), | ||
})); | ||
|
||
return orders.map((o) => { | ||
const { tickState, unrealizedCancels } = fullTickState.find( | ||
({ tickId }) => tickId === o.tick_id | ||
) ?? { tickState: undefined, unrealizedCancels: undefined }; | ||
|
||
const quantity = parseInt(o.quantity); | ||
const placedQuantity = parseInt(o.placed_quantity); | ||
|
||
const percentClaimed = new Dec( | ||
(placedQuantity - quantity) / placedQuantity | ||
); | ||
|
||
const normalizationFactor = new Dec(10).pow( | ||
new Int((quoteAsset?.decimals ?? 0) - (baseAsset?.decimals ?? 0)) | ||
); | ||
const [tickEtas, tickUnrealizedCancelled] = | ||
o.order_direction === "bid" | ||
? [ | ||
parseInt( | ||
tickState?.bid_values.effective_total_amount_swapped ?? "0" | ||
), | ||
parseInt( | ||
unrealizedCancels?.unrealized_cancels.bid_unrealized_cancels ?? | ||
"0" | ||
), | ||
] | ||
: [ | ||
parseInt( | ||
tickState?.ask_values.effective_total_amount_swapped ?? "0" | ||
), | ||
parseInt( | ||
unrealizedCancels?.unrealized_cancels.ask_unrealized_cancels ?? | ||
"0" | ||
), | ||
]; | ||
const tickTotalEtas = tickEtas + tickUnrealizedCancelled; | ||
const totalFilled = Math.max( | ||
tickTotalEtas - (parseInt(o.etas) - (placedQuantity - quantity)), | ||
0 | ||
); | ||
const percentFilled = new Dec(Math.min(totalFilled / placedQuantity, 1)); | ||
const price = tickToPrice(new Int(o.tick_id)); | ||
const status = mapOrderStatus(o, percentFilled); | ||
const output = | ||
o.order_direction === "bid" | ||
? new Dec(placedQuantity).quo(price) | ||
: new Dec(placedQuantity).mul(price); | ||
return { | ||
...o, | ||
price: price.quo(normalizationFactor), | ||
quantity, | ||
placed_quantity: placedQuantity, | ||
percentClaimed, | ||
totalFilled, | ||
percentFilled, | ||
orderbookAddress, | ||
status, | ||
output, | ||
quoteAsset, | ||
baseAsset, | ||
placed_at: dayjs(parseInt(o.placed_at) / 1_000).unix(), | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.