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');
}
}
pragmasolidity =0.5.16;import"./interfaces/ISynthPair.sol";
import"./SynthERC20.sol";
import"./libraries/Math.sol";
import"./libraries/UQ112x112.sol";
import"./interfaces/IERC20.sol";
import"./interfaces/ISynthFactory.sol";
import"./interfaces/ISynthCallee.sol";
contractSynthPairisISynthPair, SynthERC20{
usingSafeMathforuint256;
usingUQ112x112foruint224;
uint256publicconstant 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 getReservesuint256public price0CumulativeLast;
uint256public price1CumulativeLast;
uint256public kLast; // reserve0 * reserve1, as of immediately after the most recent liquidity eventuint256private unlocked =1;
modifierlock() {
require(unlocked ==1, "Synth: 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, uint256 value) private{
(bool success, bytesmemory data) = token.call(
abi.encodeWithSelector(SELECTOR, to, value)
);
require(
success && (data.length==0||abi.decode(data, (bool))),
"Synth: TRANSFER_FAILED"
);
}
eventMint(addressindexed sender, uint256 amount0, uint256 amount1);
eventBurn(addressindexed sender,
uint256 amount0,
uint256 amount1,
addressindexed to
);
eventSwap(addressindexed sender,
uint256 amount0In,
uint256 amount1In,
uint256 amount0Out,
uint256 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, "Synth: FORBIDDEN"); // sufficient check
token0 = _token0;
token1 = _token1;
}
// update reserves and, on the first call per block, price accumulatorsfunction_update(uint256 balance0,
uint256 balance1,
uint112 _reserve0,
uint112 _reserve1
) private{
require(
balance0 <=uint112(-1) && balance1 <=uint112(-1),
"Synth: 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 +=uint256(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) *
timeElapsed;
price1CumulativeLast +=uint256(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 1/6th of the growth in sqrt(k)function_mintFee(uint112 _reserve0,
uint112 _reserve1
) privatereturns (bool feeOn) {
address feeTo = ISynthFactory(factory).feeTo();
feeOn = feeTo !=address(0);
uint256 _kLast = kLast; // gas savingsif (feeOn) {
if (_kLast !=0) {
uint256 rootK = Math.sqrt(uint256(_reserve0).mul(_reserve1));
uint256 rootKLast = Math.sqrt(_kLast);
if (rootK > rootKLast) {
uint256 numerator = totalSupply.mul(rootK.sub(rootKLast));
uint256 denominator = (rootK.mul(15) /10).add(rootKLast);
uint256 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 (uint256 liquidity) {
(uint112 _reserve0, uint112 _reserve1, ) = getReserves(); // gas savingsuint256 balance0 = IERC20(token0).balanceOf(address(this));
uint256 balance1 = IERC20(token1).balanceOf(address(this));
uint256 amount0 = balance0.sub(_reserve0);
uint256 amount1 = balance1.sub(_reserve1);
bool feeOn = _mintFee(_reserve0, _reserve1);
uint256 _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, "Synth: INSUFFICIENT_LIQUIDITY_MINTED");
_mint(to, liquidity);
_update(balance0, balance1, _reserve0, _reserve1);
if (feeOn) kLast =uint256(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 (uint256 amount0, uint256 amount1) {
(uint112 _reserve0, uint112 _reserve1, ) = getReserves(); // gas savingsaddress _token0 = token0; // gas savingsaddress _token1 = token1; // gas savingsuint256 balance0 = IERC20(_token0).balanceOf(address(this));
uint256 balance1 = IERC20(_token1).balanceOf(address(this));
uint256 liquidity = balanceOf[address(this)];
bool feeOn = _mintFee(_reserve0, _reserve1);
uint256 _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,
"Synth: 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 =uint256(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(uint256 amount0Out,
uint256 amount1Out,
address to,
bytescalldata data
) externallock{
require(
amount0Out >0|| amount1Out >0,
"Synth: INSUFFICIENT_OUTPUT_AMOUNT"
);
(uint112 _reserve0, uint112 _reserve1, ) = getReserves(); // gas savingsrequire(
amount0Out < _reserve0 && amount1Out < _reserve1,
"Synth: INSUFFICIENT_LIQUIDITY"
);
uint256 balance0;
uint256 balance1;
{
// scope for _token{0,1}, avoids stack too deep errorsaddress _token0 = token0;
address _token1 = token1;
require(to != _token0 && to != _token1, "Synth: INVALID_TO");
if (amount0Out >0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokensif (amount1Out >0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokensif (data.length>0)
ISynthCallee(to).SynthCall(
msg.sender,
amount0Out,
amount1Out,
data
);
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
}
uint256 amount0In = balance0 > _reserve0 - amount0Out
? balance0 - (_reserve0 - amount0Out)
: 0;
uint256 amount1In = balance1 > _reserve1 - amount1Out
? balance1 - (_reserve1 - amount1Out)
: 0;
require(
amount0In >0|| amount1In >0,
"Synth: 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) >=uint256(_reserve0).mul(_reserve1).mul(10000**2),
"Synth: 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);
}
}