@triadxyz/triad-protocol 2.7.8-beta → 2.7.9-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/index.d.ts CHANGED
@@ -440,7 +440,7 @@ export default class TriadProtocolClient {
440
440
  *
441
441
  * @param options - RPC options
442
442
  */
443
- marketAskOrder({ marketId, orders, direction, isTrdPayout }: MarketAskOrderArgs, options?: RpcOptions): Promise<string>;
443
+ marketAskOrder({ marketId, orders, direction, isTrdPayout, feeBps }: MarketAskOrderArgs, options?: RpcOptions): Promise<string>;
444
444
  /**
445
445
  * Get Orders By Market ID
446
446
  * @param marketId - The ID of the market
package/dist/index.js CHANGED
@@ -856,16 +856,54 @@ class TriadProtocolClient {
856
856
  return __awaiter(this, void 0, void 0, function* () {
857
857
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
858
858
  const ixs = [];
859
+ const addressLookupTableAccounts = [];
859
860
  const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(direction)[0]);
860
861
  const orderBook = yield this.getOrderBook(marketId);
861
862
  let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, this.decimals));
862
- if (isTrdPayout) {
863
- const { swapIxs, addressLookupTableAccounts, outAmount, setupInstructions } = yield (0, swap_1.swap)({
863
+ const orders = Object.keys(direction)[0] === 'hype'
864
+ ? orderBook.hype.ask
865
+ : orderBook.flop.ask;
866
+ const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
867
+ const tokenProgram = (0, helpers_1.getTokenProgram)(mint);
868
+ let totalUSDCNeeded = new bn_js_1.default(0);
869
+ let tempRemainingUSDC = remainingUSDC.clone();
870
+ for (const order of sortedOrders) {
871
+ if (tempRemainingUSDC.lt(new bn_js_1.default(1000000)))
872
+ break;
873
+ if (order.authority === this.program.provider.publicKey.toBase58()) {
874
+ continue;
875
+ }
876
+ const orderPrice = new bn_js_1.default(order.price);
877
+ const availableShares = new bn_js_1.default(order.totalShares).sub(new bn_js_1.default(order.filledShares));
878
+ let adjustedPrice = orderPrice;
879
+ if (feeBps > 0) {
880
+ const priceSpread = new bn_js_1.default(1000000).sub(orderPrice);
881
+ const fee = priceSpread
882
+ .mul(orderPrice)
883
+ .mul(new bn_js_1.default(feeBps))
884
+ .div(new bn_js_1.default(10000))
885
+ .div(new bn_js_1.default(1000000));
886
+ adjustedPrice = orderPrice.add(fee);
887
+ }
888
+ const maxSharesForPrice = tempRemainingUSDC
889
+ .mul(new bn_js_1.default(Math.pow(10, this.decimals)))
890
+ .div(adjustedPrice);
891
+ const sharesToBuy = bn_js_1.default.min(maxSharesForPrice, availableShares);
892
+ if (sharesToBuy.lte(new bn_js_1.default(0)))
893
+ continue;
894
+ const usdcAmount = sharesToBuy
895
+ .mul(adjustedPrice)
896
+ .div(new bn_js_1.default(Math.pow(10, this.decimals)));
897
+ totalUSDCNeeded = totalUSDCNeeded.add(usdcAmount);
898
+ tempRemainingUSDC = tempRemainingUSDC.sub(usdcAmount);
899
+ }
900
+ if (isTrdPayout && totalUSDCNeeded.gt(new bn_js_1.default(0))) {
901
+ const { swapIxs, addressLookupTableAccounts: swapAddressLookupTableAccounts, outAmount, setupInstructions } = yield (0, swap_1.swap)({
864
902
  connection: this.program.provider.connection,
865
903
  wallet: this.program.provider.publicKey.toBase58(),
866
904
  inToken: constants_1.TRD_MINT.toString(),
867
905
  outToken: constants_1.USDC_MINT.toString(),
868
- amount
906
+ amount: totalUSDCNeeded.toNumber() / Math.pow(10, this.decimals)
869
907
  });
870
908
  if (swapIxs.length === 0) {
871
909
  return;
@@ -873,13 +911,8 @@ class TriadProtocolClient {
873
911
  remainingUSDC = new bn_js_1.default(outAmount);
874
912
  ixs.push(...setupInstructions);
875
913
  ixs.push(...swapIxs);
876
- addressLookupTableAccounts.push(...addressLookupTableAccounts);
914
+ addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
877
915
  }
878
- const orders = Object.keys(direction)[0] === 'hype'
879
- ? orderBook.hype.ask
880
- : orderBook.flop.ask;
881
- const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
882
- const tokenProgram = (0, helpers_1.getTokenProgram)(mint);
883
916
  for (const order of sortedOrders) {
884
917
  if (remainingUSDC.lt(new bn_js_1.default(1000000)))
885
918
  break;
@@ -935,7 +968,7 @@ class TriadProtocolClient {
935
968
  if (ixs.length === 0) {
936
969
  throw new Error('No matching orders found to fill the requested amount');
937
970
  }
938
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
971
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
939
972
  });
940
973
  }
941
974
  /**
@@ -947,7 +980,7 @@ class TriadProtocolClient {
947
980
  *
948
981
  * @param options - RPC options
949
982
  */
950
- marketAskOrder({ marketId, orders, direction, isTrdPayout }, options) {
983
+ marketAskOrder({ marketId, orders, direction, isTrdPayout, feeBps }, options) {
951
984
  return __awaiter(this, void 0, void 0, function* () {
952
985
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
953
986
  const ixs = [];
@@ -958,7 +991,7 @@ class TriadProtocolClient {
958
991
  : orderBook.flop.bid;
959
992
  const sortedOrders = bidOrders.sort((a, b) => Number(b.price) - Number(a.price));
960
993
  const tokenProgram = (0, helpers_1.getTokenProgram)(constants_1.USDC_MINT);
961
- let amountOfShares = 0;
994
+ let amountOfUSDC = new bn_js_1.default(0);
962
995
  for (const inputOrder of orders) {
963
996
  let remainingShares = new bn_js_1.default(inputOrder.shares * Math.pow(10, this.decimals));
964
997
  for (const order of sortedOrders) {
@@ -973,7 +1006,18 @@ class TriadProtocolClient {
973
1006
  if (sharesToSell.lt(new bn_js_1.default(0)))
974
1007
  continue;
975
1008
  remainingShares = remainingShares.sub(sharesToSell);
976
- amountOfShares += sharesToSell.toNumber();
1009
+ const orderPrice = new bn_js_1.default(order.price);
1010
+ let adjustedPrice = orderPrice;
1011
+ if (feeBps > 0) {
1012
+ const priceSpread = new bn_js_1.default(1000000).sub(orderPrice);
1013
+ const fee = priceSpread
1014
+ .mul(orderPrice)
1015
+ .mul(new bn_js_1.default(feeBps))
1016
+ .div(new bn_js_1.default(10000))
1017
+ .div(new bn_js_1.default(1000000));
1018
+ adjustedPrice = orderPrice.sub(fee);
1019
+ }
1020
+ amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, this.decimals))));
977
1021
  ixs.push(yield this.program.methods
978
1022
  .marketAskOrder({
979
1023
  shares: new bn_js_1.default(sharesToSell),
@@ -1001,7 +1045,7 @@ class TriadProtocolClient {
1001
1045
  wallet: this.program.provider.publicKey.toBase58(),
1002
1046
  inToken: constants_1.USDC_MINT.toBase58(),
1003
1047
  outToken: constants_1.TRD_MINT.toString(),
1004
- amount: amountOfShares
1048
+ amount: amountOfUSDC.toNumber()
1005
1049
  });
1006
1050
  if (swapIxs.length > 0) {
1007
1051
  ixs.push(...setupInstructions);
@@ -230,6 +230,7 @@ export type MarketAskOrderArgs = {
230
230
  flop: {};
231
231
  };
232
232
  isTrdPayout: boolean;
233
+ feeBps: number;
233
234
  };
234
235
  export type Refer = {
235
236
  id: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "2.7.8-beta",
3
+ "version": "2.7.9-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",