order status subscriptions

This commit is contained in:
Tim Olson
2023-11-06 23:49:08 -04:00
parent 8444f7af7e
commit 25aef69ea3
5 changed files with 37 additions and 8 deletions

View File

@@ -16,9 +16,8 @@ export class CacheSet {
}
async contains(chain, key) {
const series = `${chain}|${this.series}`;
const result = await redis.sIsMember(series, key)
console.log('contains', series, key, result)
const result = await redis.sIsMember(`${chain}|${this.series}`, key)
console.log('contains', `${chain}|${this.series}`, key, result)
return result
}
@@ -78,3 +77,7 @@ async function latestBlock(chain) {
export const vaults = new CacheDict('v')
export const vaultBalances = new CacheDict('vb')
export const prices = new CacheDict('p')
export const orderStatuses = new CacheSet('o')
export const vaultOpenOrders = new CacheDict('voo')
export const orderFilled = new CacheDict('of')
export const trancheFilled = new CacheDict('tf')

2
io.js
View File

@@ -11,5 +11,5 @@ export const httpServer = createServer()
export const io = new Server(httpServer, options)
const pubClient = redis.duplicate();
await pubClient.connect()
const adapter = createAdapter(pubClient, redis, {/*key:'public|'*/})
const adapter = createAdapter(pubClient, redis, {/*key:'socket.io'*/})
io.adapter(adapter)

View File

@@ -5,6 +5,7 @@ import {httpServer, io} from "./io.js";
import {ensureVault, loginAddress} from "./vault.js";
import {chainInfo, VAULT_INIT_CODE_HASH} from "./chain.js";
import {subPools, unsubPools} from "./pool.js";
import {subVaultOrders, unsubVaultOrders} from "./order.js";
// setup socket.io
@@ -17,6 +18,8 @@ io.on("connection", (socket) => {
socket.on('address', (chainId, address) => loginAddress(socket, chainId, address) )
socket.on('subPools', (chainId, addresses) => subPools(socket, chainId, addresses) )
socket.on('unsubPools', (chainId, addresses) => unsubPools(socket, chainId, addresses) )
socket.on('subVaultOrders', (chainId, address) => subVaultOrders(socket, chainId, address) )
socket.on('unsubVaultOrders', (chainId, address) => unsubVaultOrders(socket, chainId, address) )
socket.on('ensureVault', (chainId,owner,num) => ensureVault(socket, chainId, owner, num) )
socket.join('public')
socket.emit('welcome', {chainInfo, vaultInitCodeHash:VAULT_INIT_CODE_HASH})

23
order.js Normal file
View File

@@ -0,0 +1,23 @@
import {redis, vaultOpenOrders} from "./cache.js";
export function subVaultOrders( 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 ) {
}
}
})
}
export function unsubVaultOrders( socket, chainId, vault ) {
}
export function orderStatus( chainId, vault, orderIndex ) {
}

View File

@@ -29,7 +29,7 @@ export function vaultAddress(chainId, owner, num=0) {
async function emitBalances(socket, chainId, vault) {
const balances = await vaultBalances.get(chainId, vault);
socket.emit('vb', vault, balances !== null ? balances : '{}')
socket.emit('vb', chainId, vault, balances !== null ? balances : '{}')
return balances;
}
@@ -38,7 +38,7 @@ export async function loginAddress(socket, chainId, address) {
if( socket.user_room !== undefined)
socket.leave(socket.user_room)
if( address === undefined ) {
socket.emit('vaults', [])
socket.emit('vaults', chainId, address, [])
}
else {
socket.user_room = `${chainId}|${address}`
@@ -55,7 +55,7 @@ export async function loginAddress(socket, chainId, address) {
else
break
}
socket.emit('vaults', found)
socket.emit('vaults', chainId, address, found)
}
}
@@ -70,7 +70,7 @@ export async function ensureVault(socket, chainId, owner, num) {
const vault = await createVault(chainId, owner, num)
console.log(vault)
if (vault !== null) {
socket.emit('vaults', [vault])
socket.emit('vaults', chainId, owner, [vault])
await emitBalances(socket, chainId, vault)
}
else