108 lines
3.3 KiB
JavaScript
108 lines
3.3 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 {parseOrderStatus} from "@/blockchain/orderlib.js";
|
|
|
|
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) => {
|
|
console.log('pool price from message', chainId, pool, price)
|
|
const s = useStore()
|
|
if( s.chainId.value !== chainId )
|
|
return
|
|
s.poolPrices[[chainId,pool]] = price
|
|
})
|
|
|
|
socket.on('vb', async (chainId, vault, balances) => {
|
|
const s = useStore()
|
|
if( s.chainId.value !== 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()
|
|
console.log('vaults', vaults)
|
|
if( s.chainId.value !== chainId || s.account !== owner )
|
|
return
|
|
s.vaults = vaults
|
|
if( vaults.length ) {
|
|
const vault = vaults[0]
|
|
flushOrders(vault)
|
|
}
|
|
})
|
|
|
|
|
|
function handleOrderStatus(chainId, vault, orderIndex, status) {
|
|
const s = useStore()
|
|
if( s.chainId.value !== chainId )
|
|
return
|
|
// message 'o' is a single order status
|
|
const parsed = parseOrderStatus(status);
|
|
console.log('o', chainId, vault, orderIndex, status, parsed)
|
|
if( !(vault in s.orders) )
|
|
s.orders[vault] = {}
|
|
s.orders[vault][orderIndex] = parsed
|
|
}
|
|
|
|
socket.on('os', (chainId, vault, orders) => {
|
|
// message 'os' has multiple order statuses
|
|
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, filled)=>{
|
|
const s = useStore()
|
|
if( s.chainId.value !== chainId )
|
|
return
|
|
console.log('of', chainId, vault, orderIndex, filled)
|
|
if( !(vault in s.orders) ) {
|
|
console.log('warning: got fill on an order in an unknown vault')
|
|
return
|
|
}
|
|
if( !(orderIndex in s.orders[vault]) ) {
|
|
console.log(`warning: orderIndex ${orderIndex} missing from vault ${vault}`)
|
|
return
|
|
}
|
|
|
|
const status = s.orders[vault][orderIndex]
|
|
console.log('apply fills', status, filled)
|
|
status.filledIn = BigInt(filled[0][0])
|
|
status.filledOut = BigInt(filled[0][1])
|
|
for( const i in filled[1] ) {
|
|
const [filledIn, filledOut] = filled[1][i]
|
|
status.trancheFilledIn[i] = BigInt(filledIn)
|
|
status.trancheFilledOut[i] = BigInt(filledOut)
|
|
}
|
|
s.orders[vault][orderIndex] = status
|
|
console.log('applied fills', status)
|
|
})
|