Skip to content

Instantly share code, notes, and snippets.

@piorot
Created March 29, 2022 10:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save piorot/722b4794c3669b26a0f4923f40c6daa1 to your computer and use it in GitHub Desktop.
Owner should be able to withdraw resources paid as rent
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Apartment is ERC20 {
uint public balance;
constructor() ERC20("ApartmentContract", "APRTM") {
super._mint(_msgSender(), 100);
console.log("Deploying a Greeter with greeting:");
}
function withdraw() public {
payable(msg.sender).transfer(address(this).balance);
}
receive() external payable {
console.log("receive");
balance += msg.value;
}
}
it("Owner should be able to withdraw resources paid as rent", async () => {
const Apartment = await ethers.getContractFactory("Apartment");
const apartment = await Apartment.deploy();
[owner, Alice, Bob] = await ethers.getSigners();
await apartment.deployed();
await apartment.transfer(Alice.address, 20);
await Bob.sendTransaction({
to: apartment.address,
value: ethers.utils.parseEther("1")
});
const ownerBalanceBeforeWithdrawal = await owner.getBalance();
await apartment.withdraw();
expect(await (await owner.getBalance()).gt(ownerBalanceBeforeWithdrawal)).to.be.true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment