DEX & UNISWAP
Decentralized Exchanges
Uniswap under the Hood
Accessing Decentralized Exchanges
2/4/2021
GRAPH OF THE DAY
TOTAL VALUE LOCKED IN DEFI
Total Value Locked in DeFi protocols (TVL) now
stands above $25B, an incredible 2500% growth Y/Y.
Similarly, the number of DeFi users has surpassed 1.2M
Protocols like Uniswap and Compound claim 200-500K
users, with most other DeFi apps between 25-50K users.
DEX VOLUME
UNISWAP
Most popular decentralized exchange
August 30 surpassed major cryptocurrency exchange Coinbase in volume
after its users traded $426 million worth of cryptocurrencies in a single day
UNISWAP Uniswap under the hood
ERC20 TOKEN SWAP ON UNSWAP
UNISWAP EXCHANGE CONTRACT
MECHANICS OF SWAP
UNISWAP FACTORY CONTRACT
UNISWAP FACTORY CONTRACT IN
DETAIL
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);
}
UNISWAP PAIR CONTRACT
UNISWAP PAIR CONTRACT (1/3)
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;
UNISWAP PAIR CONTRACT (2/3)
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;
UNISWAP PAIR CONTRACT (3/3)
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
);
UNISWAP ROUTER CONTRACT
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity()
function addLiquidityETH()
function removeLiquidity()
function removeLiquidityETH()
function removeLiquidityWithPermit()
function removeLiquidityETHWithPermit()
function swapExactTokensForTokens()
function swapTokensForExactTokens()
function swapExactETHForTokens()
function swapTokensForExactETH()
function swapExactTokensForETH()
function swapETHForExactTokens()
function quote(uint amountA, uint reserveA, uint reserveB)
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut)
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut)
function getAmountsOut(uint amountIn, address[] calldata path)
function getAmountsIn(uint amountOut, address[] calldata path)
}
CHECK ACTIVITY OF THE ROUTER
CONTRACT
CHECK TRANSACTION EXECUTED
BY ROUTER
INTERACTING WITH
DECENTRALIZED EXCHANGE
ACCESSING UNISWAP DEX
WEB UI
API
SDK/Libraries
Smart Contracts
WEB UI (DEMO)
WEB UI (DEMO PART 2)
API ACCESS (DEMO)
JAVASCRIPT ACCESS DEMO
CALL FROM A SMART CONTRACT –
GET PRICE
IUniswapV2Router02 private uniswapV2router;
uniswapV2router =
IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
function getEstimatedEthForToken(address token, uint256 tokenAmount) public
view returns (uint256) {
if (token!=address(0) && tokenAmount>0) {
address[] memory path = getPathToETH(token);
uint[] memory r = uniswapV2router.getAmountsOut(tokenAmount, path);
return r[1];
} else {
return 0;
}
}
SWAP TOKEN TO ETH
// amountOutMin must be retrieved from an oracle of some kind
address[] memory path = new address[](2);
path[0] = address(DAI);
path[1] = UniswapV2Router02.WETH();
UniswapV2Router02.swapExactTokensForETH(amountIn, amountOutMin, path,
msg.sender, block.timestamp)
CALL FROM A SMART CONTRACT -
SELL
event SellToken(address token, uint256 tokenAmount);
function convertTokenToEth(address token, uint256 tokenAmount) public onlyOwner {
IERC20 erc20Token = IERC20(token);
emit SellToken(token, tokenAmount);
require(getTokenBalance(token)>=tokenAmount && token!=address(0), "Token balance");
require(erc20Token.approve(address(uniswapV2router), 0), "Approve failed");
require(erc20Token.approve(address(uniswapV2router), tokenAmount), "Approve failed");
uint256 deadline = block.timestamp + 300; // transaction expires in 300 seconds (5 minutes)
uint256 minETHAmountToReceieve = (getEstimatedEthForToken(token, tokenAmount)*8)/10; //20%
sleepage is OK
uniswapV2router.swapExactTokensForETH(tokenAmount, minETHAmountToReceieve,
getPathToETH(token), address(this), deadline);
}
QUESTIONS?
OUR NEXT MEETUP
STAY IN TOUCH
Gene Leybzon https://www.linkedin.com/in/leybzon/
https://www.meetup.com/members/90744
20/
https://www.leybzon.com

