-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathincrementCounterContract.ts
More file actions
136 lines (108 loc) · 3.69 KB
/
incrementCounterContract.ts
File metadata and controls
136 lines (108 loc) · 3.69 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
124
125
126
127
128
129
130
131
132
133
134
135
136
// lib/incrementCounterContract.ts
export async function incrementCounterContract(): 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 {
AccountId,
AssemblerUtils,
TransactionKernel,
TransactionRequestBuilder,
TransactionScript,
WebClient,
} = await import("@demox-labs/miden-sdk");
const nodeEndpoint = "https://rpc.testnet.miden.io";
const client = await WebClient.createClient(nodeEndpoint);
console.log("Current block number: ", (await client.syncState()).blockNum());
// Counter contract code in Miden Assembly
const counterContractCode = `
use.miden::account
use.std::sys
const.COUNTER_SLOT=0
# => []
export.get_count
push.COUNTER_SLOT
# => [index]
exec.account::get_item
# => [count]
exec.sys::truncate_stack
# => []
end
# => []
export.increment_count
push.COUNTER_SLOT
# => [index]
exec.account::get_item
# => [count]
add.1
# => [count+1]
debug.stack
push.COUNTER_SLOT
# [index, count+1]
exec.account::set_item
# => []
exec.sys::truncate_stack
# => []
end
`;
// Building the counter contract
let assembler = TransactionKernel.assembler();
// Counter contract account id on testnet
const counterContractId = AccountId.fromBech32(
"mtst1qrhk9zc2au2vxqzaynaz5ddhs4cqqghmajy",
);
// Reading the public state of the counter contract from testnet,
// and importing it into the WebClient
let counterContractAccount = await client.getAccount(counterContractId);
if (!counterContractAccount) {
await client.importAccountById(counterContractId);
await client.syncState();
counterContractAccount = await client.getAccount(counterContractId);
if (!counterContractAccount) {
throw new Error(`Account not found after import: ${counterContractId}`);
}
}
// Building the transaction script which will call the counter contract
let txScriptCode = `
use.external_contract::counter_contract
begin
call.counter_contract::increment_count
end
`;
// Creating the library to call the counter contract
let counterComponentLib = AssemblerUtils.createAccountComponentLibrary(
assembler, // assembler
"external_contract::counter_contract", // library path to call the contract
counterContractCode, // account code of the contract
);
// Creating the transaction script
let txScript = TransactionScript.compile(
txScriptCode,
assembler.withLibrary(counterComponentLib),
);
// Creating a transaction request with the transaction script
let txIncrementRequest = new TransactionRequestBuilder()
.withCustomScript(txScript)
.build();
// Executing the transaction script against the counter contract
let txResult = await client.newTransaction(
counterContractAccount.id(),
txIncrementRequest,
);
// Submitting the transaction result to the node
await client.submitTransaction(txResult);
// Sync state
await client.syncState();
// Logging the count of counter contract
let counter = await client.getAccount(counterContractAccount.id());
// Here we get the first Word from storage of the counter contract
// A word is comprised of 4 Felts, 2**64 - 2**32 + 1
let count = counter?.storage().getItem(0);
// Converting the Word represented as a hex to a single integer value
const counterValue = Number(
BigInt("0x" + count!.toHex().slice(-16).match(/../g)!.reverse().join("")),
);
console.log("Count: ", counterValue);
}