@triadxyz/triad-protocol 1.7.6-beta → 1.7.7-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/trade.d.ts CHANGED
@@ -270,6 +270,11 @@ export default class Trade {
270
270
  getUserTradeIxs(): Promise<{
271
271
  userTradePDA: PublicKey;
272
272
  ixs: TransactionInstruction[];
273
+ nonce: number;
274
+ } | {
275
+ userTradePDA: PublicKey;
276
+ ixs: TransactionInstruction[];
277
+ nonce?: undefined;
273
278
  }>;
274
279
  /**
275
280
  * Place Order
package/dist/trade.js CHANGED
@@ -408,45 +408,30 @@ class Trade {
408
408
  signer: this.provider.publicKey
409
409
  })
410
410
  .instruction());
411
+ return {
412
+ userTradePDA,
413
+ ixs,
414
+ nonce: 0
415
+ };
411
416
  }
412
- if (myUserTrades.length > 0) {
413
- try {
414
- userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
415
- // Check if we need to create a new sub user trade
416
- // This happens when all existing slots have open orders
417
- const allSlotsHaveOpenOrders = myUserTrades.every((trade) => trade.orders.some((order) => order.orderStatus === trade_1.OrderStatus.OPEN ||
418
- order.orderStatus === trade_1.OrderStatus.WAITING));
419
- if (allSlotsHaveOpenOrders) {
420
- // Find the main user trade to get the current nonce
421
- const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
422
- if (!mainUserTrade) {
423
- throw new Error('No main user trade found');
424
- }
425
- const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
426
- ixs.push(yield this.program.methods
427
- .createSubUserTrade(subUserTradePDA)
428
- .accounts({
429
- signer: this.provider.publicKey
430
- })
431
- .instruction());
432
- }
433
- }
434
- catch (error) {
435
- const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
436
- const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
437
- userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
438
- ixs.push(yield this.program.methods
439
- .createSubUserTrade(subUserTradePDA)
440
- .accounts({
441
- signer: this.provider.publicKey
442
- })
443
- .instruction());
444
- }
417
+ try {
418
+ const userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
419
+ return { userTradePDA, ixs };
420
+ }
421
+ catch (_a) {
422
+ const mainUserTrade = myUserTrades.find((trade) => !trade.isSubUser);
423
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
424
+ ixs.push(yield this.program.methods
425
+ .createSubUserTrade(subUserTradePDA)
426
+ .accounts({
427
+ signer: this.provider.publicKey
428
+ })
429
+ .instruction());
430
+ return {
431
+ userTradePDA: (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA),
432
+ ixs
433
+ };
445
434
  }
446
- return {
447
- userTradePDA,
448
- ixs
449
- };
450
435
  });
451
436
  }
452
437
  /**
@@ -466,7 +451,10 @@ class Trade {
466
451
  const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
467
452
  const ixs = [];
468
453
  const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
469
- ixs.push(...userTradeIxs);
454
+ console.log(userTradePDA);
455
+ if (userTradeIxs.length > 0) {
456
+ ixs.push(...userTradeIxs);
457
+ }
470
458
  if (Object.keys(orderSide)[0] === 'bid') {
471
459
  ixs.push(yield this.program.methods
472
460
  .placeBidOrder({
@@ -588,10 +576,8 @@ class Trade {
588
576
  8 + // total_deposits
589
577
  8 + // total_withdraws
590
578
  8 + // opened_orders
591
- // Per order: index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 28)
592
- index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 1 + 27) +
593
- // Then offset to market_id: 8 + 8 + 8
594
- (8 + 8 + 8),
579
+ index * 91 + // total size of each order
580
+ 24,
595
581
  bytes: marketIdBytes
596
582
  }
597
583
  }));
@@ -1,12 +1,40 @@
1
1
  import { Stake, StakeVault } from './../types/stake';
2
- import { Market, Order, UserTrade } from '../types/trade';
2
+ import { Market, Order, OrderDirection, OrderSide, OrderStatus, OrderType, UserTrade } from '../types/trade';
3
3
  import { PublicKey } from '@solana/web3.js';
4
4
  export declare const encodeString: (value: string, alloc?: number) => number[];
5
5
  export declare const decodeString: (bytes: number[]) => string;
6
6
  export declare const formatStakeVault: (stakeVault: any) => StakeVault;
7
7
  export declare const formatStake: (stake: any) => Stake;
8
8
  export declare const formatMarket: (account: any, address: PublicKey) => Market;
9
- export declare const formatOrder: (order: any) => Order;
9
+ export declare const formatOrder: (order: any, authority?: string) => Order;
10
10
  export declare const formatUserTrade: (account: any, publicKey: PublicKey) => UserTrade;
11
11
  export declare const calculateStakeRewards: (stake: Stake, multiplier: number) => number;
12
12
  export declare const getTokenProgram: (mint: PublicKey) => PublicKey;
13
+ export declare const getOrderDirection: (direction: {
14
+ hype: {};
15
+ } | {
16
+ flop: {};
17
+ }) => OrderDirection;
18
+ export declare const getOrderType: (type: {
19
+ limit: {};
20
+ } | {
21
+ market: {};
22
+ }) => OrderType;
23
+ export declare const getOrderSide: (side: {
24
+ bid: {};
25
+ } | {
26
+ ask: {};
27
+ }) => OrderSide;
28
+ export declare const getOrderStatus: (status: {
29
+ init: {};
30
+ } | {
31
+ open: {};
32
+ } | {
33
+ closed: {};
34
+ } | {
35
+ claimed: {};
36
+ } | {
37
+ liquidated: {};
38
+ } | {
39
+ waiting: {};
40
+ }) => OrderStatus;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTokenProgram = exports.calculateStakeRewards = exports.formatUserTrade = exports.formatOrder = exports.formatMarket = exports.formatStake = exports.formatStakeVault = exports.decodeString = exports.encodeString = void 0;
3
+ exports.getOrderStatus = exports.getOrderSide = exports.getOrderType = exports.getOrderDirection = exports.getTokenProgram = exports.calculateStakeRewards = exports.formatUserTrade = exports.formatOrder = exports.formatMarket = exports.formatStake = exports.formatStakeVault = exports.decodeString = exports.encodeString = void 0;
4
4
  const trade_1 = require("../types/trade");
5
5
  const constants_1 = require("./constants");
6
6
  const spl_token_1 = require("@solana/spl-token");
@@ -87,22 +87,22 @@ const formatMarket = (account, address) => {
87
87
  };
88
88
  };
89
89
  exports.formatMarket = formatMarket;
90
- const formatOrder = (order) => {
90
+ const formatOrder = (order, authority) => {
91
91
  return {
92
92
  ts: order.ts.toString(),
93
93
  orderId: order.orderId.toString(),
94
94
  marketId: order.marketId.toString(),
95
- orderStatus: order.orderStatus,
95
+ orderStatus: (0, exports.getOrderStatus)(order.orderStatus),
96
96
  price: order.price.toString(),
97
97
  totalAmount: order.totalAmount.toString(),
98
98
  totalShares: order.totalShares.toString(),
99
- orderType: order.orderType,
100
- orderDirection: order.orderDirection,
101
- orderSide: order.orderSide,
99
+ orderType: (0, exports.getOrderType)(order.orderType),
100
+ orderDirection: (0, exports.getOrderDirection)(order.orderDirection),
101
+ orderSide: (0, exports.getOrderSide)(order.orderSide),
102
102
  userNonce: order.userNonce.toString(),
103
103
  linkedOrderId: order.linkedOrderId,
104
104
  filledShares: order.filledShares ? order.filledShares.toString() : '',
105
- authority: order.authority || '',
105
+ authority: authority ? authority : '',
106
106
  createdAt: order.createdAt ? order.createdAt.toString() : ''
107
107
  };
108
108
  };
@@ -149,3 +149,44 @@ const getTokenProgram = (mint) => {
149
149
  return spl_token_2.TOKEN_PROGRAM_ID;
150
150
  };
151
151
  exports.getTokenProgram = getTokenProgram;
152
+ const getOrderDirection = (direction) => {
153
+ if (Object.keys(direction)[0] === 'hype') {
154
+ return trade_1.OrderDirection.HYPE;
155
+ }
156
+ return trade_1.OrderDirection.FLOP;
157
+ };
158
+ exports.getOrderDirection = getOrderDirection;
159
+ const getOrderType = (type) => {
160
+ if (Object.keys(type)[0] === 'limit') {
161
+ return trade_1.OrderType.LIMIT;
162
+ }
163
+ return trade_1.OrderType.MARKET;
164
+ };
165
+ exports.getOrderType = getOrderType;
166
+ const getOrderSide = (side) => {
167
+ if (Object.keys(side)[0] === 'bid') {
168
+ return trade_1.OrderSide.BID;
169
+ }
170
+ return trade_1.OrderSide.ASK;
171
+ };
172
+ exports.getOrderSide = getOrderSide;
173
+ const getOrderStatus = (status) => {
174
+ let currentStatus = Object.keys(status)[0];
175
+ switch (currentStatus) {
176
+ case 'init':
177
+ return trade_1.OrderStatus.INIT;
178
+ case 'open':
179
+ return trade_1.OrderStatus.OPEN;
180
+ case 'closed':
181
+ return trade_1.OrderStatus.CLOSED;
182
+ case 'claimed':
183
+ return trade_1.OrderStatus.CLAIMED;
184
+ case 'liquidated':
185
+ return trade_1.OrderStatus.LIQUIDATED;
186
+ case 'waiting':
187
+ return trade_1.OrderStatus.WAITING;
188
+ default:
189
+ throw new Error('Invalid order status');
190
+ }
191
+ };
192
+ exports.getOrderStatus = getOrderStatus;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "1.7.6-beta",
3
+ "version": "1.7.7-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",