DEV Community

Cover image for [UniswapV2] UniswapV2ERC20.sol
MOYED
MOYED

Posted on

[UniswapV2] UniswapV2ERC20.sol

This is quite similar to OpenZepplin’s ERC20.sol, but with some unique features.


Variables

nonces

To prevent replay attack.

constructor

Having problem understanding it...

Functions

permit

Give permission.

  1. If time passed deadline, revert.

    require(deadline >= block.timestamp, 'UniswapV2: EXPIRED');
    
  2. Recover address from digest and signature. (Honestly, I don’t get it.)

    bytes32 digest = keccak256(
                abi.encodePacked(
                    '\x19\x01',
                    DOMAIN_SEPARATOR,
                    keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
                )
            );
    address recoveredAddress = ecrecover(digest, v, r, s);
    
  3. If address matches the owner approve it.

    require(recoveredAddress != address(0) && recoveredAddress == owner, 'UniswapV2: INVALID_SIGNATURE');
    _approve(owner, spender, value);
    

Top comments (0)