From 7ef594bd7d59230afb822b0abdbc318a7971719b Mon Sep 17 00:00:00 2001 From: Abhijit Roy Date: Wed, 8 Jun 2022 21:10:29 +0530 Subject: [PATCH] checkContract added for check of non-destroyed deployed SC --- contracts/UniswapV2Factory.sol | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/contracts/UniswapV2Factory.sol b/contracts/UniswapV2Factory.sol index a66ad8247..64578a4fa 100644 --- a/contracts/UniswapV2Factory.sol +++ b/contracts/UniswapV2Factory.sol @@ -10,20 +10,22 @@ contract UniswapV2Factory is IUniswapV2Factory { mapping(address => mapping(address => address)) public getPair; address[] public allPairs; - event PairCreated(address indexed token0, address indexed token1, address pair, uint); + event PairCreated(address indexed token0, address indexed token1, address pair, uint256); constructor(address _feeToSetter) public { feeToSetter = _feeToSetter; } - function allPairsLength() external view returns (uint) { + function allPairsLength() external view returns (uint256) { 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(token0 != address(0), 'UniswapV2: ZERO_ADDRESS'); + _checkContract(token0); + _checkContract(token1); 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)); @@ -46,4 +48,21 @@ contract UniswapV2Factory is IUniswapV2Factory { require(msg.sender == feeToSetter, 'UniswapV2: FORBIDDEN'); feeToSetter = _feeToSetter; } + + // -- Utility function -- + /** + * Check that the account is an already deployed non-destroyed contract. + * See: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Address.sol#L12 + */ + function _checkContract(address _account) private view returns (bool) { + require(_account != address(0), 'Account cannot be zero address'); + + uint256 size; + // solhint-disable-next-line no-inline-assembly + assembly { + size := extcodesize(_account) + } + require(size != 0, 'Account code size cannot be zero'); + return true; + } }