@triadxyz/triad-protocol 2.0.9-beta → 2.1.0-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 +15 -9
  2. package/package.json +1 -1
package/dist/trade.js CHANGED
@@ -557,16 +557,16 @@ class Trade {
557
557
  const ixs = [];
558
558
  const { userTradePDA: buyerTrade, ixs: userTradeIxs } = yield this.getUserTradeIxs();
559
559
  const orderBook = yield this.getOrderBook(marketId);
560
- let remainingUSDC = amount * Math.pow(10, this.decimals);
560
+ let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, this.decimals));
561
561
  const orders = Object.keys(direction)[0] === 'hype'
562
562
  ? orderBook.hype.ask
563
563
  : orderBook.flop.ask;
564
564
  const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
565
565
  for (const order of sortedOrders) {
566
- if (remainingUSDC <= 0)
566
+ if (remainingUSDC.div(new bn_js_1.default(Math.pow(10, this.decimals))).toString() === '0')
567
567
  break;
568
568
  if (order.authority === this.program.provider.publicKey.toBase58()) {
569
- continue;
569
+ return;
570
570
  }
571
571
  let linkedSellerBidTradePDA = null;
572
572
  if (order.linkedBidOrderId !== '0') {
@@ -582,11 +582,17 @@ class Trade {
582
582
  linkedSellerBidTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
583
583
  }
584
584
  }
585
- const orderPrice = Number(order.price);
586
- const maxSharesForPrice = remainingUSDC / orderPrice;
587
- const availableShares = Number(order.totalShares);
588
- const sharesToBuy = Math.min(maxSharesForPrice, availableShares);
589
- const usdcAmount = sharesToBuy * orderPrice;
585
+ const orderPrice = new bn_js_1.default(order.price);
586
+ const availableShares = new bn_js_1.default(order.totalShares);
587
+ const maxSharesForPrice = remainingUSDC
588
+ .mul(new bn_js_1.default(Math.pow(10, this.decimals)))
589
+ .div(orderPrice);
590
+ const sharesToBuy = bn_js_1.default.min(maxSharesForPrice, availableShares);
591
+ if (sharesToBuy.lten(0))
592
+ continue;
593
+ const usdcAmount = sharesToBuy
594
+ .mul(orderPrice)
595
+ .div(new bn_js_1.default(Math.pow(10, this.decimals)));
590
596
  let sellerTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority));
591
597
  if (order.userNonce !== '0') {
592
598
  const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), Number(order.userNonce));
@@ -612,7 +618,7 @@ class Trade {
612
618
  linkedSellerBidTrade: linkedSellerBidTradePDA
613
619
  })
614
620
  .instruction());
615
- remainingUSDC -= usdcAmount;
621
+ remainingUSDC = remainingUSDC.sub(usdcAmount);
616
622
  }
617
623
  if (userTradeIxs.length > 0) {
618
624
  ixs.unshift(...userTradeIxs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "2.0.9-beta",
3
+ "version": "2.1.0-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",