¡El código fuente de este contrato está verificado!
Metadatos del Contrato
Compilador
0.8.19+commit.7dd6d404
Idioma
Solidity
Código Fuente del Contrato
Archivo 1 de 10: IERC20.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)pragmasolidity ^0.8.0;/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/interfaceIERC20{
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/eventTransfer(addressindexedfrom, addressindexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/eventApproval(addressindexed owner, addressindexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/functiontotalSupply() externalviewreturns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/functionbalanceOf(address account) externalviewreturns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransfer(address to, uint256 amount) externalreturns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/functionallowance(address owner, address spender) externalviewreturns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/functionapprove(address spender, uint256 amount) externalreturns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom, address to, uint256 amount) externalreturns (bool);
}
// SPDX-License-Identifier: GPLv3pragmasolidity 0.8.19;// 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;
}
}
}
// SPDX-License-Identifier: GPLv3pragmasolidity 0.8.19;import"./interfaces/INineInchFactory.sol";
import"./interfaces/INineInchCallee.sol";
import"@openzeppelin/contracts/token/ERC20/IERC20.sol";
import"./interfaces/INineInchERC20.sol";
import"./NineInchERC20.sol";
import"./interfaces/INineInchPair.sol";
import"../libraries/SafeMath.sol";
import"../libraries/UQ112x112.sol";
import"../libraries/Math.sol";
contractNineInchPairisNineInchERC20{
usingSafeMathforuint;
usingUQ112x112foruint224;
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);
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, "NineInch: 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))),
"NineInch: TRANSFER_FAILED"
);
}
constructor() {
factory =msg.sender;
}
// called once by the factory at time of deploymentfunctioninitialize(address _token0, address _token1) external{
require(
_token0 !=address(0) && _token1 !=address(0),
"NineInch: ZERO_ADDRESS"
);
require(_token0 != _token1, "NineInch: IDENTICAL_ADDRESSES");
require(msg.sender== factory, "NineInch: 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 <=type(uint112).max&& balance1 <=type(uint112).max,
"NineInch: 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 1/6th of the growth in sqrt(k)function_mintFee(uint112 _reserve0,
uint112 _reserve1
) privatereturns (bool feeOn) {
address feeTo = INineInchFactory(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 = rootK.mul(7).add(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, "NineInch: 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,
"NineInch: 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,
"NineInch: INSUFFICIENT_OUTPUT_AMOUNT"
);
(uint112 _reserve0, uint112 _reserve1, ) = getReserves(); // gas savingsrequire(
amount0Out < _reserve0 && amount1Out < _reserve1,
"NineInch: 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, "NineInch: INVALID_TO");
if (amount0Out >0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokensif (amount1Out >0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokensif (data.length>0)
INineInchCallee(to).nineInchCallee(
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,
"NineInch: INSUFFICIENT_INPUT_AMOUNT"
);
{
// scope for reserve{0,1}Adjusted, avoids stack too deep errorsuint balance0Adjusted = balance0.mul(10000).sub(amount0In.mul(29));
uint balance1Adjusted = balance1.mul(10000).sub(amount1In.mul(29));
require(
balance0Adjusted.mul(balance1Adjusted) >=uint(_reserve0).mul(_reserve1).mul(10000**2),
"NineInch: 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
);
}
}
Código Fuente del Contrato
Archivo 9 de 10: SafeMath.sol
// SPDX-License-Identifier: GPLv3pragmasolidity 0.8.19;/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/librarySafeMath{
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/functionadd(uint256 a, uint256 b) internalpurereturns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a, uint256 b) internalpurereturns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/functionmul(uint256 a, uint256 b) internalpurereturns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the// benefit is lost if 'b' is also tested.// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522if (a ==0) {
return0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a, uint256 b) internalpurereturns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
require(b >0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't holdreturn c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a, uint256 b) internalpurereturns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
require(b !=0, errorMessage);
return a % b;
}
functionmin(uint256 x, uint256 y) internalpurereturns (uint256 z) {
z = x < y ? x : y;
}
// babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)functionsqrt(uint256 y) internalpurereturns (uint256 z) {
if (y >3) {
z = y;
uint256 x = y /2+1;
while (x < z) {
z = x;
x = (y / x + x) /2;
}
} elseif (y !=0) {
z =1;
}
}
}
Código Fuente del Contrato
Archivo 10 de 10: UQ112x112.sol
// SPDX-License-Identifier: GPLv3pragmasolidity 0.8.19;// 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);
}
}