![](/img/trans.png)
[英]Is it possible to call a contract in solidity, using its storage but passing along msg.sender?
[英]Is it possible to call methods of contract's initializer during its initialization?
我嘗試部署以下智能合約
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();
}
}
但它失敗並顯示以下消息:
交易已恢復為初始state。注意:被叫function如果您發送價值,則應支付,並且您發送的價值應小於您當前的余額。 調試事務以獲取更多信息。
似乎不可能在初始化期間調用合同初始化程序(創建當前合同實例的合同)的方法。
我的問題是,Contract1 的地址已經存在,但不可能從 Contract2 的構造函數中調用它的任何方法,這是怎么回事?
Contract2 的構造函數調用時,Contract1 的地址尚不可用。 Contract1的地址只有在Contract1的構造函數執行完畢后才可用。
這意味着在 Contract1 的構造函數執行完成之前,Contract2 的構造函數不能調用 Contract1 的任何方法。
在這種情況下,您可以部署 Contract1,等待 tx 完成,然后在構造函數 arguments 中使用 Contract1 的地址部署 Contract2。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.