Created
March 22, 2019 15:02
-
-
Save nick/0d9da535db137b1ff23c93271411afe2 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.6+commit.b259423e.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
pragma solidity >=0.4.23; | |
contract DSAuthority { | |
function canCall( | |
address src, address dst, bytes4 sig | |
) public view returns (bool); | |
} | |
contract DSAuthEvents { | |
event LogSetAuthority (address indexed authority); | |
event LogSetOwner (address indexed owner); | |
} | |
contract DSAuth is DSAuthEvents { | |
DSAuthority public authority; | |
address public owner; | |
constructor() public { | |
owner = msg.sender; | |
emit LogSetOwner(msg.sender); | |
} | |
function setOwner(address owner_) | |
public | |
auth | |
{ | |
owner = owner_; | |
emit LogSetOwner(owner); | |
} | |
function setAuthority(DSAuthority authority_) | |
public | |
auth | |
{ | |
authority = authority_; | |
emit LogSetAuthority(address(authority)); | |
} | |
modifier auth { | |
require(isAuthorized(msg.sender, msg.sig), "ds-auth-unauthorized"); | |
_; | |
} | |
function isAuthorized(address src, bytes4 sig) internal view returns (bool) { | |
if (src == address(this)) { | |
return true; | |
} else if (src == owner) { | |
return true; | |
} else if (authority == DSAuthority(0)) { | |
return false; | |
} else { | |
return authority.canCall(src, address(this), sig); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.6; | |
contract ProxyRegistryInterface { | |
function build(address) public returns (address); | |
} | |
contract MarketplaceInterface { | |
function makeOffer( | |
uint listingID, | |
bytes32 _ipfsHash, | |
uint _finalizes, | |
address _affiliate, | |
uint256 _commission, | |
uint _value, | |
TokenInterface _currency, | |
address _arbitrator | |
) public payable; | |
} | |
contract UniswapInterface { | |
function getEthToTokenOutputPrice(uint) public returns (uint); | |
function ethToTokenSwapOutput(uint, uint) public payable returns (uint); | |
} | |
contract TokenInterface { | |
function balanceOf(address) public returns (uint); | |
function allowance(address, address) public returns (uint); | |
function approve(address, uint) public; | |
function transfer(address, uint) public returns (bool); | |
function transferFrom(address, address, uint) public returns (bool); | |
function deposit() public payable; | |
function withdraw(uint) public; | |
} | |
contract MarketplaceProxy { | |
function makeTokenOfferWithEth( | |
MarketplaceInterface marketplace, | |
uint listingID, | |
bytes32 _ipfsHash, | |
uint _finalizes, | |
address _affiliate, | |
uint256 _commission, | |
uint _value, | |
TokenInterface _currency, | |
address _arbitrator | |
) public payable { | |
marketplace.makeOffer.value(msg.value)( | |
listingID, | |
_ipfsHash, | |
_finalizes, | |
_affiliate, | |
_commission, | |
_value, | |
_currency, | |
_arbitrator | |
); | |
} | |
} | |
contract MarketplaceProxyCreateExec { | |
function makeTokenOfferWithEth( | |
address marketplace, | |
uint listingID, | |
bytes32 _ipfsHash, | |
uint _finalizes, | |
address _affiliate, | |
uint256 _commission, | |
uint _value, | |
TokenInterface _currency, | |
address _arbitrator | |
) public payable { | |
proxy = ProxyRegistryInterface(registry).build(msg.sender); | |
proxy.execute(marketplace) | |
marketplace.makeOffer.value(msg.value)( | |
listingID, | |
_ipfsHash, | |
_finalizes, | |
_affiliate, | |
_commission, | |
_value, | |
_currency, | |
_arbitrator | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// note.sol -- the `note' modifier, for logging calls as events | |
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
pragma solidity >=0.4.23; | |
contract DSNote { | |
event LogNote( | |
bytes4 indexed sig, | |
address indexed guy, | |
bytes32 indexed foo, | |
bytes32 indexed bar, | |
uint256 wad, | |
bytes fax | |
) anonymous; | |
modifier note { | |
bytes32 foo; | |
bytes32 bar; | |
uint256 wad; | |
assembly { | |
foo := calldataload(4) | |
bar := calldataload(36) | |
wad := callvalue | |
} | |
emit LogNote(msg.sig, msg.sender, foo, bar, wad, msg.data); | |
_; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// proxy.sol - execute actions atomically through the proxy's identity | |
// Copyright (C) 2017 DappHub, LLC | |
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
pragma solidity >=0.5.0 <0.6.0; | |
import "./auth.sol"; | |
import "./note.sol"; | |
// DSProxy | |
// Allows code execution using a persistant identity This can be very | |
// useful to execute a sequence of atomic actions. Since the owner of | |
// the proxy can be changed, this allows for dynamic ownership models | |
// i.e. a multisig | |
contract DSProxy is DSAuth, DSNote { | |
DSProxyCache public cache; // global cache for contracts | |
constructor(address _cacheAddr) public { | |
setCache(_cacheAddr); | |
} | |
function() external payable { | |
} | |
// use the proxy to execute calldata _data on contract _code | |
function execute(bytes memory _code, bytes memory _data) | |
public | |
payable | |
returns (address target, bytes memory response) | |
{ | |
target = cache.read(_code); | |
if (target == address(0)) { | |
// deploy contract & store its address in cache | |
target = cache.write(_code); | |
} | |
response = execute(target, _data); | |
} | |
function execute(address _target, bytes memory _data) | |
public | |
auth | |
note | |
payable | |
returns (bytes memory response) | |
{ | |
require(_target != address(0), "ds-proxy-target-address-required"); | |
// call contract in current context | |
assembly { | |
let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0) | |
let size := returndatasize | |
response := mload(0x40) | |
mstore(0x40, add(response, and(add(add(size, 0x20), 0x1f), not(0x1f)))) | |
mstore(response, size) | |
returndatacopy(add(response, 0x20), 0, size) | |
switch iszero(succeeded) | |
case 1 { | |
// throw if delegatecall failed | |
revert(add(response, 0x20), size) | |
} | |
} | |
} | |
//set new cache | |
function setCache(address _cacheAddr) | |
public | |
auth | |
note | |
returns (bool) | |
{ | |
require(_cacheAddr != address(0), "ds-proxy-cache-address-required"); | |
cache = DSProxyCache(_cacheAddr); // overwrite cache | |
return true; | |
} | |
} | |
// DSProxyFactory | |
// This factory deploys new proxy instances through build() | |
// Deployed proxy addresses are logged | |
contract DSProxyFactory { | |
event Created(address indexed sender, address indexed owner, address proxy, address cache); | |
mapping(address=>bool) public isProxy; | |
DSProxyCache public cache; | |
constructor() public { | |
cache = new DSProxyCache(); | |
} | |
// deploys a new proxy instance | |
// sets owner of proxy to caller | |
function build() public returns (address payable proxy) { | |
proxy = build(msg.sender); | |
} | |
// deploys a new proxy instance | |
// sets custom owner of proxy | |
function build(address owner) public returns (address payable proxy) { | |
proxy = address(new DSProxy(address(cache))); | |
emit Created(msg.sender, owner, address(proxy), address(cache)); | |
DSProxy(proxy).setOwner(owner); | |
isProxy[proxy] = true; | |
} | |
} | |
// DSProxyCache | |
// This global cache stores addresses of contracts previously deployed | |
// by a proxy. This saves gas from repeat deployment of the same | |
// contracts and eliminates blockchain bloat. | |
// By default, all proxies deployed from the same factory store | |
// contracts in the same cache. The cache a proxy instance uses can be | |
// changed. The cache uses the sha3 hash of a contract's bytecode to | |
// lookup the address | |
contract DSProxyCache { | |
mapping(bytes32 => address) cache; | |
function read(bytes memory _code) public view returns (address) { | |
bytes32 hash = keccak256(_code); | |
return cache[hash]; | |
} | |
function write(bytes memory _code) public returns (address target) { | |
assembly { | |
target := create(0, add(_code, 0x20), mload(_code)) | |
switch iszero(extcodesize(target)) | |
case 1 { | |
// throw if contract failed to deploy | |
revert(0, 0) | |
} | |
} | |
bytes32 hash = keccak256(_code); | |
cache[hash] = target; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// proxy.sol - execute actions atomically through the proxy's identity | |
// Copyright (C) 2017 DappHub, LLC | |
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
pragma solidity >=0.5.0 <0.6.0; | |
// DSProxy | |
// Allows code execution using a persistant identity This can be very | |
// useful to execute a sequence of atomic actions. Since the owner of | |
// the proxy can be changed, this allows for dynamic ownership models | |
// i.e. a multisig | |
contract DSProxy { | |
DSProxyCache public cache; // global cache for contracts | |
constructor(address _cacheAddr) public { | |
setCache(_cacheAddr); | |
} | |
function() external payable { | |
} | |
// use the proxy to execute calldata _data on contract _code | |
function execute(bytes memory _code, bytes memory _data) | |
public | |
payable | |
returns (address target, bytes memory response) | |
{ | |
target = cache.read(_code); | |
if (target == address(0)) { | |
// deploy contract & store its address in cache | |
target = cache.write(_code); | |
} | |
response = execute(target, _data); | |
} | |
function execute(address _target, bytes memory _data) | |
public | |
payable | |
returns (bytes memory response) | |
{ | |
require(_target != address(0), "ds-proxy-target-address-required"); | |
// call contract in current context | |
assembly { | |
let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0) | |
let size := returndatasize | |
response := mload(0x40) | |
mstore(0x40, add(response, and(add(add(size, 0x20), 0x1f), not(0x1f)))) | |
mstore(response, size) | |
returndatacopy(add(response, 0x20), 0, size) | |
switch iszero(succeeded) | |
case 1 { | |
// throw if delegatecall failed | |
revert(add(response, 0x20), size) | |
} | |
} | |
} | |
//set new cache | |
function setCache(address _cacheAddr) | |
public | |
returns (bool) | |
{ | |
require(_cacheAddr != address(0), "ds-proxy-cache-address-required"); | |
cache = DSProxyCache(_cacheAddr); // overwrite cache | |
return true; | |
} | |
} | |
// DSProxyFactory | |
// This factory deploys new proxy instances through build() | |
// Deployed proxy addresses are logged | |
contract DSProxyFactory { | |
event Created(address indexed sender, address indexed owner, address proxy, address cache); | |
mapping(address=>bool) public isProxy; | |
DSProxyCache public cache; | |
constructor() public { | |
cache = new DSProxyCache(); | |
} | |
// deploys a new proxy instance | |
// sets owner of proxy to caller | |
function build() public returns (address payable proxy) { | |
proxy = build(msg.sender); | |
} | |
// deploys a new proxy instance | |
// sets custom owner of proxy | |
function build(address owner) public returns (address payable proxy) { | |
proxy = address(new DSProxy(address(cache))); | |
emit Created(msg.sender, owner, address(proxy), address(cache)); | |
isProxy[proxy] = true; | |
} | |
} | |
// DSProxyCache | |
// This global cache stores addresses of contracts previously deployed | |
// by a proxy. This saves gas from repeat deployment of the same | |
// contracts and eliminates blockchain bloat. | |
// By default, all proxies deployed from the same factory store | |
// contracts in the same cache. The cache a proxy instance uses can be | |
// changed. The cache uses the sha3 hash of a contract's bytecode to | |
// lookup the address | |
contract DSProxyCache { | |
mapping(bytes32 => address) cache; | |
function read(bytes memory _code) public view returns (address) { | |
bytes32 hash = keccak256(_code); | |
return cache[hash]; | |
} | |
function write(bytes memory _code) public returns (address target) { | |
assembly { | |
target := create(0, add(_code, 0x20), mload(_code)) | |
switch iszero(extcodesize(target)) | |
case 1 { | |
// throw if contract failed to deploy | |
revert(0, 0) | |
} | |
} | |
bytes32 hash = keccak256(_code); | |
cache[hash] = target; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.5.0 <0.6.0; | |
contract MarketplaceProxy { | |
function makeTokenOfferWithEth( | |
address marketplace, | |
uint listingID, | |
bytes32 _ipfsHash, | |
uint _finalizes, | |
address _affiliate, | |
uint256 _commission, | |
uint _value, | |
address _currency, | |
address _arbitrator | |
) public payable { | |
bytes memory payload = abi.encodeWithSignature("makeOffer(uint256,bytes32,uint256,address,uint256,uint256,address,address)", | |
listingID, _ipfsHash, _finalizes, _affiliate, _commission, _value, _currency, _arbitrator | |
); | |
marketplace.delegatecall(payload); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment