Skip to content

Instantly share code, notes, and snippets.

@piorot
Last active March 31, 2022 06:06
Show Gist options
  • Save piorot/53f911d2267fbdb65d482a322e7c7861 to your computer and use it in GitHub Desktop.
Save piorot/53f911d2267fbdb65d482a322e7c7861 to your computer and use it in GitHub Desktop.
Apartment shareholder be able to withdraw resources proportional to his share
function withdraw() public {
require(this.balanceOf(msg.sender) > 0, "unauthorized");
uint meansToWithdraw = address(this).balance / 100 * this.balanceOf(msg.sender);
balance = balance - meansToWithdraw;
payable(msg.sender).transfer(meansToWithdraw);
}
receive() external payable {
balance += msg.value;
}
it("Apartment shareholder be able to withdraw resources proportional to his share", 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 aliceBalanceBeforeWithdrawal = await Alice.getBalance();
await apartment.connect(Alice).withdraw();
expect(await (await apartment.balance()).eq(ethers.utils.parseEther("0.8"))).to.be.true;
expect(await (await apartment.balance()).gt(ethers.utils.parseEther("0"))).to.be.true;
expect(await (await Alice.getBalance()).gt(aliceBalanceBeforeWithdrawal)).to.be.true;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment