Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split out functions, removed unused vars #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions 0_transferHbar2Contract_viaCryptoTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ const {
Client,
FileCreateTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
Hbar,
TransferTransaction,
ContractInfoQuery,
AccountBalanceQuery,
TransactionRecordQuery,
} = require("@hashgraph/sdk");
const fs = require("fs");

Expand All @@ -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);

Expand All @@ -33,48 +26,72 @@ 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);
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`);
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);
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`);
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);
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
// 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();