Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
MAINNET_INFURA_URL=""
MAINNET_INFURA_WSS_URL=""


BSC_MAINNET_WSS_URL=""

BSC_MAINNET_HTTPS="https://bsc-dataseed.binance.org/"

MORALIAS_BSC_MAINNET_WSS_URL=""


ROPSTEN_INFURA_URL=""
ROPSTEN_INFURA_WSS_URL=""

Expand Down
92 changes: 92 additions & 0 deletions contracts/ApePancakeArbitrage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.6.6 <0.8.0;

import "./utils/SafeMath.sol";
import "./interfaces/6.0/IUniswapV2Router02.sol";
import "./interfaces/6.0/IERC20.sol";

contract ApePancakeArbitrage {
using SafeMath for uint;

address private owner;
address private constant pancakeFactory = 0xBCfCcbde45cE874adCB698cC183deBcF17952812;
address private constant bakery = 0xCDe540d7eAFE93aC5fE6233Bee57E1270D3E330F;
address private constant ape = 0xcF0feBd3f17CEf5b47b0cD257aCf6025c5BFf3b7;
IUniswapV2Router02 bakeryRouter = IUniswapV2Router02(bakery);
IUniswapV2Router02 apeRouter = IUniswapV2Router02(ape);

constructor() {
owner = msg.sender;
}

function startArbitrage(
address token0,
address token1,
uint amount0,
uint amount1
) external {
address pairAddress = IUniswapV2Factory(pancakeFactory).getPair(token0, token1);
require(pairAddress != address(0), 'This pool does not exist');

IUniswapV2Pair(pairAddress).swap(
amount0,
amount1,
address(this),
bytes('not empty')
);
}

function pancakeCall(
address _sender,
uint _amount0,
uint _amount1,
bytes calldata _data
) external {
address[] memory path = new address[](2);

// obtain an amout of token that you exchanged
uint amountToken = _amount0 == 0 ? _amount1 : _amount0;

address token0 = IUniswapV2Pair(msg.sender).token0();
address token1 = IUniswapV2Pair(msg.sender).token1();

require(msg.sender == UniswapV2Library.pairFor(pancakeFactory, token0, token1));
require(_amount0 == 0 || _amount1 == 0);

// if _amount0 is zero sell token1 for token0
// else sell token0 for token1 as a result
path[0] = _amount0 == 0 ? token1 : token0;
path[1] = _amount0 == 0 ? token0 : token1;

// IERC20 token that we will sell for otherToken
IERC20 token = IERC20(_amount0 == 0 ? token1 : token0);
// token.approve(address(bakeryRouter), amountToken);
token.approve(address(apeRouter), amountToken);

// calculate the amount of token how much input token should be reimbursed
uint amountRequired = UniswapV2Library.getAmountsIn(
pancakeFactory,
amountToken,
path
)[0];

// swap token and obtain equivalent otherToken amountRequired as a result
// need to receive amountRequired at minimum amount to pay back
// uint amountReceived = bakeryRouter.swapExactTokensForTokens(
uint amountReceived = apeRouter.swapExactTokensForTokens(
amountToken,
amountRequired,
path,
msg.sender,
block.timestamp
)[1];

require(amountReceived > amountRequired); // fail if we didn't get enough tokens
IERC20 otherToken = IERC20(_amount0 == 0 ? token0 : token1);
// otherToken.transfer(msg.sender, amountRequired);
otherToken.transfer(owner, amountReceived.sub(amountRequired));
}

receive() external payable {}
}
5 changes: 2 additions & 3 deletions contracts/KyUniFlashloan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ pragma experimental ABIEncoderV2;
import "@studydefi/money-legos/dydx/contracts/DydxFlashloanBase.sol";
import "@studydefi/money-legos/dydx/contracts/ICallee.sol";
import { KyberNetworkProxy as IKyberNetworkProxy } from '@studydefi/money-legos/kyber/contracts/KyberNetworkProxy.sol';

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import './IUniswapV2Router02.sol';
import './IWeth.sol';
import "./interfaces/5.0/IUniswapV2Router02.sol";
import "./interfaces/5.0/IWeth.sol";

