complete store refactor; moved form inputs into store; refactored components out of TimedOrderEntry

This commit is contained in:
Tim Olson
2023-11-25 16:21:42 -04:00
parent c3f05deff1
commit 094108d806
13 changed files with 192 additions and 174 deletions

View File

@@ -4,7 +4,6 @@ import {useStore} from "@/store/store.js";
export function vaultAddress( owner, num=0) {
console.log('va', owner, num)
if( !owner )
return null
const s = useStore()

View File

@@ -15,10 +15,10 @@ export function subPrices( routes ) {
const subRoutes = []
let chainId = null
for( const route of routes ) {
console.log('sub route', route, subscriptionCounts)
// console.log('sub route', route, subscriptionCounts)
if( !(route in subscriptionCounts) || subscriptionCounts[route] === 0 ) {
subscriptionCounts[route] = 1
console.log('subscribing to pool', route.pool)
// console.log('subscribing to pool', route.pool)
subRoutes.push(route)
}
else {
@@ -33,9 +33,7 @@ export function subPrices( routes ) {
// perform a local query if necessary
for( const route of subRoutes ) {
const s = useStore()
console.log('route in prices?', route.pool in s.poolPrices, route.pool, s.poolPrices)
if( !(route.pool in s.poolPrices) ) {
console.log('querying initial route price', route.pool)
getPriceForRoute(route).then((price)=>s.poolPrices[route.pool]=price)
}
}
@@ -46,7 +44,7 @@ export function unsubPrices( routes ) {
let chainId = null
const unsubAddrs = []
for( const route of routes ) {
console.log('unsub route', route, subscriptionCounts)
// console.log('unsub route', route, subscriptionCounts)
if( !(route in subscriptionCounts) ) {
console.error('unsubscribed to a nonexistent route', route)
}
@@ -56,7 +54,7 @@ export function unsubPrices( routes ) {
unsubAddrs.push(route.pool)
if( chainId !== null && route.chainId !== chainId )
throw Error('cannot mix chainIds in a subscription list')
console.log('unsubscribing from pool', route.pool)
// console.log('unsubscribing from pool', route.pool)
chainId = route.chainId
}
else if( subscriptionCounts[route] < 0 ) {
@@ -71,7 +69,6 @@ export function unsubPrices( routes ) {
async function getPriceForRoute(route) {
console.log('route is',route)
if( route.exchange === Exchange.UniswapV3 ) {
const addr = uniswapV3PoolAddress(route.chainId, route.token0.address, route.token1.address, route.fee)
const store = useStore();
@@ -88,7 +85,7 @@ async function getPriceForRoute(route) {
price = FixedNumber.fromValue(price,0,WIDE_PRICE_FORMAT)
price = price.div(FixedNumber.fromValue(2n**(96n*2n),0,WIDE_PRICE_FORMAT))
price = price.round(18).toString()
console.log(`price for ${route.token0.symbol}/${route.token1.symbol}`,price)
// console.log(`price for ${route.token0.symbol}/${route.token1.symbol}`,price)
store.poolPrices[addr] = price
return price
}

View File

@@ -10,7 +10,7 @@ export async function findRoute(tokenA, tokenB) {
const chainId = useStore().chainId
const rawRoutes = await helper.getRoutes(tokenA.address, tokenB.address)
// todo expose all available pools
console.log('raw routes', rawRoutes)
// console.log('raw routes', rawRoutes)
let result = null // we actually only find a single pool for now
for (let [exchange, fee, pool] of rawRoutes) {
exchange = Number(exchange)

View File

@@ -18,7 +18,6 @@ export function uniswapV3PoolAddress(chainId, tokenAddrA, tokenAddrB, fee) {
const encoded = ethers.AbiCoder.defaultAbiCoder().encode(['address', 'address', 'uint24'], [addr0, addr1, fee]);
const salt = ethers.keccak256(encoded)
const factory = uniswapV3Addresses[chainId]?.factory
console.log('uni3addr', addr0, addr1, fee, salt, factory)
if (!factory) {
console.log('no uniswap factory for chain', chainId)
return null

View File

@@ -1,5 +1,5 @@
import {ethers} from "ethers";
import {setProvider, useStore} from "@/store/store";
import {useStore} from "@/store/store";
import {socket} from "@/socket.js";
import {contractOrNull, vaultAddress} from "@/blockchain/contract.js";
import {vaultAbi} from "@/blockchain/abi.js";
@@ -13,7 +13,7 @@ export function onChainChanged(chainId) {
store.chainId = chainId // touch the chainId last. will cause any clients of the store's provider getter to refresh
store.account = null
const provider = new ethers.BrowserProvider(window.ethereum, chainId);
setProvider(provider, chainId)
store.provider = provider
provider.listAccounts().then((accounts)=>changeAccounts(accounts.map((a)=>a.address)))
}
}
@@ -30,12 +30,10 @@ function changeAccounts(accounts) {
const addr = accounts[0]
const store = useStore()
store.account = addr
console.log('set store.account to', addr, store.account)
discoverVaults()
flushTransactions()
socket.emit('address', store.chainId, addr)
}
console.log('changeAccounts ended')
}
function onAccountsChanged(accounts) {
@@ -102,7 +100,6 @@ function discoverVaults() {
s.vaults = []
else
_discoverVaults(owner).then((result)=>{
console.log('read store.account', s.account)
if( s.account === owner ) { // double-check the account since it could have changed during our await
s.vaults = result
if( pendingOrders.length )