orderspec refactor for server and web

This commit is contained in:
Tim Olson
2023-12-07 18:37:11 -04:00
parent 545583586c
commit 83619ea248
12 changed files with 306 additions and 110 deletions

View File

@@ -86,16 +86,91 @@ export function newTranche({
}
}
// enum Exchange {
// UniswapV2,
// UniswapV3
// }
export const Exchange = {
UniswapV2: 0,
UniswapV3: 1,
}
export function sqrtX96(value) {
return BigInt(Math.round(Math.sqrt(value * 2 ** (96*2))))
export const OrderState = {
Signing: -1,
Unknown: 0,
Open: 1,
Canceled: 2,
Filled: 3,
Expired: 4,
Underfunded: 5,
}
export function parseOrderStatus(status) {
let [
order,
state,
start,
ocoGroup,
filledIn,
filledOut,
trancheFilledIn,
trancheFilledOut,
] = status
order = parseOrder(order)
filledIn = BigInt(filledIn)
filledOut = BigInt(filledOut)
trancheFilledIn = trancheFilledIn.map((f)=>BigInt(f))
trancheFilledOut = trancheFilledOut.map((f)=>BigInt(f))
return {
order, state, start, ocoGroup, filledIn, filledOut, trancheFilledIn, trancheFilledOut,
}
}
export function parseOrder(order) {
let [
tokenIn,
tokenOut,
route,
amount,
minFillAmount,
amountIsInput,
outputDirectlyToOwner,
chainOrder,
tranches,
] = order
route = parseRoute(route)
amount = BigInt(amount)
minFillAmount = BigInt(minFillAmount)
tranches = tranches.map(parseTranche)
return {
tokenIn, tokenOut, route, amount, minFillAmount, amountIsInput, outputDirectlyToOwner, chainOrder, tranches
}
}
export function parseRoute(route) {
let [exchange, fee] = route
return {exchange, fee} // todo enum?
}
export function parseTranche(tranche) {
let [
fraction,
startTimeIsRelative,
endTimeIsRelative,
minIsBarrier,
maxIsBarrier,
marketOrder,
_reserved5,
_reserved6,
_reserved7,
_reserved8,
_reserved16,
startTime,
endTime,
minIntercept,
minSlope,
maxIntercept,
maxSlope,
] = tranche
return {
fraction, startTimeIsRelative, endTimeIsRelative, minIsBarrier, maxIsBarrier, marketOrder,
startTime, endTime, minIntercept, minSlope, maxIntercept, maxSlope,
}
}