-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (92 loc) · 3.58 KB
/
index.js
File metadata and controls
99 lines (92 loc) · 3.58 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
// console.clear();
require("dotenv").config();
const {
AccountId,
PrivateKey,
Client,
FileCreateTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
Hbar
} = require("@hashgraph/sdk");
const fs = require("fs");
// Configure accounts and client
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forPreviewnet().setOperator(operatorId, operatorKey);
async function main() {
// Import the compiled contract bytecode
const contractBytecode = fs.readFileSync(
"LookupContract_sol_LookupContract.bin"
);
// Create a file on Hedera and store the bytecode
const fileCreateTx = new FileCreateTransaction()
.setContents(contractBytecode)
.setKeys([operatorKey])
.setMaxTransactionFee(new Hbar(0.75))
.freezeWith(client);
const fileCreateSign = await fileCreateTx.sign(operatorKey);
const fileCreateSubmit = await fileCreateSign.execute(client);
const fileCreateRx = await fileCreateSubmit.getReceipt(client);
const bytecodeFileId = fileCreateRx.fileId;
console.log(`- The bytecode file ID is: ${bytecodeFileId} \n`);
// Instantiate the smart contract
const contractInstantiateTx = new ContractCreateTransaction()
.setBytecodeFileId(bytecodeFileId)
.setGas(100000)
.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(`- Smart contract ID in Solidity format: ${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")
)
.setMaxQueryPayment(new Hbar(0.00000001));
const contractQuerySubmit = await contractQueryTx.execute(client);
const contractQueryResult = contractQuerySubmit.getUint256(0);
console.log(
`- Here's the phone number 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)
)
.setMaxTransactionFee(new Hbar(0.75));
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 you asked for: ${contractQueryResult1} \n`
);
}
main();