pragmasolidity =0.5.16;// a library for performing various math operationslibraryMath{
functionmin(uint x, uint y) internalpurereturns (uint z) {
z = x < y ? x : y;
}
// babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)functionsqrt(uint y) internalpurereturns (uint z) {
if (y >3) {
z = y;
uint x = y /2+1;
while (x < z) {
z = x;
x = (y / x + x) /2;
}
} elseif (y !=0) {
z =1;
}
}
}
Contract Source Code
File 6 of 9: SafeMath.sol
pragmasolidity =0.5.16;// a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)librarySafeMath{
functionadd(uint x, uint y) internalpurereturns (uint z) {
require((z = x + y) >= x, 'ds-math-add-overflow');
}
functionsub(uint x, uint y) internalpurereturns (uint z) {
require((z = x - y) <= x, 'ds-math-sub-underflow');
}
functionmul(uint x, uint y) internalpurereturns (uint z) {
require(y ==0|| (z = x * y) / y == x, 'ds-math-mul-overflow');
}
}
Contract Source Code
File 7 of 9: TempleUniswapV2Pair.sol
pragmasolidity =0.5.16;// SPDX-License-Identifier: GPL-3.0-or-laterimport'@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';
import'@uniswap/v2-core/contracts/UniswapV2ERC20.sol';
import'@uniswap/v2-core/contracts/libraries/Math.sol';
import'@uniswap/v2-core/contracts/libraries/UQ112x112.sol';
import'@uniswap/v2-core/contracts/interfaces/IERC20.sol';
import'@uniswap/v2-core/contracts/interfaces/IUniswapV2Callee.sol';
contractTempleUniswapV2PairisUniswapV2ERC20{
usingSafeMathforuint;
usingUQ112x112foruint224;
uintpublicconstant MINIMUM_LIQUIDITY =10**3;
bytes4privateconstant SELECTOR =bytes4(keccak256(bytes('transfer(address,uint256)')));
// Change from standard uniswap. Owner can only change the single allowed routeraddresspublic owner;
addresspublic token0;
addresspublic token1;
uint112private reserve0; // uses single storage slot, accessible via getReservesuint112private reserve1; // uses single storage slot, accessible via getReservesuint32private blockTimestampLast; // uses single storage slot, accessible via getReservesuintpublic price0CumulativeLast;
uintpublic price1CumulativeLast;
uintpublic kLast; // reserve0 * reserve1, as of immediately after the most recent liquidity event// The router which can interact with this pair. required to make AMM private (change// from default uniswap v2)addresspublic router;
uintprivate unlocked =1;
modifierlock() {
require(unlocked ==1, 'UniswapV2: LOCKED');
unlocked =0;
_;
unlocked =1;
}
functiongetReserves() publicviewreturns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast) {
_reserve0 = reserve0;
_reserve1 = reserve1;
_blockTimestampLast = blockTimestampLast;
}
function_safeTransfer(address token, address to, uint value) private{
(bool success, bytesmemory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
require(success && (data.length==0||abi.decode(data, (bool))), 'UniswapV2: TRANSFER_FAILED');
}
eventMint(addressindexed sender, uint amount0, uint amount1);
eventBurn(addressindexed sender, uint amount0, uint amount1, addressindexed to);
eventSwap(addressindexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
addressindexed to
);
eventSync(uint112 reserve0, uint112 reserve1);
// change from default uniswap v2 - set owner and tokens when pair is createdconstructor(address _owner, address _token0, address _token1) public{
owner = _owner;
token0 = _token0;
token1 = _token1;
}
// Owner can change the router which can call the various AMM methodsfunctionsetRouter(address _router) external{
require(msg.sender== owner, 'UniswapV2: FORBIDDEN');
router = _router;
}
// update reserves and, on the first call per block, price accumulatorsfunction_update(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) private{
require(balance0 <=uint112(-1) && balance1 <=uint112(-1), 'UniswapV2: OVERFLOW');
uint32 blockTimestamp =uint32(block.timestamp%2**32);
uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desiredif (timeElapsed >0&& _reserve0 !=0&& _reserve1 !=0) {
// * never overflows, and + overflow is desired
price0CumulativeLast +=uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed;
price1CumulativeLast +=uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed;
}
reserve0 =uint112(balance0);
reserve1 =uint112(balance1);
blockTimestampLast = blockTimestamp;
emit Sync(reserve0, reserve1);
}
// this low-level function should be called from a contract which performs important safety checksfunctionmint(address to) externallockreturns (uint liquidity) {
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savingsuint balance0 = IERC20(token0).balanceOf(address(this));
uint balance1 = IERC20(token1).balanceOf(address(this));
uint amount0 = balance0.sub(_reserve0);
uint amount1 = balance1.sub(_reserve1);
uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFeeif (_totalSupply ==0) {
liquidity = Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);
_mint(address(0), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
} else {
liquidity = Math.min(amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1);
}
require(liquidity >0, 'UniswapV2: INSUFFICIENT_LIQUIDITY_MINTED');
_mint(to, liquidity);
_update(balance0, balance1, _reserve0, _reserve1);
emit Mint(msg.sender, amount0, amount1);
}
// this low-level function should be called from a contract which performs important safety checksfunctionburn(address to) externallockreturns (uint amount0, uint amount1) {
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savingsaddress _token0 = token0; // gas savingsaddress _token1 = token1; // gas savingsuint balance0 = IERC20(_token0).balanceOf(address(this));
uint balance1 = IERC20(_token1).balanceOf(address(this));
uint liquidity = balanceOf[address(this)];
uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
amount0 = liquidity.mul(balance0) / _totalSupply; // using balances ensures pro-rata distribution
amount1 = liquidity.mul(balance1) / _totalSupply; // using balances ensures pro-rata distributionrequire(amount0 >0&& amount1 >0, 'UniswapV2: INSUFFICIENT_LIQUIDITY_BURNED');
_burn(address(this), liquidity);
_safeTransfer(_token0, to, amount0);
_safeTransfer(_token1, to, amount1);
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
_update(balance0, balance1, _reserve0, _reserve1);
emit Burn(msg.sender, amount0, amount1, to);
}
// this low-level function should be called from a contract which performs important safety checksfunctionswap(uint amount0Out, uint amount1Out, address to, bytescalldata data) externallock{
require(amount0Out >0|| amount1Out >0, 'UniswapV2: INSUFFICIENT_OUTPUT_AMOUNT');
require(msg.sender== router, 'UniswapV2: FORBIDDEN'); // access control on swap
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savingsrequire(amount0Out < _reserve0 && amount1Out < _reserve1, 'UniswapV2: INSUFFICIENT_LIQUIDITY');
uint balance0;
uint balance1;
{ // scope for _token{0,1}, avoids stack too deep errorsaddress _token0 = token0;
address _token1 = token1;
require(to != _token0 && to != _token1, 'UniswapV2: INVALID_TO');
if (amount0Out >0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokensif (amount1Out >0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokensif (data.length>0) IUniswapV2Callee(to).uniswapV2Call(msg.sender, amount0Out, amount1Out, data);
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
}
uint amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
uint amount1In = balance1 > _reserve1 - amount1Out ? balance1 - (_reserve1 - amount1Out) : 0;
require(amount0In >0|| amount1In >0, 'UniswapV2: INSUFFICIENT_INPUT_AMOUNT');
{ // scope for reserve{0,1}Adjusted, avoids stack too deep errorsuint balance0Adjusted = balance0.mul(1000).sub(amount0In.mul(3));
uint balance1Adjusted = balance1.mul(1000).sub(amount1In.mul(3));
require(balance0Adjusted.mul(balance1Adjusted) >=uint(_reserve0).mul(_reserve1).mul(1000**2), 'UniswapV2: K');
}
_update(balance0, balance1, _reserve0, _reserve1);
emit Swap(msg.sender, amount0In, amount1In, amount0Out, amount1Out, to);
}
// force balances to match reservesfunctionskim(address to) externallock{
address _token0 = token0; // gas savingsaddress _token1 = token1; // gas savings
_safeTransfer(_token0, to, IERC20(_token0).balanceOf(address(this)).sub(reserve0));
_safeTransfer(_token1, to, IERC20(_token1).balanceOf(address(this)).sub(reserve1));
}
// force reserves to match balancesfunctionsync() externallock{
_update(IERC20(token0).balanceOf(address(this)), IERC20(token1).balanceOf(address(this)), reserve0, reserve1);
}
}
Contract Source Code
File 8 of 9: UQ112x112.sol
pragmasolidity =0.5.16;// a library for handling binary fixed point numbers (https://en.wikipedia.org/wiki/Q_(number_format))// range: [0, 2**112 - 1]// resolution: 1 / 2**112libraryUQ112x112{
uint224constant Q112 =2**112;
// encode a uint112 as a UQ112x112functionencode(uint112 y) internalpurereturns (uint224 z) {
z =uint224(y) * Q112; // never overflows
}
// divide a UQ112x112 by a uint112, returning a UQ112x112functionuqdiv(uint224 x, uint112 y) internalpurereturns (uint224 z) {
z = x /uint224(y);
}
}