@veilo/sdk-core 0.5.0 → 0.7.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.
Files changed (240) hide show
  1. package/README.md +282 -18
  2. package/accounts.d.ts +2 -0
  3. package/accounts.js +3 -0
  4. package/cloak.d.ts +2 -0
  5. package/cloak.js +3 -0
  6. package/config.d.ts +2 -0
  7. package/config.js +3 -0
  8. package/dist/cjs/events/index.d.ts +6 -0
  9. package/dist/cjs/events/index.js +22 -0
  10. package/dist/cjs/events/parsing.d.ts +25 -0
  11. package/dist/cjs/events/parsing.js +158 -0
  12. package/dist/cjs/events/position.d.ts +26 -0
  13. package/dist/cjs/events/position.js +53 -0
  14. package/dist/cjs/events/recovery.d.ts +87 -0
  15. package/dist/cjs/events/recovery.js +281 -0
  16. package/dist/cjs/events/scanning.d.ts +35 -0
  17. package/dist/cjs/events/scanning.js +98 -0
  18. package/dist/cjs/events/tree.d.ts +27 -0
  19. package/dist/cjs/events/tree.js +63 -0
  20. package/dist/cjs/events/types.d.ts +39 -0
  21. package/dist/cjs/events/types.js +2 -0
  22. package/dist/cjs/events.d.ts +1 -77
  23. package/dist/cjs/events.js +6 -154
  24. package/dist/cjs/identity/encoding.d.ts +9 -0
  25. package/dist/cjs/identity/encoding.js +85 -0
  26. package/dist/cjs/identity/index.d.ts +4 -0
  27. package/dist/cjs/identity/index.js +20 -0
  28. package/dist/cjs/identity/noteKey.d.ts +33 -0
  29. package/dist/cjs/identity/noteKey.js +97 -0
  30. package/dist/cjs/identity/spending.d.ts +18 -0
  31. package/dist/cjs/identity/spending.js +46 -0
  32. package/dist/cjs/identity/viewKey.d.ts +38 -0
  33. package/dist/cjs/identity/viewKey.js +105 -0
  34. package/dist/cjs/idl/index.d.ts +18 -0
  35. package/dist/cjs/idl/index.js +24 -0
  36. package/dist/cjs/idl/privacy_pool.json +9907 -9907
  37. package/dist/cjs/idl/privacy_pool_legacy.d.ts +5 -0
  38. package/dist/cjs/idl/privacy_pool_legacy.js +3307 -0
  39. package/dist/cjs/idl/privacy_pool_legacy.json +2302 -0
  40. package/dist/cjs/idl/privacy_pool_legacy2.d.ts +5 -0
  41. package/dist/cjs/idl/privacy_pool_legacy2.js +3397 -0
  42. package/dist/cjs/idl/privacy_pool_legacy2.json +2376 -0
  43. package/dist/cjs/index.d.ts +8 -3
  44. package/dist/cjs/index.js +12 -2
  45. package/dist/cjs/notes/amount.d.ts +2 -0
  46. package/dist/cjs/notes/amount.js +21 -0
  47. package/dist/cjs/notes/index.d.ts +4 -0
  48. package/dist/cjs/notes/index.js +4 -0
  49. package/dist/cjs/notes/recovery.d.ts +16 -6
  50. package/dist/cjs/notes/recovery.js +33 -16
  51. package/dist/cjs/notes/scan.d.ts +74 -0
  52. package/dist/cjs/notes/scan.js +110 -0
  53. package/dist/cjs/notes/selection.d.ts +133 -0
  54. package/dist/cjs/notes/selection.js +260 -0
  55. package/dist/cjs/notes/spent.d.ts +60 -0
  56. package/dist/cjs/notes/spent.js +101 -0
  57. package/dist/cjs/perps/constants.d.ts +16 -0
  58. package/dist/cjs/perps/constants.js +52 -0
  59. package/dist/cjs/perps/index.d.ts +5 -0
  60. package/dist/cjs/perps/index.js +21 -0
  61. package/dist/cjs/perps/keys.d.ts +16 -0
  62. package/dist/cjs/perps/keys.js +43 -0
  63. package/dist/cjs/perps/pdas.d.ts +29 -0
  64. package/dist/cjs/perps/pdas.js +55 -0
  65. package/dist/cjs/perps/relayer.d.ts +9 -0
  66. package/dist/cjs/perps/relayer.js +35 -0
  67. package/dist/cjs/perps/types.d.ts +126 -0
  68. package/dist/cjs/perps/types.js +2 -0
  69. package/dist/cjs/positions/index.d.ts +4 -0
  70. package/dist/cjs/positions/index.js +20 -0
  71. package/dist/cjs/positions/keys.d.ts +29 -0
  72. package/dist/cjs/positions/keys.js +71 -0
  73. package/dist/cjs/positions/pdas.d.ts +15 -0
  74. package/dist/cjs/positions/pdas.js +54 -0
  75. package/dist/cjs/positions/relayer.d.ts +4 -0
  76. package/dist/cjs/positions/relayer.js +15 -0
  77. package/dist/cjs/positions/types.d.ts +97 -0
  78. package/dist/cjs/positions/types.js +2 -0
  79. package/dist/cjs/predictions/client.d.ts +41 -0
  80. package/dist/cjs/predictions/client.js +147 -0
  81. package/dist/cjs/predictions/index.d.ts +5 -0
  82. package/dist/cjs/predictions/index.js +21 -0
  83. package/dist/cjs/predictions/keys.d.ts +14 -0
  84. package/dist/cjs/predictions/keys.js +40 -0
  85. package/dist/cjs/predictions/pdas.d.ts +6 -0
  86. package/dist/cjs/predictions/pdas.js +13 -0
  87. package/dist/cjs/predictions/relayer.d.ts +3 -0
  88. package/dist/cjs/predictions/relayer.js +11 -0
  89. package/dist/cjs/predictions/types.d.ts +178 -0
  90. package/dist/cjs/predictions/types.js +2 -0
  91. package/dist/cjs/program.d.ts +2 -0
  92. package/dist/cjs/program.js +8 -7
  93. package/dist/cjs/prover.d.ts +27 -4
  94. package/dist/cjs/prover.js +107 -48
  95. package/dist/cjs/relayer/api.d.ts +41 -0
  96. package/dist/cjs/relayer/api.js +99 -0
  97. package/dist/cjs/relayer/client.d.ts +42 -17
  98. package/dist/cjs/relayer/client.js +94 -17
  99. package/dist/cjs/relayer/errors.d.ts +1 -1
  100. package/dist/cjs/relayer/errors.js +1 -1
  101. package/dist/cjs/relayer/index.d.ts +4 -1
  102. package/dist/cjs/relayer/index.js +1 -1
  103. package/dist/cjs/relayer/internal-config.d.ts +36 -0
  104. package/dist/cjs/relayer/internal-config.js +9 -0
  105. package/dist/cjs/relayer/internal.d.ts +6 -0
  106. package/dist/cjs/relayer/internal.js +14 -0
  107. package/dist/cjs/relayer/transport.d.ts +4 -2
  108. package/dist/cjs/relayer/transport.js +10 -5
  109. package/dist/cjs/relayer/types.d.ts +40 -10
  110. package/dist/cjs/relayer/types.js +1 -1
  111. package/dist/cjs/shield/owner.js +1 -4
  112. package/dist/esm/events/index.d.ts +6 -0
  113. package/dist/esm/events/index.js +6 -0
  114. package/dist/esm/events/parsing.d.ts +25 -0
  115. package/dist/esm/events/parsing.js +120 -0
  116. package/dist/esm/events/position.d.ts +26 -0
  117. package/dist/esm/events/position.js +48 -0
  118. package/dist/esm/events/recovery.d.ts +87 -0
  119. package/dist/esm/events/recovery.js +237 -0
  120. package/dist/esm/events/scanning.d.ts +35 -0
  121. package/dist/esm/events/scanning.js +92 -0
  122. package/dist/esm/events/tree.d.ts +27 -0
  123. package/dist/esm/events/tree.js +58 -0
  124. package/dist/esm/events/types.d.ts +39 -0
  125. package/dist/esm/events/types.js +1 -0
  126. package/dist/esm/events.d.ts +1 -77
  127. package/dist/esm/events.js +3 -129
  128. package/dist/esm/identity/encoding.d.ts +9 -0
  129. package/dist/esm/identity/encoding.js +75 -0
  130. package/dist/esm/identity/index.d.ts +4 -0
  131. package/dist/esm/identity/index.js +4 -0
  132. package/dist/esm/identity/noteKey.d.ts +33 -0
  133. package/dist/esm/identity/noteKey.js +91 -0
  134. package/dist/esm/identity/spending.d.ts +18 -0
  135. package/dist/esm/identity/spending.js +35 -0
  136. package/dist/esm/identity/viewKey.d.ts +38 -0
  137. package/dist/esm/identity/viewKey.js +96 -0
  138. package/dist/esm/idl/index.d.ts +18 -0
  139. package/dist/esm/idl/index.js +18 -0
  140. package/dist/esm/idl/privacy_pool.json +9907 -9907
  141. package/dist/esm/idl/privacy_pool_legacy.d.ts +5 -0
  142. package/dist/esm/idl/privacy_pool_legacy.js +3305 -0
  143. package/dist/esm/idl/privacy_pool_legacy.json +2302 -0
  144. package/dist/esm/idl/privacy_pool_legacy2.d.ts +5 -0
  145. package/dist/esm/idl/privacy_pool_legacy2.js +3395 -0
  146. package/dist/esm/idl/privacy_pool_legacy2.json +2376 -0
  147. package/dist/esm/index.d.ts +8 -3
  148. package/dist/esm/index.js +8 -3
  149. package/dist/esm/notes/amount.d.ts +2 -0
  150. package/dist/esm/notes/amount.js +18 -0
  151. package/dist/esm/notes/index.d.ts +4 -0
  152. package/dist/esm/notes/index.js +4 -0
  153. package/dist/esm/notes/recovery.d.ts +16 -6
  154. package/dist/esm/notes/recovery.js +33 -16
  155. package/dist/esm/notes/scan.d.ts +74 -0
  156. package/dist/esm/notes/scan.js +105 -0
  157. package/dist/esm/notes/selection.d.ts +133 -0
  158. package/dist/esm/notes/selection.js +254 -0
  159. package/dist/esm/notes/spent.d.ts +60 -0
  160. package/dist/esm/notes/spent.js +96 -0
  161. package/dist/esm/perps/constants.d.ts +16 -0
  162. package/dist/esm/perps/constants.js +48 -0
  163. package/dist/esm/perps/index.d.ts +5 -0
  164. package/dist/esm/perps/index.js +5 -0
  165. package/dist/esm/perps/keys.d.ts +16 -0
  166. package/dist/esm/perps/keys.js +35 -0
  167. package/dist/esm/perps/pdas.d.ts +29 -0
  168. package/dist/esm/perps/pdas.js +47 -0
  169. package/dist/esm/perps/relayer.d.ts +9 -0
  170. package/dist/esm/perps/relayer.js +25 -0
  171. package/dist/esm/perps/types.d.ts +126 -0
  172. package/dist/esm/perps/types.js +1 -0
  173. package/dist/esm/positions/index.d.ts +4 -0
  174. package/dist/esm/positions/index.js +4 -0
  175. package/dist/esm/positions/keys.d.ts +29 -0
  176. package/dist/esm/positions/keys.js +65 -0
  177. package/dist/esm/positions/pdas.d.ts +15 -0
  178. package/dist/esm/positions/pdas.js +44 -0
  179. package/dist/esm/positions/relayer.d.ts +4 -0
  180. package/dist/esm/positions/relayer.js +10 -0
  181. package/dist/esm/positions/types.d.ts +97 -0
  182. package/dist/esm/positions/types.js +1 -0
  183. package/dist/esm/predictions/client.d.ts +41 -0
  184. package/dist/esm/predictions/client.js +141 -0
  185. package/dist/esm/predictions/index.d.ts +5 -0
  186. package/dist/esm/predictions/index.js +5 -0
  187. package/dist/esm/predictions/keys.d.ts +14 -0
  188. package/dist/esm/predictions/keys.js +33 -0
  189. package/dist/esm/predictions/pdas.d.ts +6 -0
  190. package/dist/esm/predictions/pdas.js +10 -0
  191. package/dist/esm/predictions/relayer.d.ts +3 -0
  192. package/dist/esm/predictions/relayer.js +7 -0
  193. package/dist/esm/predictions/types.d.ts +178 -0
  194. package/dist/esm/predictions/types.js +1 -0
  195. package/dist/esm/program.d.ts +2 -0
  196. package/dist/esm/program.js +4 -3
  197. package/dist/esm/prover.d.ts +27 -4
  198. package/dist/esm/prover.js +106 -15
  199. package/dist/esm/relayer/api.d.ts +41 -0
  200. package/dist/esm/relayer/api.js +66 -0
  201. package/dist/esm/relayer/client.d.ts +42 -17
  202. package/dist/esm/relayer/client.js +94 -17
  203. package/dist/esm/relayer/errors.d.ts +1 -1
  204. package/dist/esm/relayer/errors.js +1 -1
  205. package/dist/esm/relayer/index.d.ts +4 -1
  206. package/dist/esm/relayer/index.js +1 -1
  207. package/dist/esm/relayer/internal-config.d.ts +36 -0
  208. package/dist/esm/relayer/internal-config.js +6 -0
  209. package/dist/esm/relayer/internal.d.ts +6 -0
  210. package/dist/esm/relayer/internal.js +10 -0
  211. package/dist/esm/relayer/transport.d.ts +4 -2
  212. package/dist/esm/relayer/transport.js +10 -5
  213. package/dist/esm/relayer/types.d.ts +40 -10
  214. package/dist/esm/relayer/types.js +1 -1
  215. package/dist/esm/shield/owner.js +1 -4
  216. package/identity.d.ts +2 -0
  217. package/identity.js +3 -0
  218. package/idl.d.ts +2 -0
  219. package/idl.js +3 -0
  220. package/notes.d.ts +2 -0
  221. package/notes.js +3 -0
  222. package/package.json +85 -3
  223. package/perps.d.ts +2 -0
  224. package/perps.js +3 -0
  225. package/poseidon.d.ts +2 -0
  226. package/poseidon.js +3 -0
  227. package/positions.d.ts +2 -0
  228. package/positions.js +3 -0
  229. package/predictions.d.ts +2 -0
  230. package/predictions.js +3 -0
  231. package/proof.d.ts +2 -0
  232. package/proof.js +3 -0
  233. package/prover.d.ts +2 -0
  234. package/prover.js +3 -0
  235. package/relayer.d.ts +2 -0
  236. package/relayer.js +3 -0
  237. package/shield.d.ts +2 -0
  238. package/shield.js +3 -0
  239. package/transactions.d.ts +2 -0
  240. package/transactions.js +3 -0
@@ -0,0 +1,260 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MAX_INPUT_NOTES = void 0;
4
+ exports.canonicalTreeId = canonicalTreeId;
5
+ exports.selectNotesForAmount = selectNotesForAmount;
6
+ exports.planNoteConsolidation = planNoteConsolidation;
7
+ const web3_js_1 = require("@solana/web3.js");
8
+ const amount_js_1 = require("./amount.js");
9
+ /**
10
+ * Choosing which notes to spend.
11
+ *
12
+ * Two constraints drive everything here:
13
+ *
14
+ * 1. The transaction circuit is 2-in-2-out, so a single transaction can spend
15
+ * at most TWO notes.
16
+ * 2. Notes can only be co-spent if they live in the SAME Merkle tree, and the
17
+ * tree is derived from the MINT — see `canonicalTreeId`.
18
+ */
19
+ /** Inputs the transaction circuit accepts in one proof. */
20
+ exports.MAX_INPUT_NOTES = 2;
21
+ /**
22
+ * The tree a note belongs to, derived from its mint.
23
+ *
24
+ * Mirrors the relayer's `mintAddressToTreeId` byte for byte: the all-zero
25
+ * native-SOL mint maps to 0, anything else to its first four mint bytes as a
26
+ * big-endian uint32.
27
+ *
28
+ * DERIVE THIS, NEVER TRUST A STORED `treeId`. Change notes are written with the
29
+ * on-chain shard index (a small 0/1/2…) while deposits and synced notes carry
30
+ * the mint-derived id. For native SOL both are 0 so the difference hides; for
31
+ * SPL tokens the two land in different buckets, and co-spending across them
32
+ * fails at proof time as a commitment mismatch.
33
+ *
34
+ * An undecodable mint falls back to the SOL bucket rather than inventing a
35
+ * phantom tree, matching the server.
36
+ */
37
+ function canonicalTreeId(mintAddress) {
38
+ if (!mintAddress || mintAddress.trim() === "")
39
+ return 0;
40
+ try {
41
+ const bytes = new web3_js_1.PublicKey(mintAddress).toBytes();
42
+ if (bytes.every((byte) => byte === 0))
43
+ return 0;
44
+ return (((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]) >>> 0);
45
+ }
46
+ catch {
47
+ return 0;
48
+ }
49
+ }
50
+ /**
51
+ * Pick notes to cover `amount`, preferring the fewest notes and least change.
52
+ *
53
+ * Considers each tree separately, since notes cannot be co-spent across trees,
54
+ * and returns the best single-tree selection:
55
+ *
56
+ * 1. one note that covers the amount (smallest such note, least change);
57
+ * 2. otherwise the best PAIR, found by a two-pointer scan over the
58
+ * amount-sorted notes — O(n) rather than an O(n²) all-pairs search;
59
+ * 3. otherwise the largest notes greedily, flagged `requiresMerge` because
60
+ * more than two inputs cannot go into one proof.
61
+ *
62
+ * @param amount base units to cover
63
+ */
64
+ function selectNotesForAmount(notes, amount, options = {}) {
65
+ const target = (0, amount_js_1.toBaseUnits)(amount, "amount");
66
+ if (target <= 0n) {
67
+ throw new Error(`amount must be positive, got ${target}`);
68
+ }
69
+ const wanted = options.mint === undefined ? undefined : canonicalTreeId(options.mint);
70
+ const byTree = new Map();
71
+ notes.forEach((note, index) => {
72
+ const treeId = canonicalTreeId(note.mint);
73
+ if (wanted !== undefined && treeId !== wanted)
74
+ return;
75
+ const entry = {
76
+ note,
77
+ amount: (0, amount_js_1.toBaseUnits)(note.amount, `notes[${index}].amount`),
78
+ };
79
+ if (entry.amount <= 0n)
80
+ return;
81
+ const bucket = byTree.get(treeId);
82
+ if (bucket)
83
+ bucket.push(entry);
84
+ else
85
+ byTree.set(treeId, [entry]);
86
+ });
87
+ if (byTree.size === 0) {
88
+ return {
89
+ ok: false,
90
+ reason: "NO_NOTES",
91
+ available: 0n,
92
+ message: "No spendable notes for the requested mint.",
93
+ };
94
+ }
95
+ let best;
96
+ let available = 0n;
97
+ for (const [treeId, bucket] of byTree) {
98
+ const treeTotal = bucket.reduce((sum, entry) => sum + entry.amount, 0n);
99
+ if (treeTotal > available)
100
+ available = treeTotal;
101
+ const selection = selectWithinTree(bucket, target, treeId);
102
+ if (!selection)
103
+ continue;
104
+ if (!best || isBetter(selection, best))
105
+ best = selection;
106
+ }
107
+ if (!best) {
108
+ return {
109
+ ok: false,
110
+ reason: "INSUFFICIENT_FUNDS",
111
+ available,
112
+ message: `No single tree holds ${target} base units. Largest tree total is ` +
113
+ `${available}. Notes in different trees cannot be co-spent.`,
114
+ };
115
+ }
116
+ return best;
117
+ }
118
+ /** Fewer inputs first, then least change. */
119
+ function isBetter(a, b) {
120
+ if (a.notes.length !== b.notes.length)
121
+ return a.notes.length < b.notes.length;
122
+ return a.change < b.change;
123
+ }
124
+ function selectWithinTree(bucket, target, treeId) {
125
+ // Descending by amount, compared as bigint: Number() would lose precision
126
+ // above 2^53 base units.
127
+ const sorted = [...bucket].sort((a, b) => b.amount > a.amount ? 1 : b.amount < a.amount ? -1 : 0);
128
+ // 1. Smallest single note that covers the target — walk up from the end.
129
+ let single;
130
+ for (let i = sorted.length - 1; i >= 0; i--) {
131
+ if (sorted[i].amount >= target) {
132
+ single = sorted[i];
133
+ break;
134
+ }
135
+ }
136
+ if (single) {
137
+ return {
138
+ ok: true,
139
+ notes: [single.note],
140
+ treeId,
141
+ total: single.amount,
142
+ change: single.amount - target,
143
+ requiresMerge: false,
144
+ };
145
+ }
146
+ // 2. Best pair by two-pointer scan over the descending list.
147
+ let bestPair;
148
+ let bestChange = 0n;
149
+ let lo = 0;
150
+ let hi = sorted.length - 1;
151
+ while (lo < hi) {
152
+ const combined = sorted[lo].amount + sorted[hi].amount;
153
+ if (combined >= target) {
154
+ const change = combined - target;
155
+ if (!bestPair || change < bestChange) {
156
+ bestChange = change;
157
+ bestPair = [sorted[lo], sorted[hi]];
158
+ }
159
+ lo++;
160
+ }
161
+ else {
162
+ hi--;
163
+ }
164
+ }
165
+ if (bestPair) {
166
+ return {
167
+ ok: true,
168
+ notes: [bestPair[0].note, bestPair[1].note],
169
+ treeId,
170
+ total: bestPair[0].amount + bestPair[1].amount,
171
+ change: bestChange,
172
+ requiresMerge: false,
173
+ };
174
+ }
175
+ // 3. Greedy over the largest notes. More than two inputs, so it needs a merge.
176
+ const chosen = [];
177
+ let total = 0n;
178
+ for (const entry of sorted) {
179
+ chosen.push(entry);
180
+ total += entry.amount;
181
+ if (total >= target)
182
+ break;
183
+ }
184
+ if (total < target)
185
+ return undefined;
186
+ return {
187
+ ok: true,
188
+ notes: chosen.map((entry) => entry.note),
189
+ treeId,
190
+ total,
191
+ change: total - target,
192
+ requiresMerge: chosen.length > exports.MAX_INPUT_NOTES,
193
+ };
194
+ }
195
+ /**
196
+ * Plan how to make `amount` spendable when no one or two notes cover it.
197
+ *
198
+ * `selectNotesForAmount` reports `requiresMerge` but cannot act on it: the
199
+ * circuit takes two inputs, so spending five notes means merging them down
200
+ * first. Each step here is one 2-in-1-out transfer to yourself, and k notes
201
+ * need k-2 of them before a final two-input spend.
202
+ *
203
+ * Merges the two SMALLEST notes each round. That retires dust first and leaves
204
+ * the large notes untouched, so the plan is short and the final spend has the
205
+ * least change.
206
+ *
207
+ * The plan is pure — it performs nothing. Execute the steps with
208
+ * `privateTransfer`, feeding each step's output note into the next.
209
+ */
210
+ function planNoteConsolidation(notes, amount, options = {}) {
211
+ const target = (0, amount_js_1.toBaseUnits)(amount, "amount");
212
+ if (target <= 0n) {
213
+ throw new Error(`amount must be positive, got ${target}`);
214
+ }
215
+ const selection = selectNotesForAmount(notes, target, options);
216
+ if (!selection.ok)
217
+ return selection;
218
+ // Already spendable in one transaction.
219
+ if (!selection.requiresMerge) {
220
+ return {
221
+ ok: true,
222
+ treeId: selection.treeId,
223
+ steps: [],
224
+ notes: selection.notes,
225
+ total: selection.total,
226
+ change: selection.change,
227
+ };
228
+ }
229
+ // Ascending, so the two cheapest merge first.
230
+ const pool = selection.notes
231
+ .map((note, index) => ({
232
+ kind: "note",
233
+ note,
234
+ amount: (0, amount_js_1.toBaseUnits)(note.amount, `notes[${index}].amount`),
235
+ }))
236
+ .sort((a, b) => (a.amount > b.amount ? 1 : a.amount < b.amount ? -1 : 0));
237
+ const steps = [];
238
+ while (pool.length > exports.MAX_INPUT_NOTES) {
239
+ const first = pool.shift();
240
+ const second = pool.shift();
241
+ const outputAmount = first.amount + second.amount;
242
+ const step = steps.length + 1;
243
+ steps.push({ step, inputs: [first, second], outputAmount });
244
+ const produced = { kind: "step", step, amount: outputAmount };
245
+ // Keep the pool ordered so the next round still merges the two smallest.
246
+ const at = pool.findIndex((entry) => entry.amount > outputAmount);
247
+ if (at === -1)
248
+ pool.push(produced);
249
+ else
250
+ pool.splice(at, 0, produced);
251
+ }
252
+ return {
253
+ ok: true,
254
+ treeId: selection.treeId,
255
+ steps,
256
+ notes: selection.notes,
257
+ total: selection.total,
258
+ change: selection.change,
259
+ };
260
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Spend status and private balance.
3
+ *
4
+ * A note is yours until its nullifier is published; nothing in the note itself
5
+ * says whether it has been spent. So a balance is always two steps: ask which
6
+ * nullifiers are spent, then sum what is left. This module is that pair.
7
+ */
8
+ /** Server-side cap per check-batch request (notes.constants MAX_NULLIFIERS_PER_CHECK). */
9
+ export declare const MAX_NULLIFIERS_PER_CHECK = 200;
10
+ /** Grouping key used for notes that carry no mint. */
11
+ export declare const UNKNOWN_MINT = "unknown";
12
+ /**
13
+ * Ask the relayer which of these nullifiers are spent.
14
+ *
15
+ * Deduplicates, lower-cases, and splits into MAX_NULLIFIERS_PER_CHECK batches
16
+ * that are issued concurrently. Returns a Set for membership testing; a
17
+ * nullifier absent from it is unspent as far as the relayer knows.
18
+ *
19
+ * @throws if any entry is not 64-char hex — a malformed nullifier that silently
20
+ * read as "unspent" would overstate a balance.
21
+ */
22
+ export declare function checkNullifiersSpent(nullifiers: string[]): Promise<Set<string>>;
23
+ /** The minimum a note must carry to be counted. */
24
+ export interface SpendableNote {
25
+ /** Base units. bigint preferred; strings and safe integers are accepted. */
26
+ amount: bigint | string | number;
27
+ /** 64-character hex nullifier for this note. */
28
+ nullifier: string;
29
+ /** Mint this note is denominated in. Groups the result; optional. */
30
+ mint?: string;
31
+ }
32
+ export interface PrivateBalance<T extends SpendableNote = SpendableNote> {
33
+ /** Sum of every unspent note, across all mints. */
34
+ total: bigint;
35
+ /** Unspent totals keyed by mint (UNKNOWN_MINT when a note carries none). */
36
+ byMint: Record<string, bigint>;
37
+ /** The caller's own note objects, partitioned. */
38
+ unspent: T[];
39
+ spent: T[];
40
+ }
41
+ export interface PrivateBalanceOptions {
42
+ /**
43
+ * Supply spend status yourself instead of asking the relayer — e.g. from a
44
+ * chain scan of nullifier events, or a local cache. Receives normalized
45
+ * lower-case hex.
46
+ */
47
+ resolveSpent?: (nullifiers: string[]) => Promise<Set<string>> | Set<string>;
48
+ }
49
+ /**
50
+ * Sum unspent notes, per mint and in total.
51
+ *
52
+ * ```ts
53
+ * const { total, byMint, unspent } = await getPrivateBalance(myNotes);
54
+ * ```
55
+ *
56
+ * Amounts are base units and stay bigint throughout — never sum note amounts as
57
+ * numbers. Notes are matched to spend status by nullifier, so every note needs
58
+ * one; the partitioned arrays hand back your own objects, not copies.
59
+ */
60
+ export declare function getPrivateBalance<T extends SpendableNote>(notes: T[], options?: PrivateBalanceOptions): Promise<PrivateBalance<T>>;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UNKNOWN_MINT = exports.MAX_NULLIFIERS_PER_CHECK = void 0;
4
+ exports.checkNullifiersSpent = checkNullifiersSpent;
5
+ exports.getPrivateBalance = getPrivateBalance;
6
+ const api_js_1 = require("../relayer/api.js");
7
+ const amount_js_1 = require("./amount.js");
8
+ /**
9
+ * Spend status and private balance.
10
+ *
11
+ * A note is yours until its nullifier is published; nothing in the note itself
12
+ * says whether it has been spent. So a balance is always two steps: ask which
13
+ * nullifiers are spent, then sum what is left. This module is that pair.
14
+ */
15
+ /** Server-side cap per check-batch request (notes.constants MAX_NULLIFIERS_PER_CHECK). */
16
+ exports.MAX_NULLIFIERS_PER_CHECK = 200;
17
+ /** Grouping key used for notes that carry no mint. */
18
+ exports.UNKNOWN_MINT = "unknown";
19
+ const HEX64 = /^[0-9a-f]{64}$/;
20
+ /**
21
+ * Normalize a nullifier to the form the relayer answers in.
22
+ *
23
+ * The endpoint's validator is case-INSENSITIVE, but it answers with
24
+ * `Buffer.toString("hex")` — always lowercase. Sending upper-case hex therefore
25
+ * returns a `spent` list that never matches what you sent, and every note reads
26
+ * as unspent. Normalizing here is what makes membership testing sound.
27
+ */
28
+ function normalizeNullifier(nullifier, index) {
29
+ const normalized = nullifier.trim().toLowerCase();
30
+ if (!HEX64.test(normalized)) {
31
+ throw new Error(`nullifiers[${index}] must be a 64-character hex string, got ` +
32
+ `${JSON.stringify(nullifier)}`);
33
+ }
34
+ return normalized;
35
+ }
36
+ function chunk(items, size) {
37
+ const out = [];
38
+ for (let i = 0; i < items.length; i += size)
39
+ out.push(items.slice(i, i + size));
40
+ return out;
41
+ }
42
+ /**
43
+ * Ask the relayer which of these nullifiers are spent.
44
+ *
45
+ * Deduplicates, lower-cases, and splits into MAX_NULLIFIERS_PER_CHECK batches
46
+ * that are issued concurrently. Returns a Set for membership testing; a
47
+ * nullifier absent from it is unspent as far as the relayer knows.
48
+ *
49
+ * @throws if any entry is not 64-char hex — a malformed nullifier that silently
50
+ * read as "unspent" would overstate a balance.
51
+ */
52
+ async function checkNullifiersSpent(nullifiers) {
53
+ const unique = [...new Set(nullifiers.map(normalizeNullifier))];
54
+ if (unique.length === 0)
55
+ return new Set();
56
+ const batches = await Promise.all(chunk(unique, exports.MAX_NULLIFIERS_PER_CHECK).map((batch) => (0, api_js_1.checkNullifiers)(batch)));
57
+ const spent = new Set();
58
+ for (const response of batches) {
59
+ for (const nullifier of response.spent ?? []) {
60
+ spent.add(nullifier.toLowerCase());
61
+ }
62
+ }
63
+ return spent;
64
+ }
65
+ /**
66
+ * Sum unspent notes, per mint and in total.
67
+ *
68
+ * ```ts
69
+ * const { total, byMint, unspent } = await getPrivateBalance(myNotes);
70
+ * ```
71
+ *
72
+ * Amounts are base units and stay bigint throughout — never sum note amounts as
73
+ * numbers. Notes are matched to spend status by nullifier, so every note needs
74
+ * one; the partitioned arrays hand back your own objects, not copies.
75
+ */
76
+ async function getPrivateBalance(notes, options = {}) {
77
+ const normalized = notes.map((note, index) => ({
78
+ note,
79
+ amount: (0, amount_js_1.toBaseUnits)(note.amount, `notes[${index}].amount`),
80
+ nullifier: normalizeNullifier(note.nullifier, index),
81
+ }));
82
+ const resolve = options.resolveSpent ?? checkNullifiersSpent;
83
+ const spentSet = await resolve(normalized.map((entry) => entry.nullifier));
84
+ const balance = {
85
+ total: 0n,
86
+ byMint: {},
87
+ unspent: [],
88
+ spent: [],
89
+ };
90
+ for (const entry of normalized) {
91
+ if (spentSet.has(entry.nullifier)) {
92
+ balance.spent.push(entry.note);
93
+ continue;
94
+ }
95
+ const mint = entry.note.mint ?? exports.UNKNOWN_MINT;
96
+ balance.unspent.push(entry.note);
97
+ balance.total += entry.amount;
98
+ balance.byMint[mint] = (balance.byMint[mint] ?? 0n) + entry.amount;
99
+ }
100
+ return balance;
101
+ }
@@ -0,0 +1,16 @@
1
+ import type { JperpMarket, JperpMarketPayload, JperpSide } from "./types.js";
2
+ export declare const JUPITER_PERPS_PROGRAM_ID = "PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu";
3
+ export declare const JUPITER_PERPS_EVENT_AUTHORITY = "37hJBDnntwqhGbK7L6M1bLyvccj4u55CCUiLPdYkiqBN";
4
+ export declare const JUPITER_PERPS_POOL = "5BUwFW4nRbftYTDMbgxykoFWqWHPzahFSNAaaaJtVKsq";
5
+ export declare const JUPITER_JLP_MINT = "27G8MtK7VtTcCHkpASjSDdkWWYfoqT6ggEuKidVJidD4";
6
+ export declare const JPERP_USDC_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
7
+ export declare const JPERP_NATIVE_SOL_MINT = "11111111111111111111111111111111";
8
+ export declare const JPERP_WRAPPED_SOL_MINT = "So11111111111111111111111111111111111111112";
9
+ export declare const JUPITER_PERPS_CUSTODIES: Record<JperpMarket | "USDC" | "USDT", string>;
10
+ export declare const JUPITER_PERPS_DOVES_ORACLES: Record<JperpMarket, string>;
11
+ export declare const JPERP_LONG_POOL_MINTS: Record<JperpMarket, string>;
12
+ export declare const JPERP_LONG_JUPITER_MINTS: Record<JperpMarket, string>;
13
+ export declare const JPERP_LONG_DECIMALS: Record<JperpMarket, number>;
14
+ export declare const JPERP_USD_DECIMALS = 6;
15
+ /** Resolve the exact market payload duplicated by the wallet-app reference. */
16
+ export declare function getJperpMarketPayload(market: JperpMarket, side: JperpSide): JperpMarketPayload;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JPERP_USD_DECIMALS = exports.JPERP_LONG_DECIMALS = exports.JPERP_LONG_JUPITER_MINTS = exports.JPERP_LONG_POOL_MINTS = exports.JUPITER_PERPS_DOVES_ORACLES = exports.JUPITER_PERPS_CUSTODIES = exports.JPERP_WRAPPED_SOL_MINT = exports.JPERP_NATIVE_SOL_MINT = exports.JPERP_USDC_MINT = exports.JUPITER_JLP_MINT = exports.JUPITER_PERPS_POOL = exports.JUPITER_PERPS_EVENT_AUTHORITY = exports.JUPITER_PERPS_PROGRAM_ID = void 0;
4
+ exports.getJperpMarketPayload = getJperpMarketPayload;
5
+ exports.JUPITER_PERPS_PROGRAM_ID = "PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu";
6
+ exports.JUPITER_PERPS_EVENT_AUTHORITY = "37hJBDnntwqhGbK7L6M1bLyvccj4u55CCUiLPdYkiqBN";
7
+ exports.JUPITER_PERPS_POOL = "5BUwFW4nRbftYTDMbgxykoFWqWHPzahFSNAaaaJtVKsq";
8
+ exports.JUPITER_JLP_MINT = "27G8MtK7VtTcCHkpASjSDdkWWYfoqT6ggEuKidVJidD4";
9
+ exports.JPERP_USDC_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
10
+ exports.JPERP_NATIVE_SOL_MINT = "11111111111111111111111111111111";
11
+ exports.JPERP_WRAPPED_SOL_MINT = "So11111111111111111111111111111111111111112";
12
+ exports.JUPITER_PERPS_CUSTODIES = {
13
+ SOL: "7xS2gz2bTp3fwCC7knJvUWTEU9Tycczu6VhJYKgi1wdz",
14
+ ETH: "AQCGyheWPLeo6Qp9WpYS9m3Qj479t7R636N9ey1rEjEn",
15
+ BTC: "5Pv3gM9JrFFH883SWAhvJC9RPYmo8UNxuFtv5bMMALkm",
16
+ USDC: "G18jKKXQwBbrHeiK3C9MRXhkHsLHf7XgCSisykV46EZa",
17
+ USDT: "4vkNeXiYEUizLdrpdPS1eC2mccyM4NUPRtERrk6ZETkk",
18
+ };
19
+ exports.JUPITER_PERPS_DOVES_ORACLES = {
20
+ SOL: "FYq2BWQ1V5P1WFBqr3qB2Kb5yHVvSv7upzKodgQE5zXh",
21
+ ETH: "AFZnHPzy4mvVCffrVwhewHbFc93uTHvDSFrVH7GtfXF1",
22
+ BTC: "hUqAT1KQ7eW1i6Csp9CXYtpPfSAvi835V7wKi5fRfmC",
23
+ };
24
+ exports.JPERP_LONG_POOL_MINTS = {
25
+ SOL: exports.JPERP_NATIVE_SOL_MINT,
26
+ ETH: "7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs",
27
+ BTC: "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh",
28
+ };
29
+ exports.JPERP_LONG_JUPITER_MINTS = {
30
+ SOL: exports.JPERP_WRAPPED_SOL_MINT,
31
+ ETH: exports.JPERP_LONG_POOL_MINTS.ETH,
32
+ BTC: exports.JPERP_LONG_POOL_MINTS.BTC,
33
+ };
34
+ exports.JPERP_LONG_DECIMALS = {
35
+ SOL: 9,
36
+ ETH: 8,
37
+ BTC: 8,
38
+ };
39
+ exports.JPERP_USD_DECIMALS = 6;
40
+ /** Resolve the exact market payload duplicated by the wallet-app reference. */
41
+ function getJperpMarketPayload(market, side) {
42
+ const isLong = side === "long";
43
+ return {
44
+ custody: exports.JUPITER_PERPS_CUSTODIES[market],
45
+ collateralCustody: exports.JUPITER_PERPS_CUSTODIES[isLong ? market : "USDC"],
46
+ inputMint: isLong ? exports.JPERP_LONG_POOL_MINTS[market] : exports.JPERP_USDC_MINT,
47
+ jupiterInputMint: isLong
48
+ ? exports.JPERP_LONG_JUPITER_MINTS[market]
49
+ : exports.JPERP_USDC_MINT,
50
+ dovesOracle: exports.JUPITER_PERPS_DOVES_ORACLES[market],
51
+ };
52
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./types.js";
2
+ export * from "./constants.js";
3
+ export * from "./keys.js";
4
+ export * from "./pdas.js";
5
+ export * from "./relayer.js";
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types.js"), exports);
18
+ __exportStar(require("./constants.js"), exports);
19
+ __exportStar(require("./keys.js"), exports);
20
+ __exportStar(require("./pdas.js"), exports);
21
+ __exportStar(require("./relayer.js"), exports);
@@ -0,0 +1,16 @@
1
+ import { Keypair } from "@solana/web3.js";
2
+ export interface JperpKeyBundle {
3
+ withdrawalId: Uint8Array;
4
+ withdrawalIdHex: string;
5
+ claimant: Keypair;
6
+ claimantPublicKey: string;
7
+ claimantSecretKey: string;
8
+ }
9
+ export declare function deriveJperpWithdrawalId(spendingKey: Uint8Array, index: number): Uint8Array;
10
+ export declare function deriveJperpClaimantKeypair(spendingKey: Uint8Array, withdrawalId: Uint8Array): Keypair;
11
+ export declare function jperpWithdrawalIdToHex(withdrawalId: Uint8Array): string;
12
+ export declare function jperpWithdrawalIdFromHex(value: string): Uint8Array;
13
+ /** Derive every client-held key field needed across the jPerp lifecycle. */
14
+ export declare function deriveJperpKeyBundle(spendingKey: Uint8Array, index: number): JperpKeyBundle;
15
+ /** Match the client reference's per-request Jupiter PDA namespace. */
16
+ export declare function createJperpCounter(): string;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deriveJperpWithdrawalId = deriveJperpWithdrawalId;
4
+ exports.deriveJperpClaimantKeypair = deriveJperpClaimantKeypair;
5
+ exports.jperpWithdrawalIdToHex = jperpWithdrawalIdToHex;
6
+ exports.jperpWithdrawalIdFromHex = jperpWithdrawalIdFromHex;
7
+ exports.deriveJperpKeyBundle = deriveJperpKeyBundle;
8
+ exports.createJperpCounter = createJperpCounter;
9
+ const sha256_1 = require("@noble/hashes/sha256");
10
+ const web3_js_1 = require("@solana/web3.js");
11
+ const encoding_js_1 = require("../identity/encoding.js");
12
+ function deriveJperpWithdrawalId(spendingKey, index) {
13
+ (0, encoding_js_1.assertBytes)(spendingKey, 32, "spending key");
14
+ return (0, sha256_1.sha256)((0, encoding_js_1.concatBytes)((0, encoding_js_1.utf8)("jperp_withdrawal_v1"), spendingKey, (0, encoding_js_1.uint32Le)(index, "jPerp position index")));
15
+ }
16
+ function deriveJperpClaimantKeypair(spendingKey, withdrawalId) {
17
+ (0, encoding_js_1.assertBytes)(spendingKey, 32, "spending key");
18
+ (0, encoding_js_1.assertBytes)(withdrawalId, 32, "jPerp withdrawal ID");
19
+ return web3_js_1.Keypair.fromSeed((0, sha256_1.sha256)((0, encoding_js_1.concatBytes)((0, encoding_js_1.utf8)("jperp_claimant"), spendingKey, withdrawalId)));
20
+ }
21
+ function jperpWithdrawalIdToHex(withdrawalId) {
22
+ (0, encoding_js_1.assertBytes)(withdrawalId, 32, "jPerp withdrawal ID");
23
+ return (0, encoding_js_1.bytesToHex)(withdrawalId);
24
+ }
25
+ function jperpWithdrawalIdFromHex(value) {
26
+ return (0, encoding_js_1.hexToBytes)(value, 32, "jPerp withdrawal ID");
27
+ }
28
+ /** Derive every client-held key field needed across the jPerp lifecycle. */
29
+ function deriveJperpKeyBundle(spendingKey, index) {
30
+ const withdrawalId = deriveJperpWithdrawalId(spendingKey, index);
31
+ const claimant = deriveJperpClaimantKeypair(spendingKey, withdrawalId);
32
+ return {
33
+ withdrawalId,
34
+ withdrawalIdHex: jperpWithdrawalIdToHex(withdrawalId),
35
+ claimant,
36
+ claimantPublicKey: claimant.publicKey.toBase58(),
37
+ claimantSecretKey: (0, encoding_js_1.encodeBase58)(claimant.secretKey),
38
+ };
39
+ }
40
+ /** Match the client reference's per-request Jupiter PDA namespace. */
41
+ function createJperpCounter() {
42
+ return String(Math.floor(Math.random() * 1000000000000));
43
+ }
@@ -0,0 +1,29 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import type { JperpSide } from "./types.js";
3
+ export declare const JPERP_SIDE_LONG = 1;
4
+ export declare const JPERP_SIDE_SHORT = 2;
5
+ export declare const JPERP_CHANGE_INCREASE = 1;
6
+ export declare const JPERP_CHANGE_DECREASE = 2;
7
+ export declare function deriveJupiterPerpetualsPda(): PublicKey;
8
+ export declare function deriveJperpExecutorPda(params: {
9
+ programId: PublicKey;
10
+ mint: PublicKey;
11
+ claimant: PublicKey;
12
+ withdrawalId: Uint8Array;
13
+ }): PublicKey;
14
+ export declare function deriveJperpSlotPda(params: {
15
+ programId: PublicKey;
16
+ mint: PublicKey;
17
+ withdrawalId: Uint8Array;
18
+ }): PublicKey;
19
+ export declare function deriveJupiterPerpPositionPda(params: {
20
+ executor: PublicKey;
21
+ custody: PublicKey;
22
+ collateralCustody: PublicKey;
23
+ side: JperpSide;
24
+ }): PublicKey;
25
+ export declare function deriveJupiterPerpRequestPda(params: {
26
+ position: PublicKey;
27
+ counter: bigint;
28
+ change: "increase" | "decrease";
29
+ }): PublicKey;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JPERP_CHANGE_DECREASE = exports.JPERP_CHANGE_INCREASE = exports.JPERP_SIDE_SHORT = exports.JPERP_SIDE_LONG = void 0;
4
+ exports.deriveJupiterPerpetualsPda = deriveJupiterPerpetualsPda;
5
+ exports.deriveJperpExecutorPda = deriveJperpExecutorPda;
6
+ exports.deriveJperpSlotPda = deriveJperpSlotPda;
7
+ exports.deriveJupiterPerpPositionPda = deriveJupiterPerpPositionPda;
8
+ exports.deriveJupiterPerpRequestPda = deriveJupiterPerpRequestPda;
9
+ const web3_js_1 = require("@solana/web3.js");
10
+ const encoding_js_1 = require("../identity/encoding.js");
11
+ const constants_js_1 = require("./constants.js");
12
+ exports.JPERP_SIDE_LONG = 1;
13
+ exports.JPERP_SIDE_SHORT = 2;
14
+ exports.JPERP_CHANGE_INCREASE = 1;
15
+ exports.JPERP_CHANGE_DECREASE = 2;
16
+ function deriveJupiterPerpetualsPda() {
17
+ return web3_js_1.PublicKey.findProgramAddressSync([(0, encoding_js_1.utf8)("perpetuals")], new web3_js_1.PublicKey(constants_js_1.JUPITER_PERPS_PROGRAM_ID))[0];
18
+ }
19
+ function deriveJperpExecutorPda(params) {
20
+ (0, encoding_js_1.assertBytes)(params.withdrawalId, 32, "jPerp withdrawal ID");
21
+ return web3_js_1.PublicKey.findProgramAddressSync([
22
+ (0, encoding_js_1.utf8)("jperp_executor"),
23
+ params.mint.toBytes(),
24
+ params.claimant.toBytes(),
25
+ params.withdrawalId,
26
+ ], params.programId)[0];
27
+ }
28
+ function deriveJperpSlotPda(params) {
29
+ (0, encoding_js_1.assertBytes)(params.withdrawalId, 32, "jPerp withdrawal ID");
30
+ return web3_js_1.PublicKey.findProgramAddressSync([(0, encoding_js_1.utf8)("jperp_slot_v1"), params.mint.toBytes(), params.withdrawalId], params.programId)[0];
31
+ }
32
+ function deriveJupiterPerpPositionPda(params) {
33
+ return web3_js_1.PublicKey.findProgramAddressSync([
34
+ (0, encoding_js_1.utf8)("position"),
35
+ params.executor.toBytes(),
36
+ new web3_js_1.PublicKey(constants_js_1.JUPITER_PERPS_POOL).toBytes(),
37
+ params.custody.toBytes(),
38
+ params.collateralCustody.toBytes(),
39
+ Uint8Array.from([
40
+ params.side === "long" ? exports.JPERP_SIDE_LONG : exports.JPERP_SIDE_SHORT,
41
+ ]),
42
+ ], new web3_js_1.PublicKey(constants_js_1.JUPITER_PERPS_PROGRAM_ID))[0];
43
+ }
44
+ function deriveJupiterPerpRequestPda(params) {
45
+ return web3_js_1.PublicKey.findProgramAddressSync([
46
+ (0, encoding_js_1.utf8)("position_request"),
47
+ params.position.toBytes(),
48
+ (0, encoding_js_1.uint64Le)(params.counter, "jPerp request counter"),
49
+ Uint8Array.from([
50
+ params.change === "increase"
51
+ ? exports.JPERP_CHANGE_INCREASE
52
+ : exports.JPERP_CHANGE_DECREASE,
53
+ ]),
54
+ ], new web3_js_1.PublicKey(constants_js_1.JUPITER_PERPS_PROGRAM_ID))[0];
55
+ }