Hi, can anyone explain the following:

I’m trying to deploy those smart contracts


pragma solidity 0.8.17;

contract Contract1 {
constructor() {
new Contract2(address(this));
}

function foo() public pure returns (string memory) {
return "bar";
}
}

contract Contract2 {
Contract1 contract1;

constructor(address _contract1) {
contract1 = Contract1(_contract1);
contract1.foo();
}
}


but it fails with following message:

The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.


Seems like it's impossible to call methods of contract's initializer (the contract that created an instance of current one) during its initialization.

My question is how it happens that address of the Contract1 is already exists, but it's impossible to call any of its methods from the constructor of Contract2?

Jan 30, 2023, 6:46 PM
If I add following function to Contract2 instead of calling from constructor:

function bar() public view returns(string memory) {
return contract1.foo();
}

everything works fine, so seems like address of the contract exists, but there’s nothing on that till the constructor of Contract1 finishes its execution
Jan 30, 2023, 6:49 PM
My eyes
Jan 30, 2023, 6:52 PM
Haha, sorry, can share a code on gist 🙂
Jan 30, 2023, 6:53 PM
😂😂
Jan 30, 2023, 6:54 PM
First of all, what are you doin'?
Jan 30, 2023, 6:55 PM
Initializing a contract from anther contract and passing an address as an argument to make it possible to call each others functions
Initially I faced the problem when tried to create and deploy a DAO that deplyos ERC20 smart contract from OpenZeppelin with some limitations that computed by the functions at DAO’s smart contract called for _beforeTokenTransfer. The problem here is that _mint function called from the constructor uses _beforeTokenTransfer too, so it fails
Jan 30, 2023, 7:02 PM

© 2024 Draquery.com All rights reserved.