diff --git a/0_transferHbar2Contract_viaCryptoTransfer.js b/0_transferHbar2Contract_viaCryptoTransfer.js index af54a97..dee5783 100644 --- a/0_transferHbar2Contract_viaCryptoTransfer.js +++ b/0_transferHbar2Contract_viaCryptoTransfer.js @@ -6,14 +6,9 @@ const { Client, FileCreateTransaction, ContractCreateTransaction, - ContractFunctionParameters, - ContractExecuteTransaction, ContractCallQuery, - Hbar, TransferTransaction, ContractInfoQuery, - AccountBalanceQuery, - TransactionRecordQuery, } = require("@hashgraph/sdk"); const fs = require("fs"); @@ -22,8 +17,6 @@ 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); @@ -33,9 +26,21 @@ async function main() { "0_transferHbar2Contract_viaCryptoTrans_sol_hbar2Contract.bin" ); - // Create a file on Hedera and store the bytecode + const fileID = await createFile(contractBytecode); + const contractID = await initContract(fileID); + await transferToContract(contractID); + + const { contractInfoQueryBal, getBalanceBal } = await getBalances(contractID); + console.log(`- Contract balance (from getBalance fcn): ${getBalanceBal} \n`); + console.log(`- Contract balance (from ContractInfoQuery): ${contractInfoQueryBal} \n`); +} +main(); + + +// Create a file on Hedera and store the bytecode +async function createFile(bytecode) { const fileCreateTx = new FileCreateTransaction() - .setContents(contractBytecode) + .setContents(bytecode) .setKeys([operatorKey]) .freezeWith(client); const fileCreateSign = await fileCreateTx.sign(operatorKey); @@ -43,10 +48,13 @@ async function main() { const fileCreateRx = await fileCreateSubmit.getReceipt(client); const bytecodeFileId = fileCreateRx.fileId; console.log(`- The bytecode file ID is: ${bytecodeFileId} \n`); + return bytecodeFileId +} - // Instantiate the smart contract +// Instantiate the smart contract +async function initContract(fileID) { const contractInstantiateTx = new ContractCreateTransaction() - .setBytecodeFileId(bytecodeFileId) + .setBytecodeFileId(fileID) .setGas(100000); const contractInstantiateSubmit = await contractInstantiateTx.execute(client); const contractInstantiateRx = await contractInstantiateSubmit.getReceipt(client); @@ -54,9 +62,12 @@ async function main() { 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`); + return contractId; +} - // Transfer HBAR to smart contract using TransferTransaction() - const contractExecuteTx = new TransferTransaction() +// Transfer HBAR to smart contract using TransferTransaction() +async function transferToContract(contractId) { + const contractExecuteTx = new TransferTransaction() .addHbarTransfer(treasuryId, -10) .addHbarTransfer(contractId, 10) .freezeWith(client); @@ -64,17 +75,23 @@ async function main() { 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 +// Query the contract balance calling the function in the contract +async function getBalances(contractId) { 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 getBalanceBal = contractQuerySubmit.getUint256(0); + const cCheck = await new ContractInfoQuery().setContractId(contractId).execute(client); - console.log(`- Contract balance (from ContractInfoQuery): ${cCheck.balance.toString()} \n`); + const contractInfoQueryBal = cCheck.balance.toString(); + + return { + contractInfoQueryBal, + getBalanceBal + } } -main();