Created
May 16, 2022 18:13
-
-
Save KittenYang/23962e6f66c9f19f60fc263d600c3b31 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.8.7+commit.e28d00a7.js&optimize=false&runs=200&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
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.7.0 <0.9.0; | |
contract EtherWallet { | |
address payable public owner; | |
constructor() { | |
owner = payable(msg.sender); | |
} | |
receive() external payable {} | |
function withdraw(uint _amount) external { | |
require(msg.sender == owner, "caller is not owner"); | |
payable(msg.sender).transfer(_amount); | |
} | |
function getBalance() external view returns (uint) { | |
return address(this).balance; | |
} | |
} | |
library Utils { | |
function exists1(address[] calldata array, address object) public pure returns (bool) { | |
for (uint i = 0; i < array.length; ++i) { | |
if (array[i] == object) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
contract MutilSigWaller { | |
using Utils for address[]; | |
address[] owners; | |
constructor(address[] memory initialOwners) { | |
owners.push(address(msg.sender)); | |
for(uint i = 0; i < initialOwners.length; ++i) { | |
owners.push(address(initialOwners[i])); | |
} | |
} | |
modifier check() { | |
require(owners.exists1(msg.sender), "msg sender must be one of the owners"); | |
_; | |
} | |
function submit() public check { | |
} | |
} | |
// import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol"; | |
// contract UniswapExample { | |
// address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ; | |
// IUniswapV2Router02 public uniswapRouter; | |
// address private multiDaiKovan = 0xaD6D458402F60fD3Bd25163575031ACDce07538D; | |
// constructor() { | |
// uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS); | |
// } | |
// function convertEthToDai(uint daiAmount) public payable { | |
// uint deadline = block.timestamp + 15; // using 'now' for convenience, for mainnet pass deadline from frontend! | |
// uniswapRouter.swapETHForExactTokens{ value: msg.value }(daiAmount, getPathForETHtoDAI(), address(this), deadline); | |
// // refund leftover ETH to user | |
// (bool success,) = msg.sender.call{ value: address(this).balance }(""); | |
// require(success, "refund failed"); | |
// } | |
// function getEstimatedETHforDAI(uint daiAmount) public view returns (uint[] memory) { | |
// return uniswapRouter.getAmountsIn(daiAmount, getPathForETHtoDAI()); | |
// } | |
// function getPathForETHtoDAI() private view returns (address[] memory) { | |
// address[] memory path = new address[](2); | |
// path[0] = uniswapRouter.WETH(); | |
// path[1] = multiDaiKovan; | |
// return path; | |
// } | |
// // important to receive ETH | |
// receive() payable external {} | |
// } | |
contract TestUniswap { | |
address private constant UNISWAP_V2_ROUTER = | |
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; | |
address private constant UNISWAP_V1_ROUTER = | |
0xf164fC0Ec4E93095b804a4795bBe1e041497b92a; | |
address private constant UNISWAP_FACTORY = | |
0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f; | |
// address private constant WETH = 0xc778417E063141139Fce010982780140Aa0cD5Ab; | |
address private constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; | |
// const DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F" | |
// const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" | |
// const USDT = "0xdAC17F958D2ee523a2206206994597C13D831ec7" | |
// const WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" | |
// const WBTC = "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599" | |
// const WETH_10 = "0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9F" | |
event Log(string message, uint[] ams); | |
event Log2(string message); | |
event Log3(bytes reason); | |
receive() external payable { | |
emit Log2("receive"); | |
} | |
fallback() external payable { | |
emit Log2("fallback"); | |
} | |
function swap( | |
address _tokenIn, | |
address _tokenOut, | |
uint _amountIn, | |
uint _amountOutMin, | |
address _to | |
) external payable { | |
IERC20(_tokenIn).transferFrom(msg.sender, address(this), _amountIn); | |
IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn); | |
address[] memory path; | |
if (_tokenIn == WETH || _tokenOut == WETH) { | |
path = new address[](2); | |
path[0] = _tokenIn; | |
path[1] = _tokenOut; | |
} else { | |
path = new address[](3); | |
path[0] = _tokenIn; | |
path[1] = WETH; | |
path[2] = _tokenOut; | |
} | |
try IUniswapV2Router(UNISWAP_V2_ROUTER).swapExactTokensForTokens( | |
_amountIn, | |
_amountOutMin, | |
path, | |
_to, | |
block.timestamp | |
) returns (uint[] memory amounts) { | |
emit Log("amounts", amounts); | |
} catch Error(string memory reason) { | |
emit Log2(reason); // revert() and require() | |
} catch (bytes memory reason) { | |
emit Log3(reason);// catch failing assert() | |
} | |
} | |
} | |
interface IUniswapV2Router { | |
function swapExactTokensForTokens( | |
uint amountIn, | |
uint amountOutMin, | |
address[] calldata path, | |
address to, | |
uint deadline | |
) external payable returns (uint[] memory amounts); | |
} | |
interface IUniswapV2Router01 { | |
function getAmountsIn(uint amountOut, address[] memory path) external payable returns (uint[] memory amounts); | |
} | |
interface IERC20 { | |
function totalSupply() external view returns (uint); | |
function balanceOf(address account) external view returns (uint); | |
function transfer(address recipient, uint amount) external returns (bool); | |
function allowance(address owner, address spender) external view returns (uint); | |
function approve(address spender, uint amount) external returns (bool); | |
function transferFrom( | |
address sender, | |
address recipient, | |
uint amount | |
) external returns (bool); | |
} | |
interface IUniswapV2Factory { | |
event PairCreated(address indexed token0, address indexed token1, address pair, uint); | |
function getPair(address tokenA, address tokenB) external view returns (address pair); | |
function allPairs(uint) external view returns (address pair); | |
function allPairsLength() external view returns (uint); | |
function feeTo() external view returns (address); | |
function feeToSetter() external view returns (address); | |
function createPair(address tokenA, address tokenB) external returns (address pair); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment