@triadxyz/triad-protocol 2.7.8-beta → 2.8.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.
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Connection, PublicKey, TransactionInstruction, Commitment } from '@sola
2
2
  import { AnchorProvider, Program, Wallet } from '@coral-xyz/anchor';
3
3
  import BN from 'bn.js';
4
4
  import { TriadProtocol } from './types/triad_protocol';
5
- import { CreateMarketArgs, OpenOrderArgs, UserTrade, CreateCustomerArgs, MarketBidOrderArgs, CancelBidOrderArgs, CancelAskOrderArgs, PlaceBidOrderArgs, PlaceAskOrderArgs, CreatePoolArgs, BookOrder, MarketAskOrderArgs, RpcOptions, UpdateCustomerFeeArgs, Order, OrderDirection } from './types';
5
+ import { CreateMarketArgs, OpenOrderArgs, UserTrade, CreateCustomerArgs, MarketBidOrderArgs, CancelBidOrderArgs, CancelAskOrderArgs, PlaceBidOrderArgs, PlaceAskOrderArgs, CreatePoolArgs, BookOrder, MarketAskOrderArgs, RpcOptions, UpdateCustomerFeeArgs, OrderDirection } from './types';
6
6
  import Stake from './stake';
7
7
  import Poseidon from './poseidon';
8
8
  export * from './types';
