Skip to content

Commit

Permalink
improve handling of enums in client
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider committed Apr 10, 2024
1 parent 9d7173a commit 706186b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions ts/client/src/accounts/bookSide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class BookSide {
return;
}
const stack = [this.rootFixed.maybeNode];
const [left, right] = this.side === SideUtils.Bid ? [1, 0] : [0, 1];
const [left, right] = this.side.bid ? [1, 0] : [0, 1];

while (stack.length > 0) {
const index = stack.pop()!;
Expand Down Expand Up @@ -94,7 +94,7 @@ export class BookSide {
return;
}
const stack = [this.rootOraclePegged.maybeNode];
const [left, right] = this.side === SideUtils.Bid ? [1, 0] : [0, 1];
const [left, right] = this.side.bid ? [1, 0] : [0, 1];

while (stack.length > 0) {
const index = stack.pop()!;
Expand Down Expand Up @@ -122,7 +122,7 @@ export class BookSide {
public compareOrders(a: Order, b: Order): boolean {
return a.priceLots.eq(b.priceLots)
? a.seqNum.lt(b.seqNum) // if prices are equal prefer orders in the order they are placed
: this.side === SideUtils.Bid // else compare the actual prices
: this.side.bid // else compare the actual prices
? a.priceLots.gt(b.priceLots)
: b.priceLots.gt(a.priceLots);
}
Expand Down
16 changes: 8 additions & 8 deletions ts/client/src/accounts/openOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class OpenOrders {
public async placeOrder(order: OrderToPlace): Promise<string> {
// derive token account
const mint =
order.side === SideUtils.Bid
order.side.bid
? this.market.account.quoteMint
: this.market.account.baseMint;
const userTokenAccount = await getAssociatedTokenAddress(
Expand All @@ -172,11 +172,11 @@ export class OpenOrders {

const remainingAccounts = new Set<string>();
const bookSide =
order.side === SideUtils.Bid ? this.market.asks : this.market.bids;
order.side.bid ? this.market.asks : this.market.bids;
if (
bookSide &&
order.orderType !== PlaceOrderTypeUtils.PostOnly &&
order.orderType !== PlaceOrderTypeUtils.PostOnlySlide
!order.orderType?.postOnly &&
!order.orderType?.postOnlySlide
) {
for (const order of bookSide.items()) {
remainingAccounts.add(order.leafNode.owner.toBase58());
Expand Down Expand Up @@ -411,11 +411,11 @@ export class OpenOrders {
? new BN(order.quoteLimit)
: I64_MAX_BN;
const clientOrderId = new BN(order.clientOrderId || Date.now());
const orderType = order.orderType || PlaceOrderTypeUtils.Limit;
const expiryTimestamp = new BN(order.expiryTimestamp || 0);
const orderType = order.orderType ?? PlaceOrderTypeUtils.Limit;
const expiryTimestamp = new BN(order.expiryTimestamp ?? 0);
const selfTradeBehavior =
order.selfTradeBehavior || SelfTradeBehaviorUtils.DecrementTake;
const limit = order.matchLoopLimit || 16;
order.selfTradeBehavior ?? SelfTradeBehaviorUtils.DecrementTake;
const limit = order.matchLoopLimit ?? 16;

const args = {
side: order.side,
Expand Down
6 changes: 3 additions & 3 deletions ts/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ export class OpenBookV2Client {
userBaseAccount,
userQuoteAccount,
marketBaseVault: market.marketBaseVault,
marketQuoteVault: market.marketBaseVault,
marketQuoteVault: market.marketQuoteVault,
tokenProgram: TOKEN_PROGRAM_ID,
})
.instruction();
Expand All @@ -589,7 +589,7 @@ export class OpenBookV2Client {
openOrdersDelegate?: Keypair,
): Promise<[TransactionInstruction, Signer[]]> {
const marketVault =
args.side === SideUtils.Bid
args.side.bid
? market.marketQuoteVault
: market.marketBaseVault;
const accountsMeta: AccountMeta[] = remainingAccounts.map((remaining) => ({
Expand Down Expand Up @@ -638,7 +638,7 @@ export class OpenBookV2Client {
openOrdersDelegate?: Keypair,
): Promise<[TransactionInstruction, Signer[]]> {
const marketVault =
args.side === SideUtils.Bid
args.side.bid
? market.marketQuoteVault
: market.marketBaseVault;
const accountsMeta: AccountMeta[] = remainingAccounts.map((remaining) => ({
Expand Down
4 changes: 2 additions & 2 deletions ts/client/src/structs/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Order {
public isOraclePegged = false,
) {
this.seqNum =
this.side === SideUtils.Bid
this.side.bid
? U64_MAX_BN.sub(this.leafNode.key.maskn(64))
: this.leafNode.key.maskn(64);
const priceData = this.leafNode.key.ushrn(64);
Expand All @@ -39,7 +39,7 @@ export class Order {
}

public toPrettyString(): string {
return `side:${this.side === SideUtils.Bid ? 'bid' : 'ask'} price:${
return `side:${this.side.bid ? 'bid' : 'ask'} price:${
this.price
} size:${this.size} seqNum:${this.seqNum.toString()} expired:${
this.isExpired
Expand Down

0 comments on commit 706186b

Please sign in to comment.