Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call functions of a contract deployed at a specific address from solidity

I'm dealing with inheritance and external calls of contract from within solidity. I have deployed my data structure and filled it at an address MapAdr

My code can me schemed as follow. In my DataStructure.sol I have:

interface Graph {
function getNeighbours(uint8 id) external view returns (uint8[8]);
function getOrder() external view returns (uint8);
function isNeighbour(uint8 strFrom, uint8 strTo) external view returns 
(bool success);

}

contract DataStructure is Graph {
....code....
uint8 order;
constructor (uint8 size) {
order = size;
}
....code...
}

I deploy this contract and I save the address to MapAdr=0x1234567...

Now I go to my other contract

pragma solidity ^0.4.22;

import "./DataStructure.sol";


contract Data is Graph {
.....code....
DataStructure public data;

    constructor(address MapAdr) public {
    ....code...
    data = DataStructure(MapAdr);
    ....code...
    }
.....code....
}

But then DataStructure is deployed but it's address is not MapAdr.

There is a way to have an instance of the deployed contract at that specific MadAdr (so with that exactly data inserted in that datastructure) so I can query it's storage ?

The idea is to deploy several DataStructure contracts with different data inserted and then referiing to one specific when deploying Data contract.

like image 769
Xaler Avatar asked Oct 16 '25 02:10

Xaler


1 Answers

I'm not sure if this answers exactly your question, but I found this example very similar to what you are talking about, and I hope it can help you.

contract Admin {
  address private owner;

  function Admin() public { owner = msg.sender; }

  function getOwner() public returns (address) {
    return owner;
  }

  function transfer(address to) public {
    require(msg.sender == owner);
    owner = to;
  }
}

contract Lottery {
  string public result;

  Admin public admin = Admin(0x35d803f11e900fb6300946b525f0d08d1ffd4bed);  // Admin contract was deployed under this address

  function setResult(string _result) public {
    require(msg.sender == admin.getOwner());
    result = _result;
  }
}

As you can see, the Admin contract is deployed under the address 0x35d... and then it's used in the Lottery contract in the admin variable definition. Once you declare a variable as an instance of another contract, you can then use all the public interface of that contract. Check the admin.getOwner();execution.

Again, it's not following the same example as you mentioned, but it might be useful.

Hope it helps! ;-)

EDIT 1: Hardcoding the address of Admin instance in Lottery is probably a bad idea. This is just a very simple example. You might consider passing the Admin instance address as a parameter in the Lottery constructor instead. See comments below for more details.

like image 84
Martin Zugnoni Avatar answered Oct 18 '25 21:10

Martin Zugnoni