price subscriptions

This commit is contained in:
Tim Olson
2023-11-02 23:30:43 -04:00
parent 3f15985bf5
commit b483974268
10 changed files with 212 additions and 40 deletions

View File

@@ -84,16 +84,17 @@
<script setup>
import {useStore} from "@/store/store";
import {computed, ref} from "vue";
import {computed, onBeforeUnmount, ref} from "vue";
import TokenChoice from "@/components/TokenChoice.vue"
import PhoneCard from "@/components/PhoneCard.vue";
import {queryHelperContract} from "@/blockchain/contract.js";
// noinspection ES6UnusedImports
import {SingletonCoroutine, vAutoSelect} from "@/misc.js";
import {Exchange, newOrder, newTimeConstraint, TimeMode} from "@/blockchain/orderlib.js";
import {newOrder, newTimeConstraint, TimeMode} from "@/blockchain/orderlib.js";
import {FixedNumber} from "ethers";
import {pendOrder} from "@/blockchain/wallet.js";
import NeedsProvider from "@/components/NeedsProvider.vue";
import {subPrices, unsubPrices} from "@/blockchain/prices.js";
import {findRoute} from "@/blockchain/route.js";
const s = useStore()
const buy = ref(false)
@@ -131,7 +132,18 @@ const quote = computed(()=>{
const token = inverted.value ? _tokenA.value : _tokenB.value
return !token?{}:token
})
const routes = ref([])
const _routes = ref([])
const routes = computed({
get() {
return _routes.value
},
set(value) {
console.log('setting new routes', value)
subPrices(value)
unsubPrices(_routes.value)
_routes.value = value
}
})
const routesPending = ref(false)
const amount = ref(100) // todo 0
const amountIsTokenA = ref(false)
@@ -148,47 +160,33 @@ const timeUnitIndex = ref(1)
const limitIsMinimum = computed(() => !(buy.value ^ inverted.value))
const validOrder = computed(()=>amount.value > 0 && routes.value.length > 0 )
async function findRoute() {
onBeforeUnmount(() => {
unsubPrices(_routes.value)
})
async function componentFindRoute() {
console.log('finding route', _tokenA.value, _tokenB.value)
routes.value = []
if( !_tokenA.value || !_tokenB.value )
if (!_tokenA.value || !_tokenB.value)
return
const helper = await queryHelperContract()
if( !helper ) {
console.log('no helper')
return
}
routesPending.value = true
let rawRoutes
try {
rawRoutes = await helper.getRoutes(tokenA.value.address, tokenB.value.address)
const result = await findRoute(tokenA.value, tokenB.value)
console.log('found route', result)
routes.value = result
}
catch (e) {
console.log('routes exception', e)
console.log('ignoring routes exception', e)
}
finally {
routesPending.value = false
return
}
// todo expose all available pools
let result = {} // we actually only find a single pool for now
for (let [exchange, fee, pool] of rawRoutes) {
exchange = Number(exchange)
fee = Number(fee)
if (result.fee === undefined || result.fee > fee) {
switch (exchange) {
case 0: // UniswapV2
break
case 1: // UniswapV3
result = {exchange: Exchange.UniswapV3, pool, fee,}
break
}
}
}
routes.value = [result]
routesPending.value = false
console.log('found route', result)
}
const routeFinder = new SingletonCoroutine(findRoute,10)
const routeFinder = new SingletonCoroutine(componentFindRoute,10)
routeFinder.invoke()
function toggleTimeUnits() {