@triadxyz/triad-protocol 2.1.4-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 +20 -17
- package/dist/types/idl_triad_protocol.json +8 -42
- package/dist/types/trade.d.ts +2 -0
- package/dist/types/triad_protocol.d.ts +8 -42
- package/dist/utils/helpers.js +2 -1
- 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)),
|
|
@@ -667,20 +685,6 @@ class Trade {
|
|
|
667
685
|
if (order.authority === this.program.provider.publicKey.toBase58()) {
|
|
668
686
|
return;
|
|
669
687
|
}
|
|
670
|
-
let linkedSellerBidTradePDA = null;
|
|
671
|
-
if (order.linkedBidOrderId !== '0') {
|
|
672
|
-
const userOrders = yield this.getUserOrders(new web3_js_1.PublicKey(order.authority));
|
|
673
|
-
linkedSellerBidTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority));
|
|
674
|
-
const linkedBidOrder = userOrders.find((item) => item.orderId === order.linkedBidOrderId &&
|
|
675
|
-
item.marketId === marketId.toString());
|
|
676
|
-
if (!linkedBidOrder) {
|
|
677
|
-
throw new Error('Linked bid order not found');
|
|
678
|
-
}
|
|
679
|
-
if (linkedBidOrder.userNonce !== '0') {
|
|
680
|
-
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), Number(linkedBidOrder.userNonce));
|
|
681
|
-
linkedSellerBidTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
688
|
const orderPrice = new bn_js_1.default(order.price);
|
|
685
689
|
const availableShares = new bn_js_1.default(order.totalShares);
|
|
686
690
|
let adjustedPrice = orderPrice;
|
|
@@ -719,8 +723,7 @@ class Trade {
|
|
|
719
723
|
tokenProgram,
|
|
720
724
|
sellerAuthority: new web3_js_1.PublicKey(order.authority),
|
|
721
725
|
marketAta: (0, pda_2.getTokenATA)(marketPDA, mint, tokenProgram),
|
|
722
|
-
buyerAta: (0, pda_2.getTokenATA)(this.program.provider.publicKey, mint, tokenProgram)
|
|
723
|
-
linkedSellerBidTrade: linkedSellerBidTradePDA
|
|
726
|
+
buyerAta: (0, pda_2.getTokenATA)(this.program.provider.publicKey, mint, tokenProgram)
|
|
724
727
|
})
|
|
725
728
|
.instruction());
|
|
726
729
|
remainingUSDC = remainingUSDC.sub(usdcAmount);
|
|
@@ -54,15 +54,15 @@
|
|
|
54
54
|
"signer": true
|
|
55
55
|
},
|
|
56
56
|
{
|
|
57
|
-
"name": "
|
|
57
|
+
"name": "market",
|
|
58
58
|
"writable": true
|
|
59
59
|
},
|
|
60
60
|
{
|
|
61
|
-
"name": "
|
|
61
|
+
"name": "bid_user_trade",
|
|
62
62
|
"writable": true
|
|
63
63
|
},
|
|
64
64
|
{
|
|
65
|
-
"name": "
|
|
65
|
+
"name": "ask_user_trade",
|
|
66
66
|
"writable": true
|
|
67
67
|
},
|
|
68
68
|
{
|
|
@@ -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",
|
|
@@ -856,6 +847,10 @@
|
|
|
856
847
|
"writable": true,
|
|
857
848
|
"signer": true
|
|
858
849
|
},
|
|
850
|
+
{
|
|
851
|
+
"name": "market",
|
|
852
|
+
"writable": true
|
|
853
|
+
},
|
|
859
854
|
{
|
|
860
855
|
"name": "squads",
|
|
861
856
|
"writable": true,
|
|
@@ -873,15 +868,6 @@
|
|
|
873
868
|
"name": "seller_trade",
|
|
874
869
|
"writable": true
|
|
875
870
|
},
|
|
876
|
-
{
|
|
877
|
-
"name": "linked_seller_bid_trade",
|
|
878
|
-
"writable": true,
|
|
879
|
-
"optional": true
|
|
880
|
-
},
|
|
881
|
-
{
|
|
882
|
-
"name": "market",
|
|
883
|
-
"writable": true
|
|
884
|
-
},
|
|
885
871
|
{
|
|
886
872
|
"name": "mint",
|
|
887
873
|
"writable": true
|
|
@@ -2279,26 +2265,6 @@
|
|
|
2279
2265
|
]
|
|
2280
2266
|
}
|
|
2281
2267
|
},
|
|
2282
|
-
{
|
|
2283
|
-
"name": "MarketAskOrderArgs",
|
|
2284
|
-
"type": {
|
|
2285
|
-
"kind": "struct",
|
|
2286
|
-
"fields": [
|
|
2287
|
-
{
|
|
2288
|
-
"name": "shares",
|
|
2289
|
-
"type": "u64"
|
|
2290
|
-
},
|
|
2291
|
-
{
|
|
2292
|
-
"name": "order_direction",
|
|
2293
|
-
"type": {
|
|
2294
|
-
"defined": {
|
|
2295
|
-
"name": "OrderDirection"
|
|
2296
|
-
}
|
|
2297
|
-
}
|
|
2298
|
-
}
|
|
2299
|
-
]
|
|
2300
|
-
}
|
|
2301
|
-
},
|
|
2302
2268
|
{
|
|
2303
2269
|
"name": "MarketBidOrderArgs",
|
|
2304
2270
|
"type": {
|
package/dist/types/trade.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export type Market = {
|
|
|
28
28
|
payoutFee: number;
|
|
29
29
|
customer: string;
|
|
30
30
|
version: number;
|
|
31
|
+
poolId: number;
|
|
31
32
|
};
|
|
32
33
|
export type UserTrade = {
|
|
33
34
|
user: string;
|
|
@@ -121,6 +122,7 @@ export type CreateCustomerArgs = {
|
|
|
121
122
|
export type OpenOrderArgs = {
|
|
122
123
|
marketId: number;
|
|
123
124
|
amount: number;
|
|
125
|
+
token: string;
|
|
124
126
|
direction: {
|
|
125
127
|
hype: {};
|
|
126
128
|
} | {
|
|
@@ -60,15 +60,15 @@ export type TriadProtocol = {
|
|
|
60
60
|
signer: true;
|
|
61
61
|
},
|
|
62
62
|
{
|
|
63
|
-
name: '
|
|
63
|
+
name: 'market';
|
|
64
64
|
writable: true;
|
|
65
65
|
},
|
|
66
66
|
{
|
|
67
|
-
name: '
|
|
67
|
+
name: 'bidUserTrade';
|
|
68
68
|
writable: true;
|
|
69
69
|
},
|
|
70
70
|
{
|
|
71
|
-
name: '
|
|
71
|
+
name: 'askUserTrade';
|
|
72
72
|
writable: true;
|
|
73
73
|
},
|
|
74
74
|
{
|
|
@@ -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';
|
|
@@ -1164,6 +1155,10 @@ export type TriadProtocol = {
|
|
|
1164
1155
|
writable: true;
|
|
1165
1156
|
signer: true;
|
|
1166
1157
|
},
|
|
1158
|
+
{
|
|
1159
|
+
name: 'market';
|
|
1160
|
+
writable: true;
|
|
1161
|
+
},
|
|
1167
1162
|
{
|
|
1168
1163
|
name: 'squads';
|
|
1169
1164
|
writable: true;
|
|
@@ -1181,15 +1176,6 @@ export type TriadProtocol = {
|
|
|
1181
1176
|
name: 'sellerTrade';
|
|
1182
1177
|
writable: true;
|
|
1183
1178
|
},
|
|
1184
|
-
{
|
|
1185
|
-
name: 'linkedSellerBidTrade';
|
|
1186
|
-
writable: true;
|
|
1187
|
-
optional: true;
|
|
1188
|
-
},
|
|
1189
|
-
{
|
|
1190
|
-
name: 'market';
|
|
1191
|
-
writable: true;
|
|
1192
|
-
},
|
|
1193
1179
|
{
|
|
1194
1180
|
name: 'mint';
|
|
1195
1181
|
writable: true;
|
|
@@ -3000,26 +2986,6 @@ export type TriadProtocol = {
|
|
|
3000
2986
|
];
|
|
3001
2987
|
};
|
|
3002
2988
|
},
|
|
3003
|
-
{
|
|
3004
|
-
name: 'marketAskOrderArgs';
|
|
3005
|
-
type: {
|
|
3006
|
-
kind: 'struct';
|
|
3007
|
-
fields: [
|
|
3008
|
-
{
|
|
3009
|
-
name: 'shares';
|
|
3010
|
-
type: 'u64';
|
|
3011
|
-
},
|
|
3012
|
-
{
|
|
3013
|
-
name: 'orderDirection';
|
|
3014
|
-
type: {
|
|
3015
|
-
defined: {
|
|
3016
|
-
name: 'orderDirection';
|
|
3017
|
-
};
|
|
3018
|
-
};
|
|
3019
|
-
}
|
|
3020
|
-
];
|
|
3021
|
-
};
|
|
3022
|
-
},
|
|
3023
2989
|
{
|
|
3024
2990
|
name: 'marketBidOrderArgs';
|
|
3025
2991
|
type: {
|
package/dist/utils/helpers.js
CHANGED
|
@@ -83,7 +83,8 @@ const formatMarket = (account, address) => {
|
|
|
83
83
|
: '0',
|
|
84
84
|
payoutFee: account.payoutFee,
|
|
85
85
|
customer: account.customerId === 0 ? 'Triad' : account.customerId.toString(),
|
|
86
|
-
version: account.version.toNumber()
|
|
86
|
+
version: account.version.toNumber(),
|
|
87
|
+
poolId: account.poolId.toNumber()
|
|
87
88
|
};
|
|
88
89
|
};
|
|
89
90
|
exports.formatMarket = formatMarket;
|
|
@@ -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
|
+
};
|