Skip to content

Instantly share code, notes, and snippets.

@anubhavgirdhar
Created July 22, 2020 12:36
Show Gist options
  • Save anubhavgirdhar/a3ceb3a33cf04e4a8a97acc9e21e8e64 to your computer and use it in GitHub Desktop.
Save anubhavgirdhar/a3ceb3a33cf04e4a8a97acc9e21e8e64 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.6.6+commit.6c089d02.js&optimize=false&gist=
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
import "./EIP712.sol";
import "./ERC20.sol";
import "./dsProxy.sol";
contract DsaForwarder is DSAuth{
struct Signature{
bytes32 r;
bytes32 s;
uint8 v;
}
mapping (address => mapping (address => mapping (bytes4 => bool))) acl;
function canCall(
address src, address dst, bytes4 sig
) external view returns (bool){
return true;
}
function permit(address src, address dst, bytes4 sig) public {
acl[src][dst][sig] = true;
}
// function forward(address signer, Signature calldata signature, address to, uint value, bytes calldata data) isDSA (to,signer) external {
// bytes32 digest = keccak256(
// abi.encodePacked(
// "\x19\x01",
// DOMAIN_SEPARATOR,
// keccak256(abi.encode(META_TRANSACTION_TYPEHASH,signer,nonces[signer]))));
// require(signer != address(0), "invalid-address-0");
// require(signer == ecrecover(digest, signature.v, signature.r, signature.s), "invalid-signatures");
// _call(to,data);
// }
function _call(
address to,
bytes memory data,
address _inputToken,
uint _value,
address _outputToken
) public {
ERC20 erc20 = ERC20(_inputToken);
erc20.transferFrom(msg.sender,address(this),_value);
erc20.approve(to,_value);
(bool success,) = to.call(data);
if (!success) {
assembly {
let returnDataSize := returndatasize()
returndatacopy(0, 0, returnDataSize)
revert(0, returnDataSize)
}
} else {
ERC20 outputToken = ERC20(_outputToken);
outputToken.transfer(msg.sender,outputToken.balanceOf(address(this)));
}
}
}
// call args
// "0x185b0Eec4Fd74DA335702207f77999181d0eFb1c","0x1cff79cd00000000000000000000000002efdb542b9390ae7c1620b29674e02f9c0d86cc00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000084c1762b1500000000000000000000000031670617b85451e5e3813e50442eed3ce3b68d19000000000000000000000000d0a1e359811322d97991e03f863a0c30c2cf029c00000000000000000000000000000000000000000000000001aa535d3d0c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","0xd0A1E359811322d97991E03f863a0C30C2cF029C","120000000000000000","0x31670617b85451e5e3813e50442eed3ce3b68d19"
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/IERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/SafeERC20.sol";
contract Bunch {
using SafeERC20 for IERC20;
address ethAddress = address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
function multiToken(address _target,address payable _address,uint _value) internal {
if(_target == ethAddress){
address(_address).transfer(_value);
}else{
IERC20 erc20 = IERC20(_target);
erc20.safeTransfer(_address,_value);
}
}
function execute(
address[] calldata tokenAddresses,
address payable[] calldata addresses,
uint[] calldata values
)
external
payable
{
for (uint i = 0; i < tokenAddresses.length; i++) {
multiToken(tokenAddresses[i], addresses[i],values[i]);
}
}
}
// guard.sol -- simple whitelist implementation of DSAuthority
// 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.4.13;
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;
function DSAuth() public {
owner = msg.sender;
LogSetOwner(msg.sender);
}
function setOwner(address owner_)
public
auth
{
owner = owner_;
LogSetOwner(owner);
}
function setAuthority(DSAuthority authority_)
public
auth
{
authority = authority_;
LogSetAuthority(authority);
}
modifier auth {
require(isAuthorized(msg.sender, msg.sig));
_;
}
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, this, sig);
}
}
}
contract DSGuardEvents {
event LogPermit(
bytes32 indexed src,
bytes32 indexed dst,
bytes32 indexed sig
);
event LogForbid(
bytes32 indexed src,
bytes32 indexed dst,
bytes32 indexed sig
);
}
contract DSGuard is DSAuth, DSAuthority, DSGuardEvents {
bytes32 constant public ANY = bytes32(uint(-1));
mapping (bytes32 => mapping (bytes32 => mapping (bytes32 => bool))) acl;
function canCall(
address src_, address dst_, bytes4 sig
) public view returns (bool) {
var src = bytes32(src_);
var dst = bytes32(dst_);
return acl[src][dst][sig]
|| acl[src][dst][ANY]
|| acl[src][ANY][sig]
|| acl[src][ANY][ANY]
|| acl[ANY][dst][sig]
|| acl[ANY][dst][ANY]
|| acl[ANY][ANY][sig]
|| acl[ANY][ANY][ANY];
}
function permit(bytes32 src, bytes32 dst, bytes32 sig) public auth {
acl[src][dst][sig] = true;
LogPermit(src, dst, sig);
}
function forbid(bytes32 src, bytes32 dst, bytes32 sig) public auth {
acl[src][dst][sig] = false;
LogForbid(src, dst, sig);
}
function permit(address src, address dst, bytes32 sig) public {
permit(bytes32(src), bytes32(dst), sig);
}
function forbid(address src, address dst, bytes32 sig) public {
forbid(bytes32(src), bytes32(dst), sig);
}
}
contract DSGuardFactory {
mapping (address => bool) public isGuard;
function newGuard() public returns (DSGuard guard) {
guard = new DSGuard();
guard.setOwner(msg.sender);
isGuard[guard] = true;
}
}
// 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.12;
interface DSAuthority {
function canCall(
address src, address dst, bytes4 sig
) external 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);
}
}
}
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);
}
}
// 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;
}
}
pragma solidity ^0.5.0;
contract EIP712 {
mapping(address => uint256) public nonces;
struct EIP712Domain {
string name;
string version;
uint256 chainId;
address verifyingContract;
}
struct MetaTransaction {
address holder;
uint256 nonce;
}
bytes32 internal constant EIP712_DOMAIN_TYPEHASH = keccak256(bytes("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"));
bytes32 internal constant META_TRANSACTION_TYPEHASH = keccak256(bytes("MetaTransaction(address holder,uint256 nonce)"));
bytes32 internal DOMAIN_SEPARATOR = keccak256(abi.encode(
EIP712_DOMAIN_TYPEHASH,
keccak256(bytes("DSA")),
keccak256(bytes("1")),
42, // Ropsten
address(this)
));
}
pragma solidity ^0.5.0;
interface ERC20 {
function totalSupply() external view returns (uint supply);
function balanceOf(address _owner) external view returns (uint balance);
function transfer(address _to, uint _value) external returns (bool success);
function transferFrom(address _from, address _to, uint _value) external returns (bool success);
function approve(address _spender, uint _value) external returns (bool success);
function allowance(address _owner, address _spender) external view returns (uint remaining);
function decimals() external view returns(uint digits);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
pragma solidity 0.5.0;
contract HashMyFunction{
function hashMe(address[] memory targets,address payable[] memory addresses,uint[] memory values) public pure returns(bytes memory){
return abi.encodeWithSignature("multiToken(address[],address[],uint256[])",targets,addresses,values);
}
function test() external view returns (bytes memory){
return abi.encodeWithSelector(bytes4(keccak256('execute(address,bytes)')));
}
}
pragma solidity ^0.5.0;
import "./ParcelStorage.sol";
import "./ERC20.sol";
contract MultiSender is ParcelStorage{
function multiToken(address[] memory targets,address payable[] memory addresses,uint[] memory values) public payable {
for (uint i = 0; i < addresses.length; i++) {
if(targets[i] == ethAddress){
address(addresses[i]).transfer(values[i]);
}else{
ERC20 erc20 = ERC20(targets[i]);
erc20.transfer(addresses[i],values[i]);
}
}
}
function hashMe(address[] memory targets,address payable[] memory addresses,uint[] memory values) public pure returns(bytes memory){
return abi.encodeWithSignature("multiToken(address[],address[],uint256[])",targets,addresses,values);
}
function() external payable {
}
}
pragma solidity ^0.5.0;
import "parcelProxy.sol";
contract ParcelRegistry {
mapping(address => address) public registered;
modifier oneAccount {
require(registered[msg.sender] == address(0), "account exists");
_;
}
function register() external payable oneAccount returns (address) {
ParcelProxy newAccount = new ParcelProxy(msg.sender);
registered[msg.sender] = address(newAccount);
}
}
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
import "./ParcelStorage.sol";
contract ParcelProxy is ParcelStorage {
constructor(address _owner) public {
owner = _owner;
}
modifier onlyOwner {
require(msg.sender == owner, "Not the Owner");
_;
}
/**
* @dev Delegate the calls to Connector And this function is ran by cast().
* @param _target Target to of Connector.
* @param _data CallData of function in Connector.
*/
function _call(address _target, bytes memory _data) internal {
require(_target != address(0), "target-invalid");
assembly {
let succeeded := delegatecall(
gas(),
_target,
add(_data, 0x20),
mload(_data),
0,
0
)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
let size := returndatasize()
returndatacopy(0x00, 0x00, size)
revert(0x00, size)
}
}
}
/**
* @dev This is the main function
* @param targets Array of Target(s) to of Implementation.
* @param data Array of Calldata(s) of function.
*/
function execute(address[] calldata targets, bytes[] calldata data)
external
payable
onlyOwner
{
for (uint256 i = 0; i < targets.length; i++) {
_call(targets[i], data[i]);
}
}
function() external payable {}
}
pragma solidity ^0.5.0;
// import "./uniswapInterface.sol";
// import "./cTokenInterface.sol";
// import "./ERC20.sol";
// import "./comptrollerInterface.sol";
// import "./aaveInterface.sol";
// import "./aTokenInterface.sol";
interface ILendingPoolAddressesProvider {
function getLendingPool() external view returns (address);
}
contract ParcelStorage{
address public uniswapRouterAddress = 0xf164fC0Ec4E93095b804a4795bBe1e041497b92a;
// UniswapInterface uniswap = UniswapInterface(uniswapRouterAddress);
address WETH_KOVAN = 0xd0A1E359811322d97991E03f863a0C30C2cF029C;
address DAI_KOVAN = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
address cEthAddress = 0xf92FbE0D3C0dcDAE407923b2Ac17eC223b1084E4;
// cTokenInterface cEth = cTokenInterface(cEthAddress);
address cDaiAddress = 0xe7bc397DBd069fC7d0109C0636d06888bb50668c;
// cTokenInterface cDai = cTokenInterface(cDaiAddress);
address comptrollerAddress = 0x1f5D7F3CaAC149fE41b8bd62A3673FE6eC0AB73b;
// comptrollerInterface comptroller = comptrollerInterface(comptrollerAddress);
// cTokenInterface dai = cTokenInterface(DAI_KOVAN);
address public owner;
// // Retrieve LendingPool address
// ILendingPoolAddressesProvider provider = ILendingPoolAddressesProvider(address(0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5)); // mainnet address, for other addresses: https://docs.aave.com/developers/developing-on-aave/deployed-contract-instances
// LendingPool lendingPool = LendingPool(provider.getLendingPool());
// Input variables
address ethAddress = address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
uint16 referral = 0;
}
pragma solidity ^0.5.0;
contract ParcelStorage {
address public owner;
}
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
import "./ParcelStorage.sol";
import "./ERC20.sol";
contract Bunch is ParcelStorage{
// @dev Delegate the calls to Connector And this function is ran by execute().
// * @param _target Target to of Connector.
// * @param _data CallData of function in Connector.
// */
// function _call(address _target, bytes memory _data) internal {
// require(_target != address(0), "target-invalid");
// assembly {
// let succeeded := delegatecall(
// gas(),
// _target,
// add(_data, 0x20),
// mload(_data),
// 0,
// 0
// )
// switch iszero(succeeded)
// case 1 {
// // throw if delegatecall failed
// let size := returndatasize()
// returndatacopy(0x00, 0x00, size)
// revert(0x00, size)
// }
// }
// }
function multiToken(address targets,address payable addresses,uint values) internal {
if(targets == ethAddress){
address(addresses).transfer(values);
}else{
ERC20 erc20 = ERC20(targets);
erc20.transfer(addresses,values);
}
}
function execute(
address[] calldata targets,
address payable[] calldata addresses,
uint[] calldata values
)
external
payable
{
for (uint i = 0; i < targets.length; i++) {
multiToken(targets[i], addresses[i],values[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment