-
Notifications
You must be signed in to change notification settings - Fork 1
/
2_transferHbar2Contract_viaReceiveOrFallbackFcn.js
73 lines (66 loc) · 2.87 KB
/
2_transferHbar2Contract_viaReceiveOrFallbackFcn.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
console.clear();
require("dotenv").config();
const {
AccountId,
PrivateKey,
Client,
FileCreateTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
Hbar,
TransferTransaction,
ContractInfoQuery,
AccountBalanceQuery,
} = 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.forTestnet().setOperator(operatorId, operatorKey);
async function main() {
// Import the compiled contract bytecode
const contractBytecode = fs.readFileSync(
"2_transferHbar2Contract_viaReceiveOrFallbackFcn_sol_hbar2Contract.bin"
);
// Create a file on Hedera and store the bytecode
const fileCreateTx = new FileCreateTransaction()
.setContents(contractBytecode)
.setKeys([operatorKey])
.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);
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`);
// Transfer HBAR to smart contract using .setPayableAmount
const contractExecuteTx = new ContractExecuteTransaction()
.setContractId(contractId)
.setPayableAmount(100)
.setGas(300000);
const contractExecuteSubmit = await contractExecuteTx.execute(client);
const contractExecuteRx = await contractExecuteSubmit.getReceipt(client);
console.log(`- Contract function call status: ${contractExecuteRx.status} \n`);
// Query the contract balance calling the function in the contract
const contractQueryTx = new ContractCallQuery()
.setContractId(contractId)
.setGas(100000)
.setFunction("getBalance");
const contractQuerySubmit = await contractQueryTx.execute(client);
const contractQueryResult = contractQuerySubmit.getUint256(0);
console.log(`- Contract balance (from getBalance fcn): ${contractQueryResult} \n`);
const cCheck = await new ContractInfoQuery().setContractId(contractId).execute(client);
console.log(`- Contract balance (from ContractInfoQuery): ${cCheck.balance.toString()} \n`);
}
main();