// SPDX-License-Identifier: GPL-3.0pragmasolidity =0.6.12;// a library for performing various math operationslibraryOreoSwapMath{
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)// reference: https://github.com/abdk-consulting/abdk-libraries-solidity/blob/master/ABDKMath64x64.sol#L687functionsqrt(uint x) internalpurereturns (uint z) {
if (x ==0) return0;
uint xx = x;
uint r =1;
if (xx >=0x100000000000000000000000000000000) {
xx >>=128;
r <<=64;
}
if (xx >=0x10000000000000000) {
xx >>=64;
r <<=32;
}
if (xx >=0x100000000) {
xx >>=32;
r <<=16;
}
if (xx >=0x10000) {
xx >>=16;
r <<=8;
}
if (xx >=0x100) {
xx >>=8;
r <<=4;
}
if (xx >=0x10) {
xx >>=4;
r <<=2;
}
if (xx >=0x8) {
r <<=1;
}
r = (r + x / r) >>1;
r = (r + x / r) >>1;
r = (r + x / r) >>1;
r = (r + x / r) >>1;
r = (r + x / r) >>1;
r = (r + x / r) >>1;
r = (r + x / r) >>1; // Seven iterations should be enoughuint r1 = x / r;
return (r < r1 ? r : r1);
}
}
Contract Source Code
File 6 of 8: OreoSwapPair.sol
// SPDX-License-Identifier: GPL-3.0pragmasolidity =0.6.12;import'./OreoSwapLP.sol';
import'./libraries/OreoSwapMath.sol';
import'./libraries/UQ112x112.sol';
import'./interfaces/IOreoSwapBEP20.sol';
import'./interfaces/IOreoSwapFactory.sol';
import'./interfaces/IOreoSwapCallee.sol';
contractOreoSwapPairisOreoSwapLP{
usingOreoSwapSafeMathforuint;
usingUQ112x112foruint224;
uintpublicconstant MINIMUM_LIQUIDITY =10**3;
bytes4privateconstant SELECTOR =bytes4(keccak256(bytes('transfer(address,uint256)')));
addresspublic factory;
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 eventuintprivate unlocked =1;
modifierlock() {
require(unlocked ==1, 'OreoSwapPair::lock::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))), 'OreoSwapPair::_safeTransfer::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, 'OreoSwapPair::initialize::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), 'OreoSwapPair::_update::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 8/25 of the growth in sqrt(k)function_mintFee(uint112 _reserve0, uint112 _reserve1) privatereturns (bool feeOn) {
address feeTo = IOreoSwapFactory(factory).feeTo();
feeOn = feeTo !=address(0);
uint _kLast = kLast; // gas savingsif (feeOn) {
if (_kLast !=0) {
uint rootK = OreoSwapMath.sqrt(uint(_reserve0).mul(_reserve1));
uint rootKLast = OreoSwapMath.sqrt(_kLast);
if (rootK > rootKLast) {
uint numerator = totalSupply.mul(rootK.sub(rootKLast)).mul(8);
uint denominator = rootK.mul(17).add(rootKLast.mul(8));
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 = IOreoSwapBEP20(token0).balanceOf(address(this));
uint balance1 = IOreoSwapBEP20(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 = OreoSwapMath.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);
_mint(address(0), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
} else {
liquidity = OreoSwapMath.min(amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1);
}
require(liquidity >0, 'OreoSwapPair::mint::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 = IOreoSwapBEP20(_token0).balanceOf(address(this));
uint balance1 = IOreoSwapBEP20(_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, 'OreoSwapPair::burn::INSUFFICIENT_LIQUIDITY_BURNED');
_burn(address(this), liquidity);
_safeTransfer(_token0, to, amount0);
_safeTransfer(_token1, to, amount1);
balance0 = IOreoSwapBEP20(_token0).balanceOf(address(this));
balance1 = IOreoSwapBEP20(_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, 'OreoSwapPair::swap::INSUFFICIENT_OUTPUT_AMOUNT');
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savingsrequire(amount0Out < _reserve0 && amount1Out < _reserve1, 'OreoSwapPair::swap::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, 'OreoSwapPair::swap::INVALID_TO');
if (amount0Out >0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokensif (amount1Out >0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokensif (data.length>0) IOreoSwapCallee(to).OreoSwapCall(msg.sender, amount0Out, amount1Out, data);
balance0 = IOreoSwapBEP20(_token0).balanceOf(address(this));
balance1 = IOreoSwapBEP20(_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, 'OreoSwapPair::swap::INSUFFICIENT_INPUT_AMOUNT');
{ // scope for reserve{0,1}Adjusted, avoids stack too deep errorsuint balance0Adjusted = balance0.mul(10000).sub(amount0In.mul(25));
uint balance1Adjusted = balance1.mul(10000).sub(amount1In.mul(25));
require(balance0Adjusted.mul(balance1Adjusted) >=uint(_reserve0).mul(_reserve1).mul(10000**2), 'OreoSwapPair::swap::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, IOreoSwapBEP20(_token0).balanceOf(address(this)).sub(reserve0));
_safeTransfer(_token1, to, IOreoSwapBEP20(_token1).balanceOf(address(this)).sub(reserve1));
}
// force reserves to match balancesfunctionsync() externallock{
_update(IOreoSwapBEP20(token0).balanceOf(address(this)), IOreoSwapBEP20(token1).balanceOf(address(this)), reserve0, reserve1);
}
}
Contract Source Code
File 7 of 8: OreoSwapSafeMath.sol
// SPDX-License-Identifier: GPL-3.0pragmasolidity =0.6.12;// a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)libraryOreoSwapSafeMath{
functionadd(uint x, uint y) internalpurereturns (uint z) {
require((z = x + y) >= x, 'OreoSwapSafeMath::add::ds-math-add-overflow');
}
functionsub(uint x, uint y) internalpurereturns (uint z) {
require((z = x - y) <= x, 'OreoSwapSafeMath::sub::ds-math-sub-underflow');
}
functionmul(uint x, uint y) internalpurereturns (uint z) {
require(y ==0|| (z = x * y) / y == x, 'OreoSwapSafeMath::mul::ds-math-mul-overflow');
}
}
Contract Source Code
File 8 of 8: UQ112x112.sol
// SPDX-License-Identifier: GPL-3.0pragmasolidity =0.6.12;// 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);
}
}