1

I'm having trouble transferring remaining funds from a Contract to it's controlling Contract Factory. I'm trying to do the following.

  1. Create a new Service with Factory->createService()
  2. Add funds to the Service contract with Service-addFunds()
  3. Execute the Service through Factory->executeService()

And I get the following error when running the executeService function on the Factory contract.

transact to Factory.executeService errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value. Debug the transaction to get more information.

Factory contract

pragma solidity ^0.4.18;
import "http://github.com/OpenZeppelin/zeppelin-solidity/contracts/ownership/Ownable.sol";
import "./Service.sol";

contract Factory is Ownable {

    mapping(string => address) services;

    event DidExecuteService(address contractAddress);

    modifier ValidateString(string str) {
        require(bytes(str).length > 0);
        _;
    }

    function Factory() public {}

    function createService(string name) public onlyOwner ValidateString(name) {
        address assetAddress = new Game();
        services[name] = assetAddress;
    }

    function executeService(string name) public onlyOwner {
        address contractAddress = services[name];
        Service(contractAddress).execute();
        emit DidExecuteService(contractAddress);
    }

    function getContract(string name) public view returns (address) { return services[name]; }

    function countContracts() public view returns (uint) { return 0; }

    function getBalance() public view returns (uint) { return address(this).balance; }
}

Service Contract

Transfers to addresses from the recipients wallets pass but the contract fails to transfer remaining funds from the Service contract back to the Factory, the owner/msg.sender of the function.

pragma solidity ^0.4.18;
import "http://github.com/OpenZeppelin/zeppelin-solidity/contracts/ownership/Ownable.sol";
import "http://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol";

contract Service is Ownable {

    using SafeMath for uint256;

    event DidMakePayment(address player, uint256 value);
    event DidAddFunds(uint amount, uint balance);

    function Service() public {}

    function execute() payable public onlyOwner {
        uint amount = address(this).balance;
        // ERROR: Rollover to Factory
        owner.transfer(amount);
        emit DidMakePayment(owner, amount);
    }

    function addFunds() payable { emit DidAddFunds(msg.value, address(this).balance); }

    function getBalance() public view returns (uint256) { return address(this).balance; }
}
  • Where did the games[] array get defined? – Zain Rizvi Apr 7 '18 at 7:49
  • @ZainRizvi Sorry, I've now fixed a few unrelated errors and removed irrelevant code from the contracts – Mitch Pierias Apr 7 '18 at 12:30

Your Answer

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

Browse other questions tagged or ask your own question.