order placement works, including automatic vault creation

This commit is contained in:
Tim Olson
2023-10-06 19:49:31 -04:00
parent c206607547
commit c637e82ac3
8 changed files with 279 additions and 56 deletions

View File

@@ -2,9 +2,8 @@
<NeedsQueryHelper>
<PhoneCard>
<v-card-title class="big">DCA / TWAP</v-card-title>
<v-card-subtitle>Split order across time</v-card-subtitle>
<v-card-subtitle>Multiple tranches over a time range</v-card-subtitle>
<v-card-text>
<token-choice v-model="tokenA" class="token-choice mb-1">
<template v-slot:prepend>
<v-btn :text="buy ? 'Buy' : 'Sell'" :color="buy ? 'green' : 'red'"
@@ -35,15 +34,15 @@
<v-text-field label='Amount' type="number" step="1" variant="outlined" aria-valuemin="0" min="0"
v-model="amount" :rules="[validateRequired,validateAmount]" v-auto-select>
<template v-slot:append-inner>
<v-btn @click="amountIsBase=!amountIsBase" variant="outlined" class="mr-2">
{{ amountIsBase ? tokenA.symbol : tokenB.symbol }}
<v-btn @click="amountIsTokenA=!amountIsTokenA" variant="outlined" class="mr-2">
{{ amountIsTokenA ? tokenA.symbol : tokenB.symbol }}
</v-btn>
<v-btn :text="amountIsTotal ? 'total' : 'per tranche'" variant="outlined"
@click="amountIsTotal=!amountIsTotal" class="total"/>
</template>
</v-text-field>
<v-text-field label="Tranches" type="number" variant="outlined" aria-valuemin="1" min="1" max="255"
:model-value="tranches" :rules="[validateRequired,validateTranches]" v-auto-select>
v-model="tranches" :rules="[validateRequired,validateTranches]" v-auto-select>
<!-- <template v-slot:prepend-inner>-->
<!-- <div>{{ amountIsTotal ? 'Split into' : 'Times' }}</div>-->
<!-- </template>-->
@@ -60,6 +59,7 @@
<v-btn variant="outlined" :text="timeUnits[timeUnitIndex]" @click="toggleTimeUnits" class="time-units"/>
</template>
</v-text-field>
<!--
<v-text-field v-model="limitPrice" :label="(limitIsMinimum?'Minimum':'Maximum')+' Price'" type="number"
variant="outlined" aria-valuemin="0" min="0"
clearable :rules="[validateAmount, validateMin]" v-auto-select>
@@ -69,13 +69,14 @@
</v-btn>
</template>
</v-text-field>
-->
</div>
</v-card-text>
<v-card-actions class="d-flex justify-space-evenly mb-4">
<v-btn variant="outlined" color="red">Cancel</v-btn>
<v-btn variant="flat" color="green" :disabled="!validOrder">Place Order</v-btn>
<v-btn variant="flat" color="green" :disabled="!validOrder" @click="placeOrder">Place Order</v-btn>
</v-card-actions>
</PhoneCard>
</NeedsQueryHelper>
@@ -87,13 +88,15 @@ import {computed, ref} from "vue";
import TokenChoice from "@/components/TokenChoice.vue"
import PhoneCard from "@/components/PhoneCard.vue";
import {queryHelperContract} from "@/blockchain/contract.js";
import {SingletonCoroutine} from "@/misc.js";
import NeedsQueryHelper from "@/components/NeedsQueryHelper.vue";
// noinspection ES6UnusedImports
import {vAutoSelect} from "@/misc.js";
import {SingletonCoroutine, vAutoSelect} from "@/misc.js";
import NeedsQueryHelper from "@/components/NeedsQueryHelper.vue";
import {Exchange, newOrder, newTimeConstraint, TimeMode} from "@/blockchain/orderlib.js";
import {FixedNumber} from "ethers";
import {pendOrder} from "@/blockchain/wallet.js";
const s = useStore()
const buy = ref(false)
const buy = ref(true)
let _tokenA = ref(s.tokens && s.tokens.length >= 1 ? s.tokens[0] : null)
let _tokenB = ref(s.tokens && s.tokens.length >= 2 ? s.tokens[1] : null)
const tokenA = computed({
@@ -129,8 +132,8 @@ const quote = computed(()=>{
return !token?{}:token
})
const routes = ref([])
const amount = ref(0)
const amountIsBase = ref(false)
const amount = ref(100) // todo 0
const amountIsTokenA = ref(false)
const amountIsTotal = ref(true)
const tranches = ref(3)
const inverted = ref(false)
@@ -169,7 +172,7 @@ async function findRoute() {
case 0: // UniswapV2
break
case 1: // UniswapV3
result = {exchange: 'UniswapV3', pool, fee,}
result = {exchange: Exchange.UniswapV3, pool, fee,}
break
}
}
@@ -234,6 +237,41 @@ function validateMin(v) {
return true
}
async function placeOrder() {
const ta = tokenA.value;
const tb = tokenB.value;
const tokenIn = buy.value ? tb.address : ta.address
const tokenOut = buy.value ? ta.address : tb.address
const route = routes.value[0];
const amountToken = amountIsTokenA ? ta : tb
const amt = FixedNumber.fromString(amount.value.toString(), {decimals: amountToken.decimals}).value
const amountIsInput = !buy.value ^ amountIsTokenA.value
// build tranches
const n = tranches.value // num tranches
const ts = []
let duration = timeUnitIndex === 0 ? interval.value * 60 : // minutes
timeUnitIndex === 1 ? interval.value * 60 * 60 : // hours
interval.value * 24 * 60 * 60; // days
let window
if (!intervalIsTotal.value) {
window = duration
duration *= n // duration is the total time for all tranches
} else {
window = duration / n
}
const ceil = 10n ** 18n % BigInt(n) ? 1n : 0n
const amtPerTranche = 10n ** 18n / BigInt(n) + ceil
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 / n))
const end = start + window
const cs = [newTimeConstraint(TimeMode.SinceOrderStart, start, TimeMode.SinceOrderStart, end)]
ts.push([amtPerTranche, cs])
}
const order = newOrder(tokenIn, tokenOut, route.exchange, route.fee, amt, amountIsInput, ts)
await pendOrder(order)
}
</script>