@triadxyz/triad-protocol 3.3.8-beta → 3.3.9-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/index.d.ts +3 -5
- package/dist/index.js +27 -40
- package/dist/types/idl_triad_protocol.json +4 -0
- package/dist/types/triad_protocol.d.ts +4 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -11,8 +11,6 @@ export * from './types';
|
|
|
11
11
|
export * from './utils/helpers';
|
|
12
12
|
export * from './utils/merkle';
|
|
13
13
|
export * from './utils/feeCalculator';
|
|
14
|
-
export * from './utils/constants';
|
|
15
|
-
export * from './utils/pda';
|
|
16
14
|
export default class TriadProtocol {
|
|
17
15
|
private connection;
|
|
18
16
|
private wallet;
|
|
@@ -330,7 +328,7 @@ export default class TriadProtocol {
|
|
|
330
328
|
/**
|
|
331
329
|
* Get User Trade Ixs
|
|
332
330
|
*/
|
|
333
|
-
getUserTradeIxs(
|
|
331
|
+
getUserTradeIxs(): Promise<{
|
|
334
332
|
userTradePDA: PublicKey;
|
|
335
333
|
ixs: TransactionInstruction[];
|
|
336
334
|
nonce: number;
|
|
@@ -344,7 +342,7 @@ export default class TriadProtocol {
|
|
|
344
342
|
* @param marketId - The ID of the Market
|
|
345
343
|
* @param orderDirection - The direction of the Order
|
|
346
344
|
*/
|
|
347
|
-
getUserTradeNonce(
|
|
345
|
+
getUserTradeNonce(marketId: number, orderDirection: OrderDirection): Promise<{
|
|
348
346
|
userTradePDA: PublicKey;
|
|
349
347
|
userTradeIxs: TransactionInstruction[];
|
|
350
348
|
}>;
|
|
@@ -361,8 +359,8 @@ export default class TriadProtocol {
|
|
|
361
359
|
placeBidOrder({ orders, isTrdPayout }: PlaceBidOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
362
360
|
/**
|
|
363
361
|
* Place Ask Order
|
|
362
|
+
* @param args.marketId - The ID of the Market
|
|
364
363
|
* @param args.orders - Array of orders to execute
|
|
365
|
-
* @param args.orders.marketId - The ID of the Market
|
|
366
364
|
* @param args.orders.amount - The amount of the Order
|
|
367
365
|
* @param args.orders.price - The price of the Order
|
|
368
366
|
* @param args.orders.bidOrderId - The ID of the Bid Order
|
package/dist/index.js
CHANGED
|
@@ -46,8 +46,6 @@ __exportStar(require("./types"), exports);
|
|
|
46
46
|
__exportStar(require("./utils/helpers"), exports);
|
|
47
47
|
__exportStar(require("./utils/merkle"), exports);
|
|
48
48
|
__exportStar(require("./utils/feeCalculator"), exports);
|
|
49
|
-
__exportStar(require("./utils/constants"), exports);
|
|
50
|
-
__exportStar(require("./utils/pda"), exports);
|
|
51
49
|
class TriadProtocol {
|
|
52
50
|
constructor(connection, wallet, rpcOptions) {
|
|
53
51
|
this.connection = connection;
|
|
@@ -527,7 +525,7 @@ class TriadProtocol {
|
|
|
527
525
|
return __awaiter(this, void 0, void 0, function* () {
|
|
528
526
|
const ixs = [];
|
|
529
527
|
const addressLookupTableAccounts = [];
|
|
530
|
-
const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs(
|
|
528
|
+
const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
|
|
531
529
|
if (userTradeIxs.length > 0) {
|
|
532
530
|
ixs.push(...userTradeIxs);
|
|
533
531
|
}
|
|
@@ -660,21 +658,21 @@ class TriadProtocol {
|
|
|
660
658
|
/**
|
|
661
659
|
* Get User Trade Ixs
|
|
662
660
|
*/
|
|
663
|
-
getUserTradeIxs(
|
|
661
|
+
getUserTradeIxs() {
|
|
664
662
|
return __awaiter(this, void 0, void 0, function* () {
|
|
665
663
|
const ixs = [];
|
|
666
664
|
let myUserTrades = [];
|
|
667
|
-
myUserTrades = yield this.getMyUserTrades(
|
|
665
|
+
myUserTrades = yield this.getMyUserTrades(this.program.provider.publicKey);
|
|
668
666
|
if (myUserTrades.length === 0) {
|
|
669
667
|
ixs.push(yield this.program.methods
|
|
670
668
|
.createUserTrade()
|
|
671
669
|
.accounts({
|
|
672
|
-
signer:
|
|
670
|
+
signer: this.program.provider.publicKey,
|
|
673
671
|
payer: this.rpcOptions.payer
|
|
674
672
|
})
|
|
675
673
|
.instruction());
|
|
676
674
|
return {
|
|
677
|
-
userTradePDA: this.getUserPDA(
|
|
675
|
+
userTradePDA: this.getUserPDA(this.program.provider.publicKey),
|
|
678
676
|
ixs,
|
|
679
677
|
nonce: 0
|
|
680
678
|
};
|
|
@@ -695,17 +693,17 @@ class TriadProtocol {
|
|
|
695
693
|
throw new Error('No open orders found');
|
|
696
694
|
}
|
|
697
695
|
return {
|
|
698
|
-
userTradePDA: this.getUserPDA(
|
|
696
|
+
userTradePDA: this.getUserPDA(this.program.provider.publicKey, nonce),
|
|
699
697
|
ixs
|
|
700
698
|
};
|
|
701
699
|
}
|
|
702
700
|
catch (_a) {
|
|
703
701
|
const mainUserTrade = myUserTrades.find((trade) => !trade.isSubUser);
|
|
704
|
-
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId,
|
|
702
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, Number(mainUserTrade.nonce) + 1);
|
|
705
703
|
ixs.push(yield this.program.methods
|
|
706
704
|
.createSubUserTrade(subUserTradePDA)
|
|
707
705
|
.accounts({
|
|
708
|
-
signer:
|
|
706
|
+
signer: this.program.provider.publicKey,
|
|
709
707
|
payer: this.rpcOptions.payer
|
|
710
708
|
})
|
|
711
709
|
.instruction());
|
|
@@ -721,20 +719,20 @@ class TriadProtocol {
|
|
|
721
719
|
* @param marketId - The ID of the Market
|
|
722
720
|
* @param orderDirection - The direction of the Order
|
|
723
721
|
*/
|
|
724
|
-
getUserTradeNonce(
|
|
722
|
+
getUserTradeNonce(marketId, orderDirection) {
|
|
725
723
|
var _a;
|
|
726
724
|
return __awaiter(this, void 0, void 0, function* () {
|
|
727
|
-
const userOrders = yield this.getUserOrdersByMarketId(
|
|
725
|
+
const userOrders = yield this.getUserOrdersByMarketId(this.program.provider.publicKey, marketId);
|
|
728
726
|
const userNonce = (_a = userOrders.find((order) => order.orderDirection === orderDirection &&
|
|
729
727
|
order.orderStatus === types_1.OrderStatus.OPEN &&
|
|
730
728
|
Number(order.marketId) === marketId)) === null || _a === void 0 ? void 0 : _a.userNonce;
|
|
731
729
|
let userTradePDA = null;
|
|
732
730
|
let userTradeIxs = [];
|
|
733
731
|
if (userNonce) {
|
|
734
|
-
userTradePDA = this.getUserPDA(
|
|
732
|
+
userTradePDA = this.getUserPDA(this.program.provider.publicKey, Number(userNonce));
|
|
735
733
|
}
|
|
736
734
|
if (!userNonce) {
|
|
737
|
-
const { userTradePDA: user, ixs: ixsUser } = yield this.getUserTradeIxs(
|
|
735
|
+
const { userTradePDA: user, ixs: ixsUser } = yield this.getUserTradeIxs();
|
|
738
736
|
userTradePDA = user;
|
|
739
737
|
userTradeIxs = ixsUser;
|
|
740
738
|
}
|
|
@@ -755,8 +753,8 @@ class TriadProtocol {
|
|
|
755
753
|
return __awaiter(this, void 0, void 0, function* () {
|
|
756
754
|
const ixs = [];
|
|
757
755
|
const addressLookupTableAccounts = [];
|
|
758
|
-
if (orders.length >
|
|
759
|
-
throw new Error('You can only place up to
|
|
756
|
+
if (orders.length > 4) {
|
|
757
|
+
throw new Error('You can only place up to 4 orders at a time');
|
|
760
758
|
}
|
|
761
759
|
let amountInUSDC = new bn_js_1.default(0);
|
|
762
760
|
let totalAmount = 0;
|
|
@@ -785,6 +783,10 @@ class TriadProtocol {
|
|
|
785
783
|
return;
|
|
786
784
|
}
|
|
787
785
|
}
|
|
786
|
+
const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(orders[0].marketId, Object.keys(orders[0].orderDirection)[0]);
|
|
787
|
+
if (userTradeIxs.length > 0) {
|
|
788
|
+
ixs.push(...userTradeIxs);
|
|
789
|
+
}
|
|
788
790
|
for (const order of orders) {
|
|
789
791
|
ixs.push(yield this.program.methods
|
|
790
792
|
.placeBidOrder({
|
|
@@ -796,7 +798,7 @@ class TriadProtocol {
|
|
|
796
798
|
signer: this.program.provider.publicKey,
|
|
797
799
|
market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
|
|
798
800
|
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId),
|
|
799
|
-
userTrade:
|
|
801
|
+
userTrade: userTradePDA
|
|
800
802
|
})
|
|
801
803
|
.instruction());
|
|
802
804
|
}
|
|
@@ -805,8 +807,8 @@ class TriadProtocol {
|
|
|
805
807
|
}
|
|
806
808
|
/**
|
|
807
809
|
* Place Ask Order
|
|
810
|
+
* @param args.marketId - The ID of the Market
|
|
808
811
|
* @param args.orders - Array of orders to execute
|
|
809
|
-
* @param args.orders.marketId - The ID of the Market
|
|
810
812
|
* @param args.orders.amount - The amount of the Order
|
|
811
813
|
* @param args.orders.price - The price of the Order
|
|
812
814
|
* @param args.orders.bidOrderId - The ID of the Bid Order
|
|
@@ -845,8 +847,8 @@ class TriadProtocol {
|
|
|
845
847
|
cancelBidOrder({ orders }) {
|
|
846
848
|
return __awaiter(this, void 0, void 0, function* () {
|
|
847
849
|
const ixs = [];
|
|
848
|
-
if (orders.length >
|
|
849
|
-
throw new Error('You can only cancel up to
|
|
850
|
+
if (orders.length > 5) {
|
|
851
|
+
throw new Error('You can only cancel up to 5 orders at a time');
|
|
850
852
|
}
|
|
851
853
|
for (const order of orders) {
|
|
852
854
|
ixs.push(yield this.program.methods
|
|
@@ -858,7 +860,8 @@ class TriadProtocol {
|
|
|
858
860
|
signer: this.program.provider.publicKey,
|
|
859
861
|
payer: this.rpcOptions.payer,
|
|
860
862
|
market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
|
|
861
|
-
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId)
|
|
863
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId),
|
|
864
|
+
userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce)
|
|
862
865
|
})
|
|
863
866
|
.instruction());
|
|
864
867
|
}
|
|
@@ -907,8 +910,7 @@ class TriadProtocol {
|
|
|
907
910
|
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
908
911
|
const ixs = [];
|
|
909
912
|
const addressLookupTableAccounts = [];
|
|
910
|
-
const
|
|
911
|
-
const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(this.program.provider.publicKey, marketId, Object.keys(orderDirection)[0]);
|
|
913
|
+
const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(orderDirection)[0]);
|
|
912
914
|
const orderBook = yield this.getOrderBook(marketId);
|
|
913
915
|
let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, constants_1.BASE_DECIMALS));
|
|
914
916
|
if (isTrdPayout) {
|
|
@@ -986,10 +988,6 @@ class TriadProtocol {
|
|
|
986
988
|
.div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS)));
|
|
987
989
|
if (usdcAmount.lte(new bn_js_1.default(0)))
|
|
988
990
|
continue;
|
|
989
|
-
const { userTradePDA: sellerTradePDA, userTradeIxs: innerSellerTradeIxs } = yield this.getUserTradeNonce(new web3_js_1.PublicKey(order.authority), marketId, Object.keys(orderDirection)[0]);
|
|
990
|
-
if (innerSellerTradeIxs.length > 0 && sellerTradeIxs.length === 0) {
|
|
991
|
-
sellerTradeIxs.push(...innerSellerTradeIxs);
|
|
992
|
-
}
|
|
993
991
|
ixs.push(yield this.program.methods
|
|
994
992
|
.marketBidOrder({
|
|
995
993
|
amount: new bn_js_1.default(usdcAmount),
|
|
@@ -1003,16 +1001,13 @@ class TriadProtocol {
|
|
|
1003
1001
|
buyerTrade: userTradePDA,
|
|
1004
1002
|
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
|
|
1005
1003
|
sellerAuthority: new web3_js_1.PublicKey(order.authority),
|
|
1006
|
-
sellerTrade:
|
|
1004
|
+
sellerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
|
|
1007
1005
|
marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
|
|
1008
1006
|
buyerAta: (0, pda_1.getTokenATA)(this.program.provider.publicKey, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID)
|
|
1009
1007
|
})
|
|
1010
1008
|
.instruction());
|
|
1011
1009
|
remainingUSDC = remainingUSDC.sub(usdcAmount);
|
|
1012
1010
|
}
|
|
1013
|
-
if (sellerTradeIxs.length > 0) {
|
|
1014
|
-
ixs.unshift(...sellerTradeIxs);
|
|
1015
|
-
}
|
|
1016
1011
|
if (userTradeIxs.length > 0) {
|
|
1017
1012
|
ixs.unshift(...userTradeIxs);
|
|
1018
1013
|
}
|
|
@@ -1037,7 +1032,6 @@ class TriadProtocol {
|
|
|
1037
1032
|
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
1038
1033
|
const ixs = [];
|
|
1039
1034
|
const addressLookupTableAccounts = [];
|
|
1040
|
-
const buyerTradeIxs = [];
|
|
1041
1035
|
const orderBook = yield this.getOrderBook(marketId);
|
|
1042
1036
|
const bidOrders = Object.keys(orderDirection)[0] === 'hype'
|
|
1043
1037
|
? orderBook.hype.bid
|
|
@@ -1062,10 +1056,6 @@ class TriadProtocol {
|
|
|
1062
1056
|
const effectivePriceDecimal = (0, feeCalculator_1.applySellFee)(orderPrice.toNumber() / 1000000);
|
|
1063
1057
|
const adjustedPrice = new bn_js_1.default(Math.floor(effectivePriceDecimal * 1000000));
|
|
1064
1058
|
amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS))));
|
|
1065
|
-
const { userTradePDA: buyerTradePDA, userTradeIxs: innerBuyerTradeIxs } = yield this.getUserTradeNonce(new web3_js_1.PublicKey(order.authority), marketId, Object.keys(orderDirection)[0]);
|
|
1066
|
-
if (innerBuyerTradeIxs.length > 0 && buyerTradeIxs.length === 0) {
|
|
1067
|
-
buyerTradeIxs.push(...innerBuyerTradeIxs);
|
|
1068
|
-
}
|
|
1069
1059
|
ixs.push(yield this.program.methods
|
|
1070
1060
|
.marketAskOrder({
|
|
1071
1061
|
shares: new bn_js_1.default(sharesToSell),
|
|
@@ -1078,7 +1068,7 @@ class TriadProtocol {
|
|
|
1078
1068
|
payer: this.rpcOptions.payer,
|
|
1079
1069
|
market: marketPDA,
|
|
1080
1070
|
buyerAuthority: new web3_js_1.PublicKey(order.authority),
|
|
1081
|
-
buyerTrade:
|
|
1071
|
+
buyerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
|
|
1082
1072
|
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
|
|
1083
1073
|
marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
|
|
1084
1074
|
sellerTrade: this.getUserPDA(this.program.provider.publicKey, inputOrder.userNonce)
|
|
@@ -1101,9 +1091,6 @@ class TriadProtocol {
|
|
|
1101
1091
|
addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
|
|
1102
1092
|
}
|
|
1103
1093
|
}
|
|
1104
|
-
if (buyerTradeIxs.length > 0) {
|
|
1105
|
-
ixs.unshift(...buyerTradeIxs);
|
|
1106
|
-
}
|
|
1107
1094
|
if (ixs.length === 0) {
|
|
1108
1095
|
throw new Error('No matching orders found to fill the requested amount');
|
|
1109
1096
|
}
|