@veilo/sdk-core 0.1.17 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1257 -272
- package/dist/cjs/client.d.ts +407 -0
- package/dist/cjs/client.js +914 -0
- package/dist/cjs/config.d.ts +82 -0
- package/dist/cjs/config.js +57 -0
- package/dist/cjs/events.d.ts +77 -0
- package/dist/cjs/events.js +167 -0
- package/dist/cjs/idl/privacy_pool.json +10313 -0
- package/dist/cjs/index.d.ts +13 -0
- package/dist/cjs/index.js +57 -0
- package/dist/cjs/merkle.d.ts +64 -0
- package/dist/cjs/merkle.js +133 -0
- package/dist/cjs/poseidon.d.ts +29 -0
- package/dist/cjs/poseidon.js +100 -0
- package/dist/cjs/program.d.ts +26 -0
- package/dist/cjs/program.js +38 -0
- package/dist/cjs/proof.d.ts +183 -0
- package/dist/cjs/proof.js +292 -0
- package/dist/cjs/prover.d.ts +54 -0
- package/dist/cjs/prover.js +112 -0
- package/dist/cjs/relayer.d.ts +295 -0
- package/dist/cjs/relayer.js +246 -0
- package/dist/cjs/retry.d.ts +32 -0
- package/dist/cjs/retry.js +75 -0
- package/dist/cjs/utxo.d.ts +215 -0
- package/dist/cjs/utxo.js +394 -0
- package/dist/esm/client.js +887 -0
- package/dist/esm/config.js +51 -0
- package/dist/esm/events.js +129 -0
- package/dist/esm/idl/privacy_pool.json +10313 -0
- package/dist/esm/index.js +22 -0
- package/dist/esm/merkle.js +129 -0
- package/dist/esm/poseidon.js +87 -0
- package/dist/esm/program.js +31 -0
- package/dist/esm/proof.js +281 -0
- package/dist/esm/prover.js +75 -0
- package/dist/esm/relayer.js +238 -0
- package/dist/esm/retry.js +71 -0
- package/dist/esm/utxo.js +372 -0
- package/package.json +47 -11
- package/src/client.ts +0 -352
- package/src/config.ts +0 -13
- package/src/index.ts +0 -6
- package/src/merkle.ts +0 -178
- package/src/note.ts +0 -193
- package/src/poseidon.ts +0 -62
- package/src/proof.ts +0 -170
- package/test/script.js +0 -0
- package/test-tsconfig.json +0 -19
- package/tests/note.test.ts +0 -50
- package/tests/sdk.integration.test.ts +0 -210
- package/tsconfig.json +0 -18
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
|
|
2
|
+
// -----------------------------------------------------------------------------
|
|
3
|
+
// Constants
|
|
4
|
+
// -----------------------------------------------------------------------------
|
|
5
|
+
/** Default relayer fee in basis points (50 = 0.5%). */
|
|
6
|
+
export const DEFAULT_FEE_BPS = 50;
|
|
7
|
+
/** Merkle tree depth - matches on-chain MerkleTreeAccount subtrees array size */
|
|
8
|
+
export const MERKLE_TREE_DEPTH = 22;
|
|
9
|
+
/** Root history size - matches on-chain MerkleTreeAccount root_history array size */
|
|
10
|
+
export const ROOT_HISTORY_SIZE = 256;
|
|
11
|
+
/** Native SOL mint address (Pubkey::default() = all zeros) */
|
|
12
|
+
export const NATIVE_SOL_MINT = PublicKey.default;
|
|
13
|
+
// -----------------------------------------------------------------------------
|
|
14
|
+
// Helpers
|
|
15
|
+
// -----------------------------------------------------------------------------
|
|
16
|
+
/** Convert SOL to lamports (e.g. `sol(1.5)` → `1_500_000_000n`). */
|
|
17
|
+
export function sol(n) {
|
|
18
|
+
return BigInt(n * LAMPORTS_PER_SOL);
|
|
19
|
+
}
|
|
20
|
+
// -----------------------------------------------------------------------------
|
|
21
|
+
// Fee Calculation Helpers
|
|
22
|
+
// -----------------------------------------------------------------------------
|
|
23
|
+
/**
|
|
24
|
+
* Compute the relayer fee for a withdrawal.
|
|
25
|
+
* Mirrors the formula in the relayer-server:
|
|
26
|
+
* fee = max(amount * feeBps / 10_000, minWithdrawalFee)
|
|
27
|
+
*
|
|
28
|
+
* @returns fee in base token units, and the amount that reaches the recipient
|
|
29
|
+
*/
|
|
30
|
+
export function computeWithdrawalFee(amount, feeBps, minWithdrawalFee) {
|
|
31
|
+
const computed = (amount * BigInt(feeBps)) / 10000n;
|
|
32
|
+
const fee = computed > minWithdrawalFee ? computed : minWithdrawalFee;
|
|
33
|
+
return { fee, toRecipient: amount - fee };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Compute the relayer fee for a cross-pool swap.
|
|
37
|
+
* Mirrors the formula in the relayer-server:
|
|
38
|
+
* relayerFee = max(estimatedOutput * swapFeeBps / 10_000, minSwapFee)
|
|
39
|
+
* destAmount = minAmountOut - relayerFee
|
|
40
|
+
*
|
|
41
|
+
* @param estimatedOutput Jupiter quote output amount
|
|
42
|
+
* @param swapFeeBps From the destination pool's PrivacyConfigAccount
|
|
43
|
+
* @param minSwapFee From the destination pool's PrivacyConfigAccount
|
|
44
|
+
* @param minAmountOut User-agreed minimum swap output (after slippage)
|
|
45
|
+
* @returns relayerFee and the amount committed into the destination UTXO
|
|
46
|
+
*/
|
|
47
|
+
export function computeSwapFee(estimatedOutput, swapFeeBps, minSwapFee, minAmountOut) {
|
|
48
|
+
const computed = (estimatedOutput * BigInt(swapFeeBps)) / 10000n;
|
|
49
|
+
const relayerFee = computed > minSwapFee ? computed : minSwapFee;
|
|
50
|
+
return { relayerFee, destAmount: minAmountOut - relayerFee };
|
|
51
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
2
|
+
import { MerkleTree } from "./merkle";
|
|
3
|
+
// -----------------------------------------------------------------------------
|
|
4
|
+
// Event Scanning
|
|
5
|
+
// -----------------------------------------------------------------------------
|
|
6
|
+
/**
|
|
7
|
+
* Fetch and parse CommitmentEvents for a program, optionally filtered by mint.
|
|
8
|
+
*
|
|
9
|
+
* @param program - Anchor program instance
|
|
10
|
+
* @param mintAddress - Filter to a specific pool; omit to get all pools
|
|
11
|
+
* @param before - Start paging before this signature (exclusive)
|
|
12
|
+
* @param until - Stop paging at this signature (exclusive)
|
|
13
|
+
* @param limit - Max signatures to fetch per page (default 1000)
|
|
14
|
+
*/
|
|
15
|
+
export async function scanCommitmentEvents(params) {
|
|
16
|
+
const { program, mintAddress, before, until, limit = 1000 } = params;
|
|
17
|
+
const out = [];
|
|
18
|
+
const eventParser = new anchor.EventParser(program.programId, new anchor.BorshCoder(program.idl));
|
|
19
|
+
const sigs = await program.provider.connection.getSignaturesForAddress(program.programId, { before, until, limit });
|
|
20
|
+
for (const sigInfo of sigs) {
|
|
21
|
+
if (sigInfo.err)
|
|
22
|
+
continue;
|
|
23
|
+
const tx = await program.provider.connection.getTransaction(sigInfo.signature, { commitment: "confirmed", maxSupportedTransactionVersion: 0 });
|
|
24
|
+
if (!tx)
|
|
25
|
+
continue;
|
|
26
|
+
const events = Array.from(eventParser.parseLogs(tx.meta?.logMessages ?? []));
|
|
27
|
+
for (const ev of events) {
|
|
28
|
+
if (ev.name !== "commitmentEvent")
|
|
29
|
+
continue;
|
|
30
|
+
const d = ev.data;
|
|
31
|
+
const evMint = d.mintAddress;
|
|
32
|
+
if (mintAddress && !evMint.equals(mintAddress))
|
|
33
|
+
continue;
|
|
34
|
+
out.push({
|
|
35
|
+
commitment: new Uint8Array(d.commitment),
|
|
36
|
+
leafIndex: Number(d.leafIndex),
|
|
37
|
+
newRoot: new Uint8Array(d.newRoot),
|
|
38
|
+
timestamp: Number(d.timestamp),
|
|
39
|
+
mintAddress: evMint,
|
|
40
|
+
treeId: Number(d.treeId),
|
|
41
|
+
txSignature: sigInfo.signature,
|
|
42
|
+
slot: sigInfo.slot,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Fetch and parse NullifierSpent events for a program, optionally filtered by mint.
|
|
50
|
+
*/
|
|
51
|
+
export async function scanNullifierEvents(params) {
|
|
52
|
+
const { program, mintAddress, before, until, limit = 1000 } = params;
|
|
53
|
+
const out = [];
|
|
54
|
+
const eventParser = new anchor.EventParser(program.programId, new anchor.BorshCoder(program.idl));
|
|
55
|
+
const sigs = await program.provider.connection.getSignaturesForAddress(program.programId, { before, until, limit });
|
|
56
|
+
for (const sigInfo of sigs) {
|
|
57
|
+
if (sigInfo.err)
|
|
58
|
+
continue;
|
|
59
|
+
const tx = await program.provider.connection.getTransaction(sigInfo.signature, { commitment: "confirmed", maxSupportedTransactionVersion: 0 });
|
|
60
|
+
if (!tx)
|
|
61
|
+
continue;
|
|
62
|
+
const events = Array.from(eventParser.parseLogs(tx.meta?.logMessages ?? []));
|
|
63
|
+
for (const ev of events) {
|
|
64
|
+
if (ev.name !== "nullifierSpent")
|
|
65
|
+
continue;
|
|
66
|
+
const d = ev.data;
|
|
67
|
+
const evMint = d.mintAddress;
|
|
68
|
+
if (mintAddress && !evMint.equals(mintAddress))
|
|
69
|
+
continue;
|
|
70
|
+
out.push({
|
|
71
|
+
nullifier: new Uint8Array(d.nullifier),
|
|
72
|
+
timestamp: Number(d.timestamp),
|
|
73
|
+
mintAddress: evMint,
|
|
74
|
+
treeId: Number(d.treeId),
|
|
75
|
+
txSignature: sigInfo.signature,
|
|
76
|
+
slot: sigInfo.slot,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
// -----------------------------------------------------------------------------
|
|
83
|
+
// Tree Reconstruction
|
|
84
|
+
// -----------------------------------------------------------------------------
|
|
85
|
+
/**
|
|
86
|
+
* Rebuild a local MerkleTree from on-chain CommitmentEvents.
|
|
87
|
+
* Use this to get a valid inclusion proof for spending any UTXO.
|
|
88
|
+
*
|
|
89
|
+
* CommitmentEvents are sorted by (treeId, leafIndex) so the local tree
|
|
90
|
+
* matches the on-chain state exactly.
|
|
91
|
+
*
|
|
92
|
+
* @param program - Anchor program instance
|
|
93
|
+
* @param mintAddress - Pool to reconstruct
|
|
94
|
+
* @param treeId - Which tree to reconstruct (default 0)
|
|
95
|
+
* @param depth - Merkle tree depth (default 22, must match on-chain)
|
|
96
|
+
*/
|
|
97
|
+
export async function buildTreeFromEvents(params) {
|
|
98
|
+
const { program, mintAddress, treeId = 0, depth = 22 } = params;
|
|
99
|
+
// Fetch all commitment events for this pool (page until no more results).
|
|
100
|
+
// Oldest-first so we insert leaves in the correct order.
|
|
101
|
+
const allEvents = [];
|
|
102
|
+
let before;
|
|
103
|
+
while (true) {
|
|
104
|
+
const page = await scanCommitmentEvents({
|
|
105
|
+
program,
|
|
106
|
+
mintAddress,
|
|
107
|
+
before,
|
|
108
|
+
limit: 1000,
|
|
109
|
+
});
|
|
110
|
+
if (page.length === 0)
|
|
111
|
+
break;
|
|
112
|
+
allEvents.push(...page);
|
|
113
|
+
before = page[page.length - 1].txSignature;
|
|
114
|
+
if (page.length < 1000)
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
// Keep only events for this treeId, sort by leafIndex ascending
|
|
118
|
+
const treeEvents = allEvents
|
|
119
|
+
.filter((e) => e.treeId === treeId)
|
|
120
|
+
.sort((a, b) => a.leafIndex - b.leafIndex);
|
|
121
|
+
const tree = new MerkleTree(depth);
|
|
122
|
+
for (const ev of treeEvents) {
|
|
123
|
+
tree.insert(ev.commitment);
|
|
124
|
+
}
|
|
125
|
+
const latestSignature = treeEvents.length > 0
|
|
126
|
+
? treeEvents[treeEvents.length - 1].txSignature
|
|
127
|
+
: undefined;
|
|
128
|
+
return { tree, events: treeEvents, latestSignature };
|
|
129
|
+
}
|