Files
web/src/orderbuild.js
2023-12-04 17:59:57 -04:00

62 lines
1.9 KiB
JavaScript

import {routeInverted} from "@/misc.js";
import {MAX_FRACTION, newTranche} from "@/blockchain/orderlib.js";
import {useOrderStore} from "@/store/store.js";
export function applyLimit(tranche, price=null) {
const os = useOrderStore()
if( price === null ) {
price = os.limitPrice
if (!price)
return
}
const route = os.route
const inverted = routeInverted(route)
const isAbove = os.limitIsMinimum ^ inverted
const isRatio = false // todo ratios
const decimals = 10 ** (os.tokenA.decimals - os.tokenB.decimals)
const limit = inverted ? decimals / price : price / decimals
tranche.marketOrder = false;
if( isAbove ) {
tranche.minIntercept = limit;
tranche.minSlope = 0;
}
else {
tranche.maxIntercept = limit;
tranche.maxSlope = 0;
}
}
export function timesliceTranches() {
const ts = []
const os = useOrderStore()
const n = os.tranches // num tranches
const interval = os.interval;
const timeUnitIndex = os.timeUnitIndex;
let duration =
timeUnitIndex === 0 ? interval * 60 : // minutes
timeUnitIndex === 1 ? interval * 60 * 60 : // hours
interval * 24 * 60 * 60; // days
let window
if (!os.intervalIsTotal) {
window = duration
duration *= n // duration is the total time for all tranches
} else {
window = Math.round(duration / n)
}
const amtPerTranche = Math.ceil(MAX_FRACTION / n)
duration -= 15 // subtract 15 seconds so the last tranche completes before the deadline
for (let i = 0; i < n; i++) {
const start = Math.floor(i * (duration / Math.max((n - 1), 1)))
const end = start + window
ts.push(newTranche({
fraction: amtPerTranche,
startTimeIsRelative: true,
startTime: start,
endTimeIsRelative: true,
endTime: end,
}))
}
return ts
}