2

I have deployed one contract named Storage using truffle console code for 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);
  }
}

Now I want to access the functions of this contract from another contract named Access1

code for 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;
    }
}

The problem I am facing is while compiling Access1.sol from truffle using

truffle compile

I get the error message

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

I cant understand why this happens, I have checked that contract Storage is successfully deployed and working, I have checked many examples online which are importing contract in same way.

Any help is highly appreciated, Thanks in advance

Basit Raza
  • 333
  • 1
  • 10
SwapnilKumbhar
  • 763
  • 1
  • 11
  • 31

2 Answers2

3

You need to interface deployed Storage contract functions.

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;
    }
}
Basit Raza
  • 333
  • 1
  • 10
  • Isn't it, possible to import one contract to another?? like I have done in the above code – SwapnilKumbhar Jul 10 '17 at 09:18
  • I don't think you can import deployed contract like this. – Basit Raza Jul 10 '17 at 10:37
  • Yes, it is possible to import contracts like OP requires by using import "Storage.sol" (however, as pointed out by @Michael O'Rourke, case-sensitive import is required as also path needs to be correct.

    Note that when using the imported contract, one needs to refer to an instance of the contract. Refer https://ethereum.stackexchange.com/a/20751/20155 to better understand this..

    – Bharat Mallapur Jul 03 '18 at 09:59
2

Is Storage.sol in the default contracts folder for truffle? If so, you need to write import "./Storage.sol". Also make sure it's capitalized correctly.

Michael O'Rourke
  • 324
  • 2
  • 13