@triadxyz/triad-protocol 4.0.1 → 4.0.3
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/customer.d.ts +39 -0
- package/dist/customer.js +98 -0
- package/dist/index.d.ts +13 -123
- package/dist/index.js +10 -438
- package/dist/predictor.d.ts +32 -0
- package/dist/predictor.js +109 -0
- package/dist/stake.d.ts +3 -3
- package/dist/trade.d.ts +88 -0
- package/dist/trade.js +345 -0
- package/dist/types/customer.d.ts +22 -0
- package/dist/types/customer.js +2 -0
- package/dist/types/idl_triad_protocol.json +3 -3
- package/dist/types/index.d.ts +1 -95
- package/dist/types/predictor.d.ts +20 -0
- package/dist/types/predictor.js +2 -0
- package/dist/types/stake.d.ts +34 -0
- package/dist/types/stake.js +2 -0
- package/dist/types/trade.d.ts +45 -0
- package/dist/types/trade.js +2 -0
- package/dist/types/triad_protocol.d.ts +3 -3
- package/dist/utils/constants.d.ts +2 -0
- package/dist/utils/constants.js +2 -1
- package/dist/utils/feeCalculator.js +1 -1
- package/dist/utils/helpers.d.ts +6 -2
- package/dist/utils/helpers.js +11 -1
- package/dist/utils/pda.d.ts +2 -1
- package/dist/utils/pda.js +2 -2
- package/package.json +1 -1
package/dist/trade.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/// <reference types="@coral-xyz/anchor/node_modules/@solana/web3.js" />
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { Program } from '@coral-xyz/anchor';
|
|
4
|
+
import { TriadProtocol } from './types/triad_protocol';
|
|
5
|
+
import { RpcOptions, OrderDirectionEncoded, BookOrder } from './types';
|
|
6
|
+
import { MarketBidOrderArgs, CancelBidOrderArgs, CancelAskOrderArgs, PlaceBidOrderArgs, PlaceAskOrderArgs, MarketAskOrderArgs } from './types/trade';
|
|
7
|
+
export default class Trade {
|
|
8
|
+
private program;
|
|
9
|
+
private rpcOptions;
|
|
10
|
+
constructor(program: Program<TriadProtocol>, rpcOptions: RpcOptions);
|
|
11
|
+
/**
|
|
12
|
+
* Get Orders By Market ID
|
|
13
|
+
* @param marketId - Market ID
|
|
14
|
+
*/
|
|
15
|
+
getOrderBook(marketId: number): Promise<{
|
|
16
|
+
marketId: number;
|
|
17
|
+
rewardsAvailable: string;
|
|
18
|
+
rewardsClaimed: string;
|
|
19
|
+
spreadToReward: string;
|
|
20
|
+
hype: {
|
|
21
|
+
bid: BookOrder[];
|
|
22
|
+
ask: BookOrder[];
|
|
23
|
+
};
|
|
24
|
+
flop: {
|
|
25
|
+
bid: BookOrder[];
|
|
26
|
+
ask: BookOrder[];
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* Place Bid Order
|
|
31
|
+
* @param args.orders - Array of orders to execute
|
|
32
|
+
* @param args.orders.marketId - The ID of the Market
|
|
33
|
+
* @param args.orders.amount - The amount of the Order
|
|
34
|
+
* @param args.orders.price - The price of the Order
|
|
35
|
+
* @param args.orders.orderDirection - The direction of the Order
|
|
36
|
+
*/
|
|
37
|
+
placeBidOrder({ orders }: PlaceBidOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
38
|
+
/**
|
|
39
|
+
* Place Ask Order
|
|
40
|
+
* @param args.orders - Array of orders to execute
|
|
41
|
+
* @param args.orders.amount - The amount of the Order
|
|
42
|
+
* @param args.orders.price - The price of the Order
|
|
43
|
+
* @param args.orders.marketId - The Id from the market
|
|
44
|
+
* @param args.orders.orderDirection - The direction of the Order
|
|
45
|
+
*/
|
|
46
|
+
placeAskOrder({ orders }: PlaceAskOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
47
|
+
/**
|
|
48
|
+
* Cancel Bid Order
|
|
49
|
+
* @param args.orders.bookOrderId - The ID of the Book Order
|
|
50
|
+
* @param args.orders.marketId - The ID of the Market
|
|
51
|
+
* @param args.orders.orderDirection - The direction of the Order
|
|
52
|
+
*/
|
|
53
|
+
cancelBidOrder({ orders }: CancelBidOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
54
|
+
/**
|
|
55
|
+
* Cancel Ask Order
|
|
56
|
+
* @param args.orders.marketId - The ID of the Market
|
|
57
|
+
* @param args.orders.authority - The authority of the order
|
|
58
|
+
* @param args.orders.bookOrderId - The ID of the Book Order
|
|
59
|
+
* @param args.orders.orderDirection - The direction of the Order
|
|
60
|
+
*/
|
|
61
|
+
cancelAskOrder({ orders }: CancelAskOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
62
|
+
/**
|
|
63
|
+
* Market Bid Order
|
|
64
|
+
* @param args.marketId - The ID of the Market
|
|
65
|
+
* @param args.amount - The amount of the Order
|
|
66
|
+
* @param args.orderDirection - The direction of the Order
|
|
67
|
+
*/
|
|
68
|
+
marketBidOrder({ marketId, amount, orderDirection }: MarketBidOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
69
|
+
/**
|
|
70
|
+
* Market Ask Order
|
|
71
|
+
* @param args.marketId - The ID of the Market
|
|
72
|
+
* @param args.shares - The amount of shares to sell
|
|
73
|
+
* @param args.bookOrderBidId - The ID of the Bid Order
|
|
74
|
+
* @param args.orderDirection - The direction of the Order
|
|
75
|
+
*/
|
|
76
|
+
marketAskOrder({ marketId, shares, orderDirection }: MarketAskOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
77
|
+
/**
|
|
78
|
+
* Payout Order
|
|
79
|
+
* @param args.marketId - The ID of the Market
|
|
80
|
+
* @param args.authority - The authority of the order
|
|
81
|
+
* @param args.orderDirection - The direction of the Order to Payout
|
|
82
|
+
*/
|
|
83
|
+
payoutOrder(orders: {
|
|
84
|
+
marketId: number;
|
|
85
|
+
orderDirection: OrderDirectionEncoded;
|
|
86
|
+
authority: PublicKey;
|
|
87
|
+
}[]): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
88
|
+
}
|
package/dist/trade.js
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
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
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
16
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
17
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
18
|
+
const helpers_1 = require("./utils/helpers");
|
|
19
|
+
const types_1 = require("./types");
|
|
20
|
+
const constants_1 = require("./utils/constants");
|
|
21
|
+
const pda_1 = require("./utils/pda");
|
|
22
|
+
const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
|
|
23
|
+
const feeCalculator_1 = require("./utils/feeCalculator");
|
|
24
|
+
class Trade {
|
|
25
|
+
constructor(program, rpcOptions) {
|
|
26
|
+
this.program = program;
|
|
27
|
+
this.rpcOptions = rpcOptions;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get Orders By Market ID
|
|
31
|
+
* @param marketId - Market ID
|
|
32
|
+
*/
|
|
33
|
+
getOrderBook(marketId) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
const data = {
|
|
36
|
+
marketId: marketId,
|
|
37
|
+
rewardsAvailable: '0',
|
|
38
|
+
rewardsClaimed: '0',
|
|
39
|
+
spreadToReward: '0',
|
|
40
|
+
hype: { bid: [], ask: [] },
|
|
41
|
+
flop: { bid: [], ask: [] }
|
|
42
|
+
};
|
|
43
|
+
const orderBookPDA = (0, pda_1.getOrderBookPDA)(this.program.programId, marketId);
|
|
44
|
+
const orderBook = yield this.program.account.orderBook.fetch(orderBookPDA);
|
|
45
|
+
data.rewardsAvailable = orderBook.rewardsAvailable.toString();
|
|
46
|
+
data.rewardsClaimed = orderBook.rewardsClaimed.toString();
|
|
47
|
+
data.spreadToReward = orderBook.spreadToReward.toString();
|
|
48
|
+
const processOrders = (orders, side) => {
|
|
49
|
+
for (const order of orders) {
|
|
50
|
+
if (order.price.eq(new bn_js_1.default(0)))
|
|
51
|
+
continue;
|
|
52
|
+
if ((0, helpers_1.getOrderSideFromNumber)(order.orderSide) === types_1.OrderSide.BID) {
|
|
53
|
+
data[side].bid.push((0, helpers_1.formatBookOrder)(order, marketId));
|
|
54
|
+
}
|
|
55
|
+
if ((0, helpers_1.getOrderSideFromNumber)(order.orderSide) === types_1.OrderSide.ASK) {
|
|
56
|
+
data[side].ask.push((0, helpers_1.formatBookOrder)(order, marketId));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
processOrders(orderBook.hypeOrders, 'hype');
|
|
61
|
+
processOrders(orderBook.flopOrders, 'flop');
|
|
62
|
+
return data;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Place Bid Order
|
|
67
|
+
* @param args.orders - Array of orders to execute
|
|
68
|
+
* @param args.orders.marketId - The ID of the Market
|
|
69
|
+
* @param args.orders.amount - The amount of the Order
|
|
70
|
+
* @param args.orders.price - The price of the Order
|
|
71
|
+
* @param args.orders.orderDirection - The direction of the Order
|
|
72
|
+
*/
|
|
73
|
+
placeBidOrder({ orders }) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
const ixs = [];
|
|
76
|
+
if (orders.length > 6) {
|
|
77
|
+
throw new Error('Max 6 orders per transaction');
|
|
78
|
+
}
|
|
79
|
+
for (const order of orders) {
|
|
80
|
+
ixs.push(yield this.program.methods
|
|
81
|
+
.placeBidOrder({
|
|
82
|
+
amount: new bn_js_1.default(order.amount * Math.pow(10, constants_1.BASE_DECIMALS)),
|
|
83
|
+
price: new bn_js_1.default(order.price * Math.pow(10, constants_1.BASE_DECIMALS)),
|
|
84
|
+
marketId: new bn_js_1.default(order.marketId),
|
|
85
|
+
orderDirection: order.orderDirection
|
|
86
|
+
})
|
|
87
|
+
.accounts({
|
|
88
|
+
signer: this.program.provider.publicKey,
|
|
89
|
+
payer: this.rpcOptions.payer,
|
|
90
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
|
|
91
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId),
|
|
92
|
+
order: (0, pda_1.getOrderPDA)(this.program.programId, this.program.provider.publicKey, order.marketId, (0, helpers_1.getOrderDirection)(order.orderDirection))
|
|
93
|
+
})
|
|
94
|
+
.instruction());
|
|
95
|
+
}
|
|
96
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Place Ask Order
|
|
101
|
+
* @param args.orders - Array of orders to execute
|
|
102
|
+
* @param args.orders.amount - The amount of the Order
|
|
103
|
+
* @param args.orders.price - The price of the Order
|
|
104
|
+
* @param args.orders.marketId - The Id from the market
|
|
105
|
+
* @param args.orders.orderDirection - The direction of the Order
|
|
106
|
+
*/
|
|
107
|
+
placeAskOrder({ orders }) {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
const ixs = [];
|
|
110
|
+
if (orders.length > 6) {
|
|
111
|
+
throw new Error('Max 6 orders per transaction');
|
|
112
|
+
}
|
|
113
|
+
for (const order of orders) {
|
|
114
|
+
ixs.push(yield this.program.methods
|
|
115
|
+
.placeAskOrder({
|
|
116
|
+
shares: new bn_js_1.default(order.amount * Math.pow(10, constants_1.BASE_DECIMALS)),
|
|
117
|
+
price: new bn_js_1.default(order.price * Math.pow(10, constants_1.BASE_DECIMALS)),
|
|
118
|
+
orderDirection: order.orderDirection
|
|
119
|
+
})
|
|
120
|
+
.accounts({
|
|
121
|
+
signer: this.program.provider.publicKey,
|
|
122
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
|
|
123
|
+
order: (0, pda_1.getOrderPDA)(this.program.programId, this.program.provider.publicKey, order.marketId, (0, helpers_1.getOrderDirection)(order.orderDirection)),
|
|
124
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId)
|
|
125
|
+
})
|
|
126
|
+
.instruction());
|
|
127
|
+
}
|
|
128
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Cancel Bid Order
|
|
133
|
+
* @param args.orders.bookOrderId - The ID of the Book Order
|
|
134
|
+
* @param args.orders.marketId - The ID of the Market
|
|
135
|
+
* @param args.orders.orderDirection - The direction of the Order
|
|
136
|
+
*/
|
|
137
|
+
cancelBidOrder({ orders }) {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
const ixs = [];
|
|
140
|
+
if (orders.length > 6) {
|
|
141
|
+
throw new Error('You can only cancel up to 6 orders at a time');
|
|
142
|
+
}
|
|
143
|
+
for (const order of orders) {
|
|
144
|
+
ixs.push(yield this.program.methods
|
|
145
|
+
.cancelBidOrder({
|
|
146
|
+
marketId: new bn_js_1.default(order.marketId),
|
|
147
|
+
bookOrderId: new bn_js_1.default(order.bookOrderId),
|
|
148
|
+
orderDirection: order.orderDirection
|
|
149
|
+
})
|
|
150
|
+
.accounts({
|
|
151
|
+
signer: new web3_js_1.PublicKey(order.authority),
|
|
152
|
+
payer: this.rpcOptions.payer,
|
|
153
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
|
|
154
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId)
|
|
155
|
+
})
|
|
156
|
+
.instruction());
|
|
157
|
+
}
|
|
158
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Cancel Ask Order
|
|
163
|
+
* @param args.orders.marketId - The ID of the Market
|
|
164
|
+
* @param args.orders.authority - The authority of the order
|
|
165
|
+
* @param args.orders.bookOrderId - The ID of the Book Order
|
|
166
|
+
* @param args.orders.orderDirection - The direction of the Order
|
|
167
|
+
*/
|
|
168
|
+
cancelAskOrder({ orders }) {
|
|
169
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
const ixs = [];
|
|
171
|
+
if (orders.length > 5) {
|
|
172
|
+
throw new Error('Max 5 orders per transaction');
|
|
173
|
+
}
|
|
174
|
+
for (const order of orders) {
|
|
175
|
+
ixs.push(yield this.program.methods
|
|
176
|
+
.cancelAskOrder({
|
|
177
|
+
marketId: new bn_js_1.default(order.marketId),
|
|
178
|
+
bookOrderId: new bn_js_1.default(order.bookOrderId),
|
|
179
|
+
orderDirection: order.orderDirection
|
|
180
|
+
})
|
|
181
|
+
.accounts({
|
|
182
|
+
signer: order.authority,
|
|
183
|
+
payer: this.rpcOptions.payer,
|
|
184
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
|
|
185
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId),
|
|
186
|
+
order: (0, pda_1.getOrderPDA)(this.program.programId, order.authority, order.marketId, (0, helpers_1.getOrderDirection)(order.orderDirection))
|
|
187
|
+
})
|
|
188
|
+
.instruction());
|
|
189
|
+
}
|
|
190
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Market Bid Order
|
|
195
|
+
* @param args.marketId - The ID of the Market
|
|
196
|
+
* @param args.amount - The amount of the Order
|
|
197
|
+
* @param args.orderDirection - The direction of the Order
|
|
198
|
+
*/
|
|
199
|
+
marketBidOrder({ marketId, amount, orderDirection }) {
|
|
200
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
201
|
+
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
202
|
+
const ixs = [];
|
|
203
|
+
const addressLookupTableAccounts = [];
|
|
204
|
+
const orderBook = yield this.getOrderBook(marketId);
|
|
205
|
+
let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, constants_1.BASE_DECIMALS));
|
|
206
|
+
const orders = Object.keys(orderDirection)[0] === 'hype'
|
|
207
|
+
? orderBook.hype.ask
|
|
208
|
+
: orderBook.flop.ask;
|
|
209
|
+
const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
|
|
210
|
+
for (const order of sortedOrders) {
|
|
211
|
+
if (remainingUSDC.lte(new bn_js_1.default(0)))
|
|
212
|
+
break;
|
|
213
|
+
if (order.authority === this.program.provider.publicKey.toBase58()) {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
const orderPrice = new bn_js_1.default(order.price);
|
|
217
|
+
const availableShares = new bn_js_1.default(order.totalShares).sub(new bn_js_1.default(order.filledShares));
|
|
218
|
+
const effectivePriceDecimal = (0, feeCalculator_1.applyBuyFee)(orderPrice.toNumber() / 1000000, 500);
|
|
219
|
+
const adjustedPrice = new bn_js_1.default(Math.floor(effectivePriceDecimal * 1000000));
|
|
220
|
+
const maxSharesForPrice = remainingUSDC
|
|
221
|
+
.mul(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS)))
|
|
222
|
+
.div(adjustedPrice);
|
|
223
|
+
const sharesToBuy = bn_js_1.default.min(maxSharesForPrice, availableShares);
|
|
224
|
+
if (sharesToBuy.lte(new bn_js_1.default(0)))
|
|
225
|
+
continue;
|
|
226
|
+
const usdcAmount = sharesToBuy
|
|
227
|
+
.mul(adjustedPrice)
|
|
228
|
+
.div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS)));
|
|
229
|
+
if (usdcAmount.lte(new bn_js_1.default(0)))
|
|
230
|
+
continue;
|
|
231
|
+
const oppositeOrderDirection = (0, helpers_1.getOppositeOrderDirectionEncoded)(orderDirection);
|
|
232
|
+
ixs.push(yield this.program.methods
|
|
233
|
+
.marketBidOrder({
|
|
234
|
+
amount: new bn_js_1.default(usdcAmount),
|
|
235
|
+
marketId: new bn_js_1.default(marketId),
|
|
236
|
+
orderDirection,
|
|
237
|
+
bookOrderAskId: new bn_js_1.default(order.id),
|
|
238
|
+
oppositeOrderDirection
|
|
239
|
+
})
|
|
240
|
+
.accounts({
|
|
241
|
+
signer: this.program.provider.publicKey,
|
|
242
|
+
payer: this.rpcOptions.payer,
|
|
243
|
+
market: marketPDA,
|
|
244
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
|
|
245
|
+
bookOrderAskAuthority: new web3_js_1.PublicKey(order.authority),
|
|
246
|
+
order: (0, pda_1.getOrderPDA)(this.program.programId, this.program.provider.publicKey, marketId, (0, helpers_1.getOrderDirection)(orderDirection)),
|
|
247
|
+
oppositeOrder: (0, pda_1.getOrderPDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), marketId, (0, helpers_1.getOrderDirection)(oppositeOrderDirection))
|
|
248
|
+
})
|
|
249
|
+
.instruction());
|
|
250
|
+
remainingUSDC = remainingUSDC.sub(usdcAmount);
|
|
251
|
+
}
|
|
252
|
+
if (ixs.length === 0) {
|
|
253
|
+
throw new Error('No matching orders found to fill the requested amount');
|
|
254
|
+
}
|
|
255
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions, addressLookupTableAccounts);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Market Ask Order
|
|
260
|
+
* @param args.marketId - The ID of the Market
|
|
261
|
+
* @param args.shares - The amount of shares to sell
|
|
262
|
+
* @param args.bookOrderBidId - The ID of the Bid Order
|
|
263
|
+
* @param args.orderDirection - The direction of the Order
|
|
264
|
+
*/
|
|
265
|
+
marketAskOrder({ marketId, shares, orderDirection }) {
|
|
266
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
267
|
+
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
268
|
+
const ixs = [];
|
|
269
|
+
const addressLookupTableAccounts = [];
|
|
270
|
+
const orderBook = yield this.getOrderBook(marketId);
|
|
271
|
+
const bidOrders = Object.keys(orderDirection)[0] === 'hype'
|
|
272
|
+
? orderBook.hype.bid
|
|
273
|
+
: orderBook.flop.bid;
|
|
274
|
+
const sortedOrders = bidOrders.sort((a, b) => Number(b.price) - Number(a.price));
|
|
275
|
+
let amountOfUSDC = new bn_js_1.default(0);
|
|
276
|
+
let remainingShares = new bn_js_1.default(shares * Math.pow(10, constants_1.BASE_DECIMALS));
|
|
277
|
+
for (const order of sortedOrders) {
|
|
278
|
+
if (remainingShares.lte(new bn_js_1.default(0)))
|
|
279
|
+
break;
|
|
280
|
+
if (order.authority === this.program.provider.publicKey.toBase58() ||
|
|
281
|
+
order.linkedBookOrderId !== constants_1.BOOK_ORDER_NULL.toString()) {
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const availableShares = new bn_js_1.default(order.totalShares).sub(new bn_js_1.default(order.filledShares));
|
|
285
|
+
const sharesToSell = bn_js_1.default.min(remainingShares, availableShares);
|
|
286
|
+
if (sharesToSell.lt(new bn_js_1.default(0)))
|
|
287
|
+
continue;
|
|
288
|
+
remainingShares = remainingShares.sub(sharesToSell);
|
|
289
|
+
const orderPrice = new bn_js_1.default(order.price);
|
|
290
|
+
const effectivePriceDecimal = (0, feeCalculator_1.applySellFee)(orderPrice.toNumber() / 1000000, 500);
|
|
291
|
+
const adjustedPrice = new bn_js_1.default(Math.floor(effectivePriceDecimal * 1000000));
|
|
292
|
+
amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS))));
|
|
293
|
+
ixs.push(yield this.program.methods
|
|
294
|
+
.marketAskOrder({
|
|
295
|
+
shares: new bn_js_1.default(sharesToSell),
|
|
296
|
+
orderDirection,
|
|
297
|
+
bookOrderBidId: new bn_js_1.default(order.id)
|
|
298
|
+
})
|
|
299
|
+
.accounts({
|
|
300
|
+
signer: this.program.provider.publicKey,
|
|
301
|
+
payer: this.rpcOptions.payer,
|
|
302
|
+
market: marketPDA,
|
|
303
|
+
buyerAuthority: new web3_js_1.PublicKey(order.authority),
|
|
304
|
+
orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
|
|
305
|
+
marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
|
|
306
|
+
bidOrder: (0, pda_1.getOrderPDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), marketId, (0, helpers_1.getOrderDirection)(orderDirection)),
|
|
307
|
+
askOrder: (0, pda_1.getOrderPDA)(this.program.programId, this.program.provider.publicKey, marketId, (0, helpers_1.getOrderDirection)(orderDirection))
|
|
308
|
+
})
|
|
309
|
+
.instruction());
|
|
310
|
+
}
|
|
311
|
+
if (ixs.length === 0) {
|
|
312
|
+
throw new Error('No matching orders found to fill the requested amount');
|
|
313
|
+
}
|
|
314
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions, addressLookupTableAccounts);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Payout Order
|
|
319
|
+
* @param args.marketId - The ID of the Market
|
|
320
|
+
* @param args.authority - The authority of the order
|
|
321
|
+
* @param args.orderDirection - The direction of the Order to Payout
|
|
322
|
+
*/
|
|
323
|
+
payoutOrder(orders) {
|
|
324
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
325
|
+
const ixs = [];
|
|
326
|
+
if (orders.length > 4) {
|
|
327
|
+
throw new Error('Max 4 orders per transaction');
|
|
328
|
+
}
|
|
329
|
+
for (const order of orders) {
|
|
330
|
+
ixs.push(yield this.program.methods
|
|
331
|
+
.payoutOrder()
|
|
332
|
+
.accounts({
|
|
333
|
+
signer: order.authority,
|
|
334
|
+
payer: this.rpcOptions.payer,
|
|
335
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
|
|
336
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(constants_1.USDC_MINT),
|
|
337
|
+
order: (0, pda_1.getOrderPDA)(this.program.programId, order.authority, order.marketId, (0, helpers_1.getOrderDirection)(order.orderDirection))
|
|
338
|
+
})
|
|
339
|
+
.instruction());
|
|
340
|
+
}
|
|
341
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
exports.default = Trade;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="@coral-xyz/anchor/node_modules/@solana/web3.js" />
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
export type Customer = {
|
|
4
|
+
id: number;
|
|
5
|
+
authority: string;
|
|
6
|
+
name: string;
|
|
7
|
+
feeRecipient: string;
|
|
8
|
+
feeBps: number;
|
|
9
|
+
isVerified: boolean;
|
|
10
|
+
address: string;
|
|
11
|
+
};
|
|
12
|
+
export type CreateCustomerArgs = {
|
|
13
|
+
id: number;
|
|
14
|
+
name: string;
|
|
15
|
+
authority: PublicKey;
|
|
16
|
+
feeRecipient: PublicKey;
|
|
17
|
+
};
|
|
18
|
+
export type UpdateCustomerFeeArgs = {
|
|
19
|
+
customerId: number;
|
|
20
|
+
feeBps: number;
|
|
21
|
+
feeRecipient: PublicKey | null;
|
|
22
|
+
};
|
|
@@ -847,7 +847,7 @@
|
|
|
847
847
|
},
|
|
848
848
|
{
|
|
849
849
|
"kind": "arg",
|
|
850
|
-
"path": "args.
|
|
850
|
+
"path": "args.customer_id"
|
|
851
851
|
}
|
|
852
852
|
]
|
|
853
853
|
}
|
|
@@ -3106,7 +3106,7 @@
|
|
|
3106
3106
|
"type": "pubkey"
|
|
3107
3107
|
},
|
|
3108
3108
|
{
|
|
3109
|
-
"name": "
|
|
3109
|
+
"name": "customer_id",
|
|
3110
3110
|
"type": "u16"
|
|
3111
3111
|
}
|
|
3112
3112
|
]
|
|
@@ -3886,7 +3886,7 @@
|
|
|
3886
3886
|
"type": "pubkey"
|
|
3887
3887
|
},
|
|
3888
3888
|
{
|
|
3889
|
-
"name": "
|
|
3889
|
+
"name": "customer_id",
|
|
3890
3890
|
"type": "u16"
|
|
3891
3891
|
},
|
|
3892
3892
|
{
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="@coral-xyz/anchor/node_modules/@solana/web3.js" />
|
|
1
2
|
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
2
3
|
export type RpcOptions = {
|
|
3
4
|
skipPreflight?: boolean;
|
|
@@ -130,65 +131,6 @@ export type WinningDirectionEncoded = {
|
|
|
130
131
|
} | {
|
|
131
132
|
none: {};
|
|
132
133
|
};
|
|
133
|
-
export type Customer = {
|
|
134
|
-
id: number;
|
|
135
|
-
authority: string;
|
|
136
|
-
name: string;
|
|
137
|
-
feeRecipient: string;
|
|
138
|
-
feeBps: number;
|
|
139
|
-
isVerified: boolean;
|
|
140
|
-
address: string;
|
|
141
|
-
};
|
|
142
|
-
export type StakeVault = {
|
|
143
|
-
name: string;
|
|
144
|
-
collection: string;
|
|
145
|
-
authority: string;
|
|
146
|
-
amount: number;
|
|
147
|
-
isLocked: boolean;
|
|
148
|
-
tokenMint: string;
|
|
149
|
-
amountPaid: number;
|
|
150
|
-
tokenDecimals: number;
|
|
151
|
-
tokenStaked: number;
|
|
152
|
-
week: number;
|
|
153
|
-
initTs: number;
|
|
154
|
-
endTs: number;
|
|
155
|
-
revShareClaimed: number;
|
|
156
|
-
revShareAvailable: number;
|
|
157
|
-
};
|
|
158
|
-
export type Stake = {
|
|
159
|
-
address: string;
|
|
160
|
-
authority: string;
|
|
161
|
-
initTs: number;
|
|
162
|
-
checkedTs: number;
|
|
163
|
-
claimed: number;
|
|
164
|
-
available: number;
|
|
165
|
-
amount: number;
|
|
166
|
-
revShareCheckedTs: number;
|
|
167
|
-
revShareClaimed: number;
|
|
168
|
-
revShareAvailable: number;
|
|
169
|
-
};
|
|
170
|
-
export type Unstake = {
|
|
171
|
-
address: string;
|
|
172
|
-
authority: string;
|
|
173
|
-
amount: number;
|
|
174
|
-
withdrawTs: number;
|
|
175
|
-
};
|
|
176
|
-
export type PlaceBidOrderArgs = {
|
|
177
|
-
orders: {
|
|
178
|
-
amount: number;
|
|
179
|
-
price: number;
|
|
180
|
-
orderDirection: OrderDirectionEncoded;
|
|
181
|
-
marketId: number;
|
|
182
|
-
}[];
|
|
183
|
-
};
|
|
184
|
-
export type PlaceAskOrderArgs = {
|
|
185
|
-
orders: {
|
|
186
|
-
amount: number;
|
|
187
|
-
price: number;
|
|
188
|
-
orderDirection: OrderDirectionEncoded;
|
|
189
|
-
marketId: number;
|
|
190
|
-
}[];
|
|
191
|
-
};
|
|
192
134
|
export type InitializeMarketArgs = {
|
|
193
135
|
marketId: number;
|
|
194
136
|
startTime: number;
|
|
@@ -197,12 +139,6 @@ export type InitializeMarketArgs = {
|
|
|
197
139
|
feeBps: number;
|
|
198
140
|
customer: PublicKey | null;
|
|
199
141
|
};
|
|
200
|
-
export type CreateCustomerArgs = {
|
|
201
|
-
id: number;
|
|
202
|
-
name: string;
|
|
203
|
-
authority: PublicKey;
|
|
204
|
-
feeRecipient: PublicKey;
|
|
205
|
-
};
|
|
206
142
|
export type CreateMarketArgs = {
|
|
207
143
|
markets: {
|
|
208
144
|
marketId: number;
|
|
@@ -229,36 +165,6 @@ export type CreatePoolArgs = {
|
|
|
229
165
|
}[];
|
|
230
166
|
isFast?: boolean;
|
|
231
167
|
};
|
|
232
|
-
export type CancelBidOrderArgs = {
|
|
233
|
-
orders: {
|
|
234
|
-
authority: PublicKey;
|
|
235
|
-
marketId: number;
|
|
236
|
-
bookOrderId: number;
|
|
237
|
-
orderDirection: OrderDirectionEncoded;
|
|
238
|
-
}[];
|
|
239
|
-
};
|
|
240
|
-
export type CancelAskOrderArgs = {
|
|
241
|
-
orders: {
|
|
242
|
-
authority: PublicKey;
|
|
243
|
-
marketId: number;
|
|
244
|
-
bookOrderId: number;
|
|
245
|
-
orderDirection: OrderDirectionEncoded;
|
|
246
|
-
}[];
|
|
247
|
-
};
|
|
248
|
-
export type MarketBidOrderArgs = {
|
|
249
|
-
marketId: number;
|
|
250
|
-
amount: number;
|
|
251
|
-
orderDirection: OrderDirectionEncoded;
|
|
252
|
-
};
|
|
253
|
-
export type MarketAskOrderArgs = {
|
|
254
|
-
marketId: number;
|
|
255
|
-
shares: number;
|
|
256
|
-
orderDirection: OrderDirectionEncoded;
|
|
257
|
-
};
|
|
258
|
-
export type UpdateClaimVaultIsActiveArgs = {
|
|
259
|
-
isActive: boolean;
|
|
260
|
-
claimVaultName: string;
|
|
261
|
-
};
|
|
262
168
|
export type UpdateMarketWinningDirectionArgs = {
|
|
263
169
|
marketId: number;
|
|
264
170
|
winningDirection: WinningDirectionEncoded;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="@coral-xyz/anchor/node_modules/@solana/web3.js" />
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
export type Predictor = {
|
|
4
|
+
address: string;
|
|
5
|
+
authority: string;
|
|
6
|
+
refer: string;
|
|
7
|
+
customerId: number;
|
|
8
|
+
balance: number;
|
|
9
|
+
};
|
|
10
|
+
export type DepositArgs = {
|
|
11
|
+
authority: PublicKey;
|
|
12
|
+
amount: number;
|
|
13
|
+
refer?: PublicKey;
|
|
14
|
+
customerId?: number;
|
|
15
|
+
};
|
|
16
|
+
export type WithdrawArgs = {
|
|
17
|
+
authority: PublicKey;
|
|
18
|
+
amount: number;
|
|
19
|
+
customerId?: number;
|
|
20
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type StakeVault = {
|
|
2
|
+
name: string;
|
|
3
|
+
collection: string;
|
|
4
|
+
authority: string;
|
|
5
|
+
amount: number;
|
|
6
|
+
isLocked: boolean;
|
|
7
|
+
tokenMint: string;
|
|
8
|
+
amountPaid: number;
|
|
9
|
+
tokenDecimals: number;
|
|
10
|
+
tokenStaked: number;
|
|
11
|
+
week: number;
|
|
12
|
+
initTs: number;
|
|
13
|
+
endTs: number;
|
|
14
|
+
revShareClaimed: number;
|
|
15
|
+
revShareAvailable: number;
|
|
16
|
+
};
|
|
17
|
+
export type Stake = {
|
|
18
|
+
address: string;
|
|
19
|
+
authority: string;
|
|
20
|
+
initTs: number;
|
|
21
|
+
checkedTs: number;
|
|
22
|
+
claimed: number;
|
|
23
|
+
available: number;
|
|
24
|
+
amount: number;
|
|
25
|
+
revShareCheckedTs: number;
|
|
26
|
+
revShareClaimed: number;
|
|
27
|
+
revShareAvailable: number;
|
|
28
|
+
};
|
|
29
|
+
export type Unstake = {
|
|
30
|
+
address: string;
|
|
31
|
+
authority: string;
|
|
32
|
+
amount: number;
|
|
33
|
+
withdrawTs: number;
|
|
34
|
+
};
|