@triadxyz/triad-protocol 1.7.9-beta → 1.8.1-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.js +45 -43
- package/package.json +1 -1
package/dist/trade.js
CHANGED
|
@@ -12,6 +12,7 @@ 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
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
15
16
|
const trade_1 = require("./types/trade");
|
|
16
17
|
const bn_js_1 = __importDefault(require("bn.js"));
|
|
17
18
|
const constants_1 = require("./utils/constants");
|
|
@@ -153,35 +154,12 @@ class Trade {
|
|
|
153
154
|
openOrder({ marketId, amount, direction, mint }, options) {
|
|
154
155
|
return __awaiter(this, void 0, void 0, function* () {
|
|
155
156
|
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
156
|
-
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
157
157
|
const ixs = [];
|
|
158
158
|
const addressLookupTableAccounts = [];
|
|
159
159
|
let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
ixs.push(yield this.program.methods
|
|
164
|
-
.createUserTrade()
|
|
165
|
-
.accounts({
|
|
166
|
-
signer: this.provider.publicKey
|
|
167
|
-
})
|
|
168
|
-
.instruction());
|
|
169
|
-
}
|
|
170
|
-
if (myUserTrades.length > 0) {
|
|
171
|
-
try {
|
|
172
|
-
userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
|
|
173
|
-
}
|
|
174
|
-
catch (_a) {
|
|
175
|
-
const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
|
|
176
|
-
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
|
|
177
|
-
userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
178
|
-
ixs.push(yield this.program.methods
|
|
179
|
-
.createSubUserTrade(subUserTradePDA)
|
|
180
|
-
.accounts({
|
|
181
|
-
signer: this.provider.publicKey
|
|
182
|
-
})
|
|
183
|
-
.instruction());
|
|
184
|
-
}
|
|
160
|
+
const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
|
|
161
|
+
if (userTradeIxs.length > 0) {
|
|
162
|
+
ixs.push(...userTradeIxs);
|
|
185
163
|
}
|
|
186
164
|
ixs.push(yield this.program.methods
|
|
187
165
|
.openOrder({
|
|
@@ -516,7 +494,8 @@ class Trade {
|
|
|
516
494
|
signer: this.provider.publicKey,
|
|
517
495
|
market: marketPDA,
|
|
518
496
|
userTrade: userTradePDA,
|
|
519
|
-
mint
|
|
497
|
+
mint,
|
|
498
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(mint)
|
|
520
499
|
}), options);
|
|
521
500
|
}
|
|
522
501
|
return (0, sendTransactionWithOptions_1.default)(this.program.methods.cancelAskOrder(new bn_js_1.default(orderId)).accounts({
|
|
@@ -541,22 +520,45 @@ class Trade {
|
|
|
541
520
|
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
542
521
|
const ixs = [];
|
|
543
522
|
const { userTradePDA: buyerTrade, ixs: userTradeIxs } = yield this.getUserTradeIxs();
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
523
|
+
const orderBook = yield this.getOrderBook(marketId);
|
|
524
|
+
let remainingUSDC = amount * Math.pow(10, this.decimals);
|
|
525
|
+
const orders = direction[0].hype ? orderBook.hype.ask : orderBook.flop.ask;
|
|
526
|
+
const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
|
|
527
|
+
for (const order of sortedOrders) {
|
|
528
|
+
if (remainingUSDC <= 0)
|
|
529
|
+
break;
|
|
530
|
+
const orderPrice = Number(order.price);
|
|
531
|
+
const maxSharesForPrice = remainingUSDC / orderPrice;
|
|
532
|
+
const availableShares = Number(order.totalShares);
|
|
533
|
+
const sharesToBuy = Math.min(maxSharesForPrice, availableShares);
|
|
534
|
+
const usdcAmount = sharesToBuy * orderPrice;
|
|
535
|
+
let sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority));
|
|
536
|
+
if (order.userNonce !== '0') {
|
|
537
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), Number(order.userNonce));
|
|
538
|
+
sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
539
|
+
}
|
|
540
|
+
ixs.push(yield this.program.methods
|
|
541
|
+
.marketBidOrder({
|
|
542
|
+
amount: new bn_js_1.default(usdcAmount),
|
|
543
|
+
orderDirection: direction
|
|
544
|
+
})
|
|
545
|
+
.accounts({
|
|
546
|
+
signer: this.provider.publicKey,
|
|
547
|
+
market: marketPDA,
|
|
548
|
+
buyerTrade,
|
|
549
|
+
sellerTrade: sellerTradePDA,
|
|
550
|
+
mint,
|
|
551
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(mint)
|
|
552
|
+
})
|
|
553
|
+
.instruction());
|
|
554
|
+
remainingUSDC -= usdcAmount;
|
|
555
|
+
}
|
|
556
|
+
if (userTradeIxs.length > 0) {
|
|
557
|
+
ixs.unshift(...userTradeIxs);
|
|
558
|
+
}
|
|
559
|
+
if (ixs.length === 0) {
|
|
560
|
+
throw new Error('No matching orders found to fill the requested amount');
|
|
561
|
+
}
|
|
560
562
|
return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
|
|
561
563
|
});
|
|
562
564
|
}
|