const hre = require("hardhat");
const { ethers } = require('hardhat');
// Returns the ether balance of a given address.
async function getBalance(address) {
const balanceBigInt = await hre.ethers.provider.getBalance(address);
return ethers.formatEther(balanceBigInt);
}
// Logs the Ether balances for a list of addresses.
async function printBalances(addresses) {
let index = 0;
for (const address of addresses) {
console.log(`Addresses ${index} balance: `, await getBalance(address));
index++;
}
}
// Logs the memos stored on-chain from coffee purchases.
async function printMemos(memos) {
for (const memo of memos) {
const timestamp = memo.timestamp;
const tipper = memo.name;
const tipperAddress = memo.from;
const message = memo.message;
console.log(`At ${timestamp}, ${tipper} (${tipperAddress}) said: "${message}"`);
}
}
async function main() {
const [owner, tipper, tipper2, tipper3] = await hre.ethers.getSigners();
// const BuyMeACoffee = await hre.ethers.getContractFactory("BuyMeACoffee");
const buyMeACoffee = await hre.ethers.deployContract("BuyMeACoffee");
// Deploy the contract.
await buyMeACoffee.waitForDeployment();
console.log("BuyMeACoffee deployed to:", buyMeACoffee.target);
console.log("5");
// Check balances before the coffee purchase.
const addresses = [owner.address, tipper.address, buyMeACoffee.target];
console.log("== start ==");
await printBalances(addresses);
// Buy the owner a few coffees.
const tip = { value: hre.ethers.parseEther("1") };
await buyMeACoffee.connect(tipper).buyCoffee("Rahul", "You're a good coder", tip);
await buyMeACoffee.connect(tipper2).buyCoffee("Suprit", "You're a good coder", tip);
await buyMeACoffee.connect(tipper3).buyCoffee("Prasad", "You're a good coder", tip);
// Check balances after the coffee purchase.
console.log("== bought coffee ==");
await printBalances(addresses);
// Withdraw.
await buyMeACoffee.connect(owner).withdrawTips();
// Check balances after withdrawal.
console.log("== withdrawTips ==");
await printBalances(addresses);
// Check out the memos.
console.log("== memos ==");
const memos = await buyMeACoffee.getMemos();
printMemos(memos);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
1 Answer
buyMeACoffee.target is the address where that contract is deployed, while owner.address is the address of that account. The distinction is because the target of a contract can be changed, while the address of an account should not.