extracted PairChoice component

This commit is contained in:
Tim Olson
2023-11-22 16:35:15 -04:00
parent c69f3c7f54
commit c3f05deff1
5 changed files with 155 additions and 119 deletions

View File

@@ -0,0 +1,106 @@
<template>
<token-choice v-model="tokenA" class="token-choice mb-1">
<template v-slot:prepend>
<v-btn :text="s.buy ? 'Buy' : 'Sell'" :color="s.buy ? 'green' : 'red'"
variant="outlined" @click="s.buy=!s.buy" class="bs-button"/>
</template>
</token-choice>
<token-choice v-model="tokenB" class="token-choice">
<template v-slot:prepend>
<v-btn :text="!s.buy ? 'Buy' : 'Sell'" :color="!s.buy ? 'green' : 'red'"
variant="outlined" @click="s.buy=!s.buy" class="bs-button"/>
</template>
</token-choice>
<v-chip v-for="r in routes" variant="text">
{{ s.chain.name }}
<v-img src="https://upload.wikimedia.org/wikipedia/commons/e/e7/Uniswap_Logo.svg" width="1.5em"/>
<span class="uniswap-color ml-0 mr-1">v3</span>
<span>{{ s.pairSymbol }} {{ r.fee / 10000 }}%</span>&nbsp;
<route-price :route="r" :inverted="routeInverted(r)" class="text-green clickable" @click="s.inverted=!s.inverted"/>
</v-chip>
<div v-if="s.routesPending">
<v-progress-circular indeterminate/> Searching for {{s.pairSymbol}} pools...
</div>
<v-alert v-if="!s.route && !s.routesPending" text="No pool found!"/>
</template>
<script setup>
import TokenChoice from "@/components/TokenChoice.vue"
import {useStore} from "@/store/store";
import RoutePrice from "@/components/RoutePrice.vue";
import {findRoute} from "@/blockchain/route.js";
import {SingletonCoroutine, routeInverted} from "@/misc.js";
import {computed, ref} from "vue";
const s = useStore()
const tokenA = computed({
get() {
return s.tokenA
},
set(value) {
if( !s.tokenA || s.tokenA.address !== value.address ) {
s.tokenA.value = value
routeFinder.invoke()
}
}
})
const tokenB = computed({
get() {
return s.tokenB
},
set(value) {
if( !s.tokenB || s.tokenB.address !== value.address ) {
s.tokenB = value
routeFinder.invoke()
}
}
})
const routes = computed({
get() {
return s.routes.value
},
set(value) {
console.log('setting new routes', s.routes.value, value)
s.routes.value = value
}
})
async function componentFindRoute() {
const tokenA = s.tokenA
const tokenB = s.tokenB
console.log('finding route', tokenA, tokenB)
s.routes = []
if (!tokenA || !tokenB)
return
s.routesPending = true
try {
const result = await findRoute(tokenA, tokenB)
console.log('found route', result)
s.routes = result
}
catch (e) {
console.log('ignoring routes exception', e)
}
finally {
s.routesPending = false
}
}
const routeFinder = new SingletonCoroutine(componentFindRoute,10)
routeFinder.invoke()
</script>
<style scoped lang="scss">
@use "src/styles/vars" as *;
</style>

View File

@@ -3,40 +3,14 @@
<phone-card class="toecard">
<v-card-title class="big">DCA / TWAP</v-card-title>
<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'"
variant="outlined" @click="buy=!buy" class="bs-button"/>
</template>
</token-choice>
<token-choice v-model="tokenB" class="token-choice">
<template v-slot:prepend>
<v-btn :text="!buy ? 'Buy' : 'Sell'" :color="!buy ? 'green' : 'red'"
variant="outlined" @click="buy=!buy" class="bs-button"/>
</template>
</token-choice>
<v-chip v-for="r in routes" variant="text">
{{ s.chain.name }}
<v-img src="https://upload.wikimedia.org/wikipedia/commons/e/e7/Uniswap_Logo.svg" width="1.5em"/>
<span class="uniswap-color ml-0 mr-1">v3</span>
<span>{{pairSymbol}} {{r.fee/10000}}%</span>&nbsp;
<route-price :route="r" :inverted="routeInverted(r)" class="text-green clickable" @click="inverted=!inverted"/>
</v-chip>
<div v-if="routesPending">
<v-progress-circular indeterminate/> Searching for {{pairSymbol}} pools...
</div>
<v-alert v-if="!route && !routesPending" text="No pool found!"/>
<div v-if="route && !routesPending">
<v-card-item>
<pair-choice/>
<div v-if="s.route && !s.routesPending">
<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="amountIsTokenA=!amountIsTokenA" variant="outlined" class="mr-2">
{{ amountIsTokenA ? tokenA.symbol : tokenB.symbol }}
{{ amountIsTokenA ? s.tokenA.symbol : s.tokenB.symbol }}
</v-btn>
<v-btn :text="amountIsTotal ? 'total' : 'per tranche'" variant="outlined"
@click="amountIsTotal=!amountIsTotal" class="total"/>
@@ -65,18 +39,18 @@
clearable :rules="[validateAmount, validateMin]" v-auto-select>
<template v-slot:append-inner>
<v-btn variant="outlined" @click="inverted=!inverted">
{{pairSymbol}}
{{s.pairSymbol}}
</v-btn>
</template>
<template #details style="flex-direction: column-reverse">
<div>
Current price&nbsp;<route-price :inverted="routeInverted(route)" :route="route" class="text-green"/>
Current price&nbsp;<route-price :inverted="routeInverted(s.route)" :route="s.route" class="text-green"/>
</div>
</template>
</v-text-field>
</div>
</v-card-text>
</v-card-item>
<v-card-actions class="d-flex justify-space-evenly mb-4">
<v-btn variant="outlined" color="red">Cancel</v-btn>
@@ -88,75 +62,25 @@
<script setup>
import {useStore} from "@/store/store";
import {computed, onBeforeUnmount, ref} from "vue";
import TokenChoice from "@/components/TokenChoice.vue"
import {computed, ref} from "vue";
import PhoneCard from "@/components/PhoneCard.vue";
// noinspection ES6UnusedImports
import {SingletonCoroutine, vAutoSelect} from "@/misc.js";
import {routeInverted, SingletonCoroutine, vAutoSelect} from "@/misc.js";
import {newLimitConstraint, newOrder, newTimeConstraint, sqrtX96, TimeMode} from "@/blockchain/orderlib.js";
import {FixedNumber} from "ethers";
import {pendOrder} from "@/blockchain/wallet.js";
import NeedsProvider from "@/components/NeedsProvider.vue";
import {findRoute} from "@/blockchain/route.js";
import RoutePrice from "@/components/RoutePrice.vue";
import router from "@/router/index.js";
import PairChoice from "@/components/PairChoice.vue";
const s = useStore()
const buy = ref(false)
let _tokenA = ref(Object.values(s.tokens).length >= 1 ? Object.values(s.tokens)[0] : null)
let _tokenB = ref(Object.values(s.tokens).length >= 2 ? Object.values(s.tokens)[1] : null)
const tokenA = computed({
get() {
return _tokenA.value
},
set(value) {
if( !_tokenA.value || _tokenA.value.address !== value.address ) {
_tokenA.value = value
routeFinder.invoke()
}
}
})
const tokenB = computed({
get() {
return _tokenB.value
},
set(value) {
if( !_tokenB.value || _tokenB.value.address !== value.address ) {
_tokenB.value = value
routeFinder.invoke()
}
}
})
const pairSymbol = computed(()=>base.value?.symbol+'\\'+quote.value?.symbol)
const base = computed(()=>{
const token = inverted.value ? _tokenB.value : _tokenA.value
return !token?{}:token
})
const quote = computed(()=>{
const token = inverted.value ? _tokenA.value : _tokenB.value
return !token?{}:token
})
const _routes = ref([])
const routes = computed({
get() {
return _routes.value
},
set(value) {
console.log('setting new routes', _routes.value, value)
_routes.value = value
}
})
const route = computed(()=>_routes.value.length===0 ? null : _routes.value[0])
const routesPending = ref(false)
const amount = ref(100) // todo 0
const amountIsTokenA = ref(false)
const amountIsTotal = ref(true)
const tranches = ref(3)
const inverted = ref(false)
function routeInverted(route) {
return route && (route.token0 === tokenA.value) === inverted.value
}
const minPrice = ref(null)
const maxPrice = ref(null)
const limitPrice = ref(null)
@@ -164,33 +88,10 @@ const interval = ref(1)
const intervalIsTotal = ref(true)
const timeUnits = ['minutes', 'hours', 'days']
const timeUnitIndex = ref(0)
const limitIsMinimum = computed(() => !(buy.value ^ inverted.value))
const validOrder = computed(()=>amount.value > 0 && routes.value.length > 0 )
const limitIsMinimum = computed(() => !(s.buy ^ s.inverted))
const validOrder = computed(()=>amount.value > 0 && s.routes.length > 0 )
async function componentFindRoute() {
console.log('finding route', _tokenA.value, _tokenB.value)
routes.value = []
if (!_tokenA.value || !_tokenB.value)
return
routesPending.value = true
try {
const result = await findRoute(tokenA.value, tokenB.value)
console.log('found route', result)
routes.value = result
}
catch (e) {
console.log('ignoring routes exception', e)
}
finally {
routesPending.value = false
}
}
const routeFinder = new SingletonCoroutine(componentFindRoute,10)
routeFinder.invoke()
function toggleTimeUnits() {
timeUnitIndex.value++
if (timeUnitIndex.value >= timeUnits.length)
@@ -246,14 +147,14 @@ function validateMin(v) {
}
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 ta = s.tokenA;
const tb = s.tokenB;
const tokenIn = s.buy ? tb.address : ta.address
const tokenOut = s.buy ? ta.address : tb.address
const route = s.route
const amountToken = amountIsTokenA.value ? ta : tb
const amt = FixedNumber.fromString(amount.value.toString(), {decimals: amountToken.decimals}).value
const amountIsInput = amountIsTokenA.value !== buy.value
const amountIsInput = amountIsTokenA.value !== s.buy
// build tranches
const n = tranches.value // num tranches
@@ -278,12 +179,11 @@ function placeOrder() {
const inverted = routeInverted(route)
const isAbove = limitIsMinimum.value ^ inverted
const isRatio = false // todo ratios
const decimals = 10 ** (tokenA.value.decimals - tokenB.value.decimals)
const decimals = 10 ** (s.tokenA.decimals - s.tokenB.decimals)
const limit = inverted ? decimals/limitPrice.value : limitPrice.value/decimals
priceConstraint = !limitPrice.value ? null : newLimitConstraint(isAbove, isRatio, limit)
}
for (let i = 0; i < n; i++) {
// const start = Math.floor(i * (duration / (n - 1)))
const start = Math.floor(i * (duration / Math.max((n - 1), 1)))
const end = start + window
console.log('tranche window', start, end, (end-start)/60)