vault recent orders (status bugfix)
This commit is contained in:
5
cache.js
5
cache.js
@@ -29,9 +29,9 @@ export class CacheDict {
|
|||||||
this.series = series
|
this.series = series
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(chain, key) {
|
async get(chain, key, default_=null) {
|
||||||
const result = await redis.hGet(`${chain}|${this.series}`, key)
|
const result = await redis.hGet(`${chain}|${this.series}`, key)
|
||||||
return result === null ? null : '' + result
|
return result === null ? default_ : '' + result
|
||||||
}
|
}
|
||||||
|
|
||||||
async contains(chain, key) {
|
async contains(chain, key) {
|
||||||
@@ -80,5 +80,6 @@ export const vaultBalances = new CacheDict('vb')
|
|||||||
export const prices = new CacheDict('p')
|
export const prices = new CacheDict('p')
|
||||||
export const orderStatuses = new CacheDict('o')
|
export const orderStatuses = new CacheDict('o')
|
||||||
export const vaultOpenOrders = new CacheDict('voo')
|
export const vaultOpenOrders = new CacheDict('voo')
|
||||||
|
export const vaultRecentlyClosedOrders = new CacheDict('vrco')
|
||||||
export const orderFilled = new CacheDict('of')
|
export const orderFilled = new CacheDict('of')
|
||||||
export const ohlcs = new CacheDict('ohlc')
|
export const ohlcs = new CacheDict('ohlc')
|
||||||
|
|||||||
49
order.js
49
order.js
@@ -1,12 +1,20 @@
|
|||||||
import {orderFilled, orderStatuses, vaultOpenOrders} from "./cache.js"
|
import {orderFilled, orderStatuses, vaultOpenOrders, vaultRecentlyClosedOrders} from "./cache.js"
|
||||||
import {sql} from "./db.js";
|
import {sql} from "./db.js";
|
||||||
|
|
||||||
|
|
||||||
export function sendVaultOrders( socket, chainId, vault ) {
|
export function sendVaultOrders( socket, chainId, vault ) {
|
||||||
vaultOpenOrders.get(chainId, vault).then(async (orderIndexes)=>{
|
Promise.all([
|
||||||
|
vaultOpenOrders.get(chainId, vault, '[]'),
|
||||||
|
vaultRecentlyClosedOrders.get(chainId, vault, '[]'),
|
||||||
|
archivedOrders(chainId, vault),
|
||||||
|
]).then(async (got)=>{
|
||||||
|
const [openIndexes, closedIndexes, recents] = got
|
||||||
|
console.log('vault open orders', openIndexes)
|
||||||
|
console.log('vault closed orders', closedIndexes)
|
||||||
const statuses = {}
|
const statuses = {}
|
||||||
if( orderIndexes !== null ) {
|
const indexes = [...JSON.parse(openIndexes), ...JSON.parse(closedIndexes)]
|
||||||
for (const orderIndex of JSON.parse(orderIndexes)) {
|
if( openIndexes !== null ) {
|
||||||
|
for (const orderIndex of indexes) {
|
||||||
// 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 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)
|
||||||
@@ -14,23 +22,22 @@ export function sendVaultOrders( socket, chainId, vault ) {
|
|||||||
statuses[orderIndex] = status
|
statuses[orderIndex] = status
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
recentOrders(socket, chainId, vault).then(async (recents)=>{
|
console.log('vault archived orders', recents)
|
||||||
const proms = []
|
const proms = []
|
||||||
for( let [orderIndex, status] of recents ) {
|
for( let [orderIndex, status] of recents ) {
|
||||||
if( !(orderIndex in statuses) ) {
|
if( !(orderIndex in statuses) ) {
|
||||||
// only write the database version if there's no open order in the memcache
|
// only write the database version if there's no open order in the memcache
|
||||||
const orderKey = `${vault}|${orderIndex.toString().padStart(5,'0')}`
|
const orderKey = `${vault}|${orderIndex.toString().padStart(5,'0')}`
|
||||||
proms.push(fillOrderStatus(chainId, orderKey, status))
|
proms.push(fillOrderStatus(chainId, orderKey, status))
|
||||||
statuses[orderIndex] = status
|
statuses[orderIndex] = status
|
||||||
}
|
|
||||||
}
|
}
|
||||||
await Promise.all(proms)
|
}
|
||||||
const result = []
|
await Promise.all(proms)
|
||||||
for( const index of Object.keys(statuses).sort())
|
const result = []
|
||||||
result.push([parseInt(index), statuses[index]])
|
for( const index of Object.keys(statuses).sort())
|
||||||
socket.emit('os', chainId, vault, result)
|
result.push([parseInt(index), statuses[index]])
|
||||||
})
|
socket.emit('os', chainId, vault, result)
|
||||||
}).catch((reason)=>console.error('Could not send orders', reason))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unsubVaultOrders( socket, chainId, vault ) {
|
export function unsubVaultOrders( socket, chainId, vault ) {
|
||||||
@@ -53,7 +60,7 @@ async function fillOrderStatus( chainId, orderKey, status ) {
|
|||||||
applyFills(status, JSON.parse(fills))
|
applyFills(status, JSON.parse(fills))
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function recentOrders( socket, chainId, vault, limit=25 ) {
|
export async function archivedOrders(chainId, vault, limit=25 ) {
|
||||||
const query = await sql(
|
const query = await sql(
|
||||||
`select oi.order_index, sd.value
|
`select oi.order_index, sd.value
|
||||||
from seriesdict sd,
|
from seriesdict sd,
|
||||||
|
|||||||
4
vault.js
4
vault.js
@@ -5,10 +5,6 @@ import {vaultBalances, vaults} from './cache.js';
|
|||||||
import {chainInfo, VAULT_INIT_CODE_HASH} from "./chain.js";
|
import {chainInfo, VAULT_INIT_CODE_HASH} from "./chain.js";
|
||||||
import {sendVaultOrders} from "./order.js";
|
import {sendVaultOrders} from "./order.js";
|
||||||
|
|
||||||
// Vault
|
|
||||||
// address owner
|
|
||||||
// balances { tokenAddress: amount }
|
|
||||||
// recentOrders []
|
|
||||||
|
|
||||||
const vaultAbi = await getAbi('Vault')
|
const vaultAbi = await getAbi('Vault')
|
||||||
const factoryAbi = await getAbi('Factory')
|
const factoryAbi = await getAbi('Factory')
|
||||||
|
|||||||
Reference in New Issue
Block a user