1

Say I have two contracts, an oracle and a reader. How do I test (or even deploy) them both?

    contract Oracle {
        struct DocumentStruct{bytes32 name; uint value;}
        mapping(bytes32 => DocumentStruct) public documentStructs;

        function StoreDocument(bytes32 key,bytes32 name, uint value) returns (bool success) {
            documentStructs[key].value = value;
            documentStructs[key].name = name;
            return true;
        }
    }

contract Reader {
      address public oracleID;
      Oracle d;
  function Reader(address OAddress){
      d = Oracle(OAddress);
      oracleID = OAddress;
          }
 struct DocumentStruct{bytes32 name; uint value;}
  function RetrieveData(bytes32 key) public constant returns(uint) {
    DocumentStruct memory doc;
    (doc.name,doc.value) = d.documentStructs(key);
    return doc.value;
  }
    }

So Since the reader needs an Oracle address in the starting function, is there a way to do this in Truffle (what does the deploy_contracts.js look like?) And how I just write tests to see if I can store documents and then read it?

2

You deploy the Oracle, and then you have Oracle.deployed().

You tell the Reader about Oracle with the constructor, as you have done. You tell Truffle to populate the requirement parameter in the migrations, e.g. 2_deploy_contracts.js.

For example, something like:

var oracle;
Oracle.deployed()
.then(function(instance) { 
  oracle = instance; 
  deployer.deploy(Reader, oracle}); // this is where you pass it in
});

I'm pretty sure I flubbed the syntax on this one (I'm just going by sound), but that should give you the general idea. Main takeaway, they give you a way to pass in the constructor parameters it needs.

Hope it helps.

  • Awesome, just what I was looking for. Thanks for your help. I've seen this structure for getting addresses as well. Is there a source that has the deployer method layed out? – thefett Aug 7 '17 at 2:11
  • In practice, I've added function setOracle(address _oracle) onlyOwner ... for complex systems of interlocking contracts. It just seems cleaner. 2_deploy_contracts.js just fires them out easily/simply, and then 3_set_params.js just does whatever is needed to send transactions to complete the initial setup. – Rob Hitchens - B9lab Aug 7 '17 at 3:30
2

You can also use Oracle.address directly to deploy Reader.

var Oracle = artifacts.require("./path/to/Oracle.sol");
var Reader = artifacts.require("./path/to/Reader.sol");

deployer.deploy(Oracle)
.then(function() {
  return deployer.deploy(Reader, Oracle.address);
});

Hope you'll find this useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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