-
Notifications
You must be signed in to change notification settings - Fork 1
/
9_transferHbarFromContract.js
164 lines (149 loc) · 5.79 KB
/
9_transferHbarFromContract.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
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
console.clear();
require("dotenv").config();
const {
AccountId,
PrivateKey,
Client,
FileCreateTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
TransferTransaction,
ContractInfoQuery,
} = 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 treasuryId = AccountId.fromString(process.env.TREASURY_ID);
const treasuryKey = PrivateKey.fromString(process.env.TREASURY_PVKEY);
const aliceId = AccountId.fromString(process.env.ALICE_ID);
const aliceyKey = PrivateKey.fromString(process.env.ALICE_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function main() {
// Import the compiled contract bytecode
const contractBytecode = fs.readFileSync(
"9_transferHbarFromContract_sol_hbarFromContract.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 TransferTransaction()
const contractExecuteTx = new TransferTransaction()
.addHbarTransfer(treasuryId, -30)
.addHbarTransfer(contractId, 30)
.freezeWith(client);
const contractExecuteSign = await contractExecuteTx.sign(treasuryKey);
const contractExecuteSubmit = await contractExecuteSign.execute(client);
const contractExecuteRx = await contractExecuteSubmit.getReceipt(client);
console.log(`- Crypto transfer to contract: ${contractExecuteRx.status} \n`);
// Query the contract balance calling the function in the contract
let contractQueryTx = new ContractCallQuery()
.setContractId(contractId)
.setGas(100000)
.setFunction("getBalance");
let contractQuerySubmit = await contractQueryTx.execute(client);
let contractQueryResult = contractQuerySubmit.getUint256(0);
console.log(
`- Contract balance (from getBalance fcn): ${contractQueryResult} \n`
);
let cCheck = await new ContractInfoQuery()
.setContractId(contractId)
.execute(client);
console.log(
`- Contract balance (from ContractInfoQuery): ${cCheck.balance.toString()} \n`
);
// Query the contract balance calling the function in the contract
console.log("- Transfer 10 Hbar to Alice");
contractQueryTx = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(100000)
.setFunction(
"transferHbar",
new ContractFunctionParameters()
.addAddress(aliceId.toSolidityAddress())
.addUint256(1000000000)
);
contractQuerySubmit = await contractQueryTx.execute(client);
await sleep(5000);
cCheck = await new ContractInfoQuery()
.setContractId(contractId)
.execute(client);
console.log(
`- Contract balance (from ContractInfoQuery): ${cCheck.balance.toString()} \n`
);
// Query the contract balance calling the function in the contract
console.log("- Send 10 Hbar to Alice");
contractQueryTx = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(100000)
.setFunction(
"sendHbar",
new ContractFunctionParameters()
.addAddress(aliceId.toSolidityAddress())
.addUint256(1000000000)
);
contractQuerySubmit = await contractQueryTx.execute(client);
await sleep(5000);
cCheck = await new ContractInfoQuery()
.setContractId(contractId)
.execute(client);
console.log(
`- Contract balance (from ContractInfoQuery): ${cCheck.balance.toString()} \n`
);
// Query the contract balance calling the function in the contract
console.log("- Call 10 Hbar to Alice");
contractQueryTx = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(100000)
.setFunction(
"callHbar",
new ContractFunctionParameters()
.addAddress(aliceId.toSolidityAddress())
.addUint256(1000000000)
);
contractQuerySubmit = await contractQueryTx.execute(client);
await sleep(5000);
cCheck = await new ContractInfoQuery()
.setContractId(contractId)
.execute(client);
console.log(
`- Contract balance (from ContractInfoQuery): ${cCheck.balance.toString()} \n`
);
// Query the contract balance calling the function in the contract
contractQueryTx = new ContractCallQuery()
.setContractId(contractId)
.setGas(100000)
.setFunction("getBalance");
contractQuerySubmit = await contractQueryTx.execute(client);
contractQueryResult = contractQuerySubmit.getUint256(0);
console.log(
`- Contract balance (from getBalance fcn): ${contractQueryResult} \n`
);
}
main();