@triadxyz/triad-protocol 2.7.7-beta → 2.7.8-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 +119 -49
- package/dist/types/index.d.ts +5 -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 }: 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,9 +856,25 @@ 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 { userTradePDA,
|
|
859
|
+
const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(direction)[0]);
|
|
819
860
|
const orderBook = yield this.getOrderBook(marketId);
|
|
820
861
|
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)({
|
|
864
|
+
connection: this.program.provider.connection,
|
|
865
|
+
wallet: this.program.provider.publicKey.toBase58(),
|
|
866
|
+
inToken: constants_1.TRD_MINT.toString(),
|
|
867
|
+
outToken: constants_1.USDC_MINT.toString(),
|
|
868
|
+
amount
|
|
869
|
+
});
|
|
870
|
+
if (swapIxs.length === 0) {
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
remainingUSDC = new bn_js_1.default(outAmount);
|
|
874
|
+
ixs.push(...setupInstructions);
|
|
875
|
+
ixs.push(...swapIxs);
|
|
876
|
+
addressLookupTableAccounts.push(...addressLookupTableAccounts);
|
|
877
|
+
}
|
|
821
878
|
const orders = Object.keys(direction)[0] === 'hype'
|
|
822
879
|
? orderBook.hype.ask
|
|
823
880
|
: orderBook.flop.ask;
|
|
@@ -884,65 +941,78 @@ class TriadProtocolClient {
|
|
|
884
941
|
/**
|
|
885
942
|
* Market Ask Order
|
|
886
943
|
* @param args.marketId - The ID of the Market
|
|
887
|
-
* @param args.
|
|
888
|
-
* @param args.bidOrderId - The ID of the Bid Order
|
|
944
|
+
* @param args.orders - Array of orders to execute
|
|
889
945
|
* @param args.direction - The direction of the Order
|
|
890
|
-
* @param args.mint - The mint of the Order
|
|
891
946
|
* @param args.isTrdPayout - Whether to payout in TRD or not
|
|
892
947
|
*
|
|
893
948
|
* @param options - RPC options
|
|
894
949
|
*/
|
|
895
|
-
marketAskOrder({ marketId,
|
|
950
|
+
marketAskOrder({ marketId, orders, direction, isTrdPayout }, options) {
|
|
896
951
|
return __awaiter(this, void 0, void 0, function* () {
|
|
897
952
|
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
898
953
|
const ixs = [];
|
|
899
|
-
const
|
|
954
|
+
const addressLookupTableAccounts = [];
|
|
900
955
|
const orderBook = yield this.getOrderBook(marketId);
|
|
901
|
-
|
|
902
|
-
const orders = Object.keys(direction)[0] === 'hype'
|
|
956
|
+
const bidOrders = Object.keys(direction)[0] === 'hype'
|
|
903
957
|
? orderBook.hype.bid
|
|
904
958
|
: orderBook.flop.bid;
|
|
905
|
-
const sortedOrders =
|
|
906
|
-
const tokenProgram = (0, helpers_1.getTokenProgram)(
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
959
|
+
const sortedOrders = bidOrders.sort((a, b) => Number(b.price) - Number(a.price));
|
|
960
|
+
const tokenProgram = (0, helpers_1.getTokenProgram)(constants_1.USDC_MINT);
|
|
961
|
+
let amountOfShares = 0;
|
|
962
|
+
for (const inputOrder of orders) {
|
|
963
|
+
let remainingShares = new bn_js_1.default(inputOrder.shares * Math.pow(10, this.decimals));
|
|
964
|
+
for (const order of sortedOrders) {
|
|
965
|
+
if (remainingShares.lte(new bn_js_1.default(0)))
|
|
966
|
+
break;
|
|
967
|
+
if (order.authority === this.program.provider.publicKey.toBase58() ||
|
|
968
|
+
order.linkedBookOrderId !== constants_1.BOOK_ORDER_NULL.toString()) {
|
|
969
|
+
continue;
|
|
970
|
+
}
|
|
971
|
+
const availableShares = new bn_js_1.default(order.totalShares).sub(new bn_js_1.default(order.filledShares));
|
|
972
|
+
const sharesToSell = bn_js_1.default.min(remainingShares, availableShares);
|
|
973
|
+
if (sharesToSell.lt(new bn_js_1.default(0)))
|
|
974
|
+
continue;
|
|
975
|
+
remainingShares = remainingShares.sub(sharesToSell);
|
|
976
|
+
amountOfShares += sharesToSell.toNumber();
|
|
977
|
+
ixs.push(yield this.program.methods
|
|
978
|
+
.marketAskOrder({
|
|
979
|
+
shares: new bn_js_1.default(sharesToSell),
|
|
980
|
+
orderDirection: direction,
|
|
981
|
+
bidOrderId: new bn_js_1.default(inputOrder.bidOrderId),
|
|
982
|
+
bookOrderBidId: new bn_js_1.default(order.id)
|
|
983
|
+
})
|
|
984
|
+
.accounts({
|
|
985
|
+
signer: this.program.provider.publicKey,
|
|
986
|
+
market: marketPDA,
|
|
987
|
+
buyerAuthority: new web3_js_1.PublicKey(order.authority),
|
|
988
|
+
buyerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
|
|
989
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
|
|
990
|
+
mint: constants_1.USDC_MINT,
|
|
991
|
+
tokenProgram,
|
|
992
|
+
marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, tokenProgram),
|
|
993
|
+
sellerTrade: this.getUserPDA(this.program.provider.publicKey, inputOrder.userNonce)
|
|
994
|
+
})
|
|
995
|
+
.instruction());
|
|
913
996
|
}
|
|
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
997
|
}
|
|
939
|
-
if (
|
|
940
|
-
|
|
998
|
+
if (isTrdPayout) {
|
|
999
|
+
const { setupInstructions, swapIxs, addressLookupTableAccounts: swapAddressLookupTableAccounts } = yield (0, swap_1.swap)({
|
|
1000
|
+
connection: this.program.provider.connection,
|
|
1001
|
+
wallet: this.program.provider.publicKey.toBase58(),
|
|
1002
|
+
inToken: constants_1.USDC_MINT.toBase58(),
|
|
1003
|
+
outToken: constants_1.TRD_MINT.toString(),
|
|
1004
|
+
amount: amountOfShares
|
|
1005
|
+
});
|
|
1006
|
+
if (swapIxs.length > 0) {
|
|
1007
|
+
ixs.push(...setupInstructions);
|
|
1008
|
+
ixs.push(...swapIxs);
|
|
1009
|
+
addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
|
|
1010
|
+
}
|
|
941
1011
|
}
|
|
942
1012
|
if (ixs.length === 0) {
|
|
943
1013
|
throw new Error('No matching orders found to fill the requested amount');
|
|
944
1014
|
}
|
|
945
|
-
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
|
|
1015
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
|
|
946
1016
|
});
|
|
947
1017
|
}
|
|
948
1018
|
/**
|
package/dist/types/index.d.ts
CHANGED
|
@@ -219,14 +219,16 @@ 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;
|
|
231
233
|
};
|
|
232
234
|
export type Refer = {
|
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
|
+
};
|