historical order query

This commit is contained in:
Tim Olson
2023-11-13 20:55:57 -04:00
parent 5e184600e1
commit d8fcf4209c
3 changed files with 50 additions and 14 deletions

4
db.js
View File

@@ -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'")
})

View File

@@ -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
}

View File

@@ -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