populate getExchange bidirectionally
This commit is contained in:
@ -8,8 +8,8 @@ contract UniswapV2Factory is IUniswapV2Factory {
|
|||||||
address public feeTo;
|
address public feeTo;
|
||||||
address public feeToSetter;
|
address public feeToSetter;
|
||||||
|
|
||||||
mapping(address => mapping(address => address)) private _getExchange;
|
mapping(address => mapping(address => address)) public getExchange;
|
||||||
address[] public exchanges;
|
address[] public allExchanges;
|
||||||
|
|
||||||
event ExchangeCreated(address indexed token0, address indexed token1, address exchange, uint);
|
event ExchangeCreated(address indexed token0, address indexed token1, address exchange, uint);
|
||||||
|
|
||||||
@ -17,33 +17,26 @@ contract UniswapV2Factory is IUniswapV2Factory {
|
|||||||
feeToSetter = _feeToSetter;
|
feeToSetter = _feeToSetter;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortTokens(address tokenA, address tokenB) public pure returns (address token0, address token1) {
|
function allExchangesLength() external view returns (uint) {
|
||||||
require(tokenA != tokenB, 'UniswapV2: SAME_ADDRESS');
|
return allExchanges.length;
|
||||||
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 createExchange(address tokenA, address tokenB) external returns (address exchange) {
|
function createExchange(address tokenA, address tokenB) external returns (address exchange) {
|
||||||
(address token0, address token1) = sortTokens(tokenA, tokenB);
|
require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
|
||||||
require(_getExchange[token0][token1] == address(0), 'UniswapV2: EXCHANGE_EXISTS');
|
(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;
|
bytes memory exchangeBytecode = type(UniswapV2Exchange).creationCode;
|
||||||
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
|
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
|
||||||
assembly {
|
assembly {
|
||||||
exchange := create2(0, add(exchangeBytecode, 32), mload(exchangeBytecode), salt)
|
exchange := create2(0, add(exchangeBytecode, 32), mload(exchangeBytecode), salt)
|
||||||
}
|
}
|
||||||
IUniswapV2Exchange(exchange).initialize(token0, token1);
|
IUniswapV2Exchange(exchange).initialize(token0, token1);
|
||||||
_getExchange[token0][token1] = exchange;
|
getExchange[token0][token1] = exchange;
|
||||||
exchanges.push(exchange);
|
getExchange[token1][token0] = exchange;
|
||||||
emit ExchangeCreated(token0, token1, exchange, exchanges.length);
|
allExchanges.push(exchange);
|
||||||
|
emit ExchangeCreated(token0, token1, exchange, allExchanges.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setFeeTo(address _feeTo) external {
|
function setFeeTo(address _feeTo) external {
|
||||||
|
|||||||
@ -6,10 +6,12 @@ interface IUniswapV2Factory {
|
|||||||
function feeTo() external view returns (address);
|
function feeTo() external view returns (address);
|
||||||
function feeToSetter() 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 getExchange(address tokenA, address tokenB) external view returns (address exchange);
|
||||||
function exchanges(uint) external view returns (address exchange);
|
function allExchanges(uint) external view returns (address exchange);
|
||||||
function exchangesCount() external view returns (uint);
|
function allExchangesLength() external view returns (uint);
|
||||||
|
|
||||||
function createExchange(address tokenA, address tokenB) external returns (address exchange);
|
function createExchange(address tokenA, address tokenB) external returns (address exchange);
|
||||||
|
|
||||||
|
function setFeeTo(address) external;
|
||||||
|
function setFeeToSetter(address) external;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user