@parchment-trade/sdk 0.0.0-stage → 1.0.1

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.
Files changed (95) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +193 -2
  3. package/REFERENCE.md +338 -0
  4. package/SECURITY.md +25 -0
  5. package/dist/general/providers/index.d.ts +2 -0
  6. package/dist/general/providers/index.js +2 -0
  7. package/dist/general/providers/parchment.d.ts +1 -0
  8. package/dist/general/providers/parchment.js +1 -0
  9. package/dist/general/providers/parchmentSdk.d.ts +23 -0
  10. package/dist/general/providers/parchmentSdk.js +11 -0
  11. package/dist/general/types/configTypes.d.ts +62 -0
  12. package/dist/general/types/configTypes.js +1 -0
  13. package/dist/general/types/index.d.ts +1 -0
  14. package/dist/general/types/index.js +1 -0
  15. package/dist/general/utils/address.d.ts +49 -0
  16. package/dist/general/utils/address.js +110 -0
  17. package/dist/general/utils/caller.d.ts +58 -0
  18. package/dist/general/utils/caller.js +182 -0
  19. package/dist/general/utils/errors.d.ts +16 -0
  20. package/dist/general/utils/errors.js +26 -0
  21. package/dist/general/utils/index.d.ts +4 -0
  22. package/dist/general/utils/index.js +4 -0
  23. package/dist/general/utils/jsBigInt.d.ts +26 -0
  24. package/dist/general/utils/jsBigInt.js +74 -0
  25. package/dist/general/utils/txShape.d.ts +8 -0
  26. package/dist/general/utils/txShape.js +39 -0
  27. package/dist/index.d.ts +4 -0
  28. package/dist/index.js +4 -0
  29. package/dist/packages/index.d.ts +11 -0
  30. package/dist/packages/index.js +5 -0
  31. package/dist/packages/parchment/abortCodes.d.ts +196 -0
  32. package/dist/packages/parchment/abortCodes.js +522 -0
  33. package/dist/packages/parchment/batchClose.d.ts +268 -0
  34. package/dist/packages/parchment/batchClose.js +524 -0
  35. package/dist/packages/parchment/eventGuard.d.ts +106 -0
  36. package/dist/packages/parchment/eventGuard.js +119 -0
  37. package/dist/packages/parchment/gameCounterEvents.d.ts +59 -0
  38. package/dist/packages/parchment/gameCounterEvents.js +56 -0
  39. package/dist/packages/parchment/index.d.ts +52 -0
  40. package/dist/packages/parchment/index.js +30 -0
  41. package/dist/packages/parchment/liquidationCredit.d.ts +28 -0
  42. package/dist/packages/parchment/liquidationCredit.js +25 -0
  43. package/dist/packages/parchment/lockedMintEvents.d.ts +42 -0
  44. package/dist/packages/parchment/lockedMintEvents.js +46 -0
  45. package/dist/packages/parchment/monitoring.d.ts +369 -0
  46. package/dist/packages/parchment/monitoring.js +259 -0
  47. package/dist/packages/parchment/notes.d.ts +6 -0
  48. package/dist/packages/parchment/notes.js +1 -0
  49. package/dist/packages/parchment/onboarding.d.ts +50 -0
  50. package/dist/packages/parchment/onboarding.js +48 -0
  51. package/dist/packages/parchment/orderEffects.d.ts +207 -0
  52. package/dist/packages/parchment/orderEffects.js +268 -0
  53. package/dist/packages/parchment/orderParams.d.ts +72 -0
  54. package/dist/packages/parchment/orderParams.js +71 -0
  55. package/dist/packages/parchment/packageIds.d.ts +53 -0
  56. package/dist/packages/parchment/packageIds.js +149 -0
  57. package/dist/packages/parchment/parchment.d.ts +442 -0
  58. package/dist/packages/parchment/parchment.js +975 -0
  59. package/dist/packages/parchment/parchmentAccount.d.ts +189 -0
  60. package/dist/packages/parchment/parchmentAccount.js +273 -0
  61. package/dist/packages/parchment/parchmentMarket.d.ts +58 -0
  62. package/dist/packages/parchment/parchmentMarket.js +86 -0
  63. package/dist/packages/parchment/parchmentTypes.d.ts +2031 -0
  64. package/dist/packages/parchment/parchmentTypes.js +65 -0
  65. package/dist/packages/parchment/publicInts.d.ts +45 -0
  66. package/dist/packages/parchment/publicInts.js +383 -0
  67. package/dist/packages/parchment/reconcileEvents.d.ts +24 -0
  68. package/dist/packages/parchment/reconcileEvents.js +30 -0
  69. package/dist/packages/parchment/retainedFee.d.ts +43 -0
  70. package/dist/packages/parchment/retainedFee.js +68 -0
  71. package/dist/packages/parchment/sessionClose.d.ts +68 -0
  72. package/dist/packages/parchment/sessionClose.js +50 -0
  73. package/dist/packages/parchment/sessionKey.d.ts +78 -0
  74. package/dist/packages/parchment/sessionKey.js +154 -0
  75. package/dist/packages/parchment/slippage.d.ts +23 -0
  76. package/dist/packages/parchment/slippage.js +27 -0
  77. package/dist/packages/parchment/sponsor.d.ts +39 -0
  78. package/dist/packages/parchment/sponsor.js +48 -0
  79. package/dist/packages/parchment/stakingCalls.d.ts +33 -0
  80. package/dist/packages/parchment/stakingCalls.js +21 -0
  81. package/dist/packages/parchment/stakingEvents.d.ts +53 -0
  82. package/dist/packages/parchment/stakingEvents.js +71 -0
  83. package/dist/packages/parchment/triggerCalls.d.ts +46 -0
  84. package/dist/packages/parchment/triggerCalls.js +36 -0
  85. package/dist/packages/parchment/triggerEvents.d.ts +22 -0
  86. package/dist/packages/parchment/triggerEvents.js +33 -0
  87. package/dist/packages/parchment/triggers.d.ts +113 -0
  88. package/dist/packages/parchment/triggers.js +183 -0
  89. package/dist/packages/parchment/txBodies.d.ts +111 -0
  90. package/dist/packages/parchment/txBodies.js +683 -0
  91. package/dist/packages/parchment/txIntent.d.ts +99 -0
  92. package/dist/packages/parchment/txIntent.js +611 -0
  93. package/dist/types.d.ts +2 -0
  94. package/dist/types.js +1 -0
  95. package/package.json +58 -4
