order sanity checks

This commit is contained in:
tim
2025-03-16 21:15:00 -04:00
parent b9975cda10
commit 5876efe29f
11 changed files with 130 additions and 33 deletions

View File

@@ -70,7 +70,7 @@ import BuilderFactory from "@/components/chart/BuilderFactory.vue";
import {builderFuncs, newBuilder, orderFuncs, useChartOrderStore} from "@/orderbuild.js";
import {useOrderStore, useStore} from "@/store/store.js";
import {computed, onUnmounted, onUpdated, ref, watchEffect} from "vue";
import {lightenColor, lightenColor2} from "@/misc.js";
import {lightenColor, lightenColor2, toPrecision} from "@/misc.js";
import {useTheme} from "vuetify";
import RowBar from "@/components/chart/RowBar.vue";
import ColorBand from "@/components/chart/ColorBand.vue";
@@ -114,7 +114,38 @@ function buildOrder() {
const order = props.order
console.log('buildOrder', order)
if (!order.amount) return null
if (!order.amount)
return {order: null, warnings: ['Amount must be greater than 0']}
const symbol = co.selectedSymbol
const amountDec = order.amountIsTokenA ? symbol.base.d : symbol.quote.d
const amount = BigInt(Math.trunc(order.amount * 10 ** amountDec))
let warnings = []
const inAddr = order.buy ? symbol.quote.a : symbol.base.a
const inDec = order.buy ? symbol.quote.d : symbol.base.d
const available = s.getBalance(inAddr) / 10 ** inDec
let needed
if (order.amountIsTokenA && order.buy)
// need quote currency to buy an amount of base
needed = order.amount * co.price
else if (order.amountIsTokenA && !order.buy)
// sell an exact amount of base
needed = order.amount
else if (!order.amountIsTokenA && order.buy)
// need an exact amount of quote
needed = order.amount
else if (!order.amountIsTokenA && !order.buy)
// sell a quote amount worth of base
needed = order.amount / co.price
else
throw new Error('Invalid order')
const deficit = needed - available
if (deficit > 0) {
const inSymbol = order.buy ? symbol.quote.s : symbol.base.s
warnings.push(`Insufficient funds. Add ${toPrecision(deficit, 5)} ${inSymbol} to your vault to complete this order.`)
}
// struct SwapOrder {
// address tokenIn;
@@ -127,20 +158,24 @@ function buildOrder() {
// 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;
// }
const symbol = co.selectedSymbol
const amountDec = order.amountIsTokenA ? symbol.base.d : symbol.quote.d
const amount = BigInt(Math.trunc(order.amount * 10 ** amountDec))
const amountIsInput = !!(order.amountIsTokenA ^ order.buy)
let tranches = []
for (const builder of builders.value) {
console.log('builder', builder)
const ts = builderFuncs[builder.id]()
const built = builderFuncs[builder.id]()
const ts = built.tranches
const ws = built.warnings
console.log('tranches', ts)
tranches = [...tranches, ...ts]
warnings = [...warnings, ...ws]
}
return newOrder(tokenIn.value.a, tokenOut.value.a, symbol.exchangeId, symbol.fee, amount, amountIsInput, symbol.inverted, tranches)
return {
warnings,
order: newOrder(tokenIn.value.a, tokenOut.value.a, symbol.exchangeId, symbol.fee,
amount, amountIsInput, symbol.inverted, tranches),
}
}