To create a new deployment script that deploys this smart contract to a specified network, create a new file scripts/deploy.js and paste in the code below:
consthre=require("hardhat");asyncfunctionmain() {constBuyMeACoffee=awaithre.ethers.getContractFactory("BuyMeACoffee");constbuyMeACoffe=awaitBuyMeACoffee.deploy();awaitbuyMeACoffe.deployed();console.log(`BuyMeACoffee Contract Address`,buyMeACoffe.address);}// We recommend this pattern to be able to use async/await everywhere// and properly handle errors.main().catch((error) => {console.error(error);process.exitCode =1;});
Now that we have our configurations all set, let’s deploy to Klaytn Testnet Baobab by running the command below:
npxhardhatrunscripts/deploy.js--networkbaobab
Once the contract deploys successfully, your terminal should look like this:
Congratulations on deploying your BMC smart contract on Klaytn Baobab Network! You can verify this transaction on Klaytnscope by pasting your address in the search field.
4.2 Interacting with BMC Smart Contract
In this section, you will learn how to use hardhat scripts to withdraw the coffee tips sent into the smart contract. To get started, create a new file withdraw.js in your scripts folder and paste the code below:
consthre=require("hardhat");// contract address of BMC ContractconstbuyMeACoffeAddress="Paste BMC contract address";// address of the contract deployer// useful when calling the withdrawCoffeTips() function// ensure that this address is the SAME address as the original contract deployerconstdeployerAddress="Paste deployer address";// get the balance of a specified addressasyncfunctiongetBalance(address) {constbalanceBigInt=awaithre.ethers.provider.getBalance(address);returnhre.ethers.utils.formatEther(balanceBigInt)}asyncfunctionmain() {// initialize the deployerAddress to a signer object// this will be useful when calling the withdrawCoffeTips() to the owner addressconstsigner=awaithre.ethers.getSigner(deployerAddress);// instantiate the BMC contractconstBuyMeACoffee=awaithre.ethers.getContractAt("BuyMeACoffee", buyMeACoffeAddress, signer);constbalanceBefore=awaitgetBalance(signer.address);constcontractBalance=awaitgetBalance(BuyMeACoffee.address);console.log(`Owner balance before withdrawing tips: ${balanceBefore} KLAY`);console.log(`Contract balance before withdrawing tips: ${contractBalance} KLAY`);// Withdraw funds if there are funds to withdraw.if (contractBalance !=="0.0") {console.log("withdrawing funds..")constwithdrawCoffeTxn=awaitBuyMeACoffee.withdrawCoffeTips();awaitwithdrawCoffeTxn.wait();// check owner's balance after withdrawing coffee tipsconstbalanceAfter=awaitgetBalance(signer.address);console.log(`Owner balance after withdrawing tips ${balanceAfter} KLAY`); } else {console.log("no funds to withdraw!"); }}// We recommend this pattern to be able to use async/await everywhere// and properly handle errors.main().catch((error) => {console.error(error);process.exitCode =1;});
As you can see from the code above, having instantiated the BMC contract, the scripts will execute the withdrawCoffeTips function only when the contract balance is greater than zero. Makes sense right?
Yes! In the event where the contract has no funds, it prints "No funds to withdraw" hence saving us some gas from contract invocation.
To see this in action, lets run the script below:
npxhardhatrunscripts/withdraw.js--networkbaobab
On successful execution of the scripts, your terminal should look like this: