Remix

智能合約的建構子實例化另一個智能合約的問題

  • April 8, 2021

我在同一個“.sol”文件中有以下兩個智能合約:憑證和提案。每個 Proposals 智能合約都應引用單個 Credentials 智能合約,為此,當創建 Proposals(構造方法)時,它會實例化一個 Credentials 智能合約,如下所示。

憑據.sol

contract Credentials {

   uint256 _numberOfProviders;
   mapping(address => bool) public _owners;
   uint256 _numberOfOwners;

   constructor(address[] memory owners) payable{
       _owners[msg.sender] = true;
       _numberOfOwners = 1;
       for (uint i=0; i<owners.length; i++) {
           if(false == _owners[owners[i]]){
               _owners[owners[i]] = true;
               _numberOfOwners += 1;
           }
           
       }
       _numberOfProviders = 0;
   }
}

提案.sol

contract Proposals {

   Credentials  _credentialsContract;
   address payable _chairperson;
   
   constructor() payable{
       _chairperson = payable(msg.sender);
       address[] memory listOfOwners;
       listOfOwners[0] = msg.sender;
       _credentialsContract = new Credentials(listOfOwners);
   }
}

在 Remix 中,編譯器可以工作,但是當我嘗試部署提案時,出現以下我不理解的錯誤:

創建提案時出錯:VM 錯誤:還原。revert 事務已恢復到初始狀態。注意:如果您發送值並且您發送的值應該小於您目前的餘額,則呼叫的函式應該是應付的。調試事務以獲取更多資訊。

你可以試試這個:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;
contract Credentials {

   uint256 _numberOfProviders;
   mapping(address => bool) public _owners;
   uint256 _numberOfOwners;

   constructor(address[] memory owners) payable{
       _owners[msg.sender] = true;
       _numberOfOwners = 1;
       for (uint i=0; i<owners.length; i++) {
           if(false == _owners[owners[i]]){
               _owners[owners[i]] = true;
               _numberOfOwners += 1;
           }
           
       }
       _numberOfProviders = 0;
   }
}

contract Proposals{

   Credentials  _credentialsContract;
   address payable _chairperson;
   address[] listOfOwners;
   constructor() payable{
       _chairperson = payable(msg.sender);
       listOfOwners.push(address(msg.sender));
       _credentialsContract = new Credentials(listOfOwners);
   }
}

引用自:https://ethereum.stackexchange.com/questions/96839