Skip to content

Commit

Permalink
Check ETH balance before sending external call
Browse files Browse the repository at this point in the history
  • Loading branch information
MickdeGraaf committed Dec 7, 2020
1 parent 5034462 commit fbe2173
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions contracts/facets/Call/CallFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ contract CallFacet is ReentryProtection, ICallFacet {
bytes memory _calldata,
uint256 _value
) internal {
require(address(this).balance >= _value, "ETH_BALANCE_TOO_LOW");
(bool success, ) = _target.call{ value: _value }(_calldata);
require(success, "CALL_FAILED");
emit Call(_target, _calldata, _value);
Expand Down
13 changes: 13 additions & 0 deletions test/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ describe("CallFacet", function() {
const difference = userBalanceAfter.sub(userBalanceBefore);
expect(difference).to.eq(parseEther("9"));
});
it("Sending ether while not having enough balance should throw an error", async() => {
let ether = await ethers.provider.getBalance(experiPie.address);
expect(ether).to.eq("0");

await signers[0].sendTransaction({to: experiPie.address, value: parseEther("10")});

ether = await ethers.provider.getBalance(experiPie.address);
expect(ether).to.eq(parseEther("10"));

const user = await signers[4].getAddress();

await expect(experiPie.call([user], ["0x00"], [parseEther("10.1")])).to.be.revertedWith("ETH_BALANCE_TOO_LOW");
});
it("Send contract erc20 token", async () => {
let balance = await testTokens[0].balanceOf(experiPie.address);
expect(balance).to.eq(0);
Expand Down

0 comments on commit fbe2173

Please sign in to comment.