0

Does a contract's constructor re-run every time it is called from an EOA or does the initialized state persist in the contract storage once it is created by the first msg.sender ?

If the first contract (token) calls and initializes another contract (faucet) in the constructor, is faucet re-initialized every time an EOA calls token ?

For example from the ethereum book:

import "Faucet.sol"

contract Token is mortal {
    Faucet _faucet;

    constructor(address _f) {
        _faucet = Faucet(_f);
        _faucet.withdraw(0.1 ether) 

// If faucet has a total limit defined as a state variable (=100), 
// will 100 be depleted every time an EOA calls token ? 
// or, will it reinitialize as 100 and therefore only deplete to 99.9 ?

    }
}

1 Answer 1

0

The constructor is used to initialize state variables of a contract, and is executed once during deployment of the Smart Contract. It can't be called again afterwards, but the initialized state will persist.

In your example, a parent contract creates (one or more) child contracts.

The constructor of the parent contract itself has run once, and the constructor of every new child contract created will run once when it is deployed.

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.