@triadxyz/triad-protocol 1.8.0-beta → 1.8.1-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.
Files changed (2) hide show
  1. package/dist/trade.js +43 -42
  2. package/package.json +1 -1
package/dist/trade.js CHANGED
@@ -12,6 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
+ const web3_js_1 = require("@solana/web3.js");
15
16
  const trade_1 = require("./types/trade");
16
17
  const bn_js_1 = __importDefault(require("bn.js"));
17
18
  const constants_1 = require("./utils/constants");
@@ -153,35 +154,12 @@ class Trade {
153
154
  openOrder({ marketId, amount, direction, mint }, options) {
154
155
  return __awaiter(this, void 0, void 0, function* () {
155
156
  const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
156
- let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
157
157
  const ixs = [];
158
158
  const addressLookupTableAccounts = [];
159
159
  let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
160
- let myUserTrades = [];
161
- myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
162
- if (myUserTrades.length === 0) {
163
- ixs.push(yield this.program.methods
164
- .createUserTrade()
165
- .accounts({
166
- signer: this.provider.publicKey
167
- })
168
- .instruction());
169
- }
170
- if (myUserTrades.length > 0) {
171
- try {
172
- userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
173
- }
174
- catch (_a) {
175
- const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
176
- const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
177
- userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
178
- ixs.push(yield this.program.methods
179
- .createSubUserTrade(subUserTradePDA)
180
- .accounts({
181
- signer: this.provider.publicKey
182
- })
183
- .instruction());
184
- }
160
+ const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
161
+ if (userTradeIxs.length > 0) {
162
+ ixs.push(...userTradeIxs);
185
163
  }
186
164
  ixs.push(yield this.program.methods
187
165
  .openOrder({
@@ -542,22 +520,45 @@ class Trade {
542
520
  const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
543
521
  const ixs = [];
544
522
  const { userTradePDA: buyerTrade, ixs: userTradeIxs } = yield this.getUserTradeIxs();
545
- // TODO: Get dynamic until fill all order
546
- const sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
547
- ixs.push(...userTradeIxs);
548
- ixs.push(yield this.program.methods
549
- .marketBidOrder({
550
- amount: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
551
- orderDirection: direction
552
- })
553
- .accounts({
554
- signer: this.provider.publicKey,
555
- market: marketPDA,
556
- buyerTrade,
557
- sellerTrade: sellerTradePDA,
558
- mint
559
- })
560
- .instruction());
523
+ const orderBook = yield this.getOrderBook(marketId);
524
+ let remainingUSDC = amount * Math.pow(10, this.decimals);
525
+ const orders = direction[0].hype ? orderBook.hype.ask : orderBook.flop.ask;
526
+ const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
527
+ for (const order of sortedOrders) {
528
+ if (remainingUSDC <= 0)
529
+ break;
530
+ const orderPrice = Number(order.price);
531
+ const maxSharesForPrice = remainingUSDC / orderPrice;
532
+ const availableShares = Number(order.totalShares);
533
+ const sharesToBuy = Math.min(maxSharesForPrice, availableShares);
534
+ const usdcAmount = sharesToBuy * orderPrice;
535
+ let sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority));
536
+ if (order.userNonce !== '0') {
537
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), Number(order.userNonce));
538
+ sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
539
+ }
540
+ ixs.push(yield this.program.methods
541
+ .marketBidOrder({
542
+ amount: new bn_js_1.default(usdcAmount),
543
+ orderDirection: direction
544
+ })
545
+ .accounts({
546
+ signer: this.provider.publicKey,
547
+ market: marketPDA,
548
+ buyerTrade,
549
+ sellerTrade: sellerTradePDA,
550
+ mint,
551
+ tokenProgram: (0, helpers_1.getTokenProgram)(mint)
552
+ })
553
+ .instruction());
554
+ remainingUSDC -= usdcAmount;
555
+ }
556
+ if (userTradeIxs.length > 0) {
557
+ ixs.unshift(...userTradeIxs);
558
+ }
559
+ if (ixs.length === 0) {
560
+ throw new Error('No matching orders found to fill the requested amount');
561
+ }
561
562
  return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
562
563
  });
563
564
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "1.8.0-beta",
3
+ "version": "1.8.1-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",