Solidity

如何部署多個依賴於乙太坊錢包的合約?

  • February 22, 2017

在此處輸入圖像描述我有兩個契約“學院”和“部門”。它們是依賴契約。如何在乙太坊錢包中部署它們?

範常式式碼:

contract Department {
   function enroll(uint depID, address student) returns (bool ret) {
          return true;
     }
}

contract College {
   address student;
   Department dept_instance;

   function College ( address _student , address _department  ) {
       student = _student;
       dept_instance = _department;
    }

   function chooseDept ( uint id ) payable returns (bool value) {
       bool ret = student.send(msg.value);
       if (!ret)
           return dept_instance.enroll(id, msg.sender);
       else
           throw;
   }
}

在此處輸入圖像描述

我認為“錢包”是指 Mist。

當一個源文件有兩個合約時,如上所述,Mist 會讓你選擇要部署的合約。在這種情況下,首先部署部門。

找到部署的部門契約並將其地址複製到剪貼板。這並不明顯,但只要堅持不懈,您就可以在螢幕上獲取完整地址並複制它。

然後,返回“契約”以部署學院。

由於 College 的建構子需要address _department,因此 Mist 將提供一個欄位,您可以在其中指定數據。粘貼,因此部門的地址被傳遞到建構子中。

在第 13 行,使用 _department 創建一個 Department 實例。像這樣:dept_instance = Department(_department);

contract Department {

   function enroll(uint depID, address student) returns (bool ret) {
          return true;
   }
}

contract College {
   address student;
   Department dept_instance;

   function College ( address _student , address _department  ) {
       student = _student;
       dept_instance = Department(_department);
    }

   function chooseDept ( uint id ) payable returns (bool value) {
       bool ret = student.send(msg.value);
       if (!ret)
           return dept_instance.enroll(id, msg.sender);
       else
           throw;
   }
}

希望能幫助到你。

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