Skip to content

Commit ae69849

Browse files
authored
Merge pull request #5 from AfriaDev/implement-pancakeswap-pricefetcher
Implement pancakeswap pricefetcher
2 parents 2dcddd7 + 47e5873 commit ae69849

36 files changed

+8728
-2123
lines changed

.env.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
MAINNET_INFURA_URL=""
22
MAINNET_INFURA_WSS_URL=""
33

4+
5+
BSC_MAINNET_WSS_URL=""
6+
7+
BSC_MAINNET_HTTPS="https://bsc-dataseed.binance.org/"
8+
9+
MORALIAS_BSC_MAINNET_WSS_URL=""
10+
11+
412
ROPSTEN_INFURA_URL=""
513
ROPSTEN_INFURA_WSS_URL=""
614

contracts/ApePancakeArbitrage.sol

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
3+
pragma solidity >=0.6.6 <0.8.0;
4+
5+
import "./utils/SafeMath.sol";
6+
import "./interfaces/6.0/IUniswapV2Router02.sol";
7+
import "./interfaces/6.0/IERC20.sol";
8+
9+
contract ApePancakeArbitrage {
10+
using SafeMath for uint;
11+
12+
address private owner;
13+
address private constant pancakeFactory = 0xBCfCcbde45cE874adCB698cC183deBcF17952812;
14+
address private constant bakery = 0xCDe540d7eAFE93aC5fE6233Bee57E1270D3E330F;
15+
address private constant ape = 0xcF0feBd3f17CEf5b47b0cD257aCf6025c5BFf3b7;
16+
IUniswapV2Router02 bakeryRouter = IUniswapV2Router02(bakery);
17+
IUniswapV2Router02 apeRouter = IUniswapV2Router02(ape);
18+
19+
constructor() {
20+
owner = msg.sender;
21+
}
22+
23+
function startArbitrage(
24+
address token0,
25+
address token1,
26+
uint amount0,
27+
uint amount1
28+
) external {
29+
address pairAddress = IUniswapV2Factory(pancakeFactory).getPair(token0, token1);
30+
require(pairAddress != address(0), 'This pool does not exist');
31+
32+
IUniswapV2Pair(pairAddress).swap(
33+
amount0,
34+
amount1,
35+
address(this),
36+
bytes('not empty')
37+
);
38+
}
39+
40+
function pancakeCall(
41+
address _sender,
42+
uint _amount0,
43+
uint _amount1,
44+
bytes calldata _data
45+
) external {
46+
address[] memory path = new address[](2);
47+
48+
// obtain an amout of token that you exchanged
49+
uint amountToken = _amount0 == 0 ? _amount1 : _amount0;
50+
51+
address token0 = IUniswapV2Pair(msg.sender).token0();
52+
address token1 = IUniswapV2Pair(msg.sender).token1();
53+
54+
require(msg.sender == UniswapV2Library.pairFor(pancakeFactory, token0, token1));
55+
require(_amount0 == 0 || _amount1 == 0);
56+
57+
// if _amount0 is zero sell token1 for token0
58+
// else sell token0 for token1 as a result
59+
path[0] = _amount0 == 0 ? token1 : token0;
60+
path[1] = _amount0 == 0 ? token0 : token1;
61+
62+
// IERC20 token that we will sell for otherToken
63+
IERC20 token = IERC20(_amount0 == 0 ? token1 : token0);
64+
// token.approve(address(bakeryRouter), amountToken);
65+
token.approve(address(apeRouter), amountToken);
66+
67+
// calculate the amount of token how much input token should be reimbursed
68+
uint amountRequired = UniswapV2Library.getAmountsIn(
69+
pancakeFactory,
70+
amountToken,
71+
path
72+
)[0];
73+
74+
// swap token and obtain equivalent otherToken amountRequired as a result
75+
// need to receive amountRequired at minimum amount to pay back
76+
// uint amountReceived = bakeryRouter.swapExactTokensForTokens(
77+
uint amountReceived = apeRouter.swapExactTokensForTokens(
78+
amountToken,
79+
amountRequired,
80+
path,
81+
msg.sender,
82+
block.timestamp
83+
)[1];
84+
85+
require(amountReceived > amountRequired); // fail if we didn't get enough tokens
86+
IERC20 otherToken = IERC20(_amount0 == 0 ? token0 : token1);
87+
// otherToken.transfer(msg.sender, amountRequired);
88+
otherToken.transfer(owner, amountReceived.sub(amountRequired));
89+
}
90+
91+
receive() external payable {}
92+
}

contracts/KyUniFlashloan.sol

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ pragma experimental ABIEncoderV2;
44
import "@studydefi/money-legos/dydx/contracts/DydxFlashloanBase.sol";
55
import "@studydefi/money-legos/dydx/contracts/ICallee.sol";
66
import { KyberNetworkProxy as IKyberNetworkProxy } from '@studydefi/money-legos/kyber/contracts/KyberNetworkProxy.sol';
7-
87
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
9-
import './IUniswapV2Router02.sol';
10-
import './IWeth.sol';
8+
import "./interfaces/5.0/IUniswapV2Router02.sol";
9+
import "./interfaces/5.0/IWeth.sol";
1110

1211
contract KyUniFlashloan is ICallee, DydxFlashloanBase {
1312
enum Direction { KyberToUniswap, UniswapToKyber }

contracts/PancakeTokenSwap.sol

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
3+
pragma solidity >=0.6.6 <0.8.0;
4+
5+
import './utils/Ownable.sol';
6+
import './utils/SafeMath.sol';
7+
import './interfaces/5.0/UniswapV2Library.sol';
8+
import './interfaces/6.0/IERC20.sol';
9+
import './interfaces/5.0/IUniswapV2Pair.sol';
10+
import './interfaces/6.0/IUniswapV2Factory.sol';
11+
import './interfaces/6.0/IUniswapV2Router02.sol';
12+
13+
contract PancakeTokenSwap is Ownable {
14+
using SafeMath for uint;
15+
address private constant pancakeRouter = 0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F;
16+
address private constant WBNB = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;
17+
18+
constructor() {}
19+
20+
function startSwap(
21+
address token0,
22+
address token1,
23+
uint amount0,
24+
uint amount1
25+
) external {
26+
// transfer input tokens to this contract address
27+
IERC20(token0).transferFrom(msg.sender, address(this), amount0);
28+
// approve pancakeRouter to transfer tokens from this contract
29+
IERC20(token0).approve(pancakeRouter, amount0);
30+
31+
address[] memory path;
32+
if (token0 == WBNB || token1 == WBNB) {
33+
path = new address[](2);
34+
path[0] = token0;
35+
path[1] = token1;
36+
} else {
37+
path = new address[](3);
38+
path[0] = token0;
39+
path[1] = WBNB;
40+
path[2] = token1;
41+
}
42+
43+
IUniswapV2Router02(pancakeRouter).swapExactTokensForTokens(
44+
amount0,
45+
amount1,
46+
path,
47+
msg.sender,
48+
block.timestamp
49+
);
50+
}
51+
52+
function destruct() public onlyOwner {
53+
address payable owner = payable(owner());
54+
selfdestruct(owner);
55+
}
56+
57+
receive() external payable {}
58+
}

contracts/SushiUniFlashloan.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pragma experimental ABIEncoderV2;
44
import "@studydefi/money-legos/dydx/contracts/DydxFlashloanBase.sol";
55
import "@studydefi/money-legos/dydx/contracts/ICallee.sol";
66
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
7-
import "./IUniswapV2Router02.sol";
8-
import "./IWeth.sol";
7+
import "./interfaces/5.0/IUniswapV2Router02.sol";
8+
import "./interfaces/5.0/IWeth.sol";
99

1010
contract SushiUniFlashloan is ICallee, DydxFlashloanBase {
1111
enum Direction { SushiToUniswap, UniswapToSushi}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity >=0.5.0;
4+
5+
interface IUniswapV2Callee {
6+
function uniswapV2Call(address sender, uint amount0, uint amount1, bytes calldata data) external;
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
// SPDX-License-Identifier: MIT
4+
5+
pragma solidity >=0.5.0;
6+
7+
interface IUniswapV2Factory {
8+
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
9+
function getPair(address tokenA, address tokenB) external view returns (address pair);
10+
function allPairs(uint) external view returns (address pair);
11+
function allPairsLength() external view returns (uint);
12+
function feeTo() external view returns (address);
13+
function feeToSetter() external view returns (address);
14+
function createPair(address tokenA, address tokenB) external returns (address pair);
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity >=0.5.0;
4+
5+
interface IUniswapV2Pair {
6+
event Approval(address indexed owner, address indexed spender, uint value);
7+
event Transfer(address indexed from, address indexed to, uint value);
8+
9+
function name() external pure returns (string memory);
10+
function symbol() external pure returns (string memory);
11+
function decimals() external pure returns (uint8);
12+
function totalSupply() external view returns (uint);
13+
function balanceOf(address owner) external view returns (uint);
14+
function allowance(address owner, address spender) external view returns (uint);
15+
16+
function approve(address spender, uint value) external returns (bool);
17+
function transfer(address to, uint value) external returns (bool);
18+
function transferFrom(address from, address to, uint value) external returns (bool);
19+
20+
function DOMAIN_SEPARATOR() external view returns (bytes32);
21+
function PERMIT_TYPEHASH() external pure returns (bytes32);
22+
function nonces(address owner) external view returns (uint);
23+
24+
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
25+
26+
event Mint(address indexed sender, uint amount0, uint amount1);
27+
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
28+
event Swap(
29+
address indexed sender,
30+
uint amount0In,
31+
uint amount1In,
32+
uint amount0Out,
33+
uint amount1Out,
34+
address indexed to
35+
);
36+
event Sync(uint112 reserve0, uint112 reserve1);
37+
38+
function MINIMUM_LIQUIDITY() external pure returns (uint);
39+
function factory() external view returns (address);
40+
function token0() external view returns (address);
41+
function token1() external view returns (address);
42+
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
43+
function price0CumulativeLast() external view returns (uint);
44+
function price1CumulativeLast() external view returns (uint);
45+
function kLast() external view returns (uint);
46+
47+
function mint(address to) external returns (uint liquidity);
48+
function burn(address to) external returns (uint amount0, uint amount1);
49+
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
50+
function skim(address to) external;
51+
function sync() external;
52+
53+
function initialize(address, address) external;
54+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)