0

When I am testing my class with truffle test ./test/testinheritance.js I am getting an error. "Error: VM Exception while processing transaction: revert".

It seems to be error related to the condition that a function setHeir must be called by owner.
I am not sure how to set/get/check owner in test script so that I can test the contract.
Please help me to test this contract via script. The test script is

The test script is

var DormantAssetFactory = artifacts.require("./DormantAssetFactory.sol");
var DormantAsset = artifacts.require("./DormantAsset.sol");
var dormantAssetAddress = null;

contract('DormantAssetFactory', function (accounts) {
  it("Let's call Heir function contract", async function () {
    var dormant_asset_factory = await DormantAssetFactory.deployed();
      (await dormant_asset_factory.newDormantAsset());
    //now get the Address of first contrac /This is not correct way but to show error
      dormantAssetAddress =
      (await dormant_asset_factory.getDormantAssetAt(0));

      var contractInstance = DormantAsset.at(dormantAssetAddress); //get contract at address
      //Call function of parent of current contract
//      console.log(contractInstance);
      var result =     
      (await contractInstance.setHeir(accounts[1]));
      //Just temp conditions
    assert.equal(1, 1, "Yes we called setHeir");
  });

});

Factory class is DormantAsstFactory

pragma solidity ^0.4.18;
import './DormantAsset.sol';
contract DormantAssetFactory {
  // index of created DormantAsset contracts
  address[] public dormantAssets;
  uint256 public _heartbeatTimeout =1000000;
  function getContractCount()     public    constant    returns(uint contractCount)
  {
    return dormantAssets.length;
  }
  // deploy a new contract
  function newDormantAsset()    public     returns(address _newDormantAssetContract)
  {
    DormantAsset c = new DormantAsset(_heartbeatTimeout);
    dormantAssets.push(c);
    return c;
  }

}

This contract created inside factory and it is inherited from this contract https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/ownership/Heritable.sol

Contract Class is

pragma solidity ^0.4.18;
import 'zeppelin-solidity/contracts/ownership/Heritable.sol';
contract DormantAsset  is Heritable {
  event Sent(address indexed payee, uint256 amount, uint256 balance);
  event Received(address indexed payer, uint256 amount, uint256 balance);
  function DormantAsset(uint256 _heartbeatTimeout) Heritable(_heartbeatTimeout) public {}
  function () public payable {
    Received(msg.sender, msg.value, this.balance);
  }
  function sendTo(address payee, uint256 amount) public onlyOwner {
    require(payee != 0 && payee != address(this));
    require(amount > 0);
    payee.transfer(amount);
    Sent(payee, amount, this.balance);
  }

}

0

The owner of contract DormantAsset is dormant_asset_factory and you are trying to execute setHeir() function from accounts[0]. Function setHeir() has modifier onlyOwner and we already know that the owner is factory contract. You should implement function callSetHeir() in your factory contract and then call it from your test.

Hope it helps.


Bonus: more readable way to write function with returned value

function newDormantAsset() public returns(DormantAsset c) {
     c = new DormantAsset(_heartbeatTimeout);
     dormantAssets.push(c);
}
  • Thank you so much, May you suggest some way so that I can make the account[0] as owner instead of dormant_asset_factory? Special thanks for the Bonus :-). – Pratik Feb 3 '18 at 4:17
  • Implement function callTransferOwnership and pass accounts[0] address as parameter. When you call this function from your factory contract it will reassign the owner. – Roman Frolov Feb 3 '18 at 10:49
  • transferOwnership is located in Ownable.sol – Roman Frolov Feb 3 '18 at 10:51

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.