Solidity

如何使用 Solidity 呼叫導入的合約?

  • January 20, 2017

在我的合約中,我想使用import關鍵字來載入其他合約和庫,以使我的程式碼更乾淨。例如,我將DateTime.sol 契約攜帶到另一個文件中,並將其導入 myContract 文件中,沒有任何問題。

myContract.sol:

import "/Users/avatar/populus/contracts/DateTime.sol"; //works.
contract myContract{
  DateTime date = new DateTime(); //does not work.
  <Some_Code>
}

但是從 myContract 契約中,當我嘗試將 DateTime 契約稱為:DateTime date = new DateTime()我遇到了以下錯誤:

/usr/local/lib/python2.7/site-packages/populus/chain.py:580: in get_contract
   if contract_name not in self.contract_factories:
/usr/local/lib/python2.7/site-packages/populus/utils/functional.py:50: in __get__
   res = instance.__dict__[self.name] = self.func(instance)
/usr/local/lib/python2.7/site-packages/populus/chain.py:198: in contract_factories
   compiled_contracts = self.project.compiled_contracts
/usr/local/lib/python2.7/site-packages/populus/project.py:153: in compiled_contracts
   optimize=True,
/usr/local/lib/python2.7/site-packages/populus/compilation.py:42: in compile_project_contracts
   compiled_sources = compile_files(contract_source_paths, **compiler_kwargs)
/usr/local/lib/python2.7/site-packages/solc/main.py:129: in compile_files
   **kwargs
/usr/local/lib/python2.7/site-packages/solc/utils/string.py:91: in inner
   return force_obj_to_text(fn(*args, **kwargs))

E Dynamic exception type: boost::exception_detail::clone_impl<dev::solidity::InternalCompilerError>
E std::exception::what: std::exception
E [dev::tag_comment*] = Compiled contract not found.

但是當DateTime contract嵌入myContract.sol文件中時,我可以毫無問題地DateTime date = new DateTime();從契約中呼叫。myContract我使用了以下方法:如何從現有契約方法中呼叫契約

myContract.sol:

contract myContract{
  DateTime date = new DateTime(); //works now.
  <Some_Code>
}
contract DateTime{
  <Some_Code>
}

$$ Q $$我已經在 Solidity 瀏覽器上嘗試過這種方法並且它有效。如何在 Populus 上解決此問題? 一些資訊:

  • 平台 darwin——Python 2.7.12、pytest-3.0.2、py-1.4.31、

pluggy-0.3.1

  • 外掛:popup-1.1.0
  • 作業系統:Mac OS X

solc –version:solidity編譯器命令行界面版本:0.4.8+commit.60cc1668.Darwin.appleclang

感謝您的時間和幫助。

您遇到的錯誤與 Populus 無關:

  Dynamic exception type: boost::exception_detail::clone_impl<dev::solidity::InternalCompilerError>
  E std::exception::what: std::exception

它說由於錯誤,Solidity 無法編譯你的合約。請通過從命令行手動編譯合約,然後使用可重複的步驟針對 Solidity 發布錯誤報告來重複該問題。

此外,在執行此操作之前,請將您的 Solidity 更新到最新版本。

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