@triadxyz/triad-protocol 1.1.7-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.
@@ -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(1);
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: 1,
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
@@ -120,12 +120,13 @@ export default class Trade {
120
120
  * @param marketId - The ID of the market
121
121
  * @param amount - The amount of the order
122
122
  * @param direction - The direction of the order
123
+ * @param token - The token to use for the order
123
124
  * @param comment - The comment of the order
124
125
  *
125
126
  * @param options - RPC options
126
127
  *
127
128
  */
128
- openOrder({ marketId, amount, direction, comment }: OpenOrderArgs, options?: RpcOptions): Promise<string>;
129
+ openOrder({ marketId, amount, direction, token, comment }: OpenOrderArgs, options?: RpcOptions): Promise<string>;
129
130
  /**
130
131
  * Close Order
131
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;
@@ -115,19 +116,21 @@ class Trade {
115
116
  * @param marketId - The ID of the market
116
117
  * @param amount - The amount of the order
117
118
  * @param direction - The direction of the order
119
+ * @param token - The token to use for the order
118
120
  * @param comment - The comment of the order
119
121
  *
120
122
  * @param options - RPC options
121
123
  *
122
124
  */
123
- openOrder({ marketId, amount, direction, comment }, options) {
125
+ openOrder({ marketId, amount, direction, token, comment }, options) {
124
126
  return __awaiter(this, void 0, void 0, function* () {
125
127
  const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
126
128
  const feeVualtPDA = (0, trade_1.getFeeVaultPDA)(this.program.programId, marketId);
127
129
  const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
128
130
  const userPDA = (0, pda_1.getUserPDA)(this.program.programId, this.provider.publicKey);
129
- const userFromATA = (0, pda_1.getTokenATA)(this.provider.publicKey, this.mint);
130
131
  const ixs = [];
132
+ const addressLookupTableAccounts = [];
133
+ let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
131
134
  try {
132
135
  yield this.program.account.userTrade.fetch(userTradePDA);
133
136
  }
@@ -140,9 +143,24 @@ class Trade {
140
143
  })
141
144
  .instruction());
142
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
+ }
143
161
  ixs.push(yield this.program.methods
144
162
  .openOrder({
145
- amount: new bn_js_1.default(amount * Math.pow(10, constants_1.TRD_DECIMALS)),
163
+ amount: new bn_js_1.default(amountInTRD),
146
164
  direction: direction,
147
165
  comment: (0, helpers_1.encodeString)(comment, 64)
148
166
  })
@@ -151,11 +169,10 @@ class Trade {
151
169
  market: marketPDA,
152
170
  feeVault: feeVualtPDA,
153
171
  userTrade: userTradePDA,
154
- mint: this.mint,
155
- userFromAta: userFromATA
172
+ mint: this.mint
156
173
  })
157
174
  .instruction());
158
- return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
175
+ return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
159
176
  });
160
177
  }
161
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",
@@ -74,6 +74,7 @@ export type OpenOrderArgs = {
74
74
  marketId: number;
75
75
  amount: number;
76
76
  direction: OrderDirection;
77
+ token: string;
77
78
  comment?: string;
78
79
  };
79
80
  export type FeeVault = {
@@ -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
- export declare function getPriorityFeeEstimate(priorityLevel: any, transaction: any, RPC_URL: string): Promise<any>;
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
- exports.getPriorityFeeEstimate = void 0;
13
- const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
14
- function getPriorityFeeEstimate(priorityLevel, transaction, RPC_URL) {
15
- return __awaiter(this, void 0, void 0, function* () {
16
- const response = yield fetch(RPC_URL, {
17
- method: "POST",
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "1.1.7-beta",
3
+ "version": "1.1.8-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",