@@ -0,0 +1,683 @@
1
+ import { baseUnit, coinDigest, compact, normalizeAddress, signedUpdateBase64, u128Unit, u64Unit } from "../../general/utils/address.js";
2
+ import { ParchmentApiError } from "../../general/utils/errors.js";
3
+ import { toJsBigInt } from "../../general/utils/jsBigInt.js";
4
+ function u64Wire(value, field, code) {
5
+ return toJsBigInt(BigInt(u64Unit(value, field, code)));
6
+ }
7
+ function bool(value, field) {
8
+ if (typeof value !== "boolean") {
9
+ throw new ParchmentApiError(0, "bad_request", `${field} must be a boolean`);
10
+ }
11
+ return value;
12
+ }
13
+ function coin(value) {
14
+ if (!value)
15
+ return undefined;
16
+ return {
17
+ objectId: normalizeAddress(value.objectId),
18
+ version: u64Wire(value.version, "coin.version", "bad_amount"),
19
+ digest: coinDigest(value.digest),
20
+ };
21
+ }
22
+ function shared(value) {
23
+ if (!value)
24
+ return undefined;
25
+ const out = {};
26
+ for (const [id, version] of Object.entries(value)) {
27
+ out[normalizeAddress(id)] = u64Wire(version, "shared version", "bad_amount");
28
+ }
29
+ return out;
30
+ }
31
+ function update(value) {
32
+ return signedUpdateBase64(value);
33
+ }
34
+ export function openPreviewBody(input) {
35
+ return {
36
+ marketId: normalizeAddress(input.marketId),
37
+ margin: u64Wire(input.margin, "margin"),
38
+ leverage: u64Wire(input.leverage, "leverage"),
39
+ isLong: bool(input.isLong, "isLong"),
40
+ ...compact({
41
+ accountId: input.accountId === undefined ? undefined : normalizeAddress(input.accountId),
42
+ minFactorBps: input.minFactorBps === undefined ? undefined : u64Wire(input.minFactorBps, "minFactorBps"),
43
+ slippageBps: previewSlippageBps(input.slippageBps),
44
+ acceptableEntryWad: optionalBound(input.acceptableEntryWad ?? input.acceptablePriceWad, "acceptableEntryWad"),
45
+ topUp: optionalBaseUnit(input.topUp, "topUp"),
46
+ }),
47
+ };
48
+ }
49
+ export function closePreviewBody(input) {
50
+ return compact({
51
+ positionId: u64Wire(input.positionId, "positionId", "bad_request"),
52
+ nowMs: input.nowMs === undefined ? undefined : u64Wire(input.nowMs, "nowMs", "bad_request"),
53
+ slippageBps: previewSlippageBps(input.slippageBps),
54
+ acceptableExitWad: optionalBound(input.acceptableExitWad ?? input.acceptablePriceWad, "acceptableExitWad"),
55
+ topUp: optionalBaseUnit(input.topUp, "topUp"),
56
+ });
57
+ }
58
+ export function openAccountTxBody(input) {
59
+ return compact({
60
+ sender: normalizeAddress(input.walletAddress),
61
+ shared: shared(input.shared),
62
+ });
63
+ }
64
+ function objectDigest(value, field) {
65
+ if (value.length === 0 || value.length > 128 || !/^[0-9A-Za-z]+$/.test(value)) {
66
+ throw new ParchmentApiError(0, "bad_request", `${field} must be a base58 digest`);
67
+ }
68
+ return value;
69
+ }
70
+ function authorityDigest(value) {
71
+ return objectDigest(value, "authorityCapDigest");
72
+ }
73
+ const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
74
+ export function builderCode(value, lengthCode = "bad_request") {
75
+ const bytes = new TextEncoder().encode(value).length;
76
+ if (bytes < 1 || bytes > 32) {
77
+ throw new ParchmentApiError(0, lengthCode, "builderCode must be 1 to 32 bytes");
78
+ }
79
+ if (value.includes("\u0000") || LONE_SURROGATE.test(value)) {
80
+ throw new ParchmentApiError(0, lengthCode, "builderCode must be printable UTF-8 (no NUL, no lone surrogate)");
81
+ }
82
+ return value;
83
+ }
84
+ function capDigest(value) {
85
+ return objectDigest(value, "accountCapDigest");
86
+ }
87
+ function capRef(input) {
88
+ return {
89
+ accountCapId: normalizeAddress(input.accountCapId),
90
+ accountCapVersion: input.accountCapVersion === undefined ? undefined : u64Wire(input.accountCapVersion, "accountCapVersion", "bad_amount"),
91
+ accountCapDigest: input.accountCapDigest === undefined ? undefined : capDigest(input.accountCapDigest),
92
+ };
93
+ }
94
+ export function depositTxBody(input) {
95
+ return compact({
96
+ sender: normalizeAddress(input.walletAddress),
97
+ accountId: normalizeAddress(input.accountId),
98
+ ...capRef(input),
99
+ coin: coin(input.coin),
100
+ shared: shared(input.shared),
101
+ });
102
+ }
103
+ export function withdrawTxBody(input) {
104
+ return compact({
105
+ sender: normalizeAddress(input.walletAddress),
106
+ accountId: normalizeAddress(input.accountId),
107
+ ...capRef(input),
108
+ amount: u64Wire(input.amount, "amount", "bad_amount"),
109
+ shared: shared(input.shared),
110
+ });
111
+ }
112
+ function previewSlippageBps(value) {
113
+ if (value === undefined)
114
+ return undefined;
115
+ const bps = BigInt(baseUnit(value, "slippageBps"));
116
+ if (bps > 10000n) {
117
+ throw new ParchmentApiError(0, "bad_request", "slippageBps must be at most 10000 (100%)");
118
+ }
119
+ return toJsBigInt(bps);
120
+ }
121
+ function optionalBaseUnit(value, field) {
122
+ return value === undefined ? undefined : toJsBigInt(BigInt(baseUnit(value, field)));
123
+ }
124
+ function optionalBound(value, field) {
125
+ return value === undefined ? undefined : toJsBigInt(BigInt(u128Unit(value, field)));
126
+ }
127
+ export function requestOpenTxBody(input) {
128
+ return compact({
129
+ sender: normalizeAddress(input.walletAddress),
130
+ accountId: normalizeAddress(input.accountId),
131
+ ...capRef(input),
132
+ marketId: normalizeAddress(input.marketId),
133
+ margin: u64Wire(input.margin, "margin", "bad_amount"),
134
+ leverage: u64Wire(input.leverage, "leverage", "bad_amount"),
135
+ isLong: bool(input.isLong, "isLong"),
136
+ minFactorBps: u64Wire(input.minFactorBps ?? 0n, "minFactorBps", "bad_amount"),
137
+ acceptablePriceWad: openBound(input.acceptablePriceWad),
138
+ ...builderRef(input),
139
+ topUp: coin(input.topUp),
140
+ shared: shared(input.shared),
141
+ });
142
+ }
143
+ export const DELEGATE_FILL_MARGIN_MS = 60000n;
144
+ function liveMs(value, field) {
145
+ const text = typeof value === "number" ? (Number.isSafeInteger(value) && value >= 0 ? String(value) : "") : String(value).trim();
146
+ if (!/^[0-9]+$/.test(text) || BigInt(text) > 0xffffffffffffffffn) {
147
+ throw new ParchmentApiError(0, "bad_amount", `${field} must be a u64`);
148
+ }
149
+ return BigInt(text);
150
+ }
151
+ export function assertDelegateLiveThroughFill(delegation) {
152
+ const expiry = liveMs(delegation.expiryMs, "delegation.expiryMs");
153
+ const deadline = liveMs(delegation.fillDeadlineMs, "delegation.fillDeadlineMs");
154
+ const now = liveMs(delegation.nowMs ?? Date.now(), "delegation.nowMs");
155
+ if (expiry <= now + deadline + DELEGATE_FILL_MARGIN_MS) {
156
+ throw new ParchmentApiError(0, "delegate_expires_before_deadline", "The trading session expires before this order could fill; renew the session and try again");
157
+ }
158
+ }
159
+ export function requestOpenAsDelegateTxBody(input) {
160
+ if (input.delegation !== undefined)
161
+ assertDelegateLiveThroughFill(input.delegation);
162
+ return compact({
163
+ sender: normalizeAddress(input.walletAddress),
164
+ accountId: normalizeAddress(input.accountId),
165
+ marketId: normalizeAddress(input.marketId),
166
+ margin: u64Wire(input.margin, "margin", "bad_amount"),
167
+ leverage: u64Wire(input.leverage, "leverage", "bad_amount"),
168
+ isLong: bool(input.isLong, "isLong"),
169
+ minFactorBps: u64Wire(input.minFactorBps ?? 0n, "minFactorBps", "bad_amount"),
170
+ acceptablePriceWad: openBound(input.acceptablePriceWad),
171
+ ...builderRef(input),
172
+ shared: shared(input.shared),
173
+ });
174
+ }
175
+ function builderIdField(value) {
176
+ const text = typeof value === "bigint" ? value.toString() : typeof value === "string" ? value.trim() : "";
177
+ const hex = /^0[xX]([0-9a-fA-F]+)$/.exec(text);
178
+ const id = hex ? BigInt(`0x${hex[1]}`) : /^[0-9]+$/.test(text) ? BigInt(text) : null;
179
+ if (id === null || id > 0xffffffffn)
180
+ throw new ParchmentApiError(0, "bad_amount", "builderId must be a u32");
181
+ return toJsBigInt(id);
182
+ }
183
+ function noBuilder(value) {
184
+ return value === undefined || value === null || value === "";
185
+ }
186
+ function builderRef(input) {
187
+ const id = noBuilder(input.builderId) ? undefined : input.builderId;
188
+ const code = noBuilder(input.builderCode) ? undefined : input.builderCode;
189
+ if (id !== undefined && code !== undefined) {
190
+ throw new ParchmentApiError(0, "bad_request", "pass builderId or builderCode, not both");
191
+ }
192
+ return {
193
+ builderId: id === undefined ? undefined : builderIdField(id),
194
+ builderCode: code === undefined ? undefined : builderCode(code, "bad_code"),
195
+ };
196
+ }
197
+ function openBound(value) {
198
+ if (value === undefined || value === null || (typeof value === "string" && value.trim() === "")) {
199
+ throw new ParchmentApiError(0, "price_bound_required", "acceptablePriceWad is required for opens and must be non-zero");
200
+ }
201
+ const bound = BigInt(u128Unit(value, "acceptablePriceWad"));
202
+ if (bound === 0n)
203
+ throw new ParchmentApiError(0, "price_bound_required", "acceptablePriceWad is required for opens and must be non-zero");
204
+ return toJsBigInt(bound);
205
+ }
206
+ function closeBound(value) {
207
+ if (value === undefined || value === null || (typeof value === "string" && value.trim() === ""))
208
+ return undefined;
209
+ return toJsBigInt(BigInt(u128Unit(value, "acceptablePriceWad")));
210
+ }
211
+ export function requestCloseTxBody(input) {
212
+ return compact({
213
+ sender: normalizeAddress(input.walletAddress),
214
+ accountId: normalizeAddress(input.accountId),
215
+ ...capRef(input),
216
+ marketId: normalizeAddress(input.marketId),
217
+ positionId: u64Wire(input.positionId, "positionId", "bad_amount"),
218
+ acceptablePriceWad: closeBound(input.acceptablePriceWad),
219
+ topUp: coin(input.topUp),
220
+ shared: shared(input.shared),
221
+ });
222
+ }
223
+ export function requestCloseAsDelegateTxBody(input) {
224
+ if (input.delegation !== undefined)
225
+ assertDelegateLiveThroughFill(input.delegation);
226
+ return compact({
227
+ sender: normalizeAddress(input.walletAddress),
228
+ accountId: normalizeAddress(input.accountId),
229
+ marketId: normalizeAddress(input.marketId),
230
+ positionId: u64Wire(input.positionId, "positionId", "bad_amount"),
231
+ acceptablePriceWad: closeBound(input.acceptablePriceWad),
232
+ shared: shared(input.shared),
233
+ });
234
+ }
235
+ export function cancelOrderTxBody(input) {
236
+ return compact({
237
+ sender: normalizeAddress(input.walletAddress),
238
+ accountId: normalizeAddress(input.accountId),
239
+ ...capRef(input),
240
+ orderId: normalizeAddress(input.orderId),
241
+ shared: shared(input.shared),
242
+ });
243
+ }
244
+ export function orderRefTxBody(input) {
245
+ return compact({
246
+ sender: normalizeAddress(input.walletAddress),
247
+ accountId: normalizeAddress(input.accountId),
248
+ orderId: normalizeAddress(input.orderId),
249
+ shared: shared(input.shared),
250
+ });
251
+ }
252
+ export function expireOrderTxBody(input) {
253
+ return compact({
254
+ sender: normalizeAddress(input.walletAddress),
255
+ accountId: normalizeAddress(input.accountId),
256
+ orderId: normalizeAddress(input.orderId),
257
+ marketId: input.marketId === undefined ? undefined : normalizeAddress(input.marketId),
258
+ updateBase64: input.update === undefined ? undefined : update(input.update),
259
+ shared: shared(input.shared),
260
+ });
261
+ }
262
+ export function emergencyCloseTxBody(input) {
263
+ return compact({
264
+ sender: normalizeAddress(input.walletAddress),
265
+ accountId: normalizeAddress(input.accountId),
266
+ ...capRef(input),
267
+ marketId: normalizeAddress(input.marketId),
268
+ orderId: normalizeAddress(input.orderId),
269
+ updateBase64: input.update === undefined ? undefined : update(input.update),
270
+ shared: shared(input.shared),
271
+ });
272
+ }
273
+ export function emergencyCloseAsDelegateTxBody(input) {
274
+ return compact({
275
+ sender: normalizeAddress(input.walletAddress),
276
+ accountId: normalizeAddress(input.accountId),
277
+ marketId: normalizeAddress(input.marketId),
278
+ orderId: normalizeAddress(input.orderId),
279
+ updateBase64: input.update === undefined ? undefined : update(input.update),
280
+ shared: shared(input.shared),
281
+ });
282
+ }
283
+ export function closeRetiredTxBody(input) {
284
+ return compact({
285
+ sender: normalizeAddress(input.walletAddress),
286
+ accountId: normalizeAddress(input.accountId),
287
+ ...capRef(input),
288
+ marketId: normalizeAddress(input.marketId),
289
+ positionId: u64Wire(input.positionId, "positionId", "bad_amount"),
290
+ updateBase64: input.update === undefined ? undefined : update(input.update),
291
+ shared: shared(input.shared),
292
+ });
293
+ }
294
+ export function closeRetiredAsDelegateTxBody(input) {
295
+ return compact({
296
+ sender: normalizeAddress(input.walletAddress),
297
+ accountId: normalizeAddress(input.accountId),
298
+ marketId: normalizeAddress(input.marketId),
299
+ positionId: u64Wire(input.positionId, "positionId", "bad_amount"),
300
+ updateBase64: input.update === undefined ? undefined : update(input.update),
301
+ shared: shared(input.shared),
302
+ });
303
+ }
304
+ export function liquidateTxBody(input) {
305
+ return compact({
306
+ sender: normalizeAddress(input.walletAddress),
307
+ marketId: normalizeAddress(input.marketId),
308
+ positionId: u64Wire(input.positionId, "positionId", "bad_amount"),
309
+ updateBase64: update(input.update),
310
+ shared: shared(input.shared),
311
+ });
312
+ }
313
+ const DEFAULT_SWEEP_BATCH = 8n;
314
+ const MAX_SWEEP_BATCH = 8n;
315
+ export function sweepBatch(value) {
316
+ const amount = BigInt(baseUnit(value ?? DEFAULT_SWEEP_BATCH, "maxEntries", "bad_amount"));
317
+ if (amount < 1n || amount > MAX_SWEEP_BATCH) {
318
+ throw new ParchmentApiError(0, "bad_amount", "maxEntries must be from 1 to 8");
319
+ }
320
+ return toJsBigInt(amount);
321
+ }
322
+ export function sweepTxBody(input) {
323
+ return compact({
324
+ sender: normalizeAddress(input.walletAddress),
325
+ marketId: normalizeAddress(input.marketId),
326
+ maxEntries: sweepBatch(input.maxEntries),
327
+ updateBase64: update(input.update),
328
+ shared: shared(input.shared),
329
+ });
330
+ }
331
+ export function sweepSurplusTxBody(input) {
332
+ return compact({
333
+ sender: normalizeAddress(input.walletAddress),
334
+ shared: shared(input.shared),
335
+ });
336
+ }
337
+ export function claimPendingMintTxBody(input) {
338
+ return compact({
339
+ sender: normalizeAddress(input.walletAddress),
340
+ accountId: normalizeAddress(input.accountId),
341
+ ...capRef(input),
342
+ shared: shared(input.shared),
343
+ });
344
+ }
345
+ const DEFAULT_HARVEST_BATCH = 32n;
346
+ const MAX_HARVEST_BATCH = 128n;
347
+ export function harvestBatch(value) {
348
+ const text = baseUnit(value ?? DEFAULT_HARVEST_BATCH, "maxEntries", "bad_amount");
349
+ const amount = BigInt(text);
350
+ if (amount < 1n || amount > MAX_HARVEST_BATCH) {
351
+ throw new ParchmentApiError(0, "bad_amount", "maxEntries must be from 1 to 128");
352
+ }
353
+ return toJsBigInt(amount);
354
+ }
355
+ export function lpHarvestTxBody(input) {
356
+ return compact({
357
+ sender: normalizeAddress(input.walletAddress),
358
+ maxEntries: harvestBatch(input.maxEntries),
359
+ shared: shared(input.shared),
360
+ });
361
+ }
362
+ const ZERO_ADDRESS = `0x${"0".repeat(64)}`;
363
+ function referrerAddress(value) {
364
+ const referrer = normalizeAddress(value);
365
+ if (referrer === ZERO_ADDRESS) {
366
+ throw new ParchmentApiError(0, "zero_referrer", "referrer cannot be the zero address (0x0)");
367
+ }
368
+ return referrer;
369
+ }
370
+ export function bindReferrerTxBody(input) {
371
+ return compact({
372
+ sender: normalizeAddress(input.walletAddress),
373
+ referrer: referrerAddress(input.referrer),
374
+ shared: shared(input.shared),
375
+ });
376
+ }
377
+ export function bindTradingReferrerTxBody(input) {
378
+ return compact({
379
+ sender: normalizeAddress(input.walletAddress),
380
+ accountId: normalizeAddress(input.accountId),
381
+ referrer: referrerAddress(input.referrer),
382
+ ...capRef(input),
383
+ shared: shared(input.shared),
384
+ });
385
+ }
386
+ export function transferAccountTxBody(input) {
387
+ const sender = normalizeAddress(input.walletAddress);
388
+ const recipient = normalizeAddress(input.recipient);
389
+ if (BigInt(recipient) === 0n) {
390
+ throw new ParchmentApiError(0, "zero_recipient", "recipient cannot be the zero address (0x0)");
391
+ }
392
+ if (recipient === sender) {
393
+ throw new ParchmentApiError(0, "self_transfer", "the account is already yours");
394
+ }
395
+ return compact({
396
+ sender,
397
+ accountId: normalizeAddress(input.accountId),
398
+ recipient,
399
+ ...capRef(input),
400
+ shared: shared(input.shared),
401
+ });
402
+ }
403
+ export function stakeTxBody(input) {
404
+ return compact({
405
+ sender: normalizeAddress(input.walletAddress),
406
+ coin: coin(input.coin),
407
+ shared: shared(input.shared),
408
+ });
409
+ }
410
+ export function unstakeTxBody(input) {
411
+ return compact({
412
+ sender: normalizeAddress(input.walletAddress),
413
+ amount: u64Wire(input.amount, "amount", "bad_amount"),
414
+ shared: shared(input.shared),
415
+ });
416
+ }
417
+ export function claimTxBody(input) {
418
+ return compact({
419
+ sender: normalizeAddress(input.walletAddress),
420
+ shared: shared(input.shared),
421
+ });
422
+ }
423
+ export function releaseUnlockedTxBody(input) {
424
+ return compact({
425
+ sender: normalizeAddress(input.walletAddress),
426
+ who: input.who === undefined ? undefined : normalizeAddress(input.who),
427
+ shared: shared(input.shared),
428
+ });
429
+ }
430
+ export function exitEarlyTxBody(input) {
431
+ return compact({
432
+ sender: normalizeAddress(input.walletAddress),
433
+ shared: shared(input.shared),
434
+ });
435
+ }
436
+ export const MAX_LOCKED_ENTRIES = 244;
437
+ export function exitEarlyBoundedTxBody(input) {
438
+ if (input.unlocks.length === 0 || input.unlocks.length > MAX_LOCKED_ENTRIES) {
439
+ throw new ParchmentApiError(0, "bad_request", `unlocks must list 1 to ${MAX_LOCKED_ENTRIES} entries`);
440
+ }
441
+ const unlocks = input.unlocks.map((u) => u64Wire(u, "unlocks[]", "bad_amount"));
442
+ if (new Set(unlocks).size !== unlocks.length)
443
+ throw new ParchmentApiError(0, "bad_request", "unlocks has a duplicate");
444
+ return compact({
445
+ sender: normalizeAddress(input.walletAddress),
446
+ unlocks,
447
+ maxPenalty: u64Wire(input.maxPenalty, "maxPenalty", "bad_amount"),
448
+ shared: shared(input.shared),
449
+ });
450
+ }
451
+ export function grantCloseDelegateTxBody(input) {
452
+ return compact({
453
+ sender: normalizeAddress(input.walletAddress),
454
+ accountId: normalizeAddress(input.accountId),
455
+ ...capRef(input),
456
+ shared: shared(input.shared),
457
+ });
458
+ }
459
+ export function grantSessionAndTriggerTxBody(input) {
460
+ return compact({
461
+ sender: normalizeAddress(input.walletAddress),
462
+ accountId: normalizeAddress(input.accountId),
463
+ ...capRef(input),
464
+ sessionDelegate: normalizeAddress(input.sessionDelegate),
465
+ expiryMs: u64Wire(input.expiryMs, "expiryMs", "bad_amount"),
466
+ shared: shared(input.shared),
467
+ });
468
+ }
469
+ export function revokeSessionAndTriggerTxBody(input) {
470
+ return compact({
471
+ sender: normalizeAddress(input.walletAddress),
472
+ accountId: normalizeAddress(input.accountId),
473
+ ...capRef(input),
474
+ sessionDelegate: normalizeAddress(input.sessionDelegate),
475
+ triggerDelegate: normalizeAddress(input.triggerDelegate),
476
+ shared: shared(input.shared),
477
+ });
478
+ }
479
+ export function grantDelegateTxBody(input) {
480
+ return compact({
481
+ sender: normalizeAddress(input.walletAddress),
482
+ accountId: normalizeAddress(input.accountId),
483
+ ...capRef(input),
484
+ delegate: normalizeAddress(input.delegate),
485
+ expiryMs: u64Wire(input.expiryMs, "expiryMs", "bad_amount"),
486
+ shared: shared(input.shared),
487
+ });
488
+ }
489
+ export const BUILDER_FEE_HARD_BPS = 100n;
490
+ export const MAX_BUILDER_CREATE_FEE = 100000000n;
491
+ function builderFeeBps(value, field, min) {
492
+ const text = typeof value === "bigint" ? value.toString() : typeof value === "string" ? value.trim() : "";
493
+ const bps = /^[0-9]{1,3}$/.test(text) ? BigInt(text) : null;
494
+ if (bps === null || bps < min || bps > BUILDER_FEE_HARD_BPS) {
495
+ throw new ParchmentApiError(0, "bad_fee", `${field} must be from ${min} to ${BUILDER_FEE_HARD_BPS} bps`);
496
+ }
497
+ return toJsBigInt(bps);
498
+ }
499
+ function selfServeCode(value) {
500
+ if (typeof value !== "string" || !/^[a-z0-9_-]{3,32}$/.test(value)) {
501
+ throw new ParchmentApiError(0, "bad_code", "code must be 3 to 32 bytes of [a-z0-9_-]");
502
+ }
503
+ return value;
504
+ }
505
+ function authorityCapId(value) {
506
+ if (value === undefined || value === null) {
507
+ throw new ParchmentApiError(0, "authority_cap_required", "authorityCapId is required");
508
+ }
509
+ return normalizeAddress(value);
510
+ }
511
+ export function registerBuilderTxBody(input) {
512
+ const sender = normalizeAddress(input.walletAddress);
513
+ const code = builderCode(input.code, "bad_code");
514
+ const bps = builderFeeBps(input.feeBps, "feeBps", 0n);
515
+ const payout = normalizeAddress(input.payout);
516
+ const owner = normalizeAddress(input.owner);
517
+ return compact({
518
+ sender,
519
+ authorityCapId: authorityCapId(input.authorityCapId),
520
+ authorityCapVersion: input.authorityCapVersion === undefined ? undefined : u64Wire(input.authorityCapVersion, "authorityCapVersion", "bad_amount"),
521
+ authorityCapDigest: input.authorityCapDigest === undefined ? undefined : authorityDigest(input.authorityCapDigest),
522
+ code,
523
+ payout,
524
+ owner,
525
+ feeBps: bps,
526
+ shared: shared(input.shared),
527
+ });
528
+ }
529
+ export function setBuilderCreateFeeTxBody(input) {
530
+ const sender = normalizeAddress(input.walletAddress);
531
+ const amount = BigInt(u64Unit(input.amount, "amount", "bad_amount"));
532
+ if (amount > MAX_BUILDER_CREATE_FEE) {
533
+ throw new ParchmentApiError(0, "bad_amount", `amount must be at most ${MAX_BUILDER_CREATE_FEE} (100 USDC)`);
534
+ }
535
+ return compact({
536
+ sender,
537
+ amount: toJsBigInt(amount),
538
+ authorityCapId: authorityCapId(input.authorityCapId),
539
+ authorityCapVersion: input.authorityCapVersion === undefined ? undefined : u64Wire(input.authorityCapVersion, "authorityCapVersion", "bad_amount"),
540
+ authorityCapDigest: input.authorityCapDigest === undefined ? undefined : authorityDigest(input.authorityCapDigest),
541
+ shared: shared(input.shared),
542
+ });
543
+ }
544
+ export function installTreasuryTxBody(input) {
545
+ const sender = normalizeAddress(input.walletAddress);
546
+ const treasuryCap = coin(input.treasuryCap);
547
+ return compact({
548
+ sender,
549
+ authorityCapId: authorityCapId(input.authorityCapId),
550
+ authorityCapVersion: input.authorityCapVersion === undefined ? undefined : u64Wire(input.authorityCapVersion, "authorityCapVersion", "bad_amount"),
551
+ authorityCapDigest: input.authorityCapDigest === undefined ? undefined : authorityDigest(input.authorityCapDigest),
552
+ treasuryCap,
553
+ shared: shared(input.shared),
554
+ });
555
+ }
556
+ export function authRegisterTxBody(input) {
557
+ return compact({
558
+ sender: normalizeAddress(input.walletAddress),
559
+ authorityCapId: authorityCapId(input.authorityCapId),
560
+ authorityCapVersion: input.authorityCapVersion === undefined ? undefined : u64Wire(input.authorityCapVersion, "authorityCapVersion", "bad_amount"),
561
+ authorityCapDigest: input.authorityCapDigest === undefined ? undefined : authorityDigest(input.authorityCapDigest),
562
+ shared: shared(input.shared),
563
+ });
564
+ }
565
+ export function setBuilderPayoutTxBody(input) {
566
+ return compact({
567
+ sender: normalizeAddress(input.walletAddress),
568
+ builderId: builderIdField(input.builderId),
569
+ newPayout: normalizeAddress(input.newPayout),
570
+ shared: shared(input.shared),
571
+ });
572
+ }
573
+ export function createBuilderTxBody(input) {
574
+ const sender = normalizeAddress(input.walletAddress);
575
+ const code = selfServeCode(input.code);
576
+ const payout = normalizeAddress(input.payout);
577
+ const feeBps = builderFeeBps(input.feeBps, "feeBps", 0n);
578
+ if (!input.coin)
579
+ throw new ParchmentApiError(0, "coin_required", "a USDC coin covering the creation fee is required");
580
+ return compact({ sender, code, payout, feeBps, coin: coin(input.coin), shared: shared(input.shared) });
581
+ }
582
+ export function setBuilderFeeTxBody(input) {
583
+ return compact({
584
+ sender: normalizeAddress(input.walletAddress),
585
+ builderId: builderIdField(input.builderId),
586
+ feeBps: builderFeeBps(input.feeBps, "feeBps", 0n),
587
+ shared: shared(input.shared),
588
+ });
589
+ }
590
+ export function builderOwnerTxBody(input) {
591
+ return compact({
592
+ sender: normalizeAddress(input.walletAddress),
593
+ builderId: builderIdField(input.builderId),
594
+ shared: shared(input.shared),
595
+ });
596
+ }
597
+ export function transferBuilderTxBody(input) {
598
+ return compact({
599
+ sender: normalizeAddress(input.walletAddress),
600
+ builderId: builderIdField(input.builderId),
601
+ newOwner: normalizeAddress(input.newOwner),
602
+ shared: shared(input.shared),
603
+ });
604
+ }
605
+ export function approveBuilderTxBody(input) {
606
+ return compact({
607
+ sender: normalizeAddress(input.walletAddress),
608
+ accountId: normalizeAddress(input.accountId),
609
+ ...capRef(input),
610
+ builderId: builderIdField(input.builderId),
611
+ maxFeeBps: builderFeeBps(input.maxFeeBps, "maxFeeBps", 1n),
612
+ shared: shared(input.shared),
613
+ });
614
+ }
615
+ export function revokeBuilderApprovalTxBody(input) {
616
+ return compact({
617
+ sender: normalizeAddress(input.walletAddress),
618
+ accountId: normalizeAddress(input.accountId),
619
+ ...capRef(input),
620
+ builderId: builderIdField(input.builderId),
621
+ shared: shared(input.shared),
622
+ });
623
+ }
624
+ export function gameTxBody(input) {
625
+ return compact({
626
+ sender: normalizeAddress(input.walletAddress),
627
+ accountId: normalizeAddress(input.accountId),
628
+ ...capRef(input),
629
+ shared: shared(input.shared),
630
+ });
631
+ }
632
+ function questIdField(value) {
633
+ const text = typeof value === "bigint" ? value.toString() : typeof value === "string" ? value.trim() : "";
634
+ if (text.length === 0)
635
+ throw new ParchmentApiError(0, "quest_required", "questId is required");
636
+ if (!/^[0-9]{1,3}$/.test(text) || Number(text) > 255) {
637
+ throw new ParchmentApiError(0, "bad_quest", "questId must be a u8");
638
+ }
639
+ return toJsBigInt(BigInt(text));
640
+ }
641
+ export function claimQuestTxBody(input) {
642
+ return compact({
643
+ ...gameTxBody(input),
644
+ questId: questIdField(input.questId),
645
+ });
646
+ }
647
+ const U64_MAX = (1n << 64n) - 1n;
648
+ function crateCountField(value) {
649
+ if (value === undefined || value === null || (typeof value === "string" && value.trim().length === 0)) {
650
+ throw new ParchmentApiError(0, "count_required", "count is required");
651
+ }
652
+ const bad = () => new ParchmentApiError(0, "bad_count", "count must be an integer from 1 to 2^64 - 1");
653
+ let count;
654
+ if (typeof value === "bigint")
655
+ count = value;
656
+ else if (typeof value === "number") {
657
+ if (!Number.isSafeInteger(value))
658
+ throw bad();
659
+ count = BigInt(value);
660
+ }
661
+ else if (typeof value === "string" && /^[0-9]+$/.test(value.trim()))
662
+ count = BigInt(value.trim());
663
+ else
664
+ throw bad();
665
+ if (count < 1n || count > U64_MAX)
666
+ throw bad();
667
+ return toJsBigInt(count);
668
+ }
669
+ export function openCratesTxBody(input) {
670
+ return compact({
671
+ ...gameTxBody(input),
672
+ count: crateCountField(input.count),
673
+ });
674
+ }
675
+ export function revokeDelegateTxBody(input) {
676
+ return compact({
677
+ sender: normalizeAddress(input.walletAddress),
678
+ accountId: normalizeAddress(input.accountId),
679
+ ...capRef(input),
680
+ delegate: normalizeAddress(input.delegate),
681
+ shared: shared(input.shared),
682
+ });
683
+ }