Code

pragma solidity =0.5.16;

import './interfaces/IUniswapV2Factory.sol';
import './UniswapV2Pair.sol';

contract UniswapV2Factory is IUniswapV2Factory {
    address public feeTo;
    address public feeToSetter;

    mapping(address => mapping(address => address)) public getPair;
    address[] public allPairs;

    event PairCreated(address indexed token0, address indexed token1, address pair, uint);

    constructor(address _feeToSetter) public {
        feeToSetter = _feeToSetter;
    }

    function allPairsLength() external view returns (uint) {
        return allPairs.length;
    }

    function createPair(address tokenA, address tokenB) external returns (address pair) {
        require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
        (address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
        require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
        require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient
        bytes memory bytecode = type(UniswapV2Pair).creationCode;
        bytes32 salt = keccak256(abi.encodePacked(token0, token1));
        assembly {
            pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
        }
        IUniswapV2Pair(pair).initialize(token0, token1);
        getPair[token0][token1] = pair;
        getPair[token1][token0] = pair; // populate mapping in the reverse direction
        allPairs.push(pair);
        emit PairCreated(token0, token1, pair, allPairs.length);
    }

    function setFeeTo(address _feeTo) external {
        require(msg.sender == feeToSetter, 'UniswapV2: FORBIDDEN');
        feeTo = _feeTo;
    }

    function setFeeToSetter(address _feeToSetter) external {
        require(msg.sender == feeToSetter, 'UniswapV2: FORBIDDEN');
        feeToSetter = _feeToSetter;
    }
}

Address

在以太坊主网上,UniswapV2Factory部署在0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f

Event

PairCreated

event PairCreated(address indexed token0, address indexed token1, address pair, uint);

每次createPair时触发该事件

  • 按排序顺序保证 token0 严格小于 token1
  • 对于创建的第一个对, uint 日志是1,对于第二对,日志是2以此类推…(seeallPairs/getPair)

Read-Only Functions

getPair

function getPair(address tokenA, address tokenB) external view returns(address pair);

如果tokenAtokenB交易对已经创建,返回交易对的地址;否则返回address(0)

  • tokenAtokenB顺序可互换
  • 还可以通过 SDK 确定性地计算对地址。

allPairs

function allPairs(uint) external view returns (address pair);

返回通过 Factory 创建的第 n 个 Pair 地址,如果尚未创建足够多的 Pair,返回address(0)

  • 第一个创建的 Pair 对应0,第二个创建的 Pair 对应1,以此类推…

allPairsLength

function allPairsLength() external view returns(uint);

返回迄今为止通过 Factory 创建的 Pairs 总数

feeTo

function feeTo() external view returns (address);

参阅费用计算

feeToSetter

function feeToSetter() external view returns (address);

允许去修改 feeTo 的地址

State-Changing Function

createPair

function createPair(address tokenA, address tokenB) external returns (address pair);

创建一个 pair 如果tokenAtokenB还没有被创建

Interface

import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';
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 biew 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);
}