@triadxyz/triad-protocol 2.7.7-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 +25 -8
- package/dist/index.js +164 -50
- package/dist/types/index.d.ts +6 -3
- package/dist/utils/swap.d.ts +4 -5
- package/dist/utils/swap.js +22 -18
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Connection, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
1
|
+
import { Connection, PublicKey, TransactionInstruction, Commitment } from '@solana/web3.js';
|
|
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 } from './types';
|
|
5
|
+
import { CreateMarketArgs, OpenOrderArgs, UserTrade, CreateCustomerArgs, MarketBidOrderArgs, CancelBidOrderArgs, CancelAskOrderArgs, PlaceBidOrderArgs, PlaceAskOrderArgs, CreatePoolArgs, BookOrder, MarketAskOrderArgs, RpcOptions, UpdateCustomerFeeArgs, Order, OrderDirection } from './types';
|
|
6
6
|
import Stake from './stake';
|
|
7
7
|
import Poseidon from './poseidon';
|
|
8
8
|
export * from './types';
|
|
@@ -15,7 +15,7 @@ export default class TriadProtocolClient {
|
|
|
15
15
|
stake: Stake;
|
|
16
16
|
poseidon: Poseidon;
|
|
17
17
|
decimals: number;
|
|
18
|
-
constructor(connection: Connection, wallet: Wallet);
|
|
18
|
+
constructor(connection: Connection, wallet: Wallet, commitment?: Commitment);
|
|
19
19
|
/**
|
|
20
20
|
* Get All Pools
|
|
21
21
|
*
|
|
@@ -37,7 +37,14 @@ export default class TriadProtocolClient {
|
|
|
37
37
|
* @param wallet - User wallet PublicKey
|
|
38
38
|
*
|
|
39
39
|
*/
|
|
40
|
-
getUserOrders(wallet: PublicKey): Promise<
|
|
40
|
+
getUserOrders(wallet: PublicKey): Promise<Order[]>;
|
|
41
|
+
/**
|
|
42
|
+
* Get User Orders By Market ID
|
|
43
|
+
* @param wallet - User wallet PublicKey
|
|
44
|
+
* @param marketId - The ID of the market
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
getUserOrdersByMarketId(wallet: PublicKey, marketId: number): Promise<Order[]>;
|
|
41
48
|
/**
|
|
42
49
|
* Get Pool By ID
|
|
43
50
|
* @param poolId - The ID of the pool
|
|
@@ -357,6 +364,16 @@ export default class TriadProtocolClient {
|
|
|
357
364
|
ixs: TransactionInstruction[];
|
|
358
365
|
nonce?: undefined;
|
|
359
366
|
}>;
|
|
367
|
+
/**
|
|
368
|
+
* Get User Trade Nonce
|
|
369
|
+
* @param marketId - The ID of the Market
|
|
370
|
+
* @param direction - The direction of the Order
|
|
371
|
+
*
|
|
372
|
+
*/
|
|
373
|
+
getUserTradeNonce(marketId: number, direction: OrderDirection): Promise<{
|
|
374
|
+
userTradePDA: PublicKey;
|
|
375
|
+
userTradeIxs: TransactionInstruction[];
|
|
376
|
+
}>;
|
|
360
377
|
/**
|
|
361
378
|
* Place Bid Order
|
|
362
379
|
* @param args.marketId - The ID of the Market
|
|
@@ -408,6 +425,8 @@ export default class TriadProtocolClient {
|
|
|
408
425
|
* @param args.amount - The amount of the Order
|
|
409
426
|
* @param args.direction - The direction of the Order
|
|
410
427
|
* @param args.mint - The mint of the Order
|
|
428
|
+
* @param args.feeBps - The fee in basis points
|
|
429
|
+
* @param args.isTrdPayout - Whether to payout in TRD or not
|
|
411
430
|
*
|
|
412
431
|
* @param options - RPC options
|
|
413
432
|
*/
|
|
@@ -415,15 +434,13 @@ export default class TriadProtocolClient {
|
|
|
415
434
|
/**
|
|
416
435
|
* Market Ask Order
|
|
417
436
|
* @param args.marketId - The ID of the Market
|
|
418
|
-
* @param args.
|
|
419
|
-
* @param args.bidOrderId - The ID of the Bid Order
|
|
437
|
+
* @param args.orders - Array of orders to execute
|
|
420
438
|
* @param args.direction - The direction of the Order
|
|
421
|
-
* @param args.mint - The mint of the Order
|
|
422
439
|
* @param args.isTrdPayout - Whether to payout in TRD or not
|
|
423
440
|
*
|
|
424
441
|
* @param options - RPC options
|
|
425
442
|
*/
|
|
426
|
-
marketAskOrder({ marketId,
|
|
443
|
+
marketAskOrder({ marketId, orders, direction, isTrdPayout, feeBps }: MarketAskOrderArgs, options?: RpcOptions): Promise<string>;
|
|
427
444
|
/**
|
|
428
445
|
* Get Orders By Market ID
|
|
429
446
|
* @param marketId - The ID of the market
|
package/dist/index.js
CHANGED
|
@@ -43,10 +43,10 @@ __exportStar(require("./types/stake"), exports);
|
|
|
43
43
|
__exportStar(require("./types/poseidon"), exports);
|
|
44
44
|
__exportStar(require("./utils/helpers"), exports);
|
|
45
45
|
class TriadProtocolClient {
|
|
46
|
-
constructor(connection, wallet) {
|
|
46
|
+
constructor(connection, wallet, commitment = 'confirmed') {
|
|
47
47
|
this.decimals = constants_1.TRD_DECIMALS; // We're using only TRD or USDC so we can use the same decimals 6
|
|
48
48
|
this.provider = new anchor_1.AnchorProvider(connection, wallet, {
|
|
49
|
-
commitment
|
|
49
|
+
commitment
|
|
50
50
|
});
|
|
51
51
|
this.program = new anchor_1.Program(idl_triad_protocol_json_1.default, this.provider);
|
|
52
52
|
this.stake = new stake_1.default(this.program);
|
|
@@ -101,6 +101,18 @@ class TriadProtocolClient {
|
|
|
101
101
|
return myUserTrades.flatMap((userTrade) => userTrade.orders);
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Get User Orders By Market ID
|
|
106
|
+
* @param wallet - User wallet PublicKey
|
|
107
|
+
* @param marketId - The ID of the market
|
|
108
|
+
*
|
|
109
|
+
*/
|
|
110
|
+
getUserOrdersByMarketId(wallet, marketId) {
|
|
111
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
112
|
+
const userTrades = yield this.getMyUserTrades(wallet);
|
|
113
|
+
return userTrades.flatMap((userTrade) => userTrade.orders.filter((order) => order.marketId === marketId.toString()));
|
|
114
|
+
});
|
|
115
|
+
}
|
|
104
116
|
/**
|
|
105
117
|
* Get Pool By ID
|
|
106
118
|
* @param poolId - The ID of the pool
|
|
@@ -351,13 +363,14 @@ class TriadProtocolClient {
|
|
|
351
363
|
}
|
|
352
364
|
let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
|
|
353
365
|
if (token !== constants_1.TRD_MINT.toBase58()) {
|
|
354
|
-
const { setupInstructions, swapIxs, addressLookupTableAccounts,
|
|
366
|
+
const { setupInstructions, swapIxs, addressLookupTableAccounts, outAmount } = yield (0, swap_1.swap)({
|
|
355
367
|
connection: this.program.provider.connection,
|
|
356
368
|
wallet: this.program.provider.publicKey.toBase58(),
|
|
357
369
|
inToken: token,
|
|
370
|
+
outToken: constants_1.TRD_MINT.toString(),
|
|
358
371
|
amount
|
|
359
372
|
});
|
|
360
|
-
amountInTRD =
|
|
373
|
+
amountInTRD = outAmount;
|
|
361
374
|
if (swapIxs.length === 0) {
|
|
362
375
|
return;
|
|
363
376
|
}
|
|
@@ -490,7 +503,8 @@ class TriadProtocolClient {
|
|
|
490
503
|
connection: this.program.provider.connection,
|
|
491
504
|
wallet: this.program.provider.publicKey.toBase58(),
|
|
492
505
|
inToken: constants_1.USDC_MINT.toBase58(),
|
|
493
|
-
|
|
506
|
+
outToken: constants_1.TRD_MINT.toString(),
|
|
507
|
+
amount: order.shares / Math.pow(10, this.decimals)
|
|
494
508
|
});
|
|
495
509
|
if (swapIxs.length === 0) {
|
|
496
510
|
return;
|
|
@@ -677,6 +691,31 @@ class TriadProtocolClient {
|
|
|
677
691
|
}
|
|
678
692
|
});
|
|
679
693
|
}
|
|
694
|
+
/**
|
|
695
|
+
* Get User Trade Nonce
|
|
696
|
+
* @param marketId - The ID of the Market
|
|
697
|
+
* @param direction - The direction of the Order
|
|
698
|
+
*
|
|
699
|
+
*/
|
|
700
|
+
getUserTradeNonce(marketId, direction) {
|
|
701
|
+
var _a;
|
|
702
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
703
|
+
const userOrders = yield this.getUserOrdersByMarketId(this.program.provider.publicKey, marketId);
|
|
704
|
+
const userNonce = (_a = userOrders.find((order) => order.orderDirection === Object.keys(direction)[0] &&
|
|
705
|
+
order.orderStatus === types_1.OrderStatus.OPEN)) === null || _a === void 0 ? void 0 : _a.userNonce;
|
|
706
|
+
let userTradePDA = null;
|
|
707
|
+
let userTradeIxs = [];
|
|
708
|
+
if (userNonce) {
|
|
709
|
+
userTradePDA = this.getUserPDA(this.program.provider.publicKey, Number(userNonce));
|
|
710
|
+
}
|
|
711
|
+
if (!userNonce) {
|
|
712
|
+
const { userTradePDA: user, ixs: ixsUser } = yield this.getUserTradeIxs();
|
|
713
|
+
userTradePDA = user;
|
|
714
|
+
userTradeIxs = ixsUser;
|
|
715
|
+
}
|
|
716
|
+
return { userTradePDA, userTradeIxs };
|
|
717
|
+
});
|
|
718
|
+
}
|
|
680
719
|
/**
|
|
681
720
|
* Place Bid Order
|
|
682
721
|
* @param args.marketId - The ID of the Market
|
|
@@ -690,7 +729,7 @@ class TriadProtocolClient {
|
|
|
690
729
|
placeBidOrder({ marketId, amount, direction, mint, price }, options) {
|
|
691
730
|
return __awaiter(this, void 0, void 0, function* () {
|
|
692
731
|
const ixs = [];
|
|
693
|
-
const { userTradePDA,
|
|
732
|
+
const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(direction)[0]);
|
|
694
733
|
if (userTradeIxs.length > 0) {
|
|
695
734
|
ixs.push(...userTradeIxs);
|
|
696
735
|
}
|
|
@@ -808,6 +847,8 @@ class TriadProtocolClient {
|
|
|
808
847
|
* @param args.amount - The amount of the Order
|
|
809
848
|
* @param args.direction - The direction of the Order
|
|
810
849
|
* @param args.mint - The mint of the Order
|
|
850
|
+
* @param args.feeBps - The fee in basis points
|
|
851
|
+
* @param args.isTrdPayout - Whether to payout in TRD or not
|
|
811
852
|
*
|
|
812
853
|
* @param options - RPC options
|
|
813
854
|
*/
|
|
@@ -815,7 +856,8 @@ class TriadProtocolClient {
|
|
|
815
856
|
return __awaiter(this, void 0, void 0, function* () {
|
|
816
857
|
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
817
858
|
const ixs = [];
|
|
818
|
-
const
|
|
859
|
+
const addressLookupTableAccounts = [];
|
|
860
|
+
const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(direction)[0]);
|
|
819
861
|
const orderBook = yield this.getOrderBook(marketId);
|
|
820
862
|
let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, this.decimals));
|
|
821
863
|
const orders = Object.keys(direction)[0] === 'hype'
|
|
@@ -823,6 +865,54 @@ class TriadProtocolClient {
|
|
|
823
865
|
: orderBook.flop.ask;
|
|
824
866
|
const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
|
|
825
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)({
|
|
902
|
+
connection: this.program.provider.connection,
|
|
903
|
+
wallet: this.program.provider.publicKey.toBase58(),
|
|
904
|
+
inToken: constants_1.TRD_MINT.toString(),
|
|
905
|
+
outToken: constants_1.USDC_MINT.toString(),
|
|
906
|
+
amount: totalUSDCNeeded.toNumber() / Math.pow(10, this.decimals)
|
|
907
|
+
});
|
|
908
|
+
if (swapIxs.length === 0) {
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
remainingUSDC = new bn_js_1.default(outAmount);
|
|
912
|
+
ixs.push(...setupInstructions);
|
|
913
|
+
ixs.push(...swapIxs);
|
|
914
|
+
addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
|
|
915
|
+
}
|
|
826
916
|
for (const order of sortedOrders) {
|
|
827
917
|
if (remainingUSDC.lt(new bn_js_1.default(1000000)))
|
|
828
918
|
break;
|
|
@@ -878,71 +968,95 @@ class TriadProtocolClient {
|
|
|
878
968
|
if (ixs.length === 0) {
|
|
879
969
|
throw new Error('No matching orders found to fill the requested amount');
|
|
880
970
|
}
|
|
881
|
-
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
|
|
971
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
|
|
882
972
|
});
|
|
883
973
|
}
|
|
884
974
|
/**
|
|
885
975
|
* Market Ask Order
|
|
886
976
|
* @param args.marketId - The ID of the Market
|
|
887
|
-
* @param args.
|
|
888
|
-
* @param args.bidOrderId - The ID of the Bid Order
|
|
977
|
+
* @param args.orders - Array of orders to execute
|
|
889
978
|
* @param args.direction - The direction of the Order
|
|
890
|
-
* @param args.mint - The mint of the Order
|
|
891
979
|
* @param args.isTrdPayout - Whether to payout in TRD or not
|
|
892
980
|
*
|
|
893
981
|
* @param options - RPC options
|
|
894
982
|
*/
|
|
895
|
-
marketAskOrder({ marketId,
|
|
983
|
+
marketAskOrder({ marketId, orders, direction, isTrdPayout, feeBps }, options) {
|
|
896
984
|
return __awaiter(this, void 0, void 0, function* () {
|
|
897
985
|
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
898
986
|
const ixs = [];
|
|
899
|
-
const
|
|
987
|
+
const addressLookupTableAccounts = [];
|
|
900
988
|
const orderBook = yield this.getOrderBook(marketId);
|
|
901
|
-
|
|
902
|
-
const orders = Object.keys(direction)[0] === 'hype'
|
|
989
|
+
const bidOrders = Object.keys(direction)[0] === 'hype'
|
|
903
990
|
? orderBook.hype.bid
|
|
904
991
|
: orderBook.flop.bid;
|
|
905
|
-
const sortedOrders =
|
|
906
|
-
const tokenProgram = (0, helpers_1.getTokenProgram)(
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
992
|
+
const sortedOrders = bidOrders.sort((a, b) => Number(b.price) - Number(a.price));
|
|
993
|
+
const tokenProgram = (0, helpers_1.getTokenProgram)(constants_1.USDC_MINT);
|
|
994
|
+
let amountOfUSDC = new bn_js_1.default(0);
|
|
995
|
+
for (const inputOrder of orders) {
|
|
996
|
+
let remainingShares = new bn_js_1.default(inputOrder.shares * Math.pow(10, this.decimals));
|
|
997
|
+
for (const order of sortedOrders) {
|
|
998
|
+
if (remainingShares.lte(new bn_js_1.default(0)))
|
|
999
|
+
break;
|
|
1000
|
+
if (order.authority === this.program.provider.publicKey.toBase58() ||
|
|
1001
|
+
order.linkedBookOrderId !== constants_1.BOOK_ORDER_NULL.toString()) {
|
|
1002
|
+
continue;
|
|
1003
|
+
}
|
|
1004
|
+
const availableShares = new bn_js_1.default(order.totalShares).sub(new bn_js_1.default(order.filledShares));
|
|
1005
|
+
const sharesToSell = bn_js_1.default.min(remainingShares, availableShares);
|
|
1006
|
+
if (sharesToSell.lt(new bn_js_1.default(0)))
|
|
1007
|
+
continue;
|
|
1008
|
+
remainingShares = remainingShares.sub(sharesToSell);
|
|
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))));
|
|
1021
|
+
ixs.push(yield this.program.methods
|
|
1022
|
+
.marketAskOrder({
|
|
1023
|
+
shares: new bn_js_1.default(sharesToSell),
|
|
1024
|
+
orderDirection: direction,
|
|
1025
|
+
bidOrderId: new bn_js_1.default(inputOrder.bidOrderId),
|
|
1026
|
+
bookOrderBidId: new bn_js_1.default(order.id)
|
|
1027
|
+
})
|
|
1028
|
+
.accounts({
|
|
1029
|
+
signer: this.program.provider.publicKey,
|
|
1030
|
+
market: marketPDA,
|
|
1031
|
+
buyerAuthority: new web3_js_1.PublicKey(order.authority),
|
|
1032
|
+
buyerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
|
|
1033
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
|
|
1034
|
+
mint: constants_1.USDC_MINT,
|
|
1035
|
+
tokenProgram,
|
|
1036
|
+
marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, tokenProgram),
|
|
1037
|
+
sellerTrade: this.getUserPDA(this.program.provider.publicKey, inputOrder.userNonce)
|
|
1038
|
+
})
|
|
1039
|
+
.instruction());
|
|
913
1040
|
}
|
|
914
|
-
const availableShares = new bn_js_1.default(order.totalShares).sub(new bn_js_1.default(order.filledShares));
|
|
915
|
-
const sharesToSell = bn_js_1.default.min(remainingShares, availableShares);
|
|
916
|
-
if (sharesToSell.lt(new bn_js_1.default(0)))
|
|
917
|
-
continue;
|
|
918
|
-
remainingShares = remainingShares.sub(sharesToSell);
|
|
919
|
-
ixs.push(yield this.program.methods
|
|
920
|
-
.marketAskOrder({
|
|
921
|
-
shares: new bn_js_1.default(sharesToSell),
|
|
922
|
-
orderDirection: direction,
|
|
923
|
-
bidOrderId: new bn_js_1.default(bidOrderId),
|
|
924
|
-
bookOrderBidId: new bn_js_1.default(order.id)
|
|
925
|
-
})
|
|
926
|
-
.accounts({
|
|
927
|
-
signer: this.program.provider.publicKey,
|
|
928
|
-
market: marketPDA,
|
|
929
|
-
buyerAuthority: new web3_js_1.PublicKey(order.authority),
|
|
930
|
-
buyerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
|
|
931
|
-
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
|
|
932
|
-
mint,
|
|
933
|
-
tokenProgram,
|
|
934
|
-
marketAta: (0, pda_1.getTokenATA)(marketPDA, mint, tokenProgram),
|
|
935
|
-
sellerTrade: userTradePDA
|
|
936
|
-
})
|
|
937
|
-
.instruction());
|
|
938
1041
|
}
|
|
939
|
-
if (
|
|
940
|
-
|
|
1042
|
+
if (isTrdPayout) {
|
|
1043
|
+
const { setupInstructions, swapIxs, addressLookupTableAccounts: swapAddressLookupTableAccounts } = yield (0, swap_1.swap)({
|
|
1044
|
+
connection: this.program.provider.connection,
|
|
1045
|
+
wallet: this.program.provider.publicKey.toBase58(),
|
|
1046
|
+
inToken: constants_1.USDC_MINT.toBase58(),
|
|
1047
|
+
outToken: constants_1.TRD_MINT.toString(),
|
|
1048
|
+
amount: amountOfUSDC.toNumber()
|
|
1049
|
+
});
|
|
1050
|
+
if (swapIxs.length > 0) {
|
|
1051
|
+
ixs.push(...setupInstructions);
|
|
1052
|
+
ixs.push(...swapIxs);
|
|
1053
|
+
addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
|
|
1054
|
+
}
|
|
941
1055
|
}
|
|
942
1056
|
if (ixs.length === 0) {
|
|
943
1057
|
throw new Error('No matching orders found to fill the requested amount');
|
|
944
1058
|
}
|
|
945
|
-
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
|
|
1059
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
|
|
946
1060
|
});
|
|
947
1061
|
}
|
|
948
1062
|
/**
|
package/dist/types/index.d.ts
CHANGED
|
@@ -219,15 +219,18 @@ export type MarketBidOrderArgs = {
|
|
|
219
219
|
};
|
|
220
220
|
export type MarketAskOrderArgs = {
|
|
221
221
|
marketId: number;
|
|
222
|
-
|
|
223
|
-
|
|
222
|
+
orders: {
|
|
223
|
+
shares: number;
|
|
224
|
+
bidOrderId: number;
|
|
225
|
+
userNonce: number;
|
|
226
|
+
}[];
|
|
224
227
|
direction: {
|
|
225
228
|
hype: {};
|
|
226
229
|
} | {
|
|
227
230
|
flop: {};
|
|
228
231
|
};
|
|
229
|
-
mint: PublicKey;
|
|
230
232
|
isTrdPayout: boolean;
|
|
233
|
+
feeBps: number;
|
|
231
234
|
};
|
|
232
235
|
export type Refer = {
|
|
233
236
|
id: number;
|
package/dist/utils/swap.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { AddressLookupTableAccount, Connection, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
-
export declare const swap: ({ connection, wallet, inToken, amount }: {
|
|
2
|
+
export declare const swap: ({ connection, wallet, inToken, outToken, amount }: {
|
|
3
3
|
connection: Connection;
|
|
4
4
|
wallet: string;
|
|
5
5
|
inToken: string;
|
|
6
|
+
outToken: string;
|
|
6
7
|
amount: number;
|
|
7
8
|
}) => Promise<{
|
|
8
9
|
swapIxs: TransactionInstruction[];
|
|
9
10
|
addressLookupTableAccounts: AddressLookupTableAccount[];
|
|
10
|
-
setupInstructions:
|
|
11
|
-
|
|
12
|
-
trdAmount: any;
|
|
11
|
+
setupInstructions: TransactionInstruction[];
|
|
12
|
+
outAmount: any;
|
|
13
13
|
}>;
|
|
14
|
-
export declare const getAddressLookupTableAccounts: (connection: Connection, keys: string[]) => Promise<AddressLookupTableAccount[]>;
|
package/dist/utils/swap.js
CHANGED
|
@@ -12,35 +12,33 @@ 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
|
-
exports.
|
|
15
|
+
exports.swap = void 0;
|
|
16
|
+
const index_1 = require("./pda/index");
|
|
16
17
|
const axios_1 = __importDefault(require("axios"));
|
|
17
18
|
const web3_js_1 = require("@solana/web3.js");
|
|
18
19
|
const constants_1 = require("./constants");
|
|
19
|
-
const
|
|
20
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
21
|
+
const swap = ({ connection, wallet, inToken, outToken, amount }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
22
|
const token = TOKENS[inToken];
|
|
21
23
|
if (!token) {
|
|
22
24
|
throw new Error('Token not found');
|
|
23
25
|
}
|
|
24
26
|
const formattedAmountIn = amount * Math.pow(10, token.decimals);
|
|
25
|
-
const quoteResponse = yield axios_1.default.get(`https://
|
|
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`);
|
|
26
28
|
const { data: quoteData } = quoteResponse;
|
|
27
|
-
const swapResponse = yield axios_1.default.post('https://
|
|
29
|
+
const swapResponse = yield axios_1.default.post('https://lite-api.jup.ag/swap/v1/swap-instructions', {
|
|
30
|
+
quoteResponse: quoteData,
|
|
28
31
|
userPublicKey: wallet,
|
|
29
|
-
|
|
30
|
-
quoteResponse: quoteData
|
|
32
|
+
feeAccount: getFeeAccount()
|
|
31
33
|
});
|
|
32
|
-
const { setupInstructions, swapInstruction, addressLookupTableAddresses
|
|
34
|
+
const { setupInstructions, swapInstruction, addressLookupTableAddresses } = swapResponse.data;
|
|
33
35
|
return {
|
|
34
|
-
swapIxs: [
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
addressLookupTableAccounts: yield (0, exports.getAddressLookupTableAccounts)(connection, addressLookupTableAddresses),
|
|
41
|
-
setupInstructions: setupInstructions.map(deserializeInstruction),
|
|
42
|
-
cleanupInstruction: deserializeInstruction(cleanupInstruction),
|
|
43
|
-
trdAmount: quoteData.outAmount
|
|
36
|
+
swapIxs: [deserializeInstruction(swapInstruction)],
|
|
37
|
+
addressLookupTableAccounts: yield getAddressLookupTableAccounts(connection, addressLookupTableAddresses),
|
|
38
|
+
setupInstructions: setupInstructions.length > 0
|
|
39
|
+
? [deserializeInstruction(setupInstructions)]
|
|
40
|
+
: [],
|
|
41
|
+
outAmount: quoteData.otherAmountThreshold
|
|
44
42
|
};
|
|
45
43
|
});
|
|
46
44
|
exports.swap = swap;
|
|
@@ -69,7 +67,6 @@ const getAddressLookupTableAccounts = (connection, keys) => __awaiter(void 0, vo
|
|
|
69
67
|
return acc;
|
|
70
68
|
}, new Array());
|
|
71
69
|
});
|
|
72
|
-
exports.getAddressLookupTableAccounts = getAddressLookupTableAccounts;
|
|
73
70
|
const TOKENS = {
|
|
74
71
|
So11111111111111111111111111111111111111112: {
|
|
75
72
|
mint: 'So11111111111111111111111111111111111111112',
|
|
@@ -78,5 +75,12 @@ const TOKENS = {
|
|
|
78
75
|
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: {
|
|
79
76
|
mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
|
|
80
77
|
decimals: 6
|
|
78
|
+
},
|
|
79
|
+
t3DohmswhKk94PPbPYwA6ZKACyY3y5kbcqeQerAJjmV: {
|
|
80
|
+
mint: 't3DohmswhKk94PPbPYwA6ZKACyY3y5kbcqeQerAJjmV',
|
|
81
|
+
decimals: 6
|
|
81
82
|
}
|
|
82
83
|
};
|
|
84
|
+
const getFeeAccount = () => {
|
|
85
|
+
return (0, index_1.getTokenATA)(new web3_js_1.PublicKey('Hk1r2NUL4LbUhx1agg1w44tyZiNr72mbeLsg6suF5MA4'), constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID);
|
|
86
|
+
};
|