-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathincrementCounterContract.ts
More file actions
161 lines (128 loc) · 4.34 KB
/
incrementCounterContract.ts
File metadata and controls
161 lines (128 loc) · 4.34 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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 {
Address,
AccountBuilder,
AccountComponent,
AccountStorageMode,
AccountType,
SecretKey,
StorageMap,
StorageSlot,
TransactionRequestBuilder,
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::active_account
use miden::native_account
use.std::sys
const.COUNTER_SLOT=0
#! Inputs: []
#! Outputs: [count]
export.get_count
push.COUNTER_SLOT
# => [index]
exec.active_account::get_item
# => [count]
# clean up stack
movdn.4 dropw
# => [count]
end
#! Inputs: []
#! Outputs: []
export.increment_count
push.COUNTER_SLOT
# => [index]
exec.active_account::get_item
# => [count]
add.1
# => [count+1]
debug.stack
push.COUNTER_SLOT
# [index, count+1]
exec.native_account::set_item
# => [OLD_VALUE]
dropw
# => []
end
`;
// Building the counter contract
// Counter contract account id on testnet
const counterContractId = Address.fromBech32(
'mtst1arjemrxne8lj5qz4mg9c8mtyxg954483',
).accountId();
// 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}`);
}
}
const builder = client.createScriptBuilder();
const storageMap = new StorageMap();
const storageSlotMap = StorageSlot.map(storageMap);
const mappingAccountComponent = AccountComponent.compile(
counterContractCode,
builder,
[storageSlotMap],
).withSupportsAllTypes();
const walletSeed = new Uint8Array(32);
crypto.getRandomValues(walletSeed);
const secretKey = SecretKey.rpoFalconWithRNG(walletSeed);
const authComponent = AccountComponent.createAuthComponent(secretKey);
const accountBuilderResult = new AccountBuilder(walletSeed)
.accountType(AccountType.RegularAccountImmutableCode)
.storageMode(AccountStorageMode.public())
.withAuthComponent(authComponent)
.withComponent(mappingAccountComponent)
.build();
await client.addAccountSecretKeyToWebStore(secretKey);
await client.newAccount(accountBuilderResult.account, false);
await client.syncState();
const accountCodeLib = builder.buildLibrary(
'external_contract::counter_contract',
counterContractCode,
);
builder.linkDynamicLibrary(accountCodeLib);
// Building the transaction script which will call the counter contract
const txScriptCode = `
use.external_contract::counter_contract
begin
call.counter_contract::increment_count
end
`;
const txScript = builder.compileTxScript(txScriptCode);
const txIncrementRequest = new TransactionRequestBuilder()
.withCustomScript(txScript)
.build();
// Executing the transaction script against the counter contract
await client.submitNewTransaction(
counterContractAccount.id(),
txIncrementRequest,
);
// Sync state
await client.syncState();
// Logging the count of counter contract
const 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
const 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);
}