order status updates working

This commit is contained in:
Tim Olson
2023-11-08 23:18:36 -04:00
parent 25aef69ea3
commit 2aae833ff4
4 changed files with 28 additions and 16 deletions

View File

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