0

I want to check the balance of the contract I'm testing in Foundry with address(contractAddress).balance.

However, from the docs I only know the address of the test contract is 0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84.

How can I get the balance?

2 Answers 2

1

If you'd like to do this programmatically, you could wrap the contract creation line.

address(new Contract()).balance

address(new Contract()) will return the contract's address. You could then save it to a variable, check its balance do all the other things with it.

0

When the main contract is deployed in the test contract, this will show in the logs. If you run forge test -vvvvv with 5 v's the address of your main contract will be displayed after the yellow text → new.

Traces:
  [#####] ContractTest::setUp() 
    ├─ [#####] → new Contract@[Contract address]

This would come from a test contract as this one:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "src/Contract.sol";

contract ContractTest is Test {
    Contract contract;

    function setUp() public {
        contract = new Contract(); // This line emits creates a log of the deployed address
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.