-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscript-hscs-smart-contract.js
executable file
·116 lines (104 loc) · 3.93 KB
/
script-hscs-smart-contract.js
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
#!/usr/bin/env node
import fs from 'node:fs/promises';
import { JsonRpcProvider } from '@ethersproject/providers';
import { Wallet } from '@ethersproject/wallet';
import { ContractFactory } from '@ethersproject/contracts';
import dotenv from 'dotenv';
import {
CHARS,
createLogger,
calculateTransactionFeeFromViem,
} from '../util/util.js';
const logger = await createLogger({
scriptId: 'hscsSC',
scriptCategory: 'task',
});
const solidityFileName = 'my_contract_sol_MyContract';
let client;
async function scriptHscsSmartContract() {
logger.logStart('Hello Future World - HSCS smart contract - start');
// Read in environment variables from `.env` file in parent directory
dotenv.config({ path: '../.env' });
// Initialize the operator account
const operatorIdStr = process.env.OPERATOR_ACCOUNT_ID;
const operatorKeyStr = process.env.OPERATOR_ACCOUNT_PRIVATE_KEY;
const rpcUrl = process.env.RPC_URL;
if (!operatorIdStr || !operatorKeyStr || !rpcUrl) {
throw new Error(
'Must set OPERATOR_ACCOUNT_ID, OPERATOR_ACCOUNT_PRIVATE_KEY, and RPC_URL environment variables',
);
}
logger.logSection('Initializing operator account');
const rpcProvider = new JsonRpcProvider(rpcUrl);
const operatorWallet = new Wallet(operatorKeyStr, rpcProvider);
const operatorAddress = operatorWallet.address;
logger.log('Operator account initialized:', operatorAddress);
// Compile smart contract
await logger.logSection('Reading compiled smart contract artefacts');
const abi = await fs.readFile(`${solidityFileName}.abi`, {
encoding: 'utf8',
});
const evmBytecode = await fs.readFile(`${solidityFileName}.bin`, {
encoding: 'utf8',
});
logger.log(
'Compiled smart contract ABI:',
abi.substring(0, 32),
CHARS.HELLIP,
);
logger.log(
'Compiled smart contract EVM bytecode:',
evmBytecode.substring(0, 32),
CHARS.HELLIP,
);
// Deploy smart contract
// NOTE: Prepare smart contract for deployment
await logger.logSection('Deploying smart contract');
const myContractFactory = new ContractFactory(
abi,
evmBytecode,
operatorWallet,
);
const myContract = await myContractFactory.deploy();
const deployTx = myContract.deployTransaction;
const deploymentTxReceipt = await deployTx.wait();
console.log(
'Smart contract deployment transaction fee',
calculateTransactionFeeFromViem(deploymentTxReceipt),
);
const deploymentTxAddress = myContract.address;
logger.log('Smart contract deployment address:', deploymentTxAddress);
const deploymentTxHashscanUrl = `https://hashscan.io/testnet/contract/${deploymentTxAddress}`;
logger.log(
'Smart contract deployment Hashscan URL:\n',
...logger.applyAnsi('URL', deploymentTxHashscanUrl),
);
// Write data to smart contract
// NOTE: Invoke a smart contract transaction
await logger.logSection('Write data to smart contract');
const scWriteTxRequest = await myContract.functions.introduce(
`${logger.version} - ${logger.scriptId}`,
);
const scWriteTxReceipt = await scWriteTxRequest.wait();
logger.log(
'Smart contract write transaction fee',
calculateTransactionFeeFromViem(scWriteTxReceipt),
);
const scWriteTxHash = scWriteTxReceipt.transactionHash;
const scWriteTxHashscanUrl = `https://hashscan.io/testnet/transaction/${scWriteTxHash}`;
logger.log('Smart contract write transaction hash', scWriteTxHash);
logger.log(
'Smart contract write transaction Hashscan URL:\n',
...logger.applyAnsi('URL', scWriteTxHashscanUrl),
);
// Read data from smart contract
// NOTE: Invoke a smart contract query
await logger.logSection('Read data from smart contract');
const [scReadQueryResult] = await myContract.functions.greet();
logger.log('Smart contract read query result:', scReadQueryResult);
logger.logComplete('Hello Future World - HSCS smart contract - complete');
}
scriptHscsSmartContract().catch((ex) => {
client && client.close();
logger ? logger.logError(ex) : console.error(ex);
});