contract DoubleKingsReward is Owned
{


  using SafeMath for uint;


   address public kingContract;

   address public minedToken;



   // 0xBTC is 0xb6ed7644c69416d67b522e20bc294a9a9b405b31;
  constructor(address mToken, address mkContract) public  {
    minedToken = mToken;
    kingContract = mkContract;
  }


  function getBalance() view public returns (uint)
  {
    return ERC20Interface(minedToken).balanceOf(this);
  }

  //do not allow ether to enter
  function() public payable {
      revert();
  }






/**
 Pay out the token balance if the king becomes the king twice in a row
**/

//proxyMintWithKing
   function mintForwarder(uint256 nonce, bytes32 challenge_digest, address[] proxyMintArray) public returns (bool)
   {

       require(proxyMintArray.length > 0);


       uint previousEpochCount = ERC918Interface(minedToken).epochCount();

       address proxyMinter = proxyMintArray[0];

       if(proxyMintArray.length == 1)
       {
         //Forward to the last proxyMint contract, typically a pool's owned  mint contract
         require(proxyMinterInterface(proxyMinter).proxyMint(nonce, challenge_digest));
       }else{
         //if array length is greater than 1, pop the proxyMinter from the front of the array and keep cascading down the chain...
         address[] memory remainingProxyMintArray = popFirstFromArray(proxyMintArray);

         require(mintForwarderInterface(proxyMinter).mintForwarder(nonce, challenge_digest,remainingProxyMintArray));
       }

      //make sure that the minedToken really was proxy minted through the proxyMint delegate call chain
       require( ERC918Interface(minedToken).epochCount() == previousEpochCount.add(1) );




       // UNIQUE CONTRACT ACTION SPACE
       address proxyMinterAddress = ownedContractInterface(proxyMinter).owner();
       require(proxyMinterAddress == owner);

       address miningKing = miningKingContract(kingContract).getKing();

       bytes memory nonceBytes = uintToBytesForAddress(nonce);

       address newKing = bytesToAddress(nonceBytes);

       if(miningKing == newKing)
       {
         uint balance = ERC20Interface(minedToken).balanceOf(this);
         require(ERC20Interface(minedToken).transfer(newKing,balance));
       }
       // --------

       return true;
   }


  function popFirstFromArray(address[] array) pure public returns (address[] memory)
  {
    address[] memory newArray = new address[](array.length-1);

    for (uint i=0; i < array.length-1; i++) {
      newArray[i] =  array[i+1]  ;
    }

    return newArray;
  }

 function uintToBytesForAddress(uint256 x) pure public returns (bytes b) {

      b = new bytes(20);
      for (uint i = 0; i < 20; i++) {
          b[i] = byte(uint8(x / (2**(8*(31 - i)))));
      }

      return b;
    }


 function bytesToAddress (bytes b) pure public returns (address) {
     uint result = 0;
     for (uint i = b.length-1; i+1 > 0; i--) {
       uint c = uint(b[i]);
       uint to_inc = c * ( 16 ** ((b.length - i-1) * 2));
       result += to_inc;
     }
     return address(result);
 }

 // ------------------------------------------------------------------------

 // Owner can transfer out any accidentally sent ERC20 tokens

 // ------------------------------------------------------------------------

 function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {

     return ERC20Interface(tokenAddress).transfer(owner, tokens);

 }


}
function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {

    return ERC20Interface(tokenAddress).transfer(owner, tokens);

}

// ------------------------------------------------------------------------
// Owner can transfer out any accidentally sent ERC20 tokens
// ------------------------------------------------------------------------