37 lines
1.4 KiB
JavaScript
37 lines
1.4 KiB
JavaScript
import {orderFilled, orderStatuses, vaultOpenOrders} from "./cache.js"
|
|
import {applyFills} from "../web/src/blockchain/common.js"
|
|
|
|
|
|
export function sendVaultOrders( socket, chainId, vault ) {
|
|
vaultOpenOrders.get(chainId, vault).then(async (orderIndexes)=>{
|
|
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 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
|
|
}
|