From 2aae833ff41ec6557b59a57eb0d15b75702850ed Mon Sep 17 00:00:00 2001 From: Tim Olson <> Date: Wed, 8 Nov 2023 23:18:36 -0400 Subject: [PATCH] order status updates working --- cache.js | 3 +-- main.js | 3 --- order.js | 35 ++++++++++++++++++++++++----------- vault.js | 3 +++ 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/cache.js b/cache.js index a64be88..60f9256 100644 --- a/cache.js +++ b/cache.js @@ -77,7 +77,6 @@ async function latestBlock(chain) { export const vaults = new CacheDict('v') export const vaultBalances = new CacheDict('vb') export const prices = new CacheDict('p') -export const orderStatuses = new CacheSet('o') +export const orderStatuses = new CacheDict('o') export const vaultOpenOrders = new CacheDict('voo') export const orderFilled = new CacheDict('of') -export const trancheFilled = new CacheDict('tf') diff --git a/main.js b/main.js index c533530..2507739 100644 --- a/main.js +++ b/main.js @@ -5,7 +5,6 @@ import {httpServer, io} from "./io.js"; import {ensureVault, loginAddress} from "./vault.js"; import {chainInfo, VAULT_INIT_CODE_HASH} from "./chain.js"; import {subPools, unsubPools} from "./pool.js"; -import {subVaultOrders, unsubVaultOrders} from "./order.js"; // setup socket.io @@ -18,8 +17,6 @@ io.on("connection", (socket) => { socket.on('address', (chainId, address) => loginAddress(socket, chainId, address) ) socket.on('subPools', (chainId, addresses) => subPools(socket, chainId, addresses) ) socket.on('unsubPools', (chainId, addresses) => unsubPools(socket, chainId, addresses) ) - socket.on('subVaultOrders', (chainId, address) => subVaultOrders(socket, chainId, address) ) - socket.on('unsubVaultOrders', (chainId, address) => unsubVaultOrders(socket, chainId, address) ) socket.on('ensureVault', (chainId,owner,num) => ensureVault(socket, chainId, owner, num) ) socket.join('public') socket.emit('welcome', {chainInfo, vaultInitCodeHash:VAULT_INIT_CODE_HASH}) diff --git a/order.js b/order.js index b93ba39..f5ea839 100644 --- a/order.js +++ b/order.js @@ -1,23 +1,36 @@ -import {redis, vaultOpenOrders} from "./cache.js"; +import {orderFilled, orderStatuses, vaultOpenOrders} from "./cache.js" +import {applyFills} from "../web/src/blockchain/common.js" -export function subVaultOrders( socket, chainId, vault ) { +export function sendVaultOrders( socket, chainId, vault ) { vaultOpenOrders.get(chainId, vault).then(async (orderIndexes)=>{ - for( const orderIndex of JSON.parse(orderIndexes) ) { - // there is a race condition here since we need multiple queries to get the complete order status, - // so we diligently check for nulls and exclude such an order, since it was deleted and no longer active. - const status = orderStatus( chainId, vault, orderIndex ) - if( status != null ) { - + const result = [] + if( orderIndexes !== null ) { + for (const orderIndex of JSON.parse(orderIndexes)) { + // there is a race condition here since we need multiple queries to get the complete order status, + // so we diligently check for nulls and exclude such an order, since it was deleted and no longer active. + const status = await orderStatus(chainId, vault, orderIndex) + if (status !== null) + result.push([orderIndex, status]) } } + socket.emit('os', chainId, vault, result) }) } export function unsubVaultOrders( socket, chainId, vault ) { - + console.log('todo: unsubVaultOrders') // todo } -export function orderStatus( chainId, vault, orderIndex ) { - +export async function orderStatus( chainId, vault, orderIndex ) { + const orderKey = `${vault}|${orderIndex}` + let status = await orderStatuses.get(chainId, orderKey) + if( status !== null ) { + status = JSON.parse(status) + const fills = await orderFilled.get(chainId, orderKey) + if (fills !== null) { + applyFills(status, JSON.parse(fills)) + } + } + return status } diff --git a/vault.js b/vault.js index 24f01b0..776231b 100644 --- a/vault.js +++ b/vault.js @@ -3,6 +3,7 @@ import {getAbi} from "./abi.js"; import {getProvider, getSigner} from "./blockchain.js"; import {vaultBalances, vaults} from './cache.js'; import {chainInfo, VAULT_INIT_CODE_HASH} from "./chain.js"; +import {sendVaultOrders} from "./order.js"; // Vault // address owner @@ -56,6 +57,8 @@ export async function loginAddress(socket, chainId, address) { break } socket.emit('vaults', chainId, address, found) + for( const vault of found ) + sendVaultOrders(socket, chainId, vault) } }