forked from hiero-ledger/hiero-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount-hooks-example.js
More file actions
177 lines (151 loc) · 5.77 KB
/
account-hooks-example.js
File metadata and controls
177 lines (151 loc) · 5.77 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { decode } from "../src/encoding/hex.js";
import fs from "fs";
import {
AccountCreateTransaction,
AccountUpdateTransaction,
ContractCreateTransaction,
PrivateKey,
Hbar,
Client,
AccountId,
HookCreationDetails,
EvmHook,
HookExtensionPoint,
Long,
} from "@hiero-ledger/sdk";
import dotenv from "dotenv";
dotenv.config();
async function main() {
if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
process.env.HEDERA_NETWORK == null
) {
throw new Error(
"Environment variables OPERATOR_ID, HEDERA_NETWORK, and OPERATOR_KEY are required.",
);
}
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringECDSA(process.env.OPERATOR_KEY);
const network = process.env.HEDERA_NETWORK;
const client = Client.forName(network).setOperator(operatorId, operatorKey);
try {
console.log("Account Hooks Example Start!");
/*
* Step 1: Create the hook contract.
*/
console.log("Creating hook contract...");
// Create bytecode file for the hook contract
console.log("Creating bytecode for hook contract...");
const contractBytecodeHex = fs.readFileSync(
"./contracts/HieroHookContract.bytecode.txt",
"utf8",
);
// Create the hook contract
console.log("Creating hook contract...");
let { contractId } = await (
await (
await new ContractCreateTransaction()
.setAdminKey(operatorKey.publicKey)
.setGas(500_000)
.setBytecode(decode(contractBytecodeHex))
.setMaxTransactionFee(new Hbar(10))
.freezeWith(client)
.sign(operatorKey)
).execute(client)
).getReceipt(client);
console.log("Hook contract created with ID:", contractId.toString());
/*
* Step 2: Demonstrate creating an account with hooks.
*/
console.log("\n=== Creating Account with Hooks ===");
console.log("Creating account with EVM hook...");
const accountKey = PrivateKey.generate();
const accountPublicKey = accountKey.publicKey;
// Create a EVM hook
const evmHook = new EvmHook({ contractId });
// Create hook creation details
const adminKey = client.operatorPublicKey;
const hookWithId1002 = new HookCreationDetails({
extensionPoint: HookExtensionPoint.ACCOUNT_ALLOWANCE_HOOK,
hookId: Long.fromInt(1002),
evmHook: evmHook,
adminKey: adminKey,
});
let { accountId } = await (
await (
await new AccountCreateTransaction()
.setKeyWithoutAlias(accountPublicKey)
.setInitialBalance(new Hbar(1))
.addHook(hookWithId1002)
.setMaxTransactionFee(new Hbar(10))
.freezeWith(client)
.sign(operatorKey)
).execute(client)
).getReceipt(client);
console.log(`account id = ${accountId.toString()}`);
console.log("Successfully created account with EVM hook!");
/*
* Step 3: Demonstrate adding hooks to an existing account.
*/
console.log("\n=== Adding Hooks to Existing Account ===");
console.log("Adding hooks to existing account...");
// Create basic hooks with no storage updates
const basicHook = new EvmHook({ contractId });
const hookWithId1 = new HookCreationDetails({
extensionPoint: HookExtensionPoint.ACCOUNT_ALLOWANCE_HOOK,
hookId: Long.fromInt(1),
evmHook: basicHook,
adminKey: adminKey,
});
const basicHook2 = new EvmHook({ contractId });
const hookWithId2 = new HookCreationDetails({
extensionPoint: HookExtensionPoint.ACCOUNT_ALLOWANCE_HOOK,
hookId: Long.fromInt(2),
evmHook: basicHook2,
adminKey: adminKey,
});
try {
await (
await (
await new AccountUpdateTransaction()
.setAccountId(accountId)
.addHookToCreate(hookWithId1)
.addHookToCreate(hookWithId2)
.setMaxTransactionFee(new Hbar(10))
.freezeWith(client)
.sign(accountKey)
).execute(client)
).getReceipt(client);
console.log("Successfully added hooks to account!");
} catch (error) {
console.error("Failed to execute hook transaction:", error);
}
/*
* Step 4: Demonstrate hook deletion.
*/
console.log("\n=== Deleting Hooks from Account ===");
console.log("Deleting hooks from account...");
try {
await (
await (
await new AccountUpdateTransaction()
.setAccountId(accountId)
.addHookToDelete(Long.fromNumber(1))
.addHookToDelete(Long.fromNumber(2))
.setMaxTransactionFee(new Hbar(10))
.freezeWith(client)
.sign(accountKey)
).execute(client)
).getReceipt(client);
console.log("Successfully deleted hooks (IDs: 1, 2)");
} catch (error) {
console.error("Failed to execute hook deletion:", error);
}
console.log("Account Hooks Example Complete!");
} catch (error) {
console.error("Error occurred:", error);
}
client.close();
}
void main();