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 7 of 10: 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');
}
}
// SPDX-License-Identifier: GPL-3.0pragmasolidity =0.5.16;import'./interfaces/ISaitaSwapPair.sol';
import'./SaitaSwapERC20.sol';
import'./libraries/Math.sol';
import'./libraries/UQ112x112.sol';
import'./interfaces/IERC20.sol';
import'./interfaces/ISaitaSwapFactory.sol';
import'./interfaces/ISaitaSwapCallee.sol';
contractSaitaSwapPairisISaitaSwapPair, SaitaSwapERC20{
usingSafeMathforuint;
usingUQ112x112foruint224;
uintpublicconstant MINIMUM_LIQUIDITY =10**3;
bytes4privateconstant SELECTOR =bytes4(keccak256(bytes('transfer(address,uint256)')));
addresspublic factory;
addresspublic token0;
addresspublic token1;
uintpublic SWAP_FEE_NUMERATOR=2;
uintpublic SWAP_FEE_DENOMINATOR=1000;
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 eventuintprivate unlocked =1;
modifierlock() {
require(unlocked ==1, 'SaitaSwap: 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))), 'SaitaSwap: 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);
constructor() public{
factory =msg.sender;
}
// called once by the factory at time of deploymentfunctioninitialize(address _token0, address _token1) external{
require(msg.sender== factory, 'SaitaSwap: FORBIDDEN'); // sufficient check
token0 = _token0;
token1 = _token1;
}
// 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), 'SaitaSwap: 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);
}
// if fee is on, mint liquidity equivalent to the growth in sqrt(k) .function_mintFee(uint112 _reserve0, uint112 _reserve1) privatereturns (bool feeOn) {
address feeTo = ISaitaSwapFactory(factory).feeTo();
feeOn = feeTo !=address(0);
uint _kLast = kLast; // gas savingsif (feeOn) {
if (_kLast !=0) {
uint rootK = Math.sqrt(uint(_reserve0).mul(_reserve1));
uint rootKLast = Math.sqrt(_kLast);
if (rootK > rootKLast) {
uint numerator = totalSupply.mul(rootK.sub(rootKLast));
uint denominator = rootKLast;
uint liquidity = numerator / denominator;
if (liquidity >0) _mint(feeTo, liquidity);
}
}
} elseif (_kLast !=0) {
kLast =0;
}
}
// 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);
bool feeOn = _mintFee(_reserve0, _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, 'SaitaSwap: INSUFFICIENT_LIQUIDITY_MINTED');
_mint(to, liquidity);
_update(balance0, balance1, _reserve0, _reserve1);
if (feeOn) kLast =uint(reserve0).mul(reserve1); // reserve0 and reserve1 are up-to-dateemit 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)];
bool feeOn = _mintFee(_reserve0, _reserve1);
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, 'SaitaSwap: 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);
if (feeOn) kLast =uint(reserve0).mul(reserve1); // reserve0 and reserve1 are up-to-dateemit 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, 'SaitaSwap: INSUFFICIENT_OUTPUT_AMOUNT');
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savingsrequire(amount0Out < _reserve0 && amount1Out < _reserve1, 'SaitaSwap: 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, 'SaitaSwap: INVALID_TO');
if (amount0Out >0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokensif (amount1Out >0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokensif (data.length>0) ISaitaSwapCallee(to).SaitaSwapCall(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, 'SaitaSwap: INSUFFICIENT_INPUT_AMOUNT');
{ // scope for reserve{0,1}Adjusted, avoids stack too deep errorsuint balance0Adjusted = balance0.mul(SWAP_FEE_DENOMINATOR).sub(amount0In.mul(SWAP_FEE_NUMERATOR));
uint balance1Adjusted = balance1.mul(SWAP_FEE_DENOMINATOR).sub(amount1In.mul(SWAP_FEE_NUMERATOR));
require(balance0Adjusted.mul(balance1Adjusted) >=uint(_reserve0).mul(_reserve1).mul(SWAP_FEE_DENOMINATOR**SWAP_FEE_NUMERATOR), 'SaitaSwap: 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 10 of 10: 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);
}
}