@@ -37,14 +37,14 @@ export default class TriadProtocolClient {
37
37
  * @param wallet - User wallet PublicKey
38
38
  *
39
39
  */
40
- getUserOrders(wallet: PublicKey): Promise<Order[]>;
40
+ getUserOrders(wallet: PublicKey): Promise<import("./types").Order[]>;
41
41
  /**
42
42
  * Get User Orders By Market ID
43
43
  * @param wallet - User wallet PublicKey
44
44
  * @param marketId - The ID of the market
45
45
  *
46
46
  */
47
- getUserOrdersByMarketId(wallet: PublicKey, marketId: number): Promise<Order[]>;
47
+ getUserOrdersByMarketId(wallet: PublicKey, marketId: number): Promise<import("./types").Order[]>;
48
48
  /**
49
49
  * Get Pool By ID
50
50
  * @param poolId - The ID of the pool
@@ -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,57 @@ 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(0)))
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
+ console.log('sharesToBuy', sharesToBuy.toNumber());
893
+ if (sharesToBuy.lte(new bn_js_1.default(0)))
894
+ continue;
895
+ const usdcAmount = sharesToBuy
896
+ .mul(adjustedPrice)
897
+ .div(new bn_js_1.default(Math.pow(10, this.decimals)));
898
+ console.log('usdcAmount', usdcAmount.toNumber());
899
+ console.log('--------------------------------');
900
+ totalUSDCNeeded = totalUSDCNeeded.add(usdcAmount);
901
+ tempRemainingUSDC = tempRemainingUSDC.sub(usdcAmount);
902
+ }
903
+ if (isTrdPayout && totalUSDCNeeded.gt(new bn_js_1.default(0))) {
904
+ const { swapIxs, addressLookupTableAccounts: swapAddressLookupTableAccounts, outAmount, setupInstructions } = yield (0, swap_1.swap)({
864
905
  connection: this.program.provider.connection,
865
906
  wallet: this.program.provider.publicKey.toBase58(),
866
907
  inToken: constants_1.TRD_MINT.toString(),
867
908
  outToken: constants_1.USDC_MINT.toString(),
868
- amount
909
+ amount: totalUSDCNeeded.toNumber() / Math.pow(10, this.decimals)
869
910
  });
870
911
  if (swapIxs.length === 0) {
871
912
  return;
@@ -873,15 +914,10 @@ class TriadProtocolClient {
873
914
  remainingUSDC = new bn_js_1.default(outAmount);
874
915
  ixs.push(...setupInstructions);
875
916
  ixs.push(...swapIxs);
876
- addressLookupTableAccounts.push(...addressLookupTableAccounts);
917
+ addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
877
918
  }
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
919
  for (const order of sortedOrders) {
884
- if (remainingUSDC.lt(new bn_js_1.default(1000000)))
920
+ if (remainingUSDC.lt(new bn_js_1.default(0)))
885
921
  break;
886
922
  if (order.authority === this.program.provider.publicKey.toBase58()) {
887
923
  continue;
@@ -935,7 +971,7 @@ class TriadProtocolClient {
935
971
  if (ixs.length === 0) {
936
972
  throw new Error('No matching orders found to fill the requested amount');
937
973
  }
938
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
974
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
939
975
  });
940
976
  }
941
977
  /**
@@ -947,7 +983,7 @@ class TriadProtocolClient {
947
983
  *
948
984
  * @param options - RPC options
949
985
  */
950
- marketAskOrder({ marketId, orders, direction, isTrdPayout }, options) {
986
+ marketAskOrder({ marketId, orders, direction, isTrdPayout, feeBps }, options) {
951
987
  return __awaiter(this, void 0, void 0, function* () {
952
988
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
953
989
  const ixs = [];
@@ -958,7 +994,7 @@ class TriadProtocolClient {
958
994
  : orderBook.flop.bid;
959
995
  const sortedOrders = bidOrders.sort((a, b) => Number(b.price) - Number(a.price));
960
996
  const tokenProgram = (0, helpers_1.getTokenProgram)(constants_1.USDC_MINT);
961
- let amountOfShares = 0;
997
+ let amountOfUSDC = new bn_js_1.default(0);
962
998
  for (const inputOrder of orders) {
963
999
  let remainingShares = new bn_js_1.default(inputOrder.shares * Math.pow(10, this.decimals));
964
1000
  for (const order of sortedOrders) {
@@ -973,7 +1009,18 @@ class TriadProtocolClient {
973
1009
  if (sharesToSell.lt(new bn_js_1.default(0)))
974
1010
  continue;
975
1011
  remainingShares = remainingShares.sub(sharesToSell);
976
- amountOfShares += sharesToSell.toNumber();
1012
+ const orderPrice = new bn_js_1.default(order.price);
1013
+ let adjustedPrice = orderPrice;
1014
+ if (feeBps > 0) {
1015
+ const priceSpread = new bn_js_1.default(1000000).sub(orderPrice);
1016
+ const fee = priceSpread
1017
+ .mul(orderPrice)
1018
+ .mul(new bn_js_1.default(feeBps))
1019
+ .div(new bn_js_1.default(10000))
1020
+ .div(new bn_js_1.default(1000000));
1021
+ adjustedPrice = orderPrice.sub(fee);
1022
+ }
1023
+ amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, this.decimals))));
977
1024
  ixs.push(yield this.program.methods
978
1025
  .marketAskOrder({
979
1026
  shares: new bn_js_1.default(sharesToSell),
@@ -1001,7 +1048,7 @@ class TriadProtocolClient {
1001
1048
  wallet: this.program.provider.publicKey.toBase58(),
1002
1049
  inToken: constants_1.USDC_MINT.toBase58(),
1003
1050
  outToken: constants_1.TRD_MINT.toString(),
1004
- amount: amountOfShares
1051
+ amount: amountOfUSDC.toNumber()
1005
1052
  });
1006
1053
  if (swapIxs.length > 0) {
1007
1054
  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;
@@ -24,7 +24,7 @@ const swap = ({ connection, wallet, inToken, outToken, amount }) => __awaiter(vo
24
24
  throw new Error('Token not found');
25
25
  }
26
26
  const formattedAmountIn = amount * Math.pow(10, token.decimals);
27
- const quoteResponse = yield axios_1.default.get(`https://lite-api.jup.ag/swap/v1/quote?inputMint=${inToken}&outputMint=${outToken}&amount=${formattedAmountIn}&slippageBps=50&onlyDirectRoutes=true&platformFeeBps=60`);
27
+ const quoteResponse = yield axios_1.default.get(`https://lite-api.jup.ag/swap/v1/quote?inputMint=${inToken}&outputMint=${outToken}&amount=${formattedAmountIn}&slippageBps=20&onlyDirectRoutes=true&platformFeeBps=60`);
28
28
  const { data: quoteData } = quoteResponse;
29
29
  const swapResponse = yield axios_1.default.post('https://lite-api.jup.ag/swap/v1/swap-instructions', {
30
30
  quoteResponse: quoteData,
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.8.0-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",