vault upgrades; fees; refactoring

This commit is contained in:
Tim
2024-06-17 02:39:12 -04:00
parent fd8f20c4d4
commit 104b798d4f
15 changed files with 260 additions and 86 deletions

View File

@@ -2,7 +2,7 @@ import {uint32max, uint64max} from "@/misc.js";
import {decodeIEE754, encodeIEE754} from "@/common.js";
export const MAX_FRACTION = 65535;
export const NO_CHAIN = uint64max;
export const NO_CONDITIONAL_ORDER = uint64max;
export const NO_OCO = uint64max;
export const DISTANT_PAST = 0
export const DISTANT_FUTURE = uint32max
@@ -15,7 +15,7 @@ export const DISTANT_FUTURE = uint32max
// uint256 minFillAmount; // if a tranche has less than this amount available to fill, it is considered completed
// bool amountIsInput;
// bool outputDirectlyToOwner;
// uint64 chainOrder; // use NO_CHAIN for no chaining. chainOrder index must be < than this order's index for safety (written first) and chainOrder state must be Template
// uint64 conditionalOrder; // use NO_CONDITIONAL_ORDER for no chaining. conditionalOrder index must be < than this order's index for safety (written first) and conditionalOrder state must be Template
// Tranche[] tranches;
// }
// struct Route {
@@ -23,7 +23,7 @@ export const DISTANT_FUTURE = uint32max
// uint24 fee;
// }
export function newOrder(tokenIn, tokenOut, exchange, fee, amount, amountIsInput, tranches,
minFillAmount=null, outputDirectlyToOwner = false, chainOrder = NO_CHAIN) {
minFillAmount=null, outputDirectlyToOwner = false, conditionalOrder = NO_CONDITIONAL_ORDER) {
amountIsInput = !!amountIsInput // force convert to bool
outputDirectlyToOwner = !!outputDirectlyToOwner // force convert to bool
amount = BigInt(amount)
@@ -34,7 +34,7 @@ export function newOrder(tokenIn, tokenOut, exchange, fee, amount, amountIsInput
return {
tokenIn, tokenOut, route:{exchange, fee},
amount, minFillAmount, amountIsInput,
outputDirectlyToOwner, chainOrder, tranches
outputDirectlyToOwner, conditionalOrder, tranches
}
}
@@ -46,11 +46,11 @@ export function newOrder(tokenIn, tokenOut, exchange, fee, amount, amountIsInput
// bool minIsBarrier;
// bool maxIsBarrier;
// bool marketOrder; // if true, both min and max lines are ignored, and minIntercept is treated as a maximum slippage value (use positive numbers)
// bool _reserved5;
// bool _reserved6;
// bool minIsRatio;
// bool maxIsRatio;
// bool _reserved7;
// uint8 _reserved8;
// uint32 _reserved16;
// uint16 rateLimitFraction;
// uint24 rateLimitPeriod;
//
// uint32 startTime; // use DISTANT_PAST to disable
// uint32 endTime; // use DISTANT_FUTURE to disable
@@ -69,12 +69,16 @@ export function newTranche({
endTimeIsRelative = false,
endTime = DISTANT_FUTURE,
minIsBarrier = false,
minIsRatio = false,
minIntercept = 0,
slippage = 0, // may also set minIntercept instead
minSlope = 0,
maxIsBarrier = false,
maxIsRatio = false,
maxIntercept = 0,
maxSlope = 0,
rateLimitFraction = 0,
rateLimitPeriod = 0,
} = {}) {
if( minIntercept === 0 && minSlope === 0 && maxIntercept === 0 && maxSlope === 0 )
marketOrder = true
@@ -90,7 +94,7 @@ export function newTranche({
fraction: Math.min(MAX_FRACTION, Math.round(fraction)), marketOrder,
startTimeIsRelative, startTime, endTimeIsRelative, endTime,
minIsBarrier, minIntercept, minSlope, maxIsBarrier, maxIntercept, maxSlope,
_reserved5: false, _reserved6: false, _reserved7: false, _reserved8: 0, _reserved16: 0,
minIsRatio, maxIsRatio, _reserved7: false, rateLimitFraction, rateLimitPeriod,
}
}
@@ -110,10 +114,6 @@ export const OrderState = {
Error: 99,
}
export function orderIsOpen(order) {
return isOpen(order.state)
}
export function isOpen(state) {
return state >= 1 && state < 3
}
@@ -121,7 +121,7 @@ export function isOpen(state) {
export function parseOrderStatus(chainId, status) {
let [
order,
fillFeeBP,
fillFeeHalfBps,
state,
start,
ocoGroup,
@@ -129,14 +129,17 @@ export function parseOrderStatus(chainId, status) {
filledOut,
trancheFilledIn,
trancheFilledOut,
trancheActivationTime,
] = status
order = parseOrder(order)
filledIn = BigInt(filledIn)
filledOut = BigInt(filledOut)
trancheFilledIn = trancheFilledIn.map((f)=>BigInt(f))
trancheFilledOut = trancheFilledOut.map((f)=>BigInt(f))
trancheActivationTime = trancheActivationTime.map((v)=>Number(v))
return {
chainId, order, fillFeeBP, state, start, ocoGroup, filledIn, filledOut, trancheFilledIn, trancheFilledOut,
chainId, order, fillFeeHalfBps, state, start, ocoGroup,
filledIn, filledOut, trancheFilledIn, trancheFilledOut, trancheActivationTime
}
}
@@ -150,7 +153,7 @@ export function parseOrder(order) {
minFillAmount,
amountIsInput,
outputDirectlyToOwner,
chainOrder,
conditionalOrder,
tranches,
] = order
route = parseRoute(route)
@@ -158,7 +161,7 @@ export function parseOrder(order) {
minFillAmount = BigInt(minFillAmount)
tranches = tranches.map(parseTranche)
return {
tokenIn, tokenOut, route, amount, minFillAmount, amountIsInput, outputDirectlyToOwner, chainOrder, tranches
tokenIn, tokenOut, route, amount, minFillAmount, amountIsInput, outputDirectlyToOwner, conditionalOrder, tranches
}
}
@@ -175,11 +178,11 @@ export function parseTranche(tranche) {
minIsBarrier,
maxIsBarrier,
marketOrder,
_reserved5,
_reserved6,
minIsRatio,
maxIsRatio,
_reserved7,
_reserved8,
_reserved16,
rateLimitFraction,
rateLimitPeriod,
startTime,
endTime,
minIntercept,
@@ -193,6 +196,7 @@ export function parseTranche(tranche) {
maxSlope = decodeIEE754(maxSlope)
return {
fraction, startTimeIsRelative, endTimeIsRelative, minIsBarrier, maxIsBarrier, marketOrder,
minIsRatio, maxIsRatio, rateLimitFraction, rateLimitPeriod,
startTime, endTime, minIntercept, minSlope, maxIntercept, maxSlope,
}
}