0

I am not expert in Solidity. I am trying to call a function to another contract so that it updates values but it does not. I am using online Remix Browser. The contract to be called is Supreme. I can see that getter and setter values are working as expected. The caller contract is Hack. If Sneak function from Contract Hack sends 1 ether more than the value of cost then the sender becomes big . For example, if Sneak calls function Supreme with Cost = 2 ether then Cost becomes 2 sender replaces takes the position of 'big'. When I pass address from first contract to Caller contract and give 2 ethers in value. Click on setter, then I check cost, it remains 1 ether. I tried so many ways but I am not able to move forward.

contract Supreme {
    address public big;
    uint256 public cost;

    function Supreme() 
      public
    {
        big = msg.sender;
        cost = 3 ether;
    }

    function Road() 
      external
      payable
    {
        require(msg.value >= cost);
        big.transfer(msg.value);
        big = msg.sender;

        cost += 1 ether;
    }
}

This is caller contract

    import "browser/Supreme.sol";
contract Hack{
Supreme public sup;

Supreme s;
function Hack(Supreme _sup){
    sup = _sup;
}
function sneak(address addr) public payable{
 s.Road.value(msg.value)();
  }
   function() payable{
   s.Road(); 
   }  
}
0

Two issues:

  1. It doesn't look like you're ever creating an instance of Supreme, so I believe you're trying to send these function calls to address 0.
  2. You're not sending any ether when you call s.Road().

For the first issue, you can either have your contract deploy Supreme:

sup = new Supreme();

or (more likely what you want) you can deploy Supreme yourself and then pass the address of the deployed contract to Hack in the constructor:

function Hack(Supreme _sup) {
    sup = _sup;
}

To fix the second issue, add a value to your call:

s.Road.value(msg.value)();

EDIT

Your new code has different issues. It keeps track of sup but then doesn't use it. The sneak function also takes an address which is unused.

Here's fixed code. Be sure to pass the address of the deployed Supreme contract when you create Hack. That means you need to enter the address of that contract (like "0x123abc...") in the text field next to the "Create" button. Then send ether by changing the value field in the upper right when you call the sneak function.

import "browser/Supreme.sol";
contract Hack {
    Supreme public sup;

    function Hack(Supreme _sup) public {
        sup = _sup;
    }

    function sneak() public payable {
        sup.Road.value(msg.value)();
    }
}
  • Thanks @smarx. I changed Hack contract as you advised. However, I am still not able to see the expected results. I noticed that on the left hand side of Remix Browser, there is an error which appears when I click setter 'Sneak'. I am passing address as well as 3 ethers. Here is the error: transact to Hack.sneak errored: Error encoding arguments: SyntaxError: JSON.parse: expected ',' or ']' after array element at line 1 column 3 of the JSON data – mike Feb 8 '18 at 3:29
  • If I provide only ethers(3) and not passing address as Input to Hack with the supposition that Hack would probably pick up the address automatically, then I get the following error: transact to Hack.sneak 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 – mike Feb 8 '18 at 3:48
  • See my edit. I'm confused about what you tried, but hopefully my updated code helps you. – smarx Feb 8 '18 at 4:10
  • Thanks @smarx. I used the same code as you have provided above. When I click on create button after providing address copied from Supreme contract. It does not show me sneak or any setter/getter. Instead, I am getting the following error: creation of Hack errored: Error encoding arguments: SyntaxError: JSON.parse: expected ',' or ']' after array element at line 1 column 3 of the JSON data – mike Feb 8 '18 at 9:53
  • Can you tell me exactly what you're putting in the field next to "Create?" It should be a hexadecimal string starting with 0x and enclosed in double quotes. – smarx Feb 8 '18 at 9:56

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.