4 Comments
Nov 21, 2023Liked by Antonio Viggiano, Bloqarl

Thanks for the amazing article!!

Have 1 query - why does 'SafeTest' contract have non-zero balance at the very start of testWithdraw()??

Shouldn't it be 0?

Its not funded with any ether(no vm.deal() seen).

------------------------------------------

contract SafeTest is Test {

Safe safe;

receive() external payable {}

function setUp() public {

safe = new Safe();

}

function test_Withdraw() public {

//X balance of this contract -->>>Should be zero at this point????

uint256 preTransfer = address(this).balance;

console.log("Pre Transfer Balance ", preTransfer);

payable(address(safe)).transfer(1 ether);

------------------------------------------

Running 1 test for test/SafeTest.t.sol:SafeTest

[PASS] test_Withdraw() (gas: 26054)

Logs:

//why non-zero value for SafeTest contract

Pre Transfer Balance 79228162514264337593543950335

//after transfer balance

Pre withdraw Balance 79228162513264337593543950335

//after withdraw balance

Post withdraw Balance 79228162514264337593543950335

----------------------------------------

Expand full comment
author

Hey man, very good question.

The non-zero balance is due to Foundry's testing environment setup. When you run tests using Foundry, each test contract is assigned with a large amount of Ether by default. It ensures that the test contracts have sufficient funds for various operations without the need for explicit funding in each test.

If you have a specific test scenario, where you need to manipulate the balance and want to have a separate setup you can manipulate the starting balance by using Foundry's cheat codes, like vm.deal(address(this), amount), to set a specific balance.

Oh, and that balance that you can see in your logs "79228162514264337593543950335" is actually 2^96 - 1 Wei, which is a deliberately chosen high value.

I hope it makes sense.

Expand full comment

Got it..thanks!

Expand full comment

Great article,

I have a couple of questions:

1. In the first test, when it throws an error for '79228162514.26', is the reason that the default amount with the foundry setup is 2^96 - 1, as you mentioned below? If I want to handle more, can I set vm.deal(address(this), type(uint256).max)?"

2. For the invariant test, I removed the last function; nevertheless, I encountered this error when I tried to run it:

```

[FAIL. Reason: revert: failed to send]

[Sequence]

sender=0xA9032291e9B1869F7E380120AE558117Eb0dD843 addr=[src/Safe.sol:Safe]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=deposit() args=[]

sender=0x4e59b44847b379578588920cA78FbF26c0B4956C addr=[src/Safe.sol:Safe]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=withdraw() args=[]

invariant_withdrawDepositedBalance() (runs: 68, calls: 1007, reverts: 1)

Traces:

[2512] Safe::deposit()

└─ ← ()

[9079256848778899423] Safe::withdraw()

├─ [9079256848778896679] Create2Deployer::create2()

│ └─ ← ()

└─ ← revert: failed to send

```

Can you help me understand why this happens?

3. When I run the invariant test in the output I get (runs: 68, calls: 1007, reverts: 1) , what mean the calls?

Thank you.

Expand full comment