-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmultiSendWithDelegatedProver.ts
More file actions
123 lines (108 loc) · 4.33 KB
/
multiSendWithDelegatedProver.ts
File metadata and controls
123 lines (108 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Demonstrates multi-send functionality using a delegated prover on the Miden Network
* Creates multiple P2ID (Pay to ID) notes for different recipients
*
* @throws {Error} If the function cannot be executed in a browser environment
*/
export async function multiSendWithDelegatedProver(): Promise<void> {
// Ensure this runs only in a browser context
if (typeof window === 'undefined') return console.warn('Run in browser');
const {
WebClient,
AccountStorageMode,
Address,
NoteType,
TransactionProver,
NetworkId,
Note,
NoteAssets,
OutputNoteArray,
Felt,
FungibleAsset,
TransactionRequestBuilder,
OutputNote,
} = await import('@demox-labs/miden-sdk');
const client = await WebClient.createClient('https://rpc.testnet.miden.io');
const prover = TransactionProver.newRemoteProver(
'https://tx-prover.testnet.miden.io',
);
console.log('Latest block:', (await client.syncState()).blockNum());
// ── Creating new account ──────────────────────────────────────────────────────
console.log('Creating account for Alice…');
const alice = await client.newWallet(AccountStorageMode.public(), true, 0);
console.log('Alice accout ID:', alice.id().toString());
// ── Creating new faucet ──────────────────────────────────────────────────────
const faucet = await client.newFaucet(
AccountStorageMode.public(),
false,
'MID',
8,
BigInt(1_000_000),
0,
);
console.log('Faucet ID:', faucet.id().toString());
// ── mint 10 000 MID to Alice ──────────────────────────────────────────────────────
{
const txResult = await client.executeTransaction(
faucet.id(),
client.newMintTransactionRequest(
alice.id(),
faucet.id(),
NoteType.Public,
BigInt(10_000),
),
);
const proven = await client.proveTransaction(txResult, prover);
const submissionHeight = await client.submitProvenTransaction(
proven,
txResult,
);
await client.applyTransaction(txResult, submissionHeight);
console.log('waiting for settlement');
await new Promise((r) => setTimeout(r, 7_000));
await client.syncState();
}
// ── consume the freshly minted notes ──────────────────────────────────────────────
const noteIds = (await client.getConsumableNotes(alice.id())).map((rec) =>
rec.inputNoteRecord().id().toString(),
);
{
const txResult = await client.executeTransaction(
alice.id(),
client.newConsumeTransactionRequest(noteIds),
);
const proven = await client.proveTransaction(txResult, prover);
await client.syncState();
const submissionHeight = await client.submitProvenTransaction(
proven,
txResult,
);
await client.applyTransaction(txResult, submissionHeight);
}
// ── build 3 P2ID notes (100 MID each) ─────────────────────────────────────────────
const recipientAddresses = [
'mtst1aqezqc90x7dkzypr9m5fmlpp85w6cl04',
'mtst1apjg2ul76wrkxyr5qlcnczaskypa4ljn',
'mtst1arpee6y9cm8t7ypn33pc8fzj6gkzz7kd',
];
const assets = new NoteAssets([new FungibleAsset(faucet.id(), BigInt(100))]);
const p2idNotes = recipientAddresses.map((addr) => {
const receiverAccountId = Address.fromBech32(addr).accountId();
const note = Note.createP2IDNote(
alice.id(),
receiverAccountId,
assets,
NoteType.Public,
new Felt(BigInt(0)),
);
return OutputNote.full(note);
});
// ── create all P2ID notes ───────────────────────────────────────────────────────────────
await client.submitNewTransaction(
alice.id(),
new TransactionRequestBuilder()
.withOwnOutputNotes(new OutputNoteArray(p2idNotes))
.build(),
);
console.log('All notes created ✅');
}