0

i am trying to connect two different Smart Contracts. On one hand I have CRUD.sol, which implements CRUD operations in a struct "Data". I have programmed a method readAllData() that returns all data stored in the struct, here is the code:

contract CRUD {
    
    struct Data {
        uint id;
        bytes32 contentHash;
        string description;
    }
     
    Data[] data;

    function readAllData() external view returns (Data[] memory){
        return data;
    }
}

I want to call this function from another Smart Contract. I am doing this by using an Interface:

interface ICRUD{
    function readAllData() external view returns (Data[] memory);
}

contract verifyData{

    function read() external{
        ICRUD.readAllData();
    }
}

However, remix returns this error:

DeclarationError: Identifier not found or not unique. --> Interface.sol:5:51: | 5 | function readAllData() external view returns (Data[] memory); | ^^^^

Do you guys know what I am doing incorrectly?

Thanks in advice

2 Answers 2

0

An interface simply says what functionality the other contract has (or, which subset of the functionality we are interested in). It doesn't say where the functionality is. That's what you are missing.

In order to use an interface, the other contracts has to be deployed already, and you have to give the address when you use the interface. So your code should be something like: ICRUD(0xabc).readAllData(); where 0xabc is the address of the deployed contract.

3
  • I´ve tried that. However, now I am getting errors with the returns of functions: TypeError: Return argument type struct ICRUD.Data memory[] memory is not implicitly convertible to expected type (type of first return variable) struct verifyData.Data memory[] memory. --> verifyData.sol:20:16: | 20 | return ICRUD(0xd9145CCE52D386f254917e481eB44e9943F39138).readAllData(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    – WakiApi
    Commented Aug 3, 2022 at 9:24
  • I´ve defined Data struct in both interface and contract. However it is not working. I would like to obtain the values of the Data struct of CRUD .sol contract from this new contract.
    – WakiApi
    Commented Aug 3, 2022 at 9:25
  • Please post a new question if you have new questions, or browse existing answers Commented Aug 3, 2022 at 10:48
0

FILE CRUD.sol main contract file

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "./ICRUD.sol";
contract CRUD is ICRUD {

    struct Data {
        uint id;
        bytes32 contentHash;
        string description;
    }
     
    Data[] data;

    function AddData(uint _id, bytes32 _contentHash, string memory _description) public {
    Data memory new_data = Data(_id, _contentHash, _description);
    data.push(new_data);
}

    function readAllData(uint index) external override view returns (uint, bytes32, string memory) {
        Data memory d = data[index];

        return (d.id, d.contentHash, d.description);
    }

} 

FILE ICRUD.sol interface of CRUD.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

interface ICRUD {
    function readAllData(uint index) external view returns (uint, bytes32, string memory);

}

FILE verifyData.sol In this contract we are calling readAllData() of CRUD.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "./ICRUD.sol";

contract verifyData {

    ICRUD public crud;

    constructor(address _crudAddress) {
        crud = ICRUD(_crudAddress);
    }

    function read(uint index) external view returns (uint, bytes32, string memory) {
        return (crud.readAllData(index));
    }
}

Just Deployed CRUD.sol and added one entry of data

enter image description here

Deployed verifyData.sol with constructor paramater as address of CRUD.sol

Then Reading data from verifyData.sol enter image description here

If you want to read whole data structure and not just data entry at some index. Apply for loop for that.

You can refer to this article - https://medium.com/coinmonks/solidity-tutorial-returning-structs-from-public-functions-e78e48efb378

4
  • Hi thanks for your answe. However, I need to do it with interfaces. Moreover, I think it is now possible to return structs in solidity.
    – WakiApi
    Commented Aug 3, 2022 at 9:36
  • This is using interface. Here, ICRUD.sol file is the interface Commented Aug 5, 2022 at 17:09
  • Hi, thanks. Do I have to create migration for ICRUD in truffle? Thanks
    – WakiApi
    Commented Aug 8, 2022 at 8:01
  • Not sure. You just have to include this file in the project. This is just normal interface, just treat like any other interface you use Commented Aug 9, 2022 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.