ts: fix event queue rawevents

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-09-20 19:50:21 +02:00
parent ddb68b7991
commit f96b9ded0d
2 changed files with 66 additions and 67 deletions

View File

@ -535,7 +535,7 @@ export class PerpEventQueue {
this.head = header.head; this.head = header.head;
this.count = header.count; this.count = header.count;
this.seqNum = header.seqNum; this.seqNum = header.seqNum;
this.rawEvents = buf.slice(0, this.count).map((event) => { this.rawEvents = buf.slice(this.head, this.count).map((event) => {
if (event.eventType === PerpEventQueue.FILL_EVENT_TYPE) { if (event.eventType === PerpEventQueue.FILL_EVENT_TYPE) {
return (client.program as any)._coder.types.typeLayouts return (client.program as any)._coder.types.typeLayouts
.get('FillEvent') .get('FillEvent')
@ -581,11 +581,11 @@ export class PerpEventQueue {
} }
} }
interface Event { export interface Event {
eventType: number; eventType: number;
} }
interface OutEvent extends Event { export interface OutEvent extends Event {
side: PerpOrderType; side: PerpOrderType;
ownerSlot: number; ownerSlot: number;
timestamp: BN; timestamp: BN;
@ -594,7 +594,7 @@ interface OutEvent extends Event {
quantity: BN; quantity: BN;
} }
interface FillEvent extends Event { export interface FillEvent extends Event {
takerSide: PerpOrderType; takerSide: PerpOrderType;
makerOut: boolean; makerOut: boolean;
makerSlot: number; makerSlot: number;
@ -614,6 +614,6 @@ interface FillEvent extends Event {
quantity: BN; quantity: BN;
} }
interface LiquidateEvent extends Event { export interface LiquidateEvent extends Event {
seqNum: BN; seqNum: BN;
} }

View File

@ -1,4 +1,4 @@
import { AnchorProvider, BN, Wallet } from '@project-serum/anchor'; import { AnchorProvider, Wallet } from '@project-serum/anchor';
import { Connection, Keypair, PublicKey } from '@solana/web3.js'; import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import fs from 'fs'; import fs from 'fs';
import { I80F48 } from '../accounts/I80F48'; import { I80F48 } from '../accounts/I80F48';
@ -424,58 +424,58 @@ async function main() {
); );
console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`); console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`);
// // scenario 1 // scenario 1
// // not going to be hit orders, far from each other // not going to be hit orders, far from each other
// try { try {
// const clientId = Math.floor(Math.random() * 99999); const clientId = Math.floor(Math.random() * 99999);
// const price = const price =
// group.banksMapByName.get('BTC')![0].uiPrice! - group.banksMapByName.get('BTC')![0].uiPrice! -
// Math.floor(Math.random() * 100); Math.floor(Math.random() * 100);
// console.log(`...placing perp bid ${clientId} at ${price}`); console.log(`...placing perp bid ${clientId} at ${price}`);
// const sig = await client.perpPlaceOrder( const sig = await client.perpPlaceOrder(
// group, group,
// mangoAccount, mangoAccount,
// 'BTC-PERP', 'BTC-PERP',
// Side.bid, Side.bid,
// price, price,
// 0.01, 0.01,
// price * 0.01, price * 0.01,
// clientId, clientId,
// PerpOrderType.limit, PerpOrderType.limit,
// 0, //Date.now() + 200, 0, //Date.now() + 200,
// 1, 1,
// ); );
// console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`); console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`);
// } catch (error) { } catch (error) {
// console.log(error); console.log(error);
// } }
// try { try {
// const clientId = Math.floor(Math.random() * 99999); const clientId = Math.floor(Math.random() * 99999);
// const price = const price =
// group.banksMapByName.get('BTC')![0].uiPrice! + group.banksMapByName.get('BTC')![0].uiPrice! +
// Math.floor(Math.random() * 100); Math.floor(Math.random() * 100);
// console.log(`...placing perp ask ${clientId} at ${price}`); console.log(`...placing perp ask ${clientId} at ${price}`);
// const sig = await client.perpPlaceOrder( const sig = await client.perpPlaceOrder(
// group, group,
// mangoAccount, mangoAccount,
// 'BTC-PERP', 'BTC-PERP',
// Side.ask, Side.ask,
// price, price,
// 0.01, 0.01,
// price * 0.01, price * 0.01,
// clientId, clientId,
// PerpOrderType.limit, PerpOrderType.limit,
// 0, //Date.now() + 200, 0, //Date.now() + 200,
// 1, 1,
// ); );
// console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`); console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`);
// } catch (error) { } catch (error) {
// console.log(error); console.log(error);
// } }
// // should be able to cancel them // should be able to cancel them
// console.log(`...cancelling all perp orders`); console.log(`...cancelling all perp orders`);
// sig = await client.perpCancelAllOrders(group, mangoAccount, 'BTC-PERP', 10); sig = await client.perpCancelAllOrders(group, mangoAccount, 'BTC-PERP', 10);
// console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`); console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`);
// scenario 2 // scenario 2
// make + take orders // make + take orders
@ -511,7 +511,7 @@ async function main() {
Side.ask, Side.ask,
price, price,
0.01, 0.01,
price * 0.01, price * 0.011,
clientId, clientId,
PerpOrderType.limit, PerpOrderType.limit,
0, //Date.now() + 200, 0, //Date.now() + 200,
@ -521,17 +521,17 @@ async function main() {
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
// should be able to cancel them // // should be able to cancel them : know bug
console.log(`...cancelling all perp orders`); // console.log(`...cancelling all perp orders`);
sig = await client.perpCancelAllOrders(group, mangoAccount, 'BTC-PERP', 10); // sig = await client.perpCancelAllOrders(group, mangoAccount, 'BTC-PERP', 10);
console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`); // console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`);
const perpMarket = group.perpMarketsMap.get('BTC-PERP'); const perpMarket = group.perpMarketsMap.get('BTC-PERP');
const bids: BookSide = await perpMarket?.loadBids(client)!; const bids: BookSide = await perpMarket?.loadBids(client)!;
console.log(Array.from(bids.items())); console.log(`bids - ${Array.from(bids.items())}`);
const asks: BookSide = await perpMarket?.loadAsks(client)!; const asks: BookSide = await perpMarket?.loadAsks(client)!;
console.log(Array.from(asks.items())); console.log(`asks - ${Array.from(asks.items())}`);
await perpMarket?.loadEventQueue(client)!; await perpMarket?.loadEventQueue(client)!;
const fr = await perpMarket?.getCurrentFundingRate( const fr = await perpMarket?.getCurrentFundingRate(
@ -541,8 +541,7 @@ async function main() {
console.log(`current funding rate per hour is ${fr}`); console.log(`current funding rate per hour is ${fr}`);
const eq = await perpMarket?.loadEventQueue(client)!; const eq = await perpMarket?.loadEventQueue(client)!;
console.log(eq.rawEvents); console.log(`raw events - ${eq.rawEvents}`);
console.log(eq.eventsSince(new BN(0)));
// sleep so that keeper can catch up // sleep so that keeper can catch up
await new Promise((r) => setTimeout(r, 2000)); await new Promise((r) => setTimeout(r, 2000));