historical order query
This commit is contained in:
4
db.js
4
db.js
@@ -1,11 +1,11 @@
|
|||||||
import pg from 'pg'
|
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',
|
connectionString: process.env.DEXORDER_DB_URL || 'postgres://dexorder:redroxed@localhost:5432/dexorder',
|
||||||
max: parseInt(process.env.DEXORDER_POOL_SIZE) || 10,
|
max: parseInt(process.env.DEXORDER_POOL_SIZE) || 10,
|
||||||
idleTimeoutMillis: parseInt(process.env.DEXORDER_POOL_TIMEOUT) || 10*60*1000,
|
idleTimeoutMillis: parseInt(process.env.DEXORDER_POOL_TIMEOUT) || 10*60*1000,
|
||||||
})
|
})
|
||||||
|
|
||||||
pool.on('connect', (client) => {
|
dbpool.on('connect', (client) => {
|
||||||
client.query("SET TIME ZONE 'UTC'")
|
client.query("SET TIME ZONE 'UTC'")
|
||||||
})
|
})
|
||||||
|
|||||||
52
order.js
52
order.js
@@ -1,20 +1,37 @@
|
|||||||
import {orderFilled, orderStatuses, vaultOpenOrders} from "./cache.js"
|
import {orderFilled, orderStatuses, vaultOpenOrders} from "./cache.js"
|
||||||
import {applyFills} from "../web/src/blockchain/common.js"
|
import {applyFills} from "../web/src/blockchain/common.js"
|
||||||
|
import {dbpool} from "./db.js";
|
||||||
|
|
||||||
|
|
||||||
export function sendVaultOrders( socket, chainId, vault ) {
|
export function sendVaultOrders( socket, chainId, vault ) {
|
||||||
|
|
||||||
vaultOpenOrders.get(chainId, vault).then(async (orderIndexes)=>{
|
vaultOpenOrders.get(chainId, vault).then(async (orderIndexes)=>{
|
||||||
const result = []
|
const statuses = {}
|
||||||
if( orderIndexes !== null ) {
|
if( orderIndexes !== null ) {
|
||||||
for (const orderIndex of JSON.parse(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,
|
// 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)
|
const status = await orderStatus(chainId, vault, orderIndex)
|
||||||
if (status !== null)
|
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)
|
let status = await orderStatuses.get(chainId, orderKey)
|
||||||
if( status !== null ) {
|
if( status !== null ) {
|
||||||
status = JSON.parse(status)
|
status = JSON.parse(status)
|
||||||
const fills = await orderFilled.get(chainId, orderKey)
|
await fillOrderStatus(chainId, orderKey, status)
|
||||||
if (fills !== null) {
|
|
||||||
applyFills(status, JSON.parse(fills))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 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
|
||||||
|
}
|
||||||
|
|||||||
8
token.js
8
token.js
@@ -13,7 +13,7 @@ const tokens = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function lookupToken(chainId, address) {
|
export async function lookupToken(chainId, address) {
|
||||||
console.log('lookupToken', chainId, address)
|
// console.log('lookupToken', chainId, address)
|
||||||
const chainTokens = tokens[chainId]
|
const chainTokens = tokens[chainId]
|
||||||
if( chainTokens === undefined ) {
|
if( chainTokens === undefined ) {
|
||||||
console.log('no tokens for chain', chainId)
|
console.log('no tokens for chain', chainId)
|
||||||
@@ -21,14 +21,14 @@ export async function lookupToken(chainId, address) {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
let result = chainTokens[address]
|
let result = chainTokens[address]
|
||||||
console.log('found', result)
|
// console.log('found', result)
|
||||||
if (result === undefined) {
|
if (result === undefined) {
|
||||||
// todo look in tokens project
|
// todo look in tokens project
|
||||||
const addr = ethers.getAddress(address)
|
const addr = ethers.getAddress(address)
|
||||||
console.log('addr', addr)
|
// console.log('addr', addr)
|
||||||
const token = new ethers.Contract(addr, erc20Abi, getProvider(chainId))
|
const token = new ethers.Contract(addr, erc20Abi, getProvider(chainId))
|
||||||
const symbol = await token.symbol()
|
const symbol = await token.symbol()
|
||||||
console.log('symbol', symbol)
|
// console.log('symbol', symbol)
|
||||||
const decimals = Number(await token.decimals())
|
const decimals = Number(await token.decimals())
|
||||||
result = {name: null, symbol, decimals, address: addr}
|
result = {name: null, symbol, decimals, address: addr}
|
||||||
chainTokens[addr] = result
|
chainTokens[addr] = result
|
||||||
|
|||||||
Reference in New Issue
Block a user