104 lines
3.0 KiB
JavaScript
104 lines
3.0 KiB
JavaScript
import {io} from "socket.io-client";
|
|
import {useStore} from "@/store/store.js";
|
|
import {flushOrders, onChainChanged} from "@/blockchain/wallet.js";
|
|
import {ethers} from "ethers";
|
|
import {applyFills} from "@/blockchain/common.js";
|
|
import {ref} from "vue";
|
|
|
|
export const socket = io(import.meta.env.VITE_WS_URL || undefined, {transports: ["websocket"]})
|
|
|
|
socket.on('connect', () => {
|
|
console.log(new Date(), 'ws connected')
|
|
})
|
|
|
|
socket.on('disconnect', () => {
|
|
console.log(new Date(), 'ws disconnected')
|
|
})
|
|
|
|
socket.on('welcome', async (data) => {
|
|
console.log('welcome', data)
|
|
const s = useStore()
|
|
// todo put the vaultInitCodeHash into the chainInfo
|
|
s.chainInfo = data.chainInfo
|
|
s.vaultInitCodeHash = data.vaultInitCodeHash
|
|
const p = new ethers.BrowserProvider(window.ethereum)
|
|
const network = await p.getNetwork()
|
|
if (network !== null)
|
|
onChainChanged(network.chainId)
|
|
})
|
|
|
|
socket.on('p', async (chainId, pool, price) => {
|
|
const s = useStore()
|
|
if( s.chainId !== chainId )
|
|
return
|
|
const prices = {}
|
|
prices[pool] = price
|
|
console.log('pool price from message', pool, typeof price, price)
|
|
const poolPrices = s.poolPrices
|
|
poolPrices[pool] = price
|
|
s.poolPrices = poolPrices
|
|
})
|
|
|
|
socket.on('vb', async (chainId, vault, balances) => {
|
|
const s = useStore()
|
|
if( s.chainId !== chainId )
|
|
return
|
|
console.log('vb', vault, balances)
|
|
const vb = {}
|
|
vb[vault] = JSON.parse(balances)
|
|
s.$patch({vaultBalances:vb})
|
|
console.log('vault balances', vault, vb)
|
|
})
|
|
|
|
socket.on('vaults', (chainId, owner, vaults)=>{
|
|
const s = useStore()
|
|
if( s.chainId !== chainId || s.account !== owner )
|
|
return
|
|
console.log('vaults', vaults)
|
|
s.vaults = vaults
|
|
if( vaults.length ) {
|
|
const vault = vaults[0]
|
|
flushOrders(vault)
|
|
}
|
|
})
|
|
|
|
|
|
function handleOrderStatus(chainId, vault, orderIndex, status) {
|
|
const s = useStore()
|
|
if( s.chainId !== chainId )
|
|
return
|
|
console.log('o', chainId, vault, orderIndex, status)
|
|
const orders = s.orders
|
|
if( !(vault in orders) )
|
|
orders[vault] = {}
|
|
orders[vault][orderIndex] = status
|
|
s.orders = orders
|
|
}
|
|
|
|
socket.on('os', (chainId, vault, orders) => {
|
|
console.log('os', orders)
|
|
for( const [orderIndex, status] of orders )
|
|
handleOrderStatus(chainId, vault, orderIndex, status)
|
|
})
|
|
|
|
socket.on( 'o', handleOrderStatus)
|
|
|
|
socket.on( 'of', (chainId, vault, orderIndex, fills)=>{
|
|
const s = useStore()
|
|
if( s.chainId !== chainId )
|
|
return
|
|
console.log('of', chainId, vault, orderIndex, fills)
|
|
const orders = s.orders
|
|
if( !(vault in orders) ) {
|
|
console.log('warning: got fill on an order in an unknown vault')
|
|
return
|
|
}
|
|
if( !(orderIndex in orders[vault]) ) {
|
|
console.log(`warning: orderIndex ${orderIndex} missing from vault ${vault}`)
|
|
return
|
|
}
|
|
const order = orders[vault][orderIndex]
|
|
applyFills(order, fills)
|
|
orders[vault][orderIndex] = order
|
|
})
|