Solidity – Using Contract From Another Contract

contract-developmentcontract-invocationinheritancesoliditytruffle

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

Best Answer

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.

Related Topic