-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
66 lines (59 loc) · 2.81 KB
/
index.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
console.clear();
require("dotenv").config();
const {
AccountId,
PrivateKey,
Client,
FileCreateTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
Hbar,
ContractCreateFlow,
} = require("@hashgraph/sdk");
const fs = require("fs");
// Configure accounts and client
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringED25519(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
async function main() {
// Import the compiled contract bytecode
const contractBytecode = fs.readFileSync("LookupContract_sol_LookupContract.bin");
// Instantiate the smart contract
const contractInstantiateTx = new ContractCreateFlow()
.setBytecode(contractBytecode)
.setGas(200000)
.setConstructorParameters(new ContractFunctionParameters().addString("Alice").addUint256(111111));
const contractInstantiateSubmit = await contractInstantiateTx.execute(client);
const contractInstantiateRx = await contractInstantiateSubmit.getReceipt(client);
const contractId = contractInstantiateRx.contractId;
const contractAddress = contractId.toSolidityAddress();
console.log(`- The smart contract ID is: ${contractId} \n`);
console.log(`- The smart contract ID in Solidity format is: ${contractAddress} \n`);
// Query the contract to check changes in state variable
const contractQueryTx = new ContractCallQuery()
.setContractId(contractId)
.setGas(100000)
.setFunction("getMobileNumber", new ContractFunctionParameters().addString("Alice"));
const contractQuerySubmit = await contractQueryTx.execute(client);
const contractQueryResult = contractQuerySubmit.getUint256(0);
console.log(`- Here's the phone number that you asked for: ${contractQueryResult} \n`);
// Call contract function to update the state variable
const contractExecuteTx = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(100000)
.setFunction("setMobileNumber", new ContractFunctionParameters().addString("Bob").addUint256(222222));
const contractExecuteSubmit = await contractExecuteTx.execute(client);
const contractExecuteRx = await contractExecuteSubmit.getReceipt(client);
console.log(`- Contract function call status: ${contractExecuteRx.status} \n`);
// Query the contract to check changes in state variable
const contractQueryTx1 = new ContractCallQuery()
.setContractId(contractId)
.setGas(100000)
.setFunction("getMobileNumber", new ContractFunctionParameters().addString("Bob"));
const contractQuerySubmit1 = await contractQueryTx1.execute(client);
const contractQueryResult1 = contractQuerySubmit1.getUint256(0);
console.log(`- Here's the phone number that you asked for: ${contractQueryResult1} \n`);
}
main();