@triadxyz/triad-protocol 2.1.5-beta → 2.1.6-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/trade.d.ts +2 -1
- package/dist/trade.js +19 -1
- package/dist/types/idl_triad_protocol.json +1 -30
- package/dist/types/trade.d.ts +1 -0
- package/dist/types/triad_protocol.d.ts +1 -30
- package/dist/utils/swap.d.ts +14 -0
- package/dist/utils/swap.js +82 -0
- package/package.json +1 -1
package/dist/trade.d.ts
CHANGED
|
@@ -172,11 +172,12 @@ export default class Trade {
|
|
|
172
172
|
* @param args.amount - The amount of the Order
|
|
173
173
|
* @param args.direction - The direction of the Order
|
|
174
174
|
* @param args.mint - The mint of the Order
|
|
175
|
+
* @param args.token - The token to use for the Order
|
|
175
176
|
*
|
|
176
177
|
* @param options - RPC options
|
|
177
178
|
*
|
|
178
179
|
*/
|
|
179
|
-
openOrder({ marketId, amount, direction, mint }: OpenOrderArgs, options?: RpcOptions): Promise<string>;
|
|
180
|
+
openOrder({ marketId, amount, direction, mint, token }: OpenOrderArgs, options?: RpcOptions): Promise<string>;
|
|
180
181
|
/**
|
|
181
182
|
* Close Order
|
|
182
183
|
* @param args.marketId - The ID of the Market
|
package/dist/trade.js
CHANGED
|
@@ -21,6 +21,7 @@ const pda_1 = require("./utils/pda");
|
|
|
21
21
|
const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
|
|
22
22
|
const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
|
|
23
23
|
const pda_2 = require("./utils/pda");
|
|
24
|
+
const swap_1 = require("./utils/swap");
|
|
24
25
|
class Trade {
|
|
25
26
|
constructor(program) {
|
|
26
27
|
this.program = program;
|
|
@@ -197,11 +198,12 @@ class Trade {
|
|
|
197
198
|
* @param args.amount - The amount of the Order
|
|
198
199
|
* @param args.direction - The direction of the Order
|
|
199
200
|
* @param args.mint - The mint of the Order
|
|
201
|
+
* @param args.token - The token to use for the Order
|
|
200
202
|
*
|
|
201
203
|
* @param options - RPC options
|
|
202
204
|
*
|
|
203
205
|
*/
|
|
204
|
-
openOrder({ marketId, amount, direction, mint }, options) {
|
|
206
|
+
openOrder({ marketId, amount, direction, mint, token }, options) {
|
|
205
207
|
return __awaiter(this, void 0, void 0, function* () {
|
|
206
208
|
const ixs = [];
|
|
207
209
|
const addressLookupTableAccounts = [];
|
|
@@ -209,6 +211,22 @@ class Trade {
|
|
|
209
211
|
if (userTradeIxs.length > 0) {
|
|
210
212
|
ixs.push(...userTradeIxs);
|
|
211
213
|
}
|
|
214
|
+
let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
|
|
215
|
+
if (token !== constants_1.TRD_MINT.toBase58()) {
|
|
216
|
+
const { setupInstructions, swapIxs, addressLookupTableAccounts, trdAmount } = yield (0, swap_1.swap)({
|
|
217
|
+
connection: this.program.provider.connection,
|
|
218
|
+
wallet: this.program.provider.publicKey.toBase58(),
|
|
219
|
+
inToken: token,
|
|
220
|
+
amount
|
|
221
|
+
});
|
|
222
|
+
amountInTRD = trdAmount;
|
|
223
|
+
if (swapIxs.length === 0) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
ixs.push(...setupInstructions);
|
|
227
|
+
ixs.push(...swapIxs);
|
|
228
|
+
addressLookupTableAccounts.push(...addressLookupTableAccounts);
|
|
229
|
+
}
|
|
212
230
|
ixs.push(yield this.program.methods
|
|
213
231
|
.openOrder({
|
|
214
232
|
amount: new bn_js_1.default(amount * Math.pow(10, constants_1.TRD_DECIMALS)),
|
|
@@ -836,16 +836,7 @@
|
|
|
836
836
|
"address": "11111111111111111111111111111111"
|
|
837
837
|
}
|
|
838
838
|
],
|
|
839
|
-
"args": [
|
|
840
|
-
{
|
|
841
|
-
"name": "args",
|
|
842
|
-
"type": {
|
|
843
|
-
"defined": {
|
|
844
|
-
"name": "MarketAskOrderArgs"
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
]
|
|
839
|
+
"args": []
|
|
849
840
|
},
|
|
850
841
|
{
|
|
851
842
|
"name": "market_bid_order",
|
|
@@ -2274,26 +2265,6 @@
|
|
|
2274
2265
|
]
|
|
2275
2266
|
}
|
|
2276
2267
|
},
|
|
2277
|
-
{
|
|
2278
|
-
"name": "MarketAskOrderArgs",
|
|
2279
|
-
"type": {
|
|
2280
|
-
"kind": "struct",
|
|
2281
|
-
"fields": [
|
|
2282
|
-
{
|
|
2283
|
-
"name": "shares",
|
|
2284
|
-
"type": "u64"
|
|
2285
|
-
},
|
|
2286
|
-
{
|
|
2287
|
-
"name": "order_direction",
|
|
2288
|
-
"type": {
|
|
2289
|
-
"defined": {
|
|
2290
|
-
"name": "OrderDirection"
|
|
2291
|
-
}
|
|
2292
|
-
}
|
|
2293
|
-
}
|
|
2294
|
-
]
|
|
2295
|
-
}
|
|
2296
|
-
},
|
|
2297
2268
|
{
|
|
2298
2269
|
"name": "MarketBidOrderArgs",
|
|
2299
2270
|
"type": {
|
package/dist/types/trade.d.ts
CHANGED
|
@@ -1144,16 +1144,7 @@ export type TriadProtocol = {
|
|
|
1144
1144
|
address: '11111111111111111111111111111111';
|
|
1145
1145
|
}
|
|
1146
1146
|
];
|
|
1147
|
-
args: [
|
|
1148
|
-
{
|
|
1149
|
-
name: 'args';
|
|
1150
|
-
type: {
|
|
1151
|
-
defined: {
|
|
1152
|
-
name: 'marketAskOrderArgs';
|
|
1153
|
-
};
|
|
1154
|
-
};
|
|
1155
|
-
}
|
|
1156
|
-
];
|
|
1147
|
+
args: [];
|
|
1157
1148
|
},
|
|
1158
1149
|
{
|
|
1159
1150
|
name: 'marketBidOrder';
|
|
@@ -2995,26 +2986,6 @@ export type TriadProtocol = {
|
|
|
2995
2986
|
];
|
|
2996
2987
|
};
|
|
2997
2988
|
},
|
|
2998
|
-
{
|
|
2999
|
-
name: 'marketAskOrderArgs';
|
|
3000
|
-
type: {
|
|
3001
|
-
kind: 'struct';
|
|
3002
|
-
fields: [
|
|
3003
|
-
{
|
|
3004
|
-
name: 'shares';
|
|
3005
|
-
type: 'u64';
|
|
3006
|
-
},
|
|
3007
|
-
{
|
|
3008
|
-
name: 'orderDirection';
|
|
3009
|
-
type: {
|
|
3010
|
-
defined: {
|
|
3011
|
-
name: 'orderDirection';
|
|
3012
|
-
};
|
|
3013
|
-
};
|
|
3014
|
-
}
|
|
3015
|
-
];
|
|
3016
|
-
};
|
|
3017
|
-
},
|
|
3018
2989
|
{
|
|
3019
2990
|
name: 'marketBidOrderArgs';
|
|
3020
2991
|
type: {
|
|
@@ -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,82 @@
|
|
|
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=100`);
|
|
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
|
+
],
|
|
40
|
+
addressLookupTableAccounts: yield (0, exports.getAddressLookupTableAccounts)(connection, addressLookupTableAddresses),
|
|
41
|
+
setupInstructions: setupInstructions.map(deserializeInstruction),
|
|
42
|
+
cleanupInstruction: deserializeInstruction(cleanupInstruction),
|
|
43
|
+
trdAmount: quoteData.outAmount
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
exports.swap = swap;
|
|
47
|
+
const deserializeInstruction = (instruction) => {
|
|
48
|
+
return new web3_js_1.TransactionInstruction({
|
|
49
|
+
programId: new web3_js_1.PublicKey(instruction.programId),
|
|
50
|
+
keys: instruction.accounts.map((key) => ({
|
|
51
|
+
pubkey: new web3_js_1.PublicKey(key.pubkey),
|
|
52
|
+
isSigner: key.isSigner,
|
|
53
|
+
isWritable: key.isWritable
|
|
54
|
+
})),
|
|
55
|
+
data: Buffer.from(instruction.data, 'base64')
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
const getAddressLookupTableAccounts = (connection, keys) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
const addressLookupTableAccountInfos = yield connection.getMultipleAccountsInfo(keys.map((key) => new web3_js_1.PublicKey(key)));
|
|
60
|
+
return addressLookupTableAccountInfos.reduce((acc, accountInfo, index) => {
|
|
61
|
+
const addressLookupTableAddress = keys[index];
|
|
62
|
+
if (accountInfo) {
|
|
63
|
+
const addressLookupTableAccount = new web3_js_1.AddressLookupTableAccount({
|
|
64
|
+
key: new web3_js_1.PublicKey(addressLookupTableAddress),
|
|
65
|
+
state: web3_js_1.AddressLookupTableAccount.deserialize(accountInfo.data)
|
|
66
|
+
});
|
|
67
|
+
acc.push(addressLookupTableAccount);
|
|
68
|
+
}
|
|
69
|
+
return acc;
|
|
70
|
+
}, new Array());
|
|
71
|
+
});
|
|
72
|
+
exports.getAddressLookupTableAccounts = getAddressLookupTableAccounts;
|
|
73
|
+
const TOKENS = {
|
|
74
|
+
So11111111111111111111111111111111111111112: {
|
|
75
|
+
mint: 'So11111111111111111111111111111111111111112',
|
|
76
|
+
decimals: 9
|
|
77
|
+
},
|
|
78
|
+
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: {
|
|
79
|
+
mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
|
|
80
|
+
decimals: 6
|
|
81
|
+
}
|
|
82
|
+
};
|