populate getExchange bidirectionally

This commit is contained in:
Noah Zinsmeister
2020-01-24 12:58:21 -05:00
parent 7c6d60dcc1
commit 1f5c9aa9e7
2 changed files with 18 additions and 23 deletions

View File

@ -8,8 +8,8 @@ contract UniswapV2Factory is IUniswapV2Factory {
address public feeTo;
address public feeToSetter;
mapping(address => mapping(address => address)) private _getExchange;
address[] public exchanges;
mapping(address => mapping(address => address)) public getExchange;
address[] public allExchanges;
event ExchangeCreated(address indexed token0, address indexed token1, address exchange, uint);
@ -17,33 +17,26 @@ contract UniswapV2Factory is IUniswapV2Factory {
feeToSetter = _feeToSetter;
}
function sortTokens(address tokenA, address tokenB) public pure returns (address token0, address token1) {
require(tokenA != tokenB, 'UniswapV2: SAME_ADDRESS');
require(tokenA != address(0) && tokenB != address(0), 'UniswapV2: ZERO_ADDRESS');
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
}
function getExchange(address tokenA, address tokenB) external view returns (address exchange) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
exchange = _getExchange[token0][token1];
}
function exchangesCount() external view returns (uint) {
return exchanges.length;
function allExchangesLength() external view returns (uint) {
return allExchanges.length;
}
function createExchange(address tokenA, address tokenB) external returns (address exchange) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
require(_getExchange[token0][token1] == address(0), 'UniswapV2: EXCHANGE_EXISTS');
require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
require(getExchange[token0][token1] == address(0), 'UniswapV2: EXCHANGE_EXISTS');
require(getExchange[token1][token0] == address(0), 'UniswapV2: EXCHANGE_EXISTS');
bytes memory exchangeBytecode = type(UniswapV2Exchange).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
assembly {
exchange := create2(0, add(exchangeBytecode, 32), mload(exchangeBytecode), salt)
}
IUniswapV2Exchange(exchange).initialize(token0, token1);
_getExchange[token0][token1] = exchange;
exchanges.push(exchange);
emit ExchangeCreated(token0, token1, exchange, exchanges.length);
getExchange[token0][token1] = exchange;
getExchange[token1][token0] = exchange;
allExchanges.push(exchange);
emit ExchangeCreated(token0, token1, exchange, allExchanges.length);
}
function setFeeTo(address _feeTo) external {

View File

@ -6,10 +6,12 @@ interface IUniswapV2Factory {
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function sortTokens(address tokenA, address tokenB) external pure returns (address token0, address token1);
function getExchange(address tokenA, address tokenB) external view returns (address exchange);
function exchanges(uint) external view returns (address exchange);
function exchangesCount() external view returns (uint);
function allExchanges(uint) external view returns (address exchange);
function allExchangesLength() external view returns (uint);
function createExchange(address tokenA, address tokenB) external returns (address exchange);
function setFeeTo(address) external;
function setFeeToSetter(address) external;
}