diff --git a/cache.js b/cache.js index 98f7db0..719982b 100644 --- a/cache.js +++ b/cache.js @@ -29,9 +29,9 @@ export class CacheDict { this.series = series } - async get(chain, key) { + async get(chain, key, default_=null) { const result = await redis.hGet(`${chain}|${this.series}`, key) - return result === null ? null : '' + result + return result === null ? default_ : '' + result } async contains(chain, key) { @@ -80,5 +80,6 @@ export const vaultBalances = new CacheDict('vb') export const prices = new CacheDict('p') export const orderStatuses = new CacheDict('o') export const vaultOpenOrders = new CacheDict('voo') +export const vaultRecentlyClosedOrders = new CacheDict('vrco') export const orderFilled = new CacheDict('of') export const ohlcs = new CacheDict('ohlc') diff --git a/order.js b/order.js index 1bed58f..d40d6a8 100644 --- a/order.js +++ b/order.js @@ -1,12 +1,20 @@ -import {orderFilled, orderStatuses, vaultOpenOrders} from "./cache.js" +import {orderFilled, orderStatuses, vaultOpenOrders, vaultRecentlyClosedOrders} from "./cache.js" import {sql} from "./db.js"; export function sendVaultOrders( socket, chainId, vault ) { - vaultOpenOrders.get(chainId, vault).then(async (orderIndexes)=>{ + Promise.all([ + vaultOpenOrders.get(chainId, vault, '[]'), + vaultRecentlyClosedOrders.get(chainId, vault, '[]'), + archivedOrders(chainId, vault), + ]).then(async (got)=>{ + const [openIndexes, closedIndexes, recents] = got + console.log('vault open orders', openIndexes) + console.log('vault closed orders', closedIndexes) const statuses = {} - if( orderIndexes !== null ) { - for (const orderIndex of JSON.parse(orderIndexes)) { + const indexes = [...JSON.parse(openIndexes), ...JSON.parse(closedIndexes)] + if( openIndexes !== null ) { + for (const orderIndex of indexes) { // there is a race condition here since we need multiple queries to get the complete order status, // so we check for nulls and exclude such an order, since it was deleted and no longer active. const status = await orderStatus(chainId, vault, orderIndex) @@ -14,23 +22,22 @@ export function sendVaultOrders( socket, chainId, vault ) { statuses[orderIndex] = status } } - recentOrders(socket, chainId, vault).then(async (recents)=>{ - const proms = [] - for( let [orderIndex, status] of recents ) { - if( !(orderIndex in statuses) ) { - // only write the database version if there's no open order in the memcache - const orderKey = `${vault}|${orderIndex.toString().padStart(5,'0')}` - proms.push(fillOrderStatus(chainId, orderKey, status)) - statuses[orderIndex] = status - } + console.log('vault archived orders', recents) + const proms = [] + for( let [orderIndex, status] of recents ) { + if( !(orderIndex in statuses) ) { + // only write the database version if there's no open order in the memcache + const orderKey = `${vault}|${orderIndex.toString().padStart(5,'0')}` + proms.push(fillOrderStatus(chainId, orderKey, status)) + statuses[orderIndex] = status } - await Promise.all(proms) - const result = [] - for( const index of Object.keys(statuses).sort()) - result.push([parseInt(index), statuses[index]]) - socket.emit('os', chainId, vault, result) - }) - }).catch((reason)=>console.error('Could not send orders', reason)) + } + await Promise.all(proms) + const result = [] + for( const index of Object.keys(statuses).sort()) + result.push([parseInt(index), statuses[index]]) + socket.emit('os', chainId, vault, result) + }) } export function unsubVaultOrders( socket, chainId, vault ) { @@ -53,7 +60,7 @@ async function fillOrderStatus( chainId, orderKey, status ) { applyFills(status, JSON.parse(fills)) } -export async function recentOrders( socket, chainId, vault, limit=25 ) { +export async function archivedOrders(chainId, vault, limit=25 ) { const query = await sql( `select oi.order_index, sd.value from seriesdict sd, diff --git a/vault.js b/vault.js index 99be8fe..1b6659c 100644 --- a/vault.js +++ b/vault.js @@ -5,10 +5,6 @@ import {vaultBalances, vaults} from './cache.js'; import {chainInfo, VAULT_INIT_CODE_HASH} from "./chain.js"; import {sendVaultOrders} from "./order.js"; -// Vault -// address owner -// balances { tokenAddress: amount } -// recentOrders [] const vaultAbi = await getAbi('Vault') const factoryAbi = await getAbi('Factory')