@lightconexyz/lightcone-sdk 0.1.0
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/README.md +232 -0
- package/dist/api/client.d.ts +225 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/client.js +452 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/error.d.ts +58 -0
- package/dist/api/error.d.ts.map +1 -0
- package/dist/api/error.js +98 -0
- package/dist/api/error.js.map +1 -0
- package/dist/api/index.d.ts +23 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +51 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/types/admin.d.ts +49 -0
- package/dist/api/types/admin.d.ts.map +1 -0
- package/dist/api/types/admin.js +13 -0
- package/dist/api/types/admin.js.map +1 -0
- package/dist/api/types/index.d.ts +14 -0
- package/dist/api/types/index.d.ts.map +1 -0
- package/dist/api/types/index.js +13 -0
- package/dist/api/types/index.js.map +1 -0
- package/dist/api/types/market.d.ts +186 -0
- package/dist/api/types/market.d.ts.map +1 -0
- package/dist/api/types/market.js +6 -0
- package/dist/api/types/market.js.map +1 -0
- package/dist/api/types/order.d.ts +190 -0
- package/dist/api/types/order.d.ts.map +1 -0
- package/dist/api/types/order.js +6 -0
- package/dist/api/types/order.js.map +1 -0
- package/dist/api/types/orderbook.d.ts +36 -0
- package/dist/api/types/orderbook.d.ts.map +1 -0
- package/dist/api/types/orderbook.js +6 -0
- package/dist/api/types/orderbook.js.map +1 -0
- package/dist/api/types/position.d.ts +60 -0
- package/dist/api/types/position.d.ts.map +1 -0
- package/dist/api/types/position.js +6 -0
- package/dist/api/types/position.js.map +1 -0
- package/dist/api/types/price_history.d.ts +68 -0
- package/dist/api/types/price_history.d.ts.map +1 -0
- package/dist/api/types/price_history.js +13 -0
- package/dist/api/types/price_history.js.map +1 -0
- package/dist/api/types/trade.d.ts +67 -0
- package/dist/api/types/trade.d.ts.map +1 -0
- package/dist/api/types/trade.js +13 -0
- package/dist/api/types/trade.js.map +1 -0
- package/dist/api/validation.d.ts +24 -0
- package/dist/api/validation.d.ts.map +1 -0
- package/dist/api/validation.js +53 -0
- package/dist/api/validation.js.map +1 -0
- package/dist/auth.d.ts +80 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +149 -0
- package/dist/auth.js.map +1 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +107 -0
- package/dist/index.js.map +1 -0
- package/dist/network.d.ts +5 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/network.js +8 -0
- package/dist/network.js.map +1 -0
- package/dist/program/accounts.d.ts +98 -0
- package/dist/program/accounts.d.ts.map +1 -0
- package/dist/program/accounts.js +319 -0
- package/dist/program/accounts.js.map +1 -0
- package/dist/program/builder.d.ts +94 -0
- package/dist/program/builder.d.ts.map +1 -0
- package/dist/program/builder.js +175 -0
- package/dist/program/builder.js.map +1 -0
- package/dist/program/client.d.ts +56 -0
- package/dist/program/client.d.ts.map +1 -0
- package/dist/program/client.js +288 -0
- package/dist/program/client.js.map +1 -0
- package/dist/program/constants.d.ts +108 -0
- package/dist/program/constants.d.ts.map +1 -0
- package/dist/program/constants.js +112 -0
- package/dist/program/constants.js.map +1 -0
- package/dist/program/index.d.ts +14 -0
- package/dist/program/index.d.ts.map +1 -0
- package/dist/program/index.js +149 -0
- package/dist/program/index.js.map +1 -0
- package/dist/program/instructions.d.ts +248 -0
- package/dist/program/instructions.d.ts.map +1 -0
- package/dist/program/instructions.js +692 -0
- package/dist/program/instructions.js.map +1 -0
- package/dist/program/orders.d.ts +151 -0
- package/dist/program/orders.d.ts.map +1 -0
- package/dist/program/orders.js +417 -0
- package/dist/program/orders.js.map +1 -0
- package/dist/program/pda.d.ts +73 -0
- package/dist/program/pda.d.ts.map +1 -0
- package/dist/program/pda.js +131 -0
- package/dist/program/pda.js.map +1 -0
- package/dist/program/types.d.ts +380 -0
- package/dist/program/types.d.ts.map +1 -0
- package/dist/program/types.js +27 -0
- package/dist/program/types.js.map +1 -0
- package/dist/program/utils.d.ts +91 -0
- package/dist/program/utils.d.ts.map +1 -0
- package/dist/program/utils.js +219 -0
- package/dist/program/utils.js.map +1 -0
- package/dist/shared/index.d.ts +8 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index.js +18 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/shared/price.d.ts +41 -0
- package/dist/shared/price.d.ts.map +1 -0
- package/dist/shared/price.js +57 -0
- package/dist/shared/price.js.map +1 -0
- package/dist/shared/scaling.d.ts +45 -0
- package/dist/shared/scaling.d.ts.map +1 -0
- package/dist/shared/scaling.js +84 -0
- package/dist/shared/scaling.js.map +1 -0
- package/dist/shared/types.d.ts +19 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +23 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/websocket/client.d.ts +238 -0
- package/dist/websocket/client.d.ts.map +1 -0
- package/dist/websocket/client.js +580 -0
- package/dist/websocket/client.js.map +1 -0
- package/dist/websocket/error.d.ts +47 -0
- package/dist/websocket/error.d.ts.map +1 -0
- package/dist/websocket/error.js +83 -0
- package/dist/websocket/error.js.map +1 -0
- package/dist/websocket/handlers.d.ts +97 -0
- package/dist/websocket/handlers.d.ts.map +1 -0
- package/dist/websocket/handlers.js +277 -0
- package/dist/websocket/handlers.js.map +1 -0
- package/dist/websocket/index.d.ts +38 -0
- package/dist/websocket/index.d.ts.map +1 -0
- package/dist/websocket/index.js +75 -0
- package/dist/websocket/index.js.map +1 -0
- package/dist/websocket/state/index.d.ts +7 -0
- package/dist/websocket/state/index.d.ts.map +1 -0
- package/dist/websocket/state/index.js +14 -0
- package/dist/websocket/state/index.js.map +1 -0
- package/dist/websocket/state/orderbook.d.ts +107 -0
- package/dist/websocket/state/orderbook.d.ts.map +1 -0
- package/dist/websocket/state/orderbook.js +293 -0
- package/dist/websocket/state/orderbook.js.map +1 -0
- package/dist/websocket/state/price.d.ts +108 -0
- package/dist/websocket/state/price.d.ts.map +1 -0
- package/dist/websocket/state/price.js +243 -0
- package/dist/websocket/state/price.js.map +1 -0
- package/dist/websocket/state/user.d.ts +83 -0
- package/dist/websocket/state/user.d.ts.map +1 -0
- package/dist/websocket/state/user.js +228 -0
- package/dist/websocket/state/user.js.map +1 -0
- package/dist/websocket/subscriptions.d.ts +143 -0
- package/dist/websocket/subscriptions.d.ts.map +1 -0
- package/dist/websocket/subscriptions.js +244 -0
- package/dist/websocket/subscriptions.js.map +1 -0
- package/dist/websocket/types.d.ts +417 -0
- package/dist/websocket/types.d.ts.map +1 -0
- package/dist/websocket/types.js +195 -0
- package/dist/websocket/types.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isExchangeAccount = isExchangeAccount;
|
|
4
|
+
exports.isMarketAccount = isMarketAccount;
|
|
5
|
+
exports.isOrderStatusAccount = isOrderStatusAccount;
|
|
6
|
+
exports.isUserNonceAccount = isUserNonceAccount;
|
|
7
|
+
exports.isPositionAccount = isPositionAccount;
|
|
8
|
+
exports.isOrderbookAccount = isOrderbookAccount;
|
|
9
|
+
exports.deserializeExchange = deserializeExchange;
|
|
10
|
+
exports.deserializeMarket = deserializeMarket;
|
|
11
|
+
exports.deserializeOrderStatus = deserializeOrderStatus;
|
|
12
|
+
exports.deserializeUserNonce = deserializeUserNonce;
|
|
13
|
+
exports.deserializePosition = deserializePosition;
|
|
14
|
+
exports.deserializeOrderbook = deserializeOrderbook;
|
|
15
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
16
|
+
const constants_1 = require("./constants");
|
|
17
|
+
const types_1 = require("./types");
|
|
18
|
+
const utils_1 = require("./utils");
|
|
19
|
+
// ============================================================================
|
|
20
|
+
// DISCRIMINATOR VALIDATION
|
|
21
|
+
// ============================================================================
|
|
22
|
+
/**
|
|
23
|
+
* Validate that a buffer starts with the expected discriminator
|
|
24
|
+
*/
|
|
25
|
+
function validateDiscriminator(data, expected, accountType) {
|
|
26
|
+
const actual = data.subarray(0, 8);
|
|
27
|
+
if (!actual.equals(expected)) {
|
|
28
|
+
throw new Error(`Invalid ${accountType} discriminator. Expected ${expected.toString("hex")}, got ${actual.toString("hex")}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Check if a buffer has a valid Exchange discriminator
|
|
33
|
+
*/
|
|
34
|
+
function isExchangeAccount(data) {
|
|
35
|
+
if (data.length < 8)
|
|
36
|
+
return false;
|
|
37
|
+
return data.subarray(0, 8).equals(constants_1.DISCRIMINATOR.EXCHANGE);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Check if a buffer has a valid Market discriminator
|
|
41
|
+
*/
|
|
42
|
+
function isMarketAccount(data) {
|
|
43
|
+
if (data.length < 8)
|
|
44
|
+
return false;
|
|
45
|
+
return data.subarray(0, 8).equals(constants_1.DISCRIMINATOR.MARKET);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Check if a buffer has a valid OrderStatus discriminator
|
|
49
|
+
*/
|
|
50
|
+
function isOrderStatusAccount(data) {
|
|
51
|
+
if (data.length < 8)
|
|
52
|
+
return false;
|
|
53
|
+
return data.subarray(0, 8).equals(constants_1.DISCRIMINATOR.ORDER_STATUS);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check if a buffer has a valid UserNonce discriminator
|
|
57
|
+
*/
|
|
58
|
+
function isUserNonceAccount(data) {
|
|
59
|
+
if (data.length < 8)
|
|
60
|
+
return false;
|
|
61
|
+
return data.subarray(0, 8).equals(constants_1.DISCRIMINATOR.USER_NONCE);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check if a buffer has a valid Position discriminator
|
|
65
|
+
*/
|
|
66
|
+
function isPositionAccount(data) {
|
|
67
|
+
if (data.length < 8)
|
|
68
|
+
return false;
|
|
69
|
+
return data.subarray(0, 8).equals(constants_1.DISCRIMINATOR.POSITION);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if a buffer has a valid Orderbook discriminator
|
|
73
|
+
*/
|
|
74
|
+
function isOrderbookAccount(data) {
|
|
75
|
+
if (data.length < 8)
|
|
76
|
+
return false;
|
|
77
|
+
return data.subarray(0, 8).equals(constants_1.DISCRIMINATOR.ORDERBOOK);
|
|
78
|
+
}
|
|
79
|
+
// ============================================================================
|
|
80
|
+
// ACCOUNT DESERIALIZATION
|
|
81
|
+
// ============================================================================
|
|
82
|
+
/**
|
|
83
|
+
* Deserialize Exchange account data
|
|
84
|
+
*
|
|
85
|
+
* Layout (88 bytes):
|
|
86
|
+
* - discriminator: [u8; 8]
|
|
87
|
+
* - authority: Pubkey (32 bytes)
|
|
88
|
+
* - operator: Pubkey (32 bytes)
|
|
89
|
+
* - market_count: u64 (8 bytes)
|
|
90
|
+
* - paused: u8 (1 byte)
|
|
91
|
+
* - bump: u8 (1 byte)
|
|
92
|
+
* - _padding: [u8; 6]
|
|
93
|
+
*/
|
|
94
|
+
function deserializeExchange(data) {
|
|
95
|
+
if (data.length < constants_1.ACCOUNT_SIZE.EXCHANGE) {
|
|
96
|
+
throw new Error(`Invalid Exchange data length: ${data.length}, expected ${constants_1.ACCOUNT_SIZE.EXCHANGE}`);
|
|
97
|
+
}
|
|
98
|
+
validateDiscriminator(data, constants_1.DISCRIMINATOR.EXCHANGE, "Exchange");
|
|
99
|
+
let offset = 0;
|
|
100
|
+
const discriminator = data.subarray(offset, offset + 8);
|
|
101
|
+
offset += 8;
|
|
102
|
+
const authority = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
103
|
+
offset += 32;
|
|
104
|
+
const operator = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
105
|
+
offset += 32;
|
|
106
|
+
const marketCount = (0, utils_1.fromLeBytes)(data.subarray(offset, offset + 8));
|
|
107
|
+
offset += 8;
|
|
108
|
+
const paused = data[offset] !== 0;
|
|
109
|
+
offset += 1;
|
|
110
|
+
const bump = data[offset];
|
|
111
|
+
offset += 1;
|
|
112
|
+
// Skip padding: 6 bytes
|
|
113
|
+
return {
|
|
114
|
+
discriminator,
|
|
115
|
+
authority,
|
|
116
|
+
operator,
|
|
117
|
+
marketCount,
|
|
118
|
+
paused,
|
|
119
|
+
bump,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Deserialize Market account data
|
|
124
|
+
*
|
|
125
|
+
* Layout (120 bytes):
|
|
126
|
+
* - discriminator: [u8; 8]
|
|
127
|
+
* - market_id: u64 (8 bytes)
|
|
128
|
+
* - num_outcomes: u8 (1 byte)
|
|
129
|
+
* - status: u8 (1 byte)
|
|
130
|
+
* - winning_outcome: u8 (1 byte)
|
|
131
|
+
* - has_winning_outcome: u8 (1 byte)
|
|
132
|
+
* - bump: u8 (1 byte)
|
|
133
|
+
* - _padding: [u8; 3]
|
|
134
|
+
* - oracle: Pubkey (32 bytes)
|
|
135
|
+
* - question_id: [u8; 32]
|
|
136
|
+
* - condition_id: [u8; 32]
|
|
137
|
+
*/
|
|
138
|
+
function deserializeMarket(data) {
|
|
139
|
+
if (data.length < constants_1.ACCOUNT_SIZE.MARKET) {
|
|
140
|
+
throw new Error(`Invalid Market data length: ${data.length}, expected ${constants_1.ACCOUNT_SIZE.MARKET}`);
|
|
141
|
+
}
|
|
142
|
+
validateDiscriminator(data, constants_1.DISCRIMINATOR.MARKET, "Market");
|
|
143
|
+
let offset = 0;
|
|
144
|
+
const discriminator = data.subarray(offset, offset + 8);
|
|
145
|
+
offset += 8;
|
|
146
|
+
const marketId = (0, utils_1.fromLeBytes)(data.subarray(offset, offset + 8));
|
|
147
|
+
offset += 8;
|
|
148
|
+
const numOutcomes = data[offset];
|
|
149
|
+
offset += 1;
|
|
150
|
+
const statusByte = data[offset];
|
|
151
|
+
offset += 1;
|
|
152
|
+
const winningOutcome = data[offset];
|
|
153
|
+
offset += 1;
|
|
154
|
+
const hasWinningOutcome = data[offset] !== 0;
|
|
155
|
+
offset += 1;
|
|
156
|
+
const bump = data[offset];
|
|
157
|
+
offset += 1;
|
|
158
|
+
// Skip padding: 3 bytes
|
|
159
|
+
offset += 3;
|
|
160
|
+
const oracle = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
161
|
+
offset += 32;
|
|
162
|
+
const questionId = Buffer.from(data.subarray(offset, offset + 32));
|
|
163
|
+
offset += 32;
|
|
164
|
+
const conditionId = Buffer.from(data.subarray(offset, offset + 32));
|
|
165
|
+
offset += 32;
|
|
166
|
+
// Map status byte to enum
|
|
167
|
+
let status;
|
|
168
|
+
switch (statusByte) {
|
|
169
|
+
case 0:
|
|
170
|
+
status = types_1.MarketStatus.Pending;
|
|
171
|
+
break;
|
|
172
|
+
case 1:
|
|
173
|
+
status = types_1.MarketStatus.Active;
|
|
174
|
+
break;
|
|
175
|
+
case 2:
|
|
176
|
+
status = types_1.MarketStatus.Resolved;
|
|
177
|
+
break;
|
|
178
|
+
case 3:
|
|
179
|
+
status = types_1.MarketStatus.Cancelled;
|
|
180
|
+
break;
|
|
181
|
+
default:
|
|
182
|
+
throw new Error(`Unknown market status: ${statusByte}`);
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
discriminator,
|
|
186
|
+
marketId,
|
|
187
|
+
numOutcomes,
|
|
188
|
+
status,
|
|
189
|
+
winningOutcome,
|
|
190
|
+
hasWinningOutcome,
|
|
191
|
+
bump,
|
|
192
|
+
oracle,
|
|
193
|
+
questionId,
|
|
194
|
+
conditionId,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Deserialize OrderStatus account data
|
|
199
|
+
*
|
|
200
|
+
* Layout (24 bytes):
|
|
201
|
+
* - discriminator: [u8; 8]
|
|
202
|
+
* - remaining: u64 (8 bytes)
|
|
203
|
+
* - is_cancelled: u8 (1 byte)
|
|
204
|
+
* - _padding: [u8; 7]
|
|
205
|
+
*/
|
|
206
|
+
function deserializeOrderStatus(data) {
|
|
207
|
+
if (data.length < constants_1.ACCOUNT_SIZE.ORDER_STATUS) {
|
|
208
|
+
throw new Error(`Invalid OrderStatus data length: ${data.length}, expected ${constants_1.ACCOUNT_SIZE.ORDER_STATUS}`);
|
|
209
|
+
}
|
|
210
|
+
validateDiscriminator(data, constants_1.DISCRIMINATOR.ORDER_STATUS, "OrderStatus");
|
|
211
|
+
let offset = 0;
|
|
212
|
+
const discriminator = data.subarray(offset, offset + 8);
|
|
213
|
+
offset += 8;
|
|
214
|
+
const remaining = (0, utils_1.fromLeBytes)(data.subarray(offset, offset + 8));
|
|
215
|
+
offset += 8;
|
|
216
|
+
const isCancelled = data[offset] !== 0;
|
|
217
|
+
offset += 1;
|
|
218
|
+
// Skip padding: 7 bytes
|
|
219
|
+
return {
|
|
220
|
+
discriminator,
|
|
221
|
+
remaining,
|
|
222
|
+
isCancelled,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Deserialize UserNonce account data
|
|
227
|
+
*
|
|
228
|
+
* Layout (16 bytes):
|
|
229
|
+
* - discriminator: [u8; 8]
|
|
230
|
+
* - nonce: u64 (8 bytes)
|
|
231
|
+
*/
|
|
232
|
+
function deserializeUserNonce(data) {
|
|
233
|
+
if (data.length < constants_1.ACCOUNT_SIZE.USER_NONCE) {
|
|
234
|
+
throw new Error(`Invalid UserNonce data length: ${data.length}, expected ${constants_1.ACCOUNT_SIZE.USER_NONCE}`);
|
|
235
|
+
}
|
|
236
|
+
validateDiscriminator(data, constants_1.DISCRIMINATOR.USER_NONCE, "UserNonce");
|
|
237
|
+
let offset = 0;
|
|
238
|
+
const discriminator = data.subarray(offset, offset + 8);
|
|
239
|
+
offset += 8;
|
|
240
|
+
const nonce = (0, utils_1.fromLeBytes)(data.subarray(offset, offset + 8));
|
|
241
|
+
offset += 8;
|
|
242
|
+
return {
|
|
243
|
+
discriminator,
|
|
244
|
+
nonce,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Deserialize Position account data
|
|
249
|
+
*
|
|
250
|
+
* Layout (80 bytes):
|
|
251
|
+
* - discriminator: [u8; 8]
|
|
252
|
+
* - owner: Pubkey (32 bytes)
|
|
253
|
+
* - market: Pubkey (32 bytes)
|
|
254
|
+
* - bump: u8 (1 byte)
|
|
255
|
+
* - _padding: [u8; 7]
|
|
256
|
+
*/
|
|
257
|
+
function deserializePosition(data) {
|
|
258
|
+
if (data.length < constants_1.ACCOUNT_SIZE.POSITION) {
|
|
259
|
+
throw new Error(`Invalid Position data length: ${data.length}, expected ${constants_1.ACCOUNT_SIZE.POSITION}`);
|
|
260
|
+
}
|
|
261
|
+
validateDiscriminator(data, constants_1.DISCRIMINATOR.POSITION, "Position");
|
|
262
|
+
let offset = 0;
|
|
263
|
+
const discriminator = data.subarray(offset, offset + 8);
|
|
264
|
+
offset += 8;
|
|
265
|
+
const owner = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
266
|
+
offset += 32;
|
|
267
|
+
const market = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
268
|
+
offset += 32;
|
|
269
|
+
const bump = data[offset];
|
|
270
|
+
offset += 1;
|
|
271
|
+
// Skip padding: 7 bytes
|
|
272
|
+
return {
|
|
273
|
+
discriminator,
|
|
274
|
+
owner,
|
|
275
|
+
market,
|
|
276
|
+
bump,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Deserialize Orderbook account data
|
|
281
|
+
*
|
|
282
|
+
* Layout (144 bytes):
|
|
283
|
+
* - discriminator: [u8; 8]
|
|
284
|
+
* - market: Pubkey (32 bytes)
|
|
285
|
+
* - mint_a: Pubkey (32 bytes)
|
|
286
|
+
* - mint_b: Pubkey (32 bytes)
|
|
287
|
+
* - lookup_table: Pubkey (32 bytes)
|
|
288
|
+
* - bump: u8 (1 byte)
|
|
289
|
+
* - _padding: [u8; 7]
|
|
290
|
+
*/
|
|
291
|
+
function deserializeOrderbook(data) {
|
|
292
|
+
if (data.length < constants_1.ACCOUNT_SIZE.ORDERBOOK) {
|
|
293
|
+
throw new Error(`Invalid Orderbook data length: ${data.length}, expected ${constants_1.ACCOUNT_SIZE.ORDERBOOK}`);
|
|
294
|
+
}
|
|
295
|
+
validateDiscriminator(data, constants_1.DISCRIMINATOR.ORDERBOOK, "Orderbook");
|
|
296
|
+
let offset = 0;
|
|
297
|
+
const discriminator = data.subarray(offset, offset + 8);
|
|
298
|
+
offset += 8;
|
|
299
|
+
const market = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
300
|
+
offset += 32;
|
|
301
|
+
const mintA = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
302
|
+
offset += 32;
|
|
303
|
+
const mintB = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
304
|
+
offset += 32;
|
|
305
|
+
const lookupTable = new web3_js_1.PublicKey(data.subarray(offset, offset + 32));
|
|
306
|
+
offset += 32;
|
|
307
|
+
const bump = data[offset];
|
|
308
|
+
offset += 1;
|
|
309
|
+
// Skip padding: 7 bytes
|
|
310
|
+
return {
|
|
311
|
+
discriminator,
|
|
312
|
+
market,
|
|
313
|
+
mintA,
|
|
314
|
+
mintB,
|
|
315
|
+
lookupTable,
|
|
316
|
+
bump,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
//# sourceMappingURL=accounts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accounts.js","sourceRoot":"","sources":["../../src/program/accounts.ts"],"names":[],"mappings":";;AAoCA,8CAGC;AAKD,0CAGC;AAKD,oDAGC;AAKD,gDAGC;AAKD,8CAGC;AAKD,gDAGC;AAkBD,kDAuCC;AAkBD,8CA2EC;AAWD,wDA2BC;AASD,oDAqBC;AAYD,kDA+BC;AAcD,oDAuCC;AAzYD,6CAA4C;AAC5C,2CAA0D;AAC1D,mCAQiB;AACjB,mCAA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AI,mBAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC;IACtE,MAAM,IAAI,EAAE,CAAC;IAEb,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAC1B,MAAM,IAAI,CAAC,CAAC;IAEZ,wBAAwB;IAExB,OAAO;QACL,aAAa;QACb,MAAM;QACN,KAAK;QACL,KAAK;QACL,WAAW;QACX,IAAI;KACL,CAAC;AACJ,CAAC","sourcesContent":["import { PublicKey } from \"@solana/web3.js\";\nimport { DISCRIMINATOR, ACCOUNT_SIZE } from \"./constants\";\nimport {\n Exchange,\n Market,\n MarketStatus,\n OrderStatus,\n UserNonce,\n Position,\n Orderbook,\n} from \"./types\";\nimport { fromLeBytes } from \"./utils\";\n\n// ============================================================================\n// DISCRIMINATOR VALIDATION\n// ============================================================================\n\n/**\n * Validate that a buffer starts with the expected discriminator\n */\nfunction validateDiscriminator(\n data: Buffer,\n expected: Buffer,\n accountType: string\n): void {\n const actual = data.subarray(0, 8);\n if (!actual.equals(expected)) {\n throw new Error(\n `Invalid ${accountType} discriminator. Expected ${expected.toString(\"hex\")}, got ${actual.toString(\"hex\")}`\n );\n }\n}\n\n/**\n * Check if a buffer has a valid Exchange discriminator\n */\nexport function isExchangeAccount(data: Buffer): boolean {\n if (data.length < 8) return false;\n return data.subarray(0, 8).equals(DISCRIMINATOR.EXCHANGE);\n}\n\n/**\n * Check if a buffer has a valid Market discriminator\n */\nexport function isMarketAccount(data: Buffer): boolean {\n if (data.length < 8) return false;\n return data.subarray(0, 8).equals(DISCRIMINATOR.MARKET);\n}\n\n/**\n * Check if a buffer has a valid OrderStatus discriminator\n */\nexport function isOrderStatusAccount(data: Buffer): boolean {\n if (data.length < 8) return false;\n return data.subarray(0, 8).equals(DISCRIMINATOR.ORDER_STATUS);\n}\n\n/**\n * Check if a buffer has a valid UserNonce discriminator\n */\nexport function isUserNonceAccount(data: Buffer): boolean {\n if (data.length < 8) return false;\n return data.subarray(0, 8).equals(DISCRIMINATOR.USER_NONCE);\n}\n\n/**\n * Check if a buffer has a valid Position discriminator\n */\nexport function isPositionAccount(data: Buffer): boolean {\n if (data.length < 8) return false;\n return data.subarray(0, 8).equals(DISCRIMINATOR.POSITION);\n}\n\n/**\n * Check if a buffer has a valid Orderbook discriminator\n */\nexport function isOrderbookAccount(data: Buffer): boolean {\n if (data.length < 8) return false;\n return data.subarray(0, 8).equals(DISCRIMINATOR.ORDERBOOK);\n}\n\n// ============================================================================\n// ACCOUNT DESERIALIZATION\n// ============================================================================\n\n/**\n * Deserialize Exchange account data\n *\n * Layout (88 bytes):\n * - discriminator: [u8; 8]\n * - authority: Pubkey (32 bytes)\n * - operator: Pubkey (32 bytes)\n * - market_count: u64 (8 bytes)\n * - paused: u8 (1 byte)\n * - bump: u8 (1 byte)\n * - _padding: [u8; 6]\n */\nexport function deserializeExchange(data: Buffer): Exchange {\n if (data.length < ACCOUNT_SIZE.EXCHANGE) {\n throw new Error(\n `Invalid Exchange data length: ${data.length}, expected ${ACCOUNT_SIZE.EXCHANGE}`\n );\n }\n\n validateDiscriminator(data, DISCRIMINATOR.EXCHANGE, \"Exchange\");\n\n let offset = 0;\n\n const discriminator = data.subarray(offset, offset + 8);\n offset += 8;\n\n const authority = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const operator = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const marketCount = fromLeBytes(data.subarray(offset, offset + 8));\n offset += 8;\n\n const paused = data[offset] !== 0;\n offset += 1;\n\n const bump = data[offset];\n offset += 1;\n\n // Skip padding: 6 bytes\n\n return {\n discriminator,\n authority,\n operator,\n marketCount,\n paused,\n bump,\n };\n}\n\n/**\n * Deserialize Market account data\n *\n * Layout (120 bytes):\n * - discriminator: [u8; 8]\n * - market_id: u64 (8 bytes)\n * - num_outcomes: u8 (1 byte)\n * - status: u8 (1 byte)\n * - winning_outcome: u8 (1 byte)\n * - has_winning_outcome: u8 (1 byte)\n * - bump: u8 (1 byte)\n * - _padding: [u8; 3]\n * - oracle: Pubkey (32 bytes)\n * - question_id: [u8; 32]\n * - condition_id: [u8; 32]\n */\nexport function deserializeMarket(data: Buffer): Market {\n if (data.length < ACCOUNT_SIZE.MARKET) {\n throw new Error(\n `Invalid Market data length: ${data.length}, expected ${ACCOUNT_SIZE.MARKET}`\n );\n }\n\n validateDiscriminator(data, DISCRIMINATOR.MARKET, \"Market\");\n\n let offset = 0;\n\n const discriminator = data.subarray(offset, offset + 8);\n offset += 8;\n\n const marketId = fromLeBytes(data.subarray(offset, offset + 8));\n offset += 8;\n\n const numOutcomes = data[offset];\n offset += 1;\n\n const statusByte = data[offset];\n offset += 1;\n\n const winningOutcome = data[offset];\n offset += 1;\n\n const hasWinningOutcome = data[offset] !== 0;\n offset += 1;\n\n const bump = data[offset];\n offset += 1;\n\n // Skip padding: 3 bytes\n offset += 3;\n\n const oracle = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const questionId = Buffer.from(data.subarray(offset, offset + 32));\n offset += 32;\n\n const conditionId = Buffer.from(data.subarray(offset, offset + 32));\n offset += 32;\n\n // Map status byte to enum\n let status: MarketStatus;\n switch (statusByte) {\n case 0:\n status = MarketStatus.Pending;\n break;\n case 1:\n status = MarketStatus.Active;\n break;\n case 2:\n status = MarketStatus.Resolved;\n break;\n case 3:\n status = MarketStatus.Cancelled;\n break;\n default:\n throw new Error(`Unknown market status: ${statusByte}`);\n }\n\n return {\n discriminator,\n marketId,\n numOutcomes,\n status,\n winningOutcome,\n hasWinningOutcome,\n bump,\n oracle,\n questionId,\n conditionId,\n };\n}\n\n/**\n * Deserialize OrderStatus account data\n *\n * Layout (24 bytes):\n * - discriminator: [u8; 8]\n * - remaining: u64 (8 bytes)\n * - is_cancelled: u8 (1 byte)\n * - _padding: [u8; 7]\n */\nexport function deserializeOrderStatus(data: Buffer): OrderStatus {\n if (data.length < ACCOUNT_SIZE.ORDER_STATUS) {\n throw new Error(\n `Invalid OrderStatus data length: ${data.length}, expected ${ACCOUNT_SIZE.ORDER_STATUS}`\n );\n }\n\n validateDiscriminator(data, DISCRIMINATOR.ORDER_STATUS, \"OrderStatus\");\n\n let offset = 0;\n\n const discriminator = data.subarray(offset, offset + 8);\n offset += 8;\n\n const remaining = fromLeBytes(data.subarray(offset, offset + 8));\n offset += 8;\n\n const isCancelled = data[offset] !== 0;\n offset += 1;\n\n // Skip padding: 7 bytes\n\n return {\n discriminator,\n remaining,\n isCancelled,\n };\n}\n\n/**\n * Deserialize UserNonce account data\n *\n * Layout (16 bytes):\n * - discriminator: [u8; 8]\n * - nonce: u64 (8 bytes)\n */\nexport function deserializeUserNonce(data: Buffer): UserNonce {\n if (data.length < ACCOUNT_SIZE.USER_NONCE) {\n throw new Error(\n `Invalid UserNonce data length: ${data.length}, expected ${ACCOUNT_SIZE.USER_NONCE}`\n );\n }\n\n validateDiscriminator(data, DISCRIMINATOR.USER_NONCE, \"UserNonce\");\n\n let offset = 0;\n\n const discriminator = data.subarray(offset, offset + 8);\n offset += 8;\n\n const nonce = fromLeBytes(data.subarray(offset, offset + 8));\n offset += 8;\n\n return {\n discriminator,\n nonce,\n };\n}\n\n/**\n * Deserialize Position account data\n *\n * Layout (80 bytes):\n * - discriminator: [u8; 8]\n * - owner: Pubkey (32 bytes)\n * - market: Pubkey (32 bytes)\n * - bump: u8 (1 byte)\n * - _padding: [u8; 7]\n */\nexport function deserializePosition(data: Buffer): Position {\n if (data.length < ACCOUNT_SIZE.POSITION) {\n throw new Error(\n `Invalid Position data length: ${data.length}, expected ${ACCOUNT_SIZE.POSITION}`\n );\n }\n\n validateDiscriminator(data, DISCRIMINATOR.POSITION, \"Position\");\n\n let offset = 0;\n\n const discriminator = data.subarray(offset, offset + 8);\n offset += 8;\n\n const owner = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const market = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const bump = data[offset];\n offset += 1;\n\n // Skip padding: 7 bytes\n\n return {\n discriminator,\n owner,\n market,\n bump,\n };\n}\n\n/**\n * Deserialize Orderbook account data\n *\n * Layout (144 bytes):\n * - discriminator: [u8; 8]\n * - market: Pubkey (32 bytes)\n * - mint_a: Pubkey (32 bytes)\n * - mint_b: Pubkey (32 bytes)\n * - lookup_table: Pubkey (32 bytes)\n * - bump: u8 (1 byte)\n * - _padding: [u8; 7]\n */\nexport function deserializeOrderbook(data: Buffer): Orderbook {\n if (data.length < ACCOUNT_SIZE.ORDERBOOK) {\n throw new Error(\n `Invalid Orderbook data length: ${data.length}, expected ${ACCOUNT_SIZE.ORDERBOOK}`\n );\n }\n\n validateDiscriminator(data, DISCRIMINATOR.ORDERBOOK, \"Orderbook\");\n\n let offset = 0;\n\n const discriminator = data.subarray(offset, offset + 8);\n offset += 8;\n\n const market = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const mintA = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const mintB = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const lookupTable = new PublicKey(data.subarray(offset, offset + 32));\n offset += 32;\n\n const bump = data[offset];\n offset += 1;\n\n // Skip padding: 7 bytes\n\n return {\n discriminator,\n market,\n mintA,\n mintB,\n lookupTable,\n bump,\n };\n}\n"]}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { PublicKey, Keypair } from "@solana/web3.js";
|
|
2
|
+
import { SignedOrder, OrderSide } from "./types";
|
|
3
|
+
import { OrderbookDecimals } from "../shared/scaling";
|
|
4
|
+
/**
|
|
5
|
+
* Fluent builder for creating orders.
|
|
6
|
+
* Matches the Rust SDK's OrderBuilder pattern.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const order = new OrderBuilder()
|
|
11
|
+
* .nonce(1)
|
|
12
|
+
* .maker(makerPubkey)
|
|
13
|
+
* .market(marketPubkey)
|
|
14
|
+
* .baseMint(baseMintPubkey)
|
|
15
|
+
* .quoteMint(quoteMintPubkey)
|
|
16
|
+
* .bid()
|
|
17
|
+
* .makerAmount(1000000n)
|
|
18
|
+
* .takerAmount(500000n)
|
|
19
|
+
* .expiration(0n)
|
|
20
|
+
* .build();
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare class OrderBuilder {
|
|
24
|
+
private _nonce;
|
|
25
|
+
private _maker;
|
|
26
|
+
private _market;
|
|
27
|
+
private _baseMint;
|
|
28
|
+
private _quoteMint;
|
|
29
|
+
private _side;
|
|
30
|
+
private _makerAmount;
|
|
31
|
+
private _takerAmount;
|
|
32
|
+
private _expiration;
|
|
33
|
+
/** Set the order nonce (u32) */
|
|
34
|
+
nonce(value: number): this;
|
|
35
|
+
/** Set the maker (signer) */
|
|
36
|
+
maker(value: PublicKey): this;
|
|
37
|
+
/** Set the market */
|
|
38
|
+
market(value: PublicKey): this;
|
|
39
|
+
/** Set the base mint (conditional token) */
|
|
40
|
+
baseMint(value: PublicKey): this;
|
|
41
|
+
/** Set the quote mint (payment token) */
|
|
42
|
+
quoteMint(value: PublicKey): this;
|
|
43
|
+
/** Set side to BID */
|
|
44
|
+
bid(): this;
|
|
45
|
+
/** Set side to ASK */
|
|
46
|
+
ask(): this;
|
|
47
|
+
/** Set the order side */
|
|
48
|
+
side(value: OrderSide): this;
|
|
49
|
+
/** Set maker amount (what maker gives) */
|
|
50
|
+
makerAmount(value: bigint): this;
|
|
51
|
+
/** Set taker amount (what maker receives) */
|
|
52
|
+
takerAmount(value: bigint): this;
|
|
53
|
+
/** Set expiration timestamp (0 = no expiration) */
|
|
54
|
+
expiration(value: bigint): this;
|
|
55
|
+
/**
|
|
56
|
+
* Set price and size, auto-computing maker_amount and taker_amount using decimal scaling.
|
|
57
|
+
*
|
|
58
|
+
* @param price - Price as a decimal string (e.g., "0.75")
|
|
59
|
+
* @param size - Size as a decimal string (e.g., "100")
|
|
60
|
+
* @param decimals - Orderbook decimal configuration
|
|
61
|
+
*/
|
|
62
|
+
price(priceStr: string, sizeStr: string, decimals: OrderbookDecimals): this;
|
|
63
|
+
/**
|
|
64
|
+
* Apply decimal scaling to convert price/size to maker_amount/taker_amount.
|
|
65
|
+
* Equivalent to calling price() but with separate method name for clarity.
|
|
66
|
+
*/
|
|
67
|
+
applyScaling(priceStr: string, sizeStr: string, decimals: OrderbookDecimals): this;
|
|
68
|
+
/**
|
|
69
|
+
* Build the unsigned SignedOrder (signature will be all zeros).
|
|
70
|
+
* All required fields must be set.
|
|
71
|
+
*/
|
|
72
|
+
build(): SignedOrder;
|
|
73
|
+
/**
|
|
74
|
+
* Build and sign the order with a Keypair.
|
|
75
|
+
*/
|
|
76
|
+
buildAndSign(keypair: Keypair): SignedOrder;
|
|
77
|
+
/**
|
|
78
|
+
* Build, sign, and convert to a SubmitOrderRequest.
|
|
79
|
+
*/
|
|
80
|
+
toSubmitRequest(keypair: Keypair, orderbookId: string): {
|
|
81
|
+
maker: string;
|
|
82
|
+
nonce: string;
|
|
83
|
+
market_pubkey: string;
|
|
84
|
+
base_token: string;
|
|
85
|
+
quote_token: string;
|
|
86
|
+
side: number;
|
|
87
|
+
maker_amount: string;
|
|
88
|
+
taker_amount: string;
|
|
89
|
+
expiration: number;
|
|
90
|
+
signature: string;
|
|
91
|
+
orderbook_id: string;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/program/builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEjD,OAAO,EAAkB,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtE;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAa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AG,WAAW;IAkB3C;;OAEG;IACH,eAAe,CACb,OAAO,EAAE,OAAO,EAChB,WAAW,EAAE,MAAM,GAClB;QACD,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,MAAM,CAAC;QACd,aAAa,EAAE,MAAM,CAAC;QACtB,UAAU,EAAE,MAAM,CAAC;QACnB,WAAW,EAAE,MAAM,CAAC;QACpB,IAAI,EAAE,MAAM,CAAC;QACb,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,MAAM,CAAC;QACrB,UAAU,EAAE,MAAM,CAAC;QACnB,SAAS,EAAE,MAAM,CAAC;QAClB,YAAY,EAAE,MAAM,CAAC;KACtB;CAgBF"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrderBuilder = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const orders_1 = require("./orders");
|
|
6
|
+
const scaling_1 = require("../shared/scaling");
|
|
7
|
+
/**
|
|
8
|
+
* Fluent builder for creating orders.
|
|
9
|
+
* Matches the Rust SDK's OrderBuilder pattern.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const order = new OrderBuilder()
|
|
14
|
+
* .nonce(1)
|
|
15
|
+
* .maker(makerPubkey)
|
|
16
|
+
* .market(marketPubkey)
|
|
17
|
+
* .baseMint(baseMintPubkey)
|
|
18
|
+
* .quoteMint(quoteMintPubkey)
|
|
19
|
+
* .bid()
|
|
20
|
+
* .makerAmount(1000000n)
|
|
21
|
+
* .takerAmount(500000n)
|
|
22
|
+
* .expiration(0n)
|
|
23
|
+
* .build();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
class OrderBuilder {
|
|
27
|
+
_nonce = 0;
|
|
28
|
+
_maker = null;
|
|
29
|
+
_market = null;
|
|
30
|
+
_baseMint = null;
|
|
31
|
+
_quoteMint = null;
|
|
32
|
+
_side = types_1.OrderSide.BID;
|
|
33
|
+
_makerAmount = 0n;
|
|
34
|
+
_takerAmount = 0n;
|
|
35
|
+
_expiration = 0n;
|
|
36
|
+
/** Set the order nonce (u32) */
|
|
37
|
+
nonce(value) {
|
|
38
|
+
this._nonce = value;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/** Set the maker (signer) */
|
|
42
|
+
maker(value) {
|
|
43
|
+
this._maker = value;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/** Set the market */
|
|
47
|
+
market(value) {
|
|
48
|
+
this._market = value;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
/** Set the base mint (conditional token) */
|
|
52
|
+
baseMint(value) {
|
|
53
|
+
this._baseMint = value;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/** Set the quote mint (payment token) */
|
|
57
|
+
quoteMint(value) {
|
|
58
|
+
this._quoteMint = value;
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
/** Set side to BID */
|
|
62
|
+
bid() {
|
|
63
|
+
this._side = types_1.OrderSide.BID;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/** Set side to ASK */
|
|
67
|
+
ask() {
|
|
68
|
+
this._side = types_1.OrderSide.ASK;
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
/** Set the order side */
|
|
72
|
+
side(value) {
|
|
73
|
+
this._side = value;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
/** Set maker amount (what maker gives) */
|
|
77
|
+
makerAmount(value) {
|
|
78
|
+
this._makerAmount = value;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
/** Set taker amount (what maker receives) */
|
|
82
|
+
takerAmount(value) {
|
|
83
|
+
this._takerAmount = value;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
/** Set expiration timestamp (0 = no expiration) */
|
|
87
|
+
expiration(value) {
|
|
88
|
+
this._expiration = value;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Set price and size, auto-computing maker_amount and taker_amount using decimal scaling.
|
|
93
|
+
*
|
|
94
|
+
* @param price - Price as a decimal string (e.g., "0.75")
|
|
95
|
+
* @param size - Size as a decimal string (e.g., "100")
|
|
96
|
+
* @param decimals - Orderbook decimal configuration
|
|
97
|
+
*/
|
|
98
|
+
price(priceStr, sizeStr, decimals) {
|
|
99
|
+
const { makerAmount, takerAmount } = (0, scaling_1.scalePriceSize)(priceStr, sizeStr, this._side, decimals);
|
|
100
|
+
this._makerAmount = makerAmount;
|
|
101
|
+
this._takerAmount = takerAmount;
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Apply decimal scaling to convert price/size to maker_amount/taker_amount.
|
|
106
|
+
* Equivalent to calling price() but with separate method name for clarity.
|
|
107
|
+
*/
|
|
108
|
+
applyScaling(priceStr, sizeStr, decimals) {
|
|
109
|
+
return this.price(priceStr, sizeStr, decimals);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Build the unsigned SignedOrder (signature will be all zeros).
|
|
113
|
+
* All required fields must be set.
|
|
114
|
+
*/
|
|
115
|
+
build() {
|
|
116
|
+
if (!this._maker)
|
|
117
|
+
throw new Error("OrderBuilder: maker is required");
|
|
118
|
+
if (!this._market)
|
|
119
|
+
throw new Error("OrderBuilder: market is required");
|
|
120
|
+
if (!this._baseMint)
|
|
121
|
+
throw new Error("OrderBuilder: baseMint is required");
|
|
122
|
+
if (!this._quoteMint)
|
|
123
|
+
throw new Error("OrderBuilder: quoteMint is required");
|
|
124
|
+
return {
|
|
125
|
+
nonce: this._nonce,
|
|
126
|
+
maker: this._maker,
|
|
127
|
+
market: this._market,
|
|
128
|
+
baseMint: this._baseMint,
|
|
129
|
+
quoteMint: this._quoteMint,
|
|
130
|
+
side: this._side,
|
|
131
|
+
makerAmount: this._makerAmount,
|
|
132
|
+
takerAmount: this._takerAmount,
|
|
133
|
+
expiration: this._expiration,
|
|
134
|
+
signature: Buffer.alloc(64),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Build and sign the order with a Keypair.
|
|
139
|
+
*/
|
|
140
|
+
buildAndSign(keypair) {
|
|
141
|
+
const unsigned = this.build();
|
|
142
|
+
return (0, orders_1.signOrderFull)({
|
|
143
|
+
nonce: unsigned.nonce,
|
|
144
|
+
maker: unsigned.maker,
|
|
145
|
+
market: unsigned.market,
|
|
146
|
+
baseMint: unsigned.baseMint,
|
|
147
|
+
quoteMint: unsigned.quoteMint,
|
|
148
|
+
side: unsigned.side,
|
|
149
|
+
makerAmount: unsigned.makerAmount,
|
|
150
|
+
takerAmount: unsigned.takerAmount,
|
|
151
|
+
expiration: unsigned.expiration,
|
|
152
|
+
}, keypair);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Build, sign, and convert to a SubmitOrderRequest.
|
|
156
|
+
*/
|
|
157
|
+
toSubmitRequest(keypair, orderbookId) {
|
|
158
|
+
const signed = this.buildAndSign(keypair);
|
|
159
|
+
return {
|
|
160
|
+
maker: signed.maker.toBase58(),
|
|
161
|
+
nonce: signed.nonce.toString(),
|
|
162
|
+
market_pubkey: signed.market.toBase58(),
|
|
163
|
+
base_token: signed.baseMint.toBase58(),
|
|
164
|
+
quote_token: signed.quoteMint.toBase58(),
|
|
165
|
+
side: signed.side,
|
|
166
|
+
maker_amount: signed.makerAmount.toString(),
|
|
167
|
+
taker_amount: signed.takerAmount.toString(),
|
|
168
|
+
expiration: Number(signed.expiration),
|
|
169
|
+
signature: (0, orders_1.signatureHex)(signed),
|
|
170
|
+
orderbook_id: orderbookId,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
exports.OrderBuilder = OrderBuilder;
|
|
175
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3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AC,KAAK,CAAC,QAAQ,EAAE;YAC9B,aAAa,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE;YACvC,UAAU,EAAE,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE;YACtC,WAAW,EAAE,MAAM,CAAC,SAAS,CAAC,QAAQ,EAAE;YACxC,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,YAAY,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,EAAE;YAC3C,YAAY,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,EAAE;YAC3C,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC;YACrC,SAAS,EAAE,IAAA,qBAAY,EAAC,MAAM,CAAC;YAC/B,YAAY,EAAE,WAAW;SAC1B,CAAC;IACJ,CAAC;CACF;AA3LD,oCA2LC","sourcesContent":["import { PublicKey, Keypair } from \"@solana/web3.js\";\nimport { SignedOrder, OrderSide } from \"./types\";\nimport { signOrderFull, signatureHex } from \"./orders\";\nimport { scalePriceSize, OrderbookDecimals } from \"../shared/scaling\";\n\n/**\n * Fluent builder for creating orders.\n * Matches the Rust SDK's OrderBuilder pattern.\n *\n * @example\n * ```typescript\n * const order = new OrderBuilder()\n * .nonce(1)\n * .maker(makerPubkey)\n * .market(marketPubkey)\n * .baseMint(baseMintPubkey)\n * .quoteMint(quoteMintPubkey)\n * .bid()\n * .makerAmount(1000000n)\n * .takerAmount(500000n)\n * .expiration(0n)\n * .build();\n * ```\n */\nexport class OrderBuilder {\n private _nonce: number = 0;\n private _maker: PublicKey | null = null;\n private _market: PublicKey | null = null;\n private _baseMint: PublicKey | null = null;\n private _quoteMint: PublicKey | null = null;\n private _side: OrderSide = OrderSide.BID;\n private _makerAmount: bigint = 0n;\n private _takerAmount: bigint = 0n;\n private _expiration: bigint = 0n;\n\n /** Set the order nonce (u32) */\n nonce(value: number): this {\n this._nonce = value;\n return this;\n }\n\n /** Set the maker (signer) */\n maker(value: PublicKey): this {\n this._maker = value;\n return this;\n }\n\n /** Set the market */\n market(value: PublicKey): this {\n this._market = value;\n return this;\n }\n\n /** Set the base mint (conditional token) */\n baseMint(value: PublicKey): this {\n this._baseMint = value;\n return this;\n }\n\n /** Set the quote mint (payment token) */\n quoteMint(value: PublicKey): this {\n this._quoteMint = value;\n return this;\n }\n\n /** Set side to BID */\n bid(): this {\n this._side = OrderSide.BID;\n return this;\n }\n\n /** Set side to ASK */\n ask(): this {\n this._side = OrderSide.ASK;\n return this;\n }\n\n /** Set the order side */\n side(value: OrderSide): this {\n this._side = value;\n return this;\n }\n\n /** Set maker amount (what maker gives) */\n makerAmount(value: bigint): this {\n this._makerAmount = value;\n return this;\n }\n\n /** Set taker amount (what maker receives) */\n takerAmount(value: bigint): this {\n this._takerAmount = value;\n return this;\n }\n\n /** Set expiration timestamp (0 = no expiration) */\n expiration(value: bigint): this {\n this._expiration = value;\n return this;\n }\n\n /**\n * Set price and size, auto-computing maker_amount and taker_amount using decimal scaling.\n *\n * @param price - Price as a decimal string (e.g., \"0.75\")\n * @param size - Size as a decimal string (e.g., \"100\")\n * @param decimals - Orderbook decimal configuration\n */\n price(priceStr: string, sizeStr: string, decimals: OrderbookDecimals): this {\n const { makerAmount, takerAmount } = scalePriceSize(\n priceStr,\n sizeStr,\n this._side,\n decimals\n );\n this._makerAmount = makerAmount;\n this._takerAmount = takerAmount;\n return this;\n }\n\n /**\n * Apply decimal scaling to convert price/size to maker_amount/taker_amount.\n * Equivalent to calling price() but with separate method name for clarity.\n */\n applyScaling(\n priceStr: string,\n sizeStr: string,\n decimals: OrderbookDecimals\n ): this {\n return this.price(priceStr, sizeStr, decimals);\n }\n\n /**\n * Build the unsigned SignedOrder (signature will be all zeros).\n * All required fields must be set.\n */\n build(): SignedOrder {\n if (!this._maker) throw new Error(\"OrderBuilder: maker is required\");\n if (!this._market) throw new Error(\"OrderBuilder: market is required\");\n if (!this._baseMint) throw new Error(\"OrderBuilder: baseMint is required\");\n if (!this._quoteMint) throw new Error(\"OrderBuilder: quoteMint is required\");\n\n return {\n nonce: this._nonce,\n maker: this._maker,\n market: this._market,\n baseMint: this._baseMint,\n quoteMint: this._quoteMint,\n side: this._side,\n makerAmount: this._makerAmount,\n takerAmount: this._takerAmount,\n expiration: this._expiration,\n signature: Buffer.alloc(64),\n };\n }\n\n /**\n * Build and sign the order with a Keypair.\n */\n buildAndSign(keypair: Keypair): SignedOrder {\n const unsigned = this.build();\n return signOrderFull(\n {\n nonce: unsigned.nonce,\n maker: unsigned.maker,\n market: unsigned.market,\n baseMint: unsigned.baseMint,\n quoteMint: unsigned.quoteMint,\n side: unsigned.side,\n makerAmount: unsigned.makerAmount,\n takerAmount: unsigned.takerAmount,\n expiration: unsigned.expiration,\n },\n keypair\n );\n }\n\n /**\n * Build, sign, and convert to a SubmitOrderRequest.\n */\n toSubmitRequest(\n keypair: Keypair,\n orderbookId: string\n ): {\n maker: string;\n nonce: string;\n market_pubkey: string;\n base_token: string;\n quote_token: string;\n side: number;\n maker_amount: string;\n taker_amount: string;\n expiration: number;\n signature: string;\n orderbook_id: string;\n } {\n const signed = this.buildAndSign(keypair);\n return {\n maker: signed.maker.toBase58(),\n nonce: signed.nonce.toString(),\n market_pubkey: signed.market.toBase58(),\n base_token: signed.baseMint.toBase58(),\n quote_token: signed.quoteMint.toBase58(),\n side: signed.side,\n maker_amount: signed.makerAmount.toString(),\n taker_amount: signed.takerAmount.toString(),\n expiration: Number(signed.expiration),\n signature: signatureHex(signed),\n orderbook_id: orderbookId,\n };\n }\n}\n"]}
|