@triadxyz/triad-protocol 1.1.6-beta → 1.1.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/local-test.js +5 -21
- package/dist/trade.d.ts +4 -2
- package/dist/trade.js +43 -6
- package/dist/types/idl_triad_protocol.json +25 -1
- package/dist/types/trade.d.ts +16 -0
- package/dist/types/triad_protocol.d.ts +53 -0
- package/dist/utils/priorityFee.d.ts +2 -1
- package/dist/utils/priorityFee.js +9 -25
- package/dist/utils/sendVersionedTransaction.d.ts +2 -1
- package/dist/utils/sendVersionedTransaction.js +2 -2
- package/dist/utils/swap.d.ts +14 -0
- package/dist/utils/swap.js +93 -0
- package/package.json +1 -1
package/dist/local-test.js
CHANGED
|
@@ -27,7 +27,7 @@ const markets = [{ name: 'Triad', marketId: 0 }];
|
|
|
27
27
|
const ordersHypeAndFloopBot = [
|
|
28
28
|
{
|
|
29
29
|
marketId: 0,
|
|
30
|
-
amount: 0,
|
|
30
|
+
amount: 0.001,
|
|
31
31
|
direction: {
|
|
32
32
|
flop: {}
|
|
33
33
|
},
|
|
@@ -54,7 +54,6 @@ const updateStakeVaultStatus = () => __awaiter(void 0, void 0, void 0, function*
|
|
|
54
54
|
});
|
|
55
55
|
console.log(response);
|
|
56
56
|
});
|
|
57
|
-
updateStakeVaultStatus();
|
|
58
57
|
const updateBoost = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
58
|
const response = (yield axios_1.default.get('https://api.triadfi.co/boost'))
|
|
60
59
|
.data;
|
|
@@ -80,7 +79,7 @@ const getAllMarkets = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
80
79
|
console.log(markets);
|
|
81
80
|
});
|
|
82
81
|
const getMarket = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
83
|
-
const market = yield triadProtocol.trade.getMarketById(
|
|
82
|
+
const market = yield triadProtocol.trade.getMarketById(0);
|
|
84
83
|
console.log(market);
|
|
85
84
|
});
|
|
86
85
|
const initializeAllMarkets = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -103,9 +102,10 @@ const getOrders = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
103
102
|
const openOrder = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
103
|
for (const order of ordersHypeAndFloopBot) {
|
|
105
104
|
const response = yield triadProtocol.trade.openOrder({
|
|
106
|
-
marketId:
|
|
105
|
+
marketId: 0,
|
|
107
106
|
amount: order.amount,
|
|
108
107
|
direction: order.direction,
|
|
108
|
+
token: 'So11111111111111111111111111111111111111112',
|
|
109
109
|
comment: `hype/flop bot ${order.amount} - ${order.direction}`
|
|
110
110
|
}, {
|
|
111
111
|
skipPreflight: true
|
|
@@ -113,6 +113,7 @@ const openOrder = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
113
113
|
console.log(response);
|
|
114
114
|
}
|
|
115
115
|
});
|
|
116
|
+
openOrder();
|
|
116
117
|
const closeOrder = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
118
|
for (const order of yield getOrders()) {
|
|
118
119
|
try {
|
|
@@ -156,24 +157,7 @@ const resolveQuestion = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
156
157
|
const response = yield triadProtocol.trade.resolveQuestion(0);
|
|
157
158
|
console.log(response);
|
|
158
159
|
});
|
|
159
|
-
const claimAllRewards = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
160
|
-
const response = yield triadProtocol.stake.claimStakeRewards({
|
|
161
|
-
wallet: wallet.publicKey,
|
|
162
|
-
ranks: [],
|
|
163
|
-
collections: 1,
|
|
164
|
-
verifier: '43bkRb3xj4Vv77Y76TjQuNiPGUsnun4tFSMYXY2Y2K5LfRT79YDpHhc7jruWmDGdygUBPXYQQZntCQz6V6cRGz8F'
|
|
165
|
-
});
|
|
166
|
-
console.log(response);
|
|
167
|
-
});
|
|
168
160
|
const getReferral = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
169
161
|
const response = yield triadProtocol.getReferral('dannpl');
|
|
170
162
|
console.log(response);
|
|
171
163
|
});
|
|
172
|
-
const getUserPositions = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
173
|
-
const response = yield triadProtocol.getUserPositionsWithAmount(new web3_js_1.PublicKey('5U4TmSUBqDaefmamimoSGak8GK5xwXoM5nAaCZJpynpL'));
|
|
174
|
-
console.log(response);
|
|
175
|
-
});
|
|
176
|
-
const getTickers = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
177
|
-
const response = yield triadProtocol.getTickers();
|
|
178
|
-
console.log(response);
|
|
179
|
-
});
|
package/dist/trade.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AnchorProvider, Program } from '@coral-xyz/anchor';
|
|
2
2
|
import { TriadProtocol } from './types/triad_protocol';
|
|
3
3
|
import { PublicKey } from '@solana/web3.js';
|
|
4
|
-
import { InitializeQuestionArgs, Market, OpenOrderArgs } from './types/trade';
|
|
4
|
+
import { FeeVault, InitializeQuestionArgs, Market, OpenOrderArgs } from './types/trade';
|
|
5
5
|
import { RpcOptions } from './types';
|
|
6
6
|
import BN from 'bn.js';
|
|
7
7
|
export default class Trade {
|
|
@@ -13,6 +13,7 @@ export default class Trade {
|
|
|
13
13
|
* Get all Markets
|
|
14
14
|
*/
|
|
15
15
|
getAllMarkets(): Promise<Market[]>;
|
|
16
|
+
getFeeVault(marketId: number): Promise<FeeVault>;
|
|
16
17
|
/**
|
|
17
18
|
* Get Market by ID
|
|
18
19
|
* @param marketId - The ID of the market
|
|
@@ -119,12 +120,13 @@ export default class Trade {
|
|
|
119
120
|
* @param marketId - The ID of the market
|
|
120
121
|
* @param amount - The amount of the order
|
|
121
122
|
* @param direction - The direction of the order
|
|
123
|
+
* @param token - The token to use for the order
|
|
122
124
|
* @param comment - The comment of the order
|
|
123
125
|
*
|
|
124
126
|
* @param options - RPC options
|
|
125
127
|
*
|
|
126
128
|
*/
|
|
127
|
-
openOrder({ marketId, amount, direction, comment }: OpenOrderArgs, options?: RpcOptions): Promise<string>;
|
|
129
|
+
openOrder({ marketId, amount, direction, token, comment }: OpenOrderArgs, options?: RpcOptions): Promise<string>;
|
|
128
130
|
/**
|
|
129
131
|
* Close Order
|
|
130
132
|
* @param marketId - The ID of the market
|
package/dist/trade.js
CHANGED
|
@@ -19,6 +19,7 @@ const trade_1 = require("./utils/pda/trade");
|
|
|
19
19
|
const pda_1 = require("./utils/pda");
|
|
20
20
|
const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
|
|
21
21
|
const sendTransactionWithOptions_1 = __importDefault(require("./utils/sendTransactionWithOptions"));
|
|
22
|
+
const swap_1 = require("./utils/swap");
|
|
22
23
|
class Trade {
|
|
23
24
|
constructor(program, provider) {
|
|
24
25
|
this.mint = constants_1.TRD_MINT;
|
|
@@ -35,6 +36,26 @@ class Trade {
|
|
|
35
36
|
.then((markets) => markets.map(({ account, publicKey }) => (0, helpers_1.accountToMarket)(account, publicKey)));
|
|
36
37
|
});
|
|
37
38
|
}
|
|
39
|
+
getFeeVault(marketId) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
const feeVaultPDA = (0, trade_1.getFeeVaultPDA)(this.program.programId, marketId);
|
|
42
|
+
const response = yield this.program.account.feeVault.fetch(feeVaultPDA);
|
|
43
|
+
return {
|
|
44
|
+
bump: response.bump,
|
|
45
|
+
authority: response.authority,
|
|
46
|
+
market: response.market,
|
|
47
|
+
deposited: response.deposited.toString(),
|
|
48
|
+
withdrawn: response.withdrawn.toString(),
|
|
49
|
+
netBalance: response.netBalance.toString(),
|
|
50
|
+
projectAvailable: response.projectAvailable.toString(),
|
|
51
|
+
projectClaimed: response.projectClaimed.toString(),
|
|
52
|
+
nftHoldersAvailable: response.nftHoldersAvailable.toString(),
|
|
53
|
+
nftHoldersClaimed: response.nftHoldersClaimed.toString(),
|
|
54
|
+
marketAvailable: response.marketAvailable.toString(),
|
|
55
|
+
marketClaimed: response.marketClaimed.toString()
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
38
59
|
/**
|
|
39
60
|
* Get Market by ID
|
|
40
61
|
* @param marketId - The ID of the market
|
|
@@ -95,19 +116,21 @@ class Trade {
|
|
|
95
116
|
* @param marketId - The ID of the market
|
|
96
117
|
* @param amount - The amount of the order
|
|
97
118
|
* @param direction - The direction of the order
|
|
119
|
+
* @param token - The token to use for the order
|
|
98
120
|
* @param comment - The comment of the order
|
|
99
121
|
*
|
|
100
122
|
* @param options - RPC options
|
|
101
123
|
*
|
|
102
124
|
*/
|
|
103
|
-
openOrder({ marketId, amount, direction, comment }, options) {
|
|
125
|
+
openOrder({ marketId, amount, direction, token, comment }, options) {
|
|
104
126
|
return __awaiter(this, void 0, void 0, function* () {
|
|
105
127
|
const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
|
|
106
128
|
const feeVualtPDA = (0, trade_1.getFeeVaultPDA)(this.program.programId, marketId);
|
|
107
129
|
const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
108
130
|
const userPDA = (0, pda_1.getUserPDA)(this.program.programId, this.provider.publicKey);
|
|
109
|
-
const userFromATA = (0, pda_1.getTokenATA)(this.provider.publicKey, this.mint);
|
|
110
131
|
const ixs = [];
|
|
132
|
+
const addressLookupTableAccounts = [];
|
|
133
|
+
let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
|
|
111
134
|
try {
|
|
112
135
|
yield this.program.account.userTrade.fetch(userTradePDA);
|
|
113
136
|
}
|
|
@@ -120,9 +143,24 @@ class Trade {
|
|
|
120
143
|
})
|
|
121
144
|
.instruction());
|
|
122
145
|
}
|
|
146
|
+
if (token !== constants_1.TRD_MINT.toBase58()) {
|
|
147
|
+
const { setupInstructions, swapIxs, addressLookupTableAccounts, trdAmount } = yield (0, swap_1.swap)({
|
|
148
|
+
connection: this.provider.connection,
|
|
149
|
+
wallet: this.provider.publicKey.toBase58(),
|
|
150
|
+
inToken: token,
|
|
151
|
+
amount
|
|
152
|
+
});
|
|
153
|
+
amountInTRD = trdAmount;
|
|
154
|
+
if (swapIxs.length === 0) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
ixs.push(...setupInstructions);
|
|
158
|
+
ixs.push(...swapIxs);
|
|
159
|
+
addressLookupTableAccounts.push(...addressLookupTableAccounts);
|
|
160
|
+
}
|
|
123
161
|
ixs.push(yield this.program.methods
|
|
124
162
|
.openOrder({
|
|
125
|
-
amount: new bn_js_1.default(
|
|
163
|
+
amount: new bn_js_1.default(amountInTRD),
|
|
126
164
|
direction: direction,
|
|
127
165
|
comment: (0, helpers_1.encodeString)(comment, 64)
|
|
128
166
|
})
|
|
@@ -131,11 +169,10 @@ class Trade {
|
|
|
131
169
|
market: marketPDA,
|
|
132
170
|
feeVault: feeVualtPDA,
|
|
133
171
|
userTrade: userTradePDA,
|
|
134
|
-
mint: this.mint
|
|
135
|
-
userFromAta: userFromATA
|
|
172
|
+
mint: this.mint
|
|
136
173
|
})
|
|
137
174
|
.instruction());
|
|
138
|
-
return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
|
|
175
|
+
return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
|
|
139
176
|
});
|
|
140
177
|
}
|
|
141
178
|
/**
|
|
@@ -461,7 +461,31 @@
|
|
|
461
461
|
},
|
|
462
462
|
{
|
|
463
463
|
"name": "user_from_ata",
|
|
464
|
-
"writable": true
|
|
464
|
+
"writable": true,
|
|
465
|
+
"pda": {
|
|
466
|
+
"seeds": [
|
|
467
|
+
{
|
|
468
|
+
"kind": "account",
|
|
469
|
+
"path": "signer"
|
|
470
|
+
},
|
|
471
|
+
{
|
|
472
|
+
"kind": "account",
|
|
473
|
+
"path": "token_program"
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
"kind": "account",
|
|
477
|
+
"path": "mint"
|
|
478
|
+
}
|
|
479
|
+
],
|
|
480
|
+
"program": {
|
|
481
|
+
"kind": "const",
|
|
482
|
+
"value": [
|
|
483
|
+
140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142,
|
|
484
|
+
13, 131, 11, 90, 19, 153, 218, 255, 16, 132, 4, 142, 123, 216,
|
|
485
|
+
219, 233, 248, 89
|
|
486
|
+
]
|
|
487
|
+
}
|
|
488
|
+
}
|
|
465
489
|
},
|
|
466
490
|
{
|
|
467
491
|
"name": "market_to_ata",
|
package/dist/types/trade.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
1
2
|
export type Market = {
|
|
2
3
|
address: string;
|
|
3
4
|
bump: number;
|
|
@@ -73,5 +74,20 @@ export type OpenOrderArgs = {
|
|
|
73
74
|
marketId: number;
|
|
74
75
|
amount: number;
|
|
75
76
|
direction: OrderDirection;
|
|
77
|
+
token: string;
|
|
76
78
|
comment?: string;
|
|
77
79
|
};
|
|
80
|
+
export type FeeVault = {
|
|
81
|
+
bump: number;
|
|
82
|
+
authority: PublicKey;
|
|
83
|
+
market: PublicKey;
|
|
84
|
+
deposited: string;
|
|
85
|
+
withdrawn: string;
|
|
86
|
+
netBalance: string;
|
|
87
|
+
projectAvailable: string;
|
|
88
|
+
projectClaimed: string;
|
|
89
|
+
nftHoldersAvailable: string;
|
|
90
|
+
nftHoldersClaimed: string;
|
|
91
|
+
marketAvailable: string;
|
|
92
|
+
marketClaimed: string;
|
|
93
|
+
};
|
|
@@ -613,6 +613,59 @@ export type TriadProtocol = {
|
|
|
613
613
|
{
|
|
614
614
|
name: 'userFromAta';
|
|
615
615
|
writable: true;
|
|
616
|
+
pda: {
|
|
617
|
+
seeds: [
|
|
618
|
+
{
|
|
619
|
+
kind: 'account';
|
|
620
|
+
path: 'signer';
|
|
621
|
+
},
|
|
622
|
+
{
|
|
623
|
+
kind: 'account';
|
|
624
|
+
path: 'tokenProgram';
|
|
625
|
+
},
|
|
626
|
+
{
|
|
627
|
+
kind: 'account';
|
|
628
|
+
path: 'mint';
|
|
629
|
+
}
|
|
630
|
+
];
|
|
631
|
+
program: {
|
|
632
|
+
kind: 'const';
|
|
633
|
+
value: [
|
|
634
|
+
140,
|
|
635
|
+
151,
|
|
636
|
+
37,
|
|
637
|
+
143,
|
|
638
|
+
78,
|
|
639
|
+
36,
|
|
640
|
+
137,
|
|
641
|
+
241,
|
|
642
|
+
187,
|
|
643
|
+
61,
|
|
644
|
+
16,
|
|
645
|
+
41,
|
|
646
|
+
20,
|
|
647
|
+
142,
|
|
648
|
+
13,
|
|
649
|
+
131,
|
|
650
|
+
11,
|
|
651
|
+
90,
|
|
652
|
+
19,
|
|
653
|
+
153,
|
|
654
|
+
218,
|
|
655
|
+
255,
|
|
656
|
+
16,
|
|
657
|
+
132,
|
|
658
|
+
4,
|
|
659
|
+
142,
|
|
660
|
+
123,
|
|
661
|
+
216,
|
|
662
|
+
219,
|
|
663
|
+
233,
|
|
664
|
+
248,
|
|
665
|
+
89
|
|
666
|
+
];
|
|
667
|
+
};
|
|
668
|
+
};
|
|
616
669
|
},
|
|
617
670
|
{
|
|
618
671
|
name: 'marketToAta';
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
declare const getPriorityFee: () => Promise<number>;
|
|
2
|
+
export default getPriorityFee;
|
|
@@ -8,29 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
return
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
headers: { "Content-Type": "application/json" },
|
|
19
|
-
body: JSON.stringify({
|
|
20
|
-
jsonrpc: "2.0",
|
|
21
|
-
id: "1",
|
|
22
|
-
method: "getPriorityFeeEstimate",
|
|
23
|
-
params: [
|
|
24
|
-
{
|
|
25
|
-
transaction: bytes_1.bs58.encode(transaction.serialize()),
|
|
26
|
-
options: { priorityLevel: priorityLevel },
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
}),
|
|
30
|
-
});
|
|
31
|
-
const data = yield response.json();
|
|
32
|
-
console.log("Fee in function for", priorityLevel, " :", data.result.priorityFeeEstimate);
|
|
33
|
-
return data.result;
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
exports.getPriorityFeeEstimate = getPriorityFeeEstimate;
|
|
15
|
+
const axios_1 = __importDefault(require("axios"));
|
|
16
|
+
const getPriorityFee = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
const response = yield axios_1.default.get('https://solanacompass.com/api/fees');
|
|
18
|
+
return response.data[5].priorityTx || 1000;
|
|
19
|
+
});
|
|
20
|
+
exports.default = getPriorityFee;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TransactionInstruction } from '@solana/web3.js';
|
|
2
2
|
import { RpcOptions } from '../types';
|
|
3
|
+
import { AddressLookupTableAccount } from '@solana/web3.js';
|
|
3
4
|
import { AnchorProvider } from '@coral-xyz/anchor';
|
|
4
5
|
import { Keypair } from '@solana/web3.js';
|
|
5
|
-
declare const sendVersionedTransaction: (provider: AnchorProvider, ixs: TransactionInstruction[], options?: RpcOptions, payer?: Keypair) => Promise<string>;
|
|
6
|
+
declare const sendVersionedTransaction: (provider: AnchorProvider, ixs: TransactionInstruction[], options?: RpcOptions, payer?: Keypair, addressLookupTableAccounts?: AddressLookupTableAccount[]) => Promise<string>;
|
|
6
7
|
export default sendVersionedTransaction;
|
|
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
const web3_js_1 = require("@solana/web3.js");
|
|
13
|
-
const sendVersionedTransaction = (provider, ixs, options, payer) => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
|
+
const sendVersionedTransaction = (provider, ixs, options, payer, addressLookupTableAccounts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
14
|
if (options === null || options === void 0 ? void 0 : options.microLamports) {
|
|
15
15
|
ixs.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
16
16
|
microLamports: options.microLamports
|
|
@@ -21,7 +21,7 @@ const sendVersionedTransaction = (provider, ixs, options, payer) => __awaiter(vo
|
|
|
21
21
|
instructions: ixs,
|
|
22
22
|
recentBlockhash: blockhash,
|
|
23
23
|
payerKey: provider.publicKey
|
|
24
|
-
}).compileToV0Message());
|
|
24
|
+
}).compileToV0Message(addressLookupTableAccounts || []));
|
|
25
25
|
if (payer) {
|
|
26
26
|
tx.sign([payer]);
|
|
27
27
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AddressLookupTableAccount, Connection, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
export declare const swap: ({ connection, wallet, inToken, amount }: {
|
|
3
|
+
connection: Connection;
|
|
4
|
+
wallet: string;
|
|
5
|
+
inToken: string;
|
|
6
|
+
amount: number;
|
|
7
|
+
}) => Promise<{
|
|
8
|
+
swapIxs: TransactionInstruction[];
|
|
9
|
+
addressLookupTableAccounts: AddressLookupTableAccount[];
|
|
10
|
+
setupInstructions: any;
|
|
11
|
+
cleanupInstruction: TransactionInstruction;
|
|
12
|
+
trdAmount: any;
|
|
13
|
+
}>;
|
|
14
|
+
export declare const getAddressLookupTableAccounts: (connection: Connection, keys: string[]) => Promise<AddressLookupTableAccount[]>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.getAddressLookupTableAccounts = exports.swap = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
18
|
+
const constants_1 = require("./constants");
|
|
19
|
+
const swap = ({ connection, wallet, inToken, amount }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const token = TOKENS[inToken];
|
|
21
|
+
if (!token) {
|
|
22
|
+
throw new Error('Token not found');
|
|
23
|
+
}
|
|
24
|
+
const formattedAmountIn = amount * Math.pow(10, token.decimals);
|
|
25
|
+
const quoteResponse = yield axios_1.default.get(`https://quote-api.jup.ag/v6/quote?inputMint=${inToken}&outputMint=${constants_1.TRD_MINT.toBase58()}&amount=${formattedAmountIn}&slippageBps=10`);
|
|
26
|
+
const { data: quoteData } = quoteResponse;
|
|
27
|
+
const swapResponse = yield axios_1.default.post('https://quote-api.jup.ag/v6/swap-instructions', {
|
|
28
|
+
userPublicKey: wallet,
|
|
29
|
+
wrapAndUnwrapSol: true,
|
|
30
|
+
quoteResponse: quoteData
|
|
31
|
+
});
|
|
32
|
+
const { setupInstructions, swapInstruction, addressLookupTableAddresses, cleanupInstruction } = swapResponse.data;
|
|
33
|
+
return {
|
|
34
|
+
swapIxs: [
|
|
35
|
+
deserializeInstruction(swapInstruction),
|
|
36
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
|
37
|
+
units: 500000
|
|
38
|
+
}),
|
|
39
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
40
|
+
microLamports: yield getPriorityFee()
|
|
41
|
+
})
|
|
42
|
+
],
|
|
43
|
+
addressLookupTableAccounts: yield (0, exports.getAddressLookupTableAccounts)(connection, addressLookupTableAddresses),
|
|
44
|
+
setupInstructions: setupInstructions.map(deserializeInstruction),
|
|
45
|
+
cleanupInstruction: deserializeInstruction(cleanupInstruction),
|
|
46
|
+
trdAmount: quoteData.outAmount
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
exports.swap = swap;
|
|
50
|
+
const deserializeInstruction = (instruction) => {
|
|
51
|
+
return new web3_js_1.TransactionInstruction({
|
|
52
|
+
programId: new web3_js_1.PublicKey(instruction.programId),
|
|
53
|
+
keys: instruction.accounts.map((key) => ({
|
|
54
|
+
pubkey: new web3_js_1.PublicKey(key.pubkey),
|
|
55
|
+
isSigner: key.isSigner,
|
|
56
|
+
isWritable: key.isWritable
|
|
57
|
+
})),
|
|
58
|
+
data: Buffer.from(instruction.data, 'base64')
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const getAddressLookupTableAccounts = (connection, keys) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
+
const addressLookupTableAccountInfos = yield connection.getMultipleAccountsInfo(keys.map((key) => new web3_js_1.PublicKey(key)));
|
|
63
|
+
return addressLookupTableAccountInfos.reduce((acc, accountInfo, index) => {
|
|
64
|
+
const addressLookupTableAddress = keys[index];
|
|
65
|
+
if (accountInfo) {
|
|
66
|
+
const addressLookupTableAccount = new web3_js_1.AddressLookupTableAccount({
|
|
67
|
+
key: new web3_js_1.PublicKey(addressLookupTableAddress),
|
|
68
|
+
state: web3_js_1.AddressLookupTableAccount.deserialize(accountInfo.data)
|
|
69
|
+
});
|
|
70
|
+
acc.push(addressLookupTableAccount);
|
|
71
|
+
}
|
|
72
|
+
return acc;
|
|
73
|
+
}, new Array());
|
|
74
|
+
});
|
|
75
|
+
exports.getAddressLookupTableAccounts = getAddressLookupTableAccounts;
|
|
76
|
+
const getPriorityFee = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
|
+
const response = yield axios_1.default.get('https://solanacompass.com/api/fees');
|
|
78
|
+
return response.data[15].priorityTx || 1000;
|
|
79
|
+
});
|
|
80
|
+
const TOKENS = {
|
|
81
|
+
oreoU2P8bN6jkk3jbaiVxYnG1dCXcYxwhwyK9jSybcp: {
|
|
82
|
+
mint: 'oreoU2P8bN6jkk3jbaiVxYnG1dCXcYxwhwyK9jSybcp',
|
|
83
|
+
decimals: 11
|
|
84
|
+
},
|
|
85
|
+
So11111111111111111111111111111111111111112: {
|
|
86
|
+
mint: 'So11111111111111111111111111111111111111112',
|
|
87
|
+
decimals: 9
|
|
88
|
+
},
|
|
89
|
+
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: {
|
|
90
|
+
mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
|
|
91
|
+
decimals: 6
|
|
92
|
+
}
|
|
93
|
+
};
|