Advertisement
Guest User

Untitled

a guest
Jun 8th, 2020
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. pragma solidity ^0.5.0;
  2.  
  3. import "https://github.com/mrdavey/ez-flashloan/blob/remix/contracts/aave/FlashLoanReceiverBase.sol";
  4. import "https://github.com/mrdavey/ez-flashloan/blob/remix/contracts/aave/ILendingPool.sol";
  5. import "https://github.com/Robsonsjre/FlashloanUsecases/blob/master/contracts/interfaces/IUniswap.sol";
  6.  
  7.  
  8. /*
  9. * Arbitrageur is a contract to simulate the usage of flashloans
  10. * to make profit out of a market inbalacement
  11. *
  12. * For this example we deployed 2 Uniswap instances which we'll
  13. * call by ExchangeA and ExchangeB
  14. *
  15. * The steps happens as following:
  16. * 1. Borrow DAI from Aave
  17. * 2. Buy BAT with DAI on ExchangeA
  18. * 3. Sell BAT for DAI on ExchangeB
  19. * 4. Repay Aave loan
  20. * 5. Keep the profits
  21. */
  22. contract Arbitrageur is Addresses {
  23. address public constant DAI_ADDRESS = 0x58AD4cB396411B691A9AAb6F74545b2C5217FE6a;
  24. address public constant BAT_ADDRESS = 0x5ad67de6Fb697e92a7dE99d991F7CdB77EdF5F74;
  25. address public constant UNISWAP_FACTORY_B = 0x54Ac34e5cE84C501165674782582ADce2FDdc8F4;
  26. address public constant UNISWAP_FACTORY_A = 0xECc6C0542710a0EF07966D7d1B10fA38bbb86523;
  27.  
  28. ILendingPool public lendingPool;
  29. IUniswapExchange public exchangeA;
  30. IUniswapExchange public exchangeB;
  31. IUniswapFactory public uniswapFactoryA;
  32. IUniswapFactory public uniswapFactoryB;
  33.  
  34. constructor() public {
  35. // Instantiate Uniswap Factory A
  36. uniswapFactoryA = IUniswapFactory(UNISWAP_FACTORY_A);
  37. // get Exchange A Address
  38. address exchangeA_address = uniswapFactoryA.getExchange(DAI_ADDRESS);
  39. // Instantiate Exchange A
  40. exchangeA = IUniswapExchange(exchangeA_address);
  41.  
  42. //Instantiate Uniswap Factory B
  43. uniswapFactoryB = IUniswapFactory(UNISWAP_FACTORY_B);
  44. // get Exchange B Address
  45. address exchangeB_address = uniswapFactoryB.getExchange(BAT_ADDRESS);
  46. //Instantiate Exchange B
  47. exchangeB = IUniswapExchange(exchangeB_address);
  48. // get lendingPoolAddress
  49. address lendingPoolAddress = addressesProvider.getLendingPool();
  50. //Instantiate Aaave Lending Pool B
  51. lendingPool = ILendingPool(lendingPoolAddress);
  52. }
  53.  
  54. /*
  55. * Start the arbitrage
  56. */
  57. function makeArbitrage(uint256 amount) public onlyOwner {
  58. bytes memory data = "";
  59.  
  60. ERC20 dai = ERC20(DAI_ADDRESS);
  61. lendingPool.flashLoan(address(this), DAI_ADDRESS, amount, data);
  62.  
  63. // Any left amount of DAI is considered profit
  64. uint256 profit = dai.balanceOf(address(this));
  65. // Sending back the profits
  66. require(
  67. dai.transfer(msg.sender, profit),
  68. "Could not transfer back the profit"
  69. );
  70. }
  71.  
  72. function executeOperation(
  73. address _reserve,
  74. uint256 _amount,
  75. uint256 _fee,
  76. bytes calldata _params
  77. ) external {
  78. // If transactions are not mined until deadline the transaction is reverted
  79. uint256 deadline = getDeadline();
  80.  
  81. ERC20 dai = ERC20(DAI_ADDRESS);
  82. ERC20 bat = ERC20(BAT_ADDRESS);
  83.  
  84. // Buying ETH at Exchange A
  85. require(
  86. dai.approve(address(exchangeA), _amount),
  87. "Could not approve DAI sell"
  88. );
  89.  
  90. uint256 tokenBought = exchangeA.tokenToTokenSwapInput(
  91. _amount,
  92. 1,
  93. 1,
  94. deadline,
  95. BAT_ADDRESS
  96. );
  97.  
  98. require(
  99. bat.approve(address(exchangeB), tokenBought),
  100. "Could not approve DAI sell"
  101. );
  102.  
  103. // Selling ETH at Exchange B
  104. uint256 daiBought = exchangeB.tokenToTokenSwapInput(
  105. tokenBought,
  106. 1,
  107. 1,
  108. deadline,
  109. DAI_ADDRESS
  110. );
  111.  
  112. require(daiBought > _amount, "Did not profit");
  113.  
  114. // Repay loan
  115. uint256 totalDebt = _amount.add(_fee);
  116. transferFundsBackToPoolInternal(_reserve, totalDebt);
  117. }
  118.  
  119. function getDeadline() internal returns (uint256) {
  120. return now + 3000;
  121. }
  122.  
  123. /*
  124. * Increase the price difference between both exchanges
  125. * so there can still be arbitrage oportunities in the
  126. * example
  127. */
  128. function imbalanceExchanges(uint256 _amount) external {
  129. ERC20 dai = ERC20(DAI_ADDRESS);
  130. require(
  131. dai.transferFrom(msg.sender, address(this), _amount),
  132. "Could not tranfer DAI"
  133. );
  134. require(
  135. dai.approve(address(exchangeB), _amount),
  136. "Could not approve DAI sell"
  137. );
  138.  
  139. exchangeB.tokenToEthTransferInput(
  140. _amount,
  141. 1,
  142. getDeadline(),
  143. msg.sender
  144. );
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement