Solidity

使用來自另一個合約的合約

  • July 10, 2017

Storage我已經部署了一份使用松露控制台程式碼命名的契約Storage.sol

pragma solidity ^0.4.8;
contract Storage {

 struct EntityStruct {
   string str_address;
   uint entityData;
   //more fields
 }

 EntityStruct[] public entityStructs;

 function newEntity(string entityAddress, uint entityData) public returns(uint rowNumber) {
   EntityStruct memory newEntity;
   newEntity.str_address = entityAddress;
   newEntity.entityData    = entityData;
   return entityStructs.push(newEntity)-1;
 }

 function getEntityCount() public constant returns(uint entityCount) {
   return entityStructs.length;
 }

 function getEntityByRowNumber(uint rowNumber) public constant returns(string entity, uint data) {
   string a = entityStructs[rowNumber].str_address;
   uint b =  entityStructs[rowNumber].entityData;
   return (a,b);
 }
}

現在我想從另一個名為Access1

程式碼Access1.sol

pragma solidity ^0.4.8;
import 'storage.sol';
contract Access1{
address storgeContractAddress = "0xcd53170a761f024a0441eb15e2f995ae94634c06";

function createEntity(address entityAddress,uint entityData){
       //Storage s = Storage(storgeContractAddress);
       storgeContractAddress.newEntity.call(entityAddress,entityData);
   }

   function getEntityCount()public constant returns(uint entityCount){
       //Storage s = Storage(storgeContractAddress);
       uint count=storgeContractAddress.getEntityCount.call();
       return count;
   }
}

我面臨的問題是Access1.sol從松露編譯時使用

松露編譯

我收到錯誤消息

Error: Source "storage.sol" not found: File not supplied initially.
import 'storage.sol';
^-------------------^

我不明白為什麼會發生這種情況,我已經檢查了契約儲存是否已成功部署並正常工作,我已經檢查了許多線上範例,它們以相同的方式導入契約。

任何幫助都非常感謝,在此先感謝

Storage.sol在松露的預設契約文件夾中嗎?如果是這樣,您需要編寫import "./Storage.sol". 還要確保它的大小寫正確。

您需要連接已部署的Storage合約功能。

pragma solidity ^0.4.8;

contract Storage {
   function getEntityCount() public constant returns(uint entityCount);
}

contract Access1{
   address storgeContractAddress = "0xcd53170a761f024a0441eb15e2f995ae94634c06";
   Storage storage;    
   function Access1(){
    storage =  Storage(storgeContractAddress);
   }
   function getEntityCount()public constant returns(uint entityCount){
       uint count=storage.getEntityCount.call();
       return count;
   }
}

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