-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreateMintConsume.ts
More file actions
87 lines (71 loc) · 2.54 KB
/
createMintConsume.ts
File metadata and controls
87 lines (71 loc) · 2.54 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
// lib/createMintConsume.ts
export async function createMintConsume(): Promise<void> {
if (typeof window === 'undefined') {
console.warn('webClient() can only run in the browser');
return;
}
// dynamic import → only in the browser, so WASM is loaded client‑side
const { WebClient, AccountStorageMode, NoteType, Address } = await import(
'@demox-labs/miden-sdk'
);
const nodeEndpoint = 'https://rpc.testnet.miden.io';
const client = await WebClient.createClient(nodeEndpoint);
// 1. Sync with the latest blockchain state
const state = await client.syncState();
console.log('Latest block number:', state.blockNum());
// 2. Create Alice's account
console.log('Creating account for Alice…');
const alice = await client.newWallet(AccountStorageMode.public(), true, 0);
console.log('Alice ID:', alice.id().toString());
// 3. Deploy a fungible faucet
console.log('Creating faucet…');
const faucet = await client.newFaucet(
AccountStorageMode.public(),
false,
'MID',
8,
BigInt(1_000_000),
0,
);
console.log('Faucet ID:', faucet.id().toString());
await client.syncState();
// 4. Mint tokens to Alice
await client.syncState();
console.log('Minting tokens to Alice...');
const mintTxRequest = client.newMintTransactionRequest(
alice.id(),
faucet.id(),
NoteType.Public,
BigInt(1000),
);
await client.submitNewTransaction(faucet.id(), mintTxRequest);
console.log('Waiting 10 seconds for transaction confirmation...');
await new Promise((resolve) => setTimeout(resolve, 10000));
await client.syncState();
// 5. Fetch minted notes
const mintedNotes = await client.getConsumableNotes(alice.id());
const mintedNoteIds = mintedNotes.map((n) =>
n.inputNoteRecord().id().toString(),
);
console.log('Minted note IDs:', mintedNoteIds);
// 6. Consume minted notes
console.log('Consuming minted notes...');
const consumeTxRequest = client.newConsumeTransactionRequest(mintedNoteIds);
await client.submitNewTransaction(alice.id(), consumeTxRequest);
await client.syncState();
console.log('Notes consumed.');
// 7. Send tokens to Bob
const bobAccountId = Address.fromBech32(
'mtst1apve54rq8ux0jqqqqrkh5y0r0y8cwza6_qruqqypuyph',
).accountId();
console.log("Sending tokens to Bob's account...");
const sendTxRequest = client.newSendTransactionRequest(
alice.id(),
bobAccountId,
faucet.id(),
NoteType.Public,
BigInt(100),
);
await client.submitNewTransaction(alice.id(), sendTxRequest);
console.log('Tokens sent successfully!');
}