contract KyUniFlashloan is ICallee, DydxFlashloanBase {
enum Direction { KyberToUniswap, UniswapToKyber }
Expand Down
58 changes: 58 additions & 0 deletions contracts/PancakeTokenSwap.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.6.6 <0.8.0;

import './utils/Ownable.sol';
import './utils/SafeMath.sol';
import './interfaces/5.0/UniswapV2Library.sol';
import './interfaces/6.0/IERC20.sol';
import './interfaces/5.0/IUniswapV2Pair.sol';
import './interfaces/6.0/IUniswapV2Factory.sol';
import './interfaces/6.0/IUniswapV2Router02.sol';

contract PancakeTokenSwap is Ownable {
using SafeMath for uint;
address private constant pancakeRouter = 0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F;
address private constant WBNB = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;

constructor() {}

function startSwap(
address token0,
address token1,
uint amount0,
uint amount1
) external {
// transfer input tokens to this contract address
IERC20(token0).transferFrom(msg.sender, address(this), amount0);
// approve pancakeRouter to transfer tokens from this contract
IERC20(token0).approve(pancakeRouter, amount0);

address[] memory path;
if (token0 == WBNB || token1 == WBNB) {
path = new address[](2);
path[0] = token0;
path[1] = token1;
} else {
path = new address[](3);
path[0] = token0;
path[1] = WBNB;
path[2] = token1;
}

IUniswapV2Router02(pancakeRouter).swapExactTokensForTokens(
amount0,
amount1,
path,
msg.sender,
block.timestamp
);
}

function destruct() public onlyOwner {
address payable owner = payable(owner());
selfdestruct(owner);
}

receive() external payable {}
}
4 changes: 2 additions & 2 deletions contracts/SushiUniFlashloan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma experimental ABIEncoderV2;
import "@studydefi/money-legos/dydx/contracts/DydxFlashloanBase.sol";
import "@studydefi/money-legos/dydx/contracts/ICallee.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./IUniswapV2Router02.sol";
import "./IWeth.sol";
import "./interfaces/5.0/IUniswapV2Router02.sol";
import "./interfaces/5.0/IWeth.sol";

contract SushiUniFlashloan is ICallee, DydxFlashloanBase {
enum Direction { SushiToUniswap, UniswapToSushi}
Expand Down
7 changes: 7 additions & 0 deletions contracts/interfaces/5.0/IUniswapV2Callee.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.5.0;

interface IUniswapV2Callee {
function uniswapV2Call(address sender, uint amount0, uint amount1, bytes calldata data) external;
}
15 changes: 15 additions & 0 deletions contracts/interfaces/5.0/IUniswapV2Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


// SPDX-License-Identifier: MIT

pragma solidity >=0.5.0;

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);
}
54 changes: 54 additions & 0 deletions contracts/interfaces/5.0/IUniswapV2Pair.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.5.0;

interface IUniswapV2Pair {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);

function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);

function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);

function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);

function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;

event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);

function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);

function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function sync() external;

function initialize(address, address) external;
}
File renamed without changes.
84 changes: 84 additions & 0 deletions contracts/interfaces/5.0/UniswapV2Library.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.5.0;

import "../../utils/SafeMath.sol";
import './IUniswapV2Pair.sol';


library UniswapV2Library {
using SafeMath for uint;

// returns sorted token addresses, used to handle return values from pairs sorted in this order
function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
require(tokenA != tokenB, 'UniswapV2Library: IDENTICAL_ADDRESSES');
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2Library: ZERO_ADDRESS');
}

// calculates the CREATE2 address for a pair without making any external calls
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' // init code hash
))));
}

// fetches and sorts the reserves for a pair
function getReserves(address factory, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB) {
(address token0,) = sortTokens(tokenA, tokenB);
(uint reserve0, uint reserve1,) = IUniswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves();
(reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
}

// given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) {
require(amountA > 0, 'UniswapV2Library: INSUFFICIENT_AMOUNT');
require(reserveA > 0 && reserveB > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
amountB = amountA.mul(reserveB) / reserveA;
}

// given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
uint amountInWithFee = amountIn.mul(997);
uint numerator = amountInWithFee.mul(reserveOut);
uint denominator = reserveIn.mul(1000).add(amountInWithFee);
amountOut = numerator / denominator;
}

// given an output amount of an asset and pair reserves, returns a required input amount of the other asset
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn) {
require(amountOut > 0, 'UniswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT');
require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
uint numerator = reserveIn.mul(amountOut).mul(1000);
uint denominator = reserveOut.sub(amountOut).mul(997);
amountIn = (numerator / denominator).add(1);
}

// performs chained getAmountOut calculations on any number of pairs
function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts) {
require(path.length >= 2, 'UniswapV2Library: INVALID_PATH');
amounts = new uint[](path.length);
amounts[0] = amountIn;
for (uint i; i < path.length - 1; i++) {
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]);
amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut);
}
}

// performs chained getAmountIn calculations on any number of pairs
function getAmountsIn(address factory, uint amountOut, address[] memory path) internal view returns (uint[] memory amounts) {
require(path.length >= 2, 'UniswapV2Library: INVALID_PATH');
amounts = new uint[](path.length);
amounts[amounts.length - 1] = amountOut;
for (uint i = path.length - 1; i > 0; i--) {
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]);
amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut);
}
}
}
Loading