Dex and Uniswap

  • 1.
    DEX & UNISWAP DecentralizedExchanges Uniswap under the Hood Accessing Decentralized Exchanges 2/4/2021
  • 2.
  • 3.
    TOTAL VALUE LOCKEDIN DEFI Total Value Locked in DeFi protocols (TVL) now stands above $25B, an incredible 2500% growth Y/Y. Similarly, the number of DeFi users has surpassed 1.2M Protocols like Uniswap and Compound claim 200-500K users, with most other DeFi apps between 25-50K users.
  • 4.
  • 5.
    UNISWAP Most popular decentralizedexchange August 30 surpassed major cryptocurrency exchange Coinbase in volume after its users traded $426 million worth of cryptocurrencies in a single day
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
    UNISWAP FACTORY CONTRACTIN DETAIL 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); }
  • 12.
  • 13.
    UNISWAP PAIR CONTRACT(1/3) 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;
  • 14.
    UNISWAP PAIR CONTRACT(2/3) 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;
  • 15.
    UNISWAP PAIR CONTRACT(3/3) 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 );
  • 16.
    UNISWAP ROUTER CONTRACT interfaceIUniswapV2Router01 { function factory() external pure returns (address); function WETH() external pure returns (address); function addLiquidity() function addLiquidityETH() function removeLiquidity() function removeLiquidityETH() function removeLiquidityWithPermit() function removeLiquidityETHWithPermit() function swapExactTokensForTokens() function swapTokensForExactTokens() function swapExactETHForTokens() function swapTokensForExactETH() function swapExactTokensForETH() function swapETHForExactTokens() function quote(uint amountA, uint reserveA, uint reserveB) function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) function getAmountsOut(uint amountIn, address[] calldata path) function getAmountsIn(uint amountOut, address[] calldata path) }
  • 17.
    CHECK ACTIVITY OFTHE ROUTER CONTRACT
  • 18.
  • 19.
  • 20.
    ACCESSING UNISWAP DEX WEBUI API SDK/Libraries Smart Contracts
  • 21.
  • 22.
    WEB UI (DEMOPART 2)
  • 23.
  • 24.
  • 25.
    CALL FROM ASMART CONTRACT – GET PRICE IUniswapV2Router02 private uniswapV2router; uniswapV2router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D); function getEstimatedEthForToken(address token, uint256 tokenAmount) public view returns (uint256) { if (token!=address(0) && tokenAmount>0) { address[] memory path = getPathToETH(token); uint[] memory r = uniswapV2router.getAmountsOut(tokenAmount, path); return r[1]; } else { return 0; } }
  • 26.
    SWAP TOKEN TOETH // amountOutMin must be retrieved from an oracle of some kind address[] memory path = new address[](2); path[0] = address(DAI); path[1] = UniswapV2Router02.WETH(); UniswapV2Router02.swapExactTokensForETH(amountIn, amountOutMin, path, msg.sender, block.timestamp)
  • 27.
    CALL FROM ASMART CONTRACT - SELL event SellToken(address token, uint256 tokenAmount); function convertTokenToEth(address token, uint256 tokenAmount) public onlyOwner { IERC20 erc20Token = IERC20(token); emit SellToken(token, tokenAmount); require(getTokenBalance(token)>=tokenAmount && token!=address(0), "Token balance"); require(erc20Token.approve(address(uniswapV2router), 0), "Approve failed"); require(erc20Token.approve(address(uniswapV2router), tokenAmount), "Approve failed"); uint256 deadline = block.timestamp + 300; // transaction expires in 300 seconds (5 minutes) uint256 minETHAmountToReceieve = (getEstimatedEthForToken(token, tokenAmount)*8)/10; //20% sleepage is OK uniswapV2router.swapExactTokensForETH(tokenAmount, minETHAmountToReceieve, getPathToETH(token), address(this), deadline); }
  • 28.
  • 29.
  • 30.
    STAY IN TOUCH GeneLeybzon https://www.linkedin.com/in/leybzon/ https://www.meetup.com/members/90744 20/ https://www.leybzon.com

Editor's Notes

  • #3 https://www.theblockcrypto.com/data/on-chain-metrics/ethereum
  • #4 https://defipulse.com
  • #5 https://www.theblockcrypto.com/linked/91539/januarys-decentralized-exchange-volumes-are-on-track-to-reach-record-highs Uniswap currently makes up 44.85% of January’s trading volume, with SushiSwap (22.19%) and Curve (12.98%) in second and third place, respectively.
  • #6 https://mpra.ub.uni-muenchen.de/103925/1/MPRA_paper_103925.pdf https://info.uniswap.org/home
  • #8 https://blog.oceanprotocol.com/the-developers-guide-to-uniswap-48fcf6e9ee1e
  • #9 https://blog.oceanprotocol.com/the-developers-guide-to-uniswap-48fcf6e9ee1e
  • #10 https://uniswap.org/docs/v2/core-concepts/swaps/
  • #11 https://uniswap.org/docs/v2/smart-contracts/factory/ https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2Factory.sol
  • #12 https://uniswap.org/docs/v2/smart-contracts/factory/ https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2Factory.sol
  • #13 https://uniswap.org/docs/v2/smart-contracts/router02/
  • #14 https://uniswap.org/docs/v2/smart-contracts/pair/
  • #15 https://uniswap.org/docs/v2/smart-contracts/pair/
  • #16 https://uniswap.org/docs/v2/smart-contracts/pair/
  • #17 Note: routers are stateless and do not hold token balances, they can be replaced https://uniswap.org/docs/v2/smart-contracts/router02/#weth https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/UniswapV2Router02.sol
  • #18 https://etherscan.io/address/0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
  • #19 https://etherscan.io/tx/0x2f87f81ff330a4a89ef0c561f869e22adc0bf94699ffb073e9b0ff1e82fbae76
  • #22 https://app.uniswap.org/#/swap
  • #23 https://info.uniswap.org/pair/0xB6909B960DbbE7392D405429eB2b3649752b4838
  • #24 https://thegraph.com/explorer/subgraph/uniswap/uniswap-v2
  • #25 Documents\NodeJS\uniswap>node get_midprice.mjs 605.28682 0.0016521093
  • #26 https://uniswap.org/docs/v2/smart-contract-integration/quick-start/
  • #27 https://uniswap.org/docs/v2/smart-contract-integration/trading-from-a-smart-contract/
  • #28 https://uniswap.org/docs/v2/smart-contract-integration/quick-start/
  • #30 LINK – decentralized oracle network