DEV Community

Cover image for [UniswapV2] FlashSwap Walkthrough
MOYED
MOYED

Posted on

[UniswapV2] FlashSwap Walkthrough

Sources

Using the new Uniswap v2 in your contracts

Router02 | Uniswap


Pay

Allow users to pay ETH instead of DAI to service.

function pay(uint paymentAmountInDai) public payable {
      if (msg.value > 0) {
          convertEthToDai(paymentAmountInDai);
      } else {
          require(daiToken.transferFrom(msg.sender, address(this), paymentAmountInDai);
      }
      // do something with that DAI
      ...
}
Enter fullscreen mode Exit fullscreen mode

convertEthToDai

Paramters

  • daiAmount is amount of Dai which user is going to pay.
  • deadline is time limit for transaction

  1. path is array of token address that involves in exchage. For example, ETH can be first traded to WETH, then WETH traded to USDC, then USDC to Dai (for example).

    address[] memory path = new address[](2);
    

    → How do you know the number of path my trade takes?

  2. As we convert ETH to DAI, first value of path is address of WETH, second value of path is address of DAI. Be careful that we deal with WETH, not ETH itself.

    path[0] = uniswapRouter.WETH();
    path[1] = daiToken;
    
  3. Swap ETH for exact amount of tokens. ETH are given by msg.value .

    uniswapRouter.swapETHForExactTokens.value(msg.value)(daiAmount, path, address(this), deadline);
    

    → syntax?

  4. Refund leftover ETH to user(msg.sender ).

    msg.sender.call.value(address(this).balance)("");
    
    

getEstimatedETHforDAI

Calculate the amount of ETH user has to pay.

Use getAmountsIn function to calculate the optimal input amount for given DAI amount of daiAmount .

function getEstimatedETHforDAI(uint daiAmount) public view returns (uint[] memory) {
    address[] memory path = new address[](2);
    path[0] = uniswapRouter.WETH();
    path[1] = multiDaiKovan;

    return uniswapRouter.getAmountsIn(daiAmount, path);
}
Enter fullscreen mode Exit fullscreen mode

Example

pragma solidity 0.7.1;

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 = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;

  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 {}
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)