-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreateMintConsume.ts
More file actions
85 lines (69 loc) · 2.48 KB
/
createMintConsume.ts
File metadata and controls
85 lines (69 loc) · 2.48 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
// 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, AccountId, NoteType } = await import(
"@demox-labs/miden-sdk"
);
const nodeEndpoint = "https://rpc.testnet.miden.io";
const client = await WebClient.createClient(nodeEndpoint);
// 1. Sync and log block
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 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 = "0x599a54603f0cf9000000ed7a11e379";
console.log("Sending tokens to Bob's account...");
const sendTxRequest = client.newSendTransactionRequest(
alice.id(),
AccountId.fromHex(bobAccountId),
faucet.id(),
NoteType.Public,
BigInt(100),
);
await client.submitNewTransaction(alice.id(), sendTxRequest);
console.log("Tokens sent successfully!");
}