OriginProtocol / origin-playground

Playground for us to try out new ideas, specifically around Identity (ERC 725) & the Origin Marketplace
https://playground.originprotocol.com
MIT License
159 stars 72 forks source link

Relay execution value #11

Open juliosantos opened 6 years ago

juliosantos commented 6 years ago

This isn't tested yet — I'm not sure it's desired. The standard should likely be updated first, as per this comment and the next (or the contract should include another payable function). Thoughts?

nick commented 6 years ago

Makes sense... we can wait for the standard to be updated first though perhaps

DanielVF commented 6 years ago

@ricktobacco, this line is a bit of a glimpse into the darkest abysses of Solidity. :)

At the simplest level, it runs some code on another contract.

An example of doing something similar in JavaScript would be something like:

// Equivalent to foo.bar()

const objectName = "foo"
const functionName = "bar"
window[objectName][functionName].call()

Going back to Solidity, the first argument to the solidity call function is the hash of the name/ABI of the function you want to call. Here is an example, calling a function on another contract when you know all about about the other contract, and then calling an arbitrary function on that other contract:

https://ethfiddle.com/79HU5M1aCH

pragma solidity ^0.4.18;

contract CodeRunner {
  address otherContract;

  function setOtherContract(address _otherContract) public {
    otherContract = _otherContract;
  }

  function callWithKnownFunction() public {
    // Here we cast the other contract to a "Target" and then call "foo" on it.
    Target(otherContract).foo();
  }

  function callWithCall() public returns (bool){
    // Now we don't know what kind of contract the other contract is
    // We're just blindly firing off a call to run a function we hope is there
    bytes4 hashOfFunctionToCall = bytes4(keccak256("foo()"));
    bool success = otherContract.call(hashOfFunctionToCall);
    return success;
  }

}

contract Target {
  uint _callCount = 0;

  function foo() public {
    _callCount += 1;
  }

  function callCount() public view returns (uint) {
    return _callCount;
  }
}

So if you do a bytes4(keccak256("foo()")); then you can get the hash of the foo() function, which you can use to call it.

In this identity playground code, someone can submit a contract address and a function hash that they wish the identity to call (using the execute method). Later on, that could be approved, and then the identity contract would call the function you submitted on the contract you submitted from the identity contract.

When doing a call in Solidity, you can also explicitly set the amount of gas to be used and the amount of eth to transfer. This commit explicitly sets an amount to transfer.