From d8fcf4209c79977daac518429ebd1c2a0b405a10 Mon Sep 17 00:00:00 2001 From: Tim Olson <> Date: Mon, 13 Nov 2023 20:55:57 -0400 Subject: [PATCH] historical order query --- db.js | 4 ++-- order.js | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- token.js | 8 ++++---- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/db.js b/db.js index 24ab5b3..7932107 100644 --- a/db.js +++ b/db.js @@ -1,11 +1,11 @@ import pg from 'pg' -export const pool = new pg.Pool({ +export const dbpool = new pg.Pool({ connectionString: process.env.DEXORDER_DB_URL || 'postgres://dexorder:redroxed@localhost:5432/dexorder', max: parseInt(process.env.DEXORDER_POOL_SIZE) || 10, idleTimeoutMillis: parseInt(process.env.DEXORDER_POOL_TIMEOUT) || 10*60*1000, }) -pool.on('connect', (client) => { +dbpool.on('connect', (client) => { client.query("SET TIME ZONE 'UTC'") }) diff --git a/order.js b/order.js index f5ea839..d3208af 100644 --- a/order.js +++ b/order.js @@ -1,20 +1,37 @@ import {orderFilled, orderStatuses, vaultOpenOrders} from "./cache.js" import {applyFills} from "../web/src/blockchain/common.js" +import {dbpool} from "./db.js"; export function sendVaultOrders( socket, chainId, vault ) { + vaultOpenOrders.get(chainId, vault).then(async (orderIndexes)=>{ - const result = [] + const statuses = {} 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. + // 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) if (status !== null) - result.push([orderIndex, status]) + statuses[orderIndex] = status } } - socket.emit('os', chainId, vault, result) + 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}` + 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([index, statuses[index]]) + socket.emit('os', chainId, vault, result) + }) }) } @@ -27,10 +44,29 @@ export async function orderStatus( chainId, 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)) - } + await fillOrderStatus(chainId, orderKey, status) } return status } + +async function fillOrderStatus( chainId, orderKey, status ) { + const fills = await orderFilled.get(chainId, orderKey) + if (fills !== null) + applyFills(status, JSON.parse(fills)) +} + +export async function recentOrders( socket, chainId, vault, limit=25 ) { + const db = await dbpool.connect() + const sql = + `select oi.order_index, sd.value from seriesdict sd, orderindex oi + where oi.chain=${chainId} and oi.vault='${vault}' + and sd.series='o' and sd.key=concat('${vault}','|',to_char(oi.order_index, 'FM00000')) + order by oi.order_index desc limit ${limit}`; + console.log('sql',sql) + const query = await db.query(sql) + console.log('got historical statuses', query) + const result = [] + for( const {order_index, value} of query.rows) + result.push([order_index,JSON.parse(value)]) + return result +} diff --git a/token.js b/token.js index 85de8c5..217201b 100644 --- a/token.js +++ b/token.js @@ -13,7 +13,7 @@ const tokens = { } export async function lookupToken(chainId, address) { - console.log('lookupToken', chainId, address) + // console.log('lookupToken', chainId, address) const chainTokens = tokens[chainId] if( chainTokens === undefined ) { console.log('no tokens for chain', chainId) @@ -21,14 +21,14 @@ export async function lookupToken(chainId, address) { } try { let result = chainTokens[address] - console.log('found', result) + // console.log('found', result) if (result === undefined) { // todo look in tokens project const addr = ethers.getAddress(address) - console.log('addr', addr) + // console.log('addr', addr) const token = new ethers.Contract(addr, erc20Abi, getProvider(chainId)) const symbol = await token.symbol() - console.log('symbol', symbol) + // console.log('symbol', symbol) const decimals = Number(await token.decimals()) result = {name: null, symbol, decimals, address: addr} chainTokens[addr] = result