StableSwapBaseV1Core
2 removals
Words removed | 2 |
Total words | 2446 |
Words removed (%) | 0.08 |
534 lines
6 additions
Words added | 7 |
Total words | 2451 |
Words added (%) | 0.29 |
537 lines
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT
pragma solidity 0.8.11;
pragma solidity 0.8.11;
import "hardhat/console.sol";
interface erc20 {
interface erc20 {
function totalSupply() external view returns (uint256);
function totalSupply() external view returns (uint256);
function transfer(address recipient, uint amount) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);
function decimals() external view returns (uint8);
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
function symbol() external view returns (string memory);
function balanceOf(address) external view returns (uint);
function balanceOf(address) external view returns (uint);
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
function approve(address spender, uint value) external returns (bool);
function approve(address spender, uint value) external returns (bool);
}
}
library Math {
library Math {
function min(uint a, uint b) internal pure returns (uint) {
function min(uint a, uint b) internal pure returns (uint) {
return a < b ? a : b;
return a < b ? a : b;
}
}
function sqrt(uint y) internal pure returns (uint z) {
function sqrt(uint y) internal pure returns (uint z) {
if (y > 3) {
if (y > 3) {
z = y;
z = y;
uint x = y / 2 + 1;
uint x = y / 2 + 1;
while (x < z) {
while (x < z) {
z = x;
z = x;
x = (y / x + x) / 2;
x = (y / x + x) / 2;
}
}
} else if (y != 0) {
} else if (y != 0) {
z = 1;
z = 1;
}
}
}
}
}
}
interface IBaseV1Callee {
interface IBaseV1Callee {
function hook(address sender, uint amount0, uint amount1, bytes calldata data) external;
function hook(address sender, uint amount0, uint amount1, bytes calldata data) external;
}
}
// The base pair of pools, either stable or volatile
// The base pair of pools, either stable or volatile
contract BaseV1Pair {
contract BaseV1Pair {
string public name;
string public name;
string public symbol;
string public symbol;
uint8 public constant decimals = 18;
uint8 public constant decimals = 18;
// Used to denote stable or volatile pair, not immutable since construction happens in the initialize method for CREATE2 deterministic addresses
// Used to denote stable or volatile pair, not immutable since construction happens in the initialize method for CREATE2 deterministic addresses
bool public immutable stable;
bool public immutable stable;
uint public totalSupply = 0;
uint public totalSupply = 0;
mapping(address => mapping (address => uint)) public allowance;
mapping(address => mapping (address => uint)) public allowance;
mapping(address => uint) public balanceOf;
mapping(address => uint) public balanceOf;
bytes32 internal DOMAIN_SEPARATOR;
bytes32 internal DOMAIN_SEPARATOR;
// keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
// keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
bytes32 internal constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
bytes32 internal constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
mapping(address => uint) public nonces;
mapping(address => uint) public nonces;
uint internal constant MINIMUM_LIQUIDITY = 10**3;
uint internal constant MINIMUM_LIQUIDITY = 10**3;
address public immutable token0;
address public immutable token0;
address public immutable token1;
address public immutable token1;
address immutable factory;
address immutable factory;
// Structure to capture time period obervations every 30 minutes, used for local oracles
// Structure to capture time period obervations every 30 minutes, used for local oracles
struct Observation {
struct Observation {
uint timestamp;
uint timestamp;
uint reserve0Cumulative;
uint reserve0Cumulative;
uint reserve1Cumulative;
uint reserve1Cumulative;
}
}
// Capture oracle reading every 30 minutes
// Capture oracle reading every 30 minutes
uint constant periodSize = 1800;
uint constant periodSize = 0;
Observation[] public observations;
Observation[] public observations;
uint internal immutable decimals0;
uint internal immutable decimals0;
uint internal immutable decimals1;
uint internal immutable decimals1;
uint public reserve0;
uint public reserve0;
uint public reserve1;
uint public reserve1;
uint public blockTimestampLast;
uint public blockTimestampLast;
uint public reserve0CumulativeLast;
uint public reserve0CumulativeLast;
uint public reserve1CumulativeLast;
uint public reserve1CumulativeLast;
// position assigned to each LP to track their current index0 & index1 vs the global position
// position assigned to each LP to track their current index0 & index1 vs the global position
mapping(address => uint) public supplyIndex0;
mapping(address => uint) public supplyIndex0;
mapping(address => uint) public supplyIndex1;
mapping(address => uint) public supplyIndex1;
event Mint(address indexed sender, uint amount0, uint amount1);
event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
event Swap(
address indexed sender,
address indexed sender,
uint amount0In,
uint amount0In,
uint amount1In,
uint amount1In,
uint amount0Out,
uint amount0Out,
uint amount1Out,
uint amount1Out,
address indexed to
address indexed to
);
);
event Sync(uint reserve0, uint reserve1);
event Sync(uint reserve0, uint reserve1);
event Claim(address indexed sender, address indexed recipient, uint amount0, uint amount1);
event Claim(address indexed sender, address indexed recipient, uint amount0, uint amount1);
event Transfer(address indexed from, address indexed to, uint amount);
event Transfer(address indexed from, address indexed to, uint amount);
event Approval(address indexed owner, address indexed spender, uint amount);
event Approval(address indexed owner, address indexed spender, uint amount);
constructor() {
constructor() {
factory = msg.sender;
factory = msg.sender;
(address _token0, address _token1, bool _stable) = BaseV1Factory(msg.sender).getInitializable();
(address _token0, address _token1, bool _stable) = BaseV1Factory(msg.sender).getInitializable();
(token0, token1, stable) = (_token0, _token1, _stable);
(token0, token1, stable) = (_token0, _token1, _stable);
if (_stable) {
if (_stable) {
name = string(abi.encodePacked("StableV1 AMM - ", erc20(_token0).symbol(), "/", erc20(_token1).symbol()));
name = string(abi.encodePacked("StableV1 AMM - ", erc20(_token0).symbol(), "/", erc20(_token1).symbol()));
symbol = string(abi.encodePacked("sAMM-", erc20(_token0).symbol(), "/", erc20(_token1).symbol()));
symbol = string(abi.encodePacked("sAMM-", erc20(_token0).symbol(), "/", erc20(_token1).symbol()));
} else {
} else {
name = string(abi.encodePacked("VolatileV1 AMM - ", erc20(_token0).symbol(), "/", erc20(_token1).symbol()));
name = string(abi.encodePacked("VolatileV1 AMM - ", erc20(_token0).symbol(), "/", erc20(_token1).symbol()));
symbol = string(abi.encodePacked("vAMM-", erc20(_token0).symbol(), "/", erc20(_token1).symbol()));
symbol = string(abi.encodePacked("vAMM-", erc20(_token0).symbol(), "/", erc20(_token1).symbol()));
}
}
decimals0 = 10**erc20(_token0).decimals();
decimals0 = 10**erc20(_token0).decimals();
decimals1 = 10**erc20(_token1).decimals();
decimals1 = 10**erc20(_token1).decimals();
observations.push(Observation(block.timestamp, 0, 0));
observations.push(Observation(block.timestamp, 0, 0));
}
}
// simple re-entrancy check
// simple re-entrancy check
uint internal _unlocked = 1;
uint internal _unlocked = 1;
modifier lock() {
modifier lock() {
require(_unlocked == 1);
require(_unlocked == 1);
_unlocked = 2;
_unlocked = 2;
_;
_;
_unlocked = 1;
_unlocked = 1;
}
}
function observationLength() external view returns (uint) {
function observationLength() external view returns (uint) {
return observations.length;
return observations.length;
}
}
function lastObservation() public view returns (Observation memory) {
function lastObservation() public view returns (Observation memory) {
return observations[observations.length-1];
return observations[observations.length-1];
}
}
function metadata() external view returns (uint dec0, uint dec1, uint r0, uint r1, bool st, address t0, address t1) {
function metadata() external view returns (uint dec0, uint dec1, uint r0, uint r1, bool st, address t0, address t1) {
return (decimals0, decimals1, reserve0, reserve1, stable, token0, token1);
return (decimals0, decimals1, reserve0, reserve1, stable, token0, token1);
}
}
function tokens() external view returns (address, address) {
function tokens() external view returns (address, address) {
return (token0, token1);
return (token0, token1);
}
}
function getReserves() public view returns (uint _reserve0, uint _reserve1, uint _blockTimestampLast) {
function getReserves() public view returns (uint _reserve0, uint _reserve1, uint _blockTimestampLast) {
_reserve0 = reserve0;
_reserve0 = reserve0;
_reserve1 = reserve1;
_reserve1 = reserve1;
_blockTimestampLast = blockTimestampLast;
_blockTimestampLast = blockTimestampLast;
}
}
// update reserves and, on the first call per block, price accumulators
// update reserves and, on the first call per block, price accumulators
function _update(uint balance0, uint balance1, uint _reserve0, uint _reserve1) internal {
function _update(uint balance0, uint balance1, uint _reserve0, uint _reserve1) internal {
uint blockTimestamp = block.timestamp;
uint blockTimestamp = block.timestamp;
uint timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
uint timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
reserve0CumulativeLast += _reserve0 * timeElapsed;
reserve0CumulativeLast += _reserve0 * timeElapsed;
reserve1CumulativeLast += _reserve1 * timeElapsed;
reserve1CumulativeLast += _reserve1 * timeElapsed;
}
}
Observation memory _point = lastObservation();
Observation memory _point = lastObservation();
timeElapsed = blockTimestamp - _point.timestamp; // compare the last observation with current timestamp, if greater than 30 minutes, record a new event
timeElapsed = blockTimestamp - _point.timestamp; // compare the last observation with current timestamp, if greater than 30 minutes, record a new event
if (timeElapsed > periodSize) {
if (timeElapsed > periodSize) {
observations.push(Observation(blockTimestamp, reserve0CumulativeLast, reserve1CumulativeLast));
observations.push(Observation(blockTimestamp, reserve0CumulativeLast, reserve1CumulativeLast));
}
}
reserve0 = balance0;
reserve0 = balance0;
reserve1 = balance1;
reserve1 = balance1;
blockTimestampLast = blockTimestamp;
blockTimestampLast = blockTimestamp;
emit Sync(reserve0, reserve1);
emit Sync(reserve0, reserve1);
}
}
// produces the cumulative price using counterfactuals to save gas and avoid a call to sync.
// produces the cumulative price using counterfactuals to save gas and avoid a call to sync.
function currentCumulativePrices() public view returns (uint reserve0Cumulative, uint reserve1Cumulative, uint blockTimestamp) {
function currentCumulativePrices() public view returns (uint reserve0Cumulative, uint reserve1Cumulative, uint blockTimestamp) {
blockTimestamp = block.timestamp;
blockTimestamp = block.timestamp;
reserve0Cumulative = reserve0CumulativeLast;
reserve0Cumulative = reserve0CumulativeLast;
reserve1Cumulative = reserve1CumulativeLast;
reserve1Cumulative = reserve1CumulativeLast;
// if time has elapsed since the last update on the pair, mock the accumulated price values
// if time has elapsed since the last update on the pair, mock the accumulated price values
(uint _reserve0, uint _reserve1, uint _blockTimestampLast) = getReserves();
(uint _reserve0, uint _reserve1, uint _blockTimestampLast) = getReserves();
if (_blockTimestampLast != blockTimestamp) {
if (_blockTimestampLast != blockTimestamp) {
// subtraction overflow is desired
// subtraction overflow is desired
uint timeElapsed = blockTimestamp - _blockTimestampLast;
uint timeElapsed = blockTimestamp - _blockTimestampLast;
reserve0Cumulative += _reserve0 * timeElapsed;
reserve0Cumulative += _reserve0 * timeElapsed;
reserve1Cumulative += _reserve1 * timeElapsed;
reserve1Cumulative += _reserve1 * timeElapsed;
}
}
}
}
// gives the current twap price measured from amountIn * tokenIn gives amountOut
// gives the current twap price measured from amountIn * tokenIn gives amountOut
function current(address tokenIn, uint amountIn) external view returns (uint amountOut) {
function current(address tokenIn, uint amountIn) external view returns (uint amountOut) {
Observation memory _observation = lastObservation();
Observation memory _observation = lastObservation();
(uint reserve0Cumulative, uint reserve1Cumulative,) = currentCumulativePrices();
(uint reserve0Cumulative, uint reserve1Cumulative,) = currentCumulativePrices();
if (block.timestamp == _observation.timestamp) {
if (block.timestamp == _observation.timestamp) {
_observation = observations[observations.length-2];
_observation = observations[observations.length-2];
}
}
uint timeElapsed = block.timestamp - _observation.timestamp;
uint timeElapsed = block.timestamp - _observation.timestamp;
uint _reserve0 = (reserve0Cumulative - _observation.reserve0Cumulative) / timeElapsed;
uint _reserve0 = (reserve0Cumulative - _observation.reserve0Cumulative) / timeElapsed;
uint _reserve1 = (reserve1Cumulative - _observation.reserve1Cumulative) / timeElapsed;
uint _reserve1 = (reserve1Cumulative - _observation.reserve1Cumulative) / timeElapsed;
amountOut = _getAmountOut(amountIn, tokenIn, _reserve0, _reserve1);
amountOut = _getAmountOut(amountIn, tokenIn, _reserve0, _reserve1);
}
}
// as per `current`, however allows user configured granularity, up to the full window size
// as per `current`, however allows user configured granularity, up to the full window size
function quote(address tokenIn, uint amountIn, uint granularity) external view returns (uint amountOut) {
function quote(address tokenIn, uint amountIn, uint granularity) external view returns (uint amountOut) {
console.log("tokenIn: ", tokenIn);
uint [] memory _prices = sample(tokenIn, amountIn, granularity, 1);
uint [] memory _prices = sample(tokenIn, amountIn, granularity, 1);
uint priceAverageCumulative;
uint priceAverageCumulative;
for (uint i = 0; i < _prices.length; i++) {
for (uint i = 0; i < _prices.length; i++) {
priceAverageCumulative += _prices[i];
priceAverageCumulative += _prices[i];
}
}
return priceAverageCumulative / granularity;
return priceAverageCumulative / granularity;
}
}
// returns a memory set of twap prices
// returns a memory set of twap prices
function prices(address tokenIn, uint amountIn, uint points) external view returns (uint[] memory) {
function prices(address tokenIn, uint amountIn, uint points) external view returns (uint[] memory) {
return sample(tokenIn, amountIn, points, 1);
return sample(tokenIn, amountIn, points, 1);
}
}
function sample(address tokenIn, uint amountIn, uint points, uint window) public view returns (uint[] memory) {
function sample(address tokenIn, uint amountIn, uint points, uint window) public view returns (uint[] memory) {
uint[] memory _prices = new uint[](points);
uint[] memory _prices = new uint[](points);
uint length = observations.length-1;
uint length = observations.length-1;
uint i = length - (points * window);
uint i = length - (points * window);
uint nextIndex = 0;
uint nextIndex = 0;
uint index = 0;
uint index = 0;
for (; i < length; i+=window) {
for (; i < length; i+=window) {
nextIndex = i + window;
nextIndex = i + window;
uint timeElapsed = observations[nextIndex].timestamp - observations[i].timestamp;
uint timeElapsed = observations[nextIndex].timestamp - observations[i].timestamp;
uint _reserve0 = (observations[nextIndex].reserve0Cumulative - observations[i].reserve0Cumulative) / timeElapsed;
uint _reserve0 = (observations[nextIndex].reserve0Cumulative - observations[i].reserve0Cumulative) / timeElapsed;
uint _reserve1 = (observations[nextIndex].reserve1Cumulative - observations[i].reserve1Cumulative) / timeElapsed;
uint _reserve1 = (observations[nextIndex].reserve1Cumulative - observations[i].reserve1Cumulative) / timeElapsed;
_prices[index] = _getAmountOut(amountIn, tokenIn, _reserve0, _reserve1);
_prices[index] = _getAmountOut(amountIn, tokenIn, _reserve0, _reserve1);
index = index + 1;
index = index + 1;
}
}
return _prices;
return _prices;
}
}
// this low-level function should be called from a contract which performs important safety checks
// this low-level function should be called from a contract which performs important safety checks
// standard uniswap v2 implementation
// standard uniswap v2 implementation
function mint(address to) external lock returns (uint liquidity) {
function mint(address to) external lock returns (uint liquidity) {
(uint _reserve0, uint _reserve1) = (reserve0, reserve1);
(uint _reserve0, uint _reserve1) = (reserve0, reserve1);
uint _balance0 = erc20(token0).balanceOf(address(this));
uint _balance0 = erc20(token0).balanceOf(address(this));
uint _balance1 = erc20(token1).balanceOf(address(this));
uint _balance1 = erc20(token1).balanceOf(address(this));
uint _amount0 = _balance0 - _reserve0;
uint _amount0 = _balance0 - _reserve0;
uint _amount1 = _balance1 - _reserve1;
uint _amount1 = _balance1 - _reserve1;
uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
if (_totalSupply == 0) {
if (_totalSupply == 0) {
liquidity = Math.sqrt(_amount0 * _amount1) - MINIMUM_LIQUIDITY;
liquidity = Math.sqrt(_amount0 * _amount1) - MINIMUM_LIQUIDITY;
_mint(address(0), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
_mint(address(0), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
} else {
} else {
liquidity = Math.min(_amount0 * _totalSupply / _reserve0, _amount1 * _totalSupply / _reserve1);
liquidity = Math.min(_amount0 * _totalSupply / _reserve0, _amount1 * _totalSupply / _reserve1);
}
}
require(liquidity > 0, 'ILM'); // BaseV1: INSUFFICIENT_LIQUIDITY_MINTED
require(liquidity > 0, 'ILM'); // BaseV1: INSUFFICIENT_LIQUIDITY_MINTED
_mint(to, liquidity);
_mint(to, liquidity);
_update(_balance0, _balance1, _reserve0, _reserve1);
_update(_balance0, _balance1, _reserve0, _reserve1);
emit Mint(msg.sender, _amount0, _amount1);
emit Mint(msg.sender, _amount0, _amount1);
}
}
// this low-level function should be called from a contract which performs important safety checks
// this low-level function should be called from a contract which performs important safety checks
// standard uniswap v2 implementation
// standard uniswap v2 implementation
function burn(address to) external lock returns (uint amount0, uint amount1) {
function burn(address to) external lock returns (uint amount0, uint amount1) {
(uint _reserve0, uint _reserve1) = (reserve0, reserve1);
(uint _reserve0, uint _reserve1) = (reserve0, reserve1);
(address _token0, address _token1) = (token0, token1);
(address _token0, address _token1) = (token0, token1);
uint _balance0 = erc20(_token0).balanceOf(address(this));
uint _balance0 = erc20(_token0).balanceOf(address(this));
uint _balance1 = erc20(_token1).balanceOf(address(this));
uint _balance1 = erc20(_token1).balanceOf(address(this));
uint _liquidity = balanceOf[address(this)];
uint _liquidity = balanceOf[address(this)];
uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
amount0 = _liquidity * _balance0 / _totalSupply; // using balances ensures pro-rata distribution
amount0 = _liquidity * _balance0 / _totalSupply; // using balances ensures pro-rata distribution
amount1 = _liquidity * _balance1 / _totalSupply; // using balances ensures pro-rata distribution
amount1 = _liquidity * _balance1 / _totalSupply; // using balances ensures pro-rata distribution
require(amount0 > 0 && amount1 > 0, 'ILB'); // BaseV1: INSUFFICIENT_LIQUIDITY_BURNED
require(amount0 > 0 && amount1 > 0, 'ILB'); // BaseV1: INSUFFICIENT_LIQUIDITY_BURNED
_burn(address(this), _liquidity);
_burn(address(this), _liquidity);
_safeTransfer(_token0, to, amount0);
_safeTransfer(_token0, to, amount0);
_safeTransfer(_token1, to, amount1);
_safeTransfer(_token1, to, amount1);
_balance0 = erc20(_token0).balanceOf(address(this));
_balance0 = erc20(_token0).balanceOf(address(this));
_balance1 = erc20(_token1).balanceOf(address(this));
_balance1 = erc20(_token1).balanceOf(address(this));
_update(_balance0, _balance1, _reserve0, _reserve1);
_update(_balance0, _balance1, _reserve0, _reserve1);
emit Burn(msg.sender, amount0, amount1, to);
emit Burn(msg.sender, amount0, amount1, to);
}
}
// this low-level function should be called from a contract which performs important safety checks
// this low-level function should be called from a contract which performs important safety checks
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external lock {
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external lock {
require(!BaseV1Factory(factory).isPaused());
require(!BaseV1Factory(factory).isPaused());
require(amount0Out > 0 || amount1Out > 0, 'IOA'); // BaseV1: INSUFFICIENT_OUTPUT_AMOUNT
require(amount0Out > 0 || amount1Out > 0, 'IOA'); // BaseV1: INSUFFICIENT_OUTPUT_AMOUNT
(uint _reserve0, uint _reserve1) = (reserve0, reserve1);
(uint _reserve0, uint _reserve1) = (reserve0, reserve1);
require(amount0Out < _reserve0 && amount1Out < _reserve1, 'IL'); // BaseV1: INSUFFICIENT_LIQUIDITY
require(amount0Out < _reserve0 && amount1Out < _reserve1, 'IL'); // BaseV1: INSUFFICIENT_LIQUIDITY
uint _balance0;
uint _balance0;
uint _balance1;
uint _balance1;
{ // scope for _token{0,1}, avoids stack too deep errors
{ // scope for _token{0,1}, avoids stack too deep errors
(address _token0, address _token1) = (token0, token1);
(address _token0, address _token1) = (token0, token1);
require(to != _token0 && to != _token1, 'IT'); // BaseV1: INVALID_TO
require(to != _token0 && to != _token1, 'IT'); // BaseV1: INVALID_TO
if (amount0Out > 0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokens
if (amount0Out > 0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokens
if (amount1Out > 0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokens
if (amount1Out > 0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokens
if (data.length > 0) IBaseV1Callee(to).hook(msg.sender, amount0Out, amount1Out, data); // callback, used for flash loans
if (data.length > 0) IBaseV1Callee(to).hook(msg.sender, amount0Out, amount1Out, data); // callback, used for flash loans
_balance0 = erc20(_token0).balanceOf(address(this));
_balance0 = erc20(_token0).balanceOf(address(this));
_balance1 = erc20(_token1).balanceOf(address(this));
_balance1 = erc20(_token1).balanceOf(address(this));
}
}
uint amount0In = _balance0 > _reserve0 - amount0Out ? _balance0 - (_reserve0 - amount0Out) : 0;
uint amount0In = _balance0 > _reserve0 - amount0Out ? _balance0 - (_reserve0 - amount0Out) : 0;
uint amount1In = _balance1 > _reserve1 - amount1Out ? _balance1 - (_reserve1 - amount1Out) : 0;
uint amount1In = _balance1 > _reserve1 - amount1Out ? _balance1 - (_reserve1 - amount1Out) : 0;
require(amount0In > 0 || amount1In > 0, 'IIA'); // BaseV1: INSUFFICIENT_INPUT_AMOUNT
require(amount0In > 0 || amount1In > 0, 'IIA'); // BaseV1: INSUFFICIENT_INPUT_AMOUNT
{ // scope for reserve{0,1}Adjusted, avoids stack too deep errors
{ // scope for reserve{0,1}Adjusted, avoids stack too deep errors
(address _token0, address _token1) = (token0, token1);
(address _token0, address _token1) = (token0, token1);
_balance0 = erc20(_token0).balanceOf(address(this)); // since we removed tokens, we need to reconfirm balances, can also simply use previous balance - amountIn/ 10000, but doing balanceOf again as safety check
_balance0 = erc20(_token0).balanceOf(address(this)); // since we removed tokens, we need to reconfirm balances, can also simply use previous balance - amountIn/ 10000, but doing balanceOf again as safety check
_balance1 = erc20(_token1).balanceOf(address(this));
_balance1 = erc20(_token1).balanceOf(address(this));
// The curve, either x3y+y3x for stable pools, or x*y for volatile pools
// The curve, either x3y+y3x for stable pools, or x*y for volatile pools
require(_k(_balance0, _balance1) >= _k(_reserve0, _reserve1), 'K'); // BaseV1: K
require(_k(_balance0, _balance1) >= _k(_reserve0, _reserve1), 'K'); // BaseV1: K
}
}
_update(_balance0, _balance1, _reserve0, _reserve1);
_update(_balance0, _balance1, _reserve0, _reserve1);
emit Swap(msg.sender, amount0In, amount1In, amount0Out, amount1Out, to);
emit Swap(msg.sender, amount0In, amount1In, amount0Out, amount1Out, to);
}
}
// force balances to match reserves
// force balances to match reserves
function skim(address to) external lock {
function skim(address to) external lock {
(address _token0, address _token1) = (token0, token1);
(address _token0, address _token1) = (token0, token1);
_safeTransfer(_token0, to, erc20(_token0).balanceOf(address(this)) - (reserve0));
_safeTransfer(_token0, to, erc20(_token0).balanceOf(address(this)) - (reserve0));
_safeTransfer(_token1, to, erc20(_token1).balanceOf(address(this)) - (reserve1));
_safeTransfer(_token1, to, erc20(_token1).balanceOf(address(this)) - (reserve1));
}
}
// force reserves to match balances
// force reserves to match balances
function sync() external lock {
function sync() external lock {
_update(erc20(token0).balanceOf(address(this)), erc20(token1).balanceOf(address(this)), reserve0, reserve1);
_update(erc20(token0).balanceOf(address(this)), erc20(token1).balanceOf(address(this)), reserve0, reserve1);
}
}
function _f(uint x0, uint y) internal pure returns (uint) {
function _f(uint x0, uint y) internal pure returns (uint) {
return x0*(y*y/1e18*y/1e18)/1e18+(x0*x0/1e18*x0/1e18)*y/1e18;
return x0*(y*y/1e18*y/1e18)/1e18+(x0*x0/1e18*x0/1e18)*y/1e18;
}
}
function _d(uint x0, uint y) internal pure returns (uint) {
function _d(uint x0, uint y) internal pure returns (uint) {
return 3*x0*(y*y/1e18)/1e18+(x0*x0/1e18*x0/1e18);
return 3*x0*(y*y/1e18)/1e18+(x0*x0/1e18*x0/1e18);
}
}
function _get_y(uint x0, uint xy, uint y) internal pure returns (uint) {
function _get_y(uint x0, uint xy, uint y) internal pure returns (uint) {
for (uint i = 0; i < 255; i++) {
for (uint i = 0; i < 255; i++) {
uint y_prev = y;
uint y_prev = y;
uint k = _f(x0, y);
uint k = _f(x0, y);
if (k < xy) {
if (k < xy) {
uint dy = (xy - k)*1e18/_d(x0, y);
uint dy = (xy - k)*1e18/_d(x0, y);
y = y + dy;
y = y + dy;
} else {
} else {
uint dy = (k - xy)*1e18/_d(x0, y);
uint dy = (k - xy)*1e18/_d(x0, y);
y = y - dy;
y = y - dy;
}
}
if (y > y_prev) {
if (y > y_prev) {
if (y - y_prev <= 1) {
if (y - y_prev <= 1) {
return y;
return y;
}
}
} else {
} else {
if (y_prev - y <= 1) {
if (y_prev - y <= 1) {
return y;
return y;
}
}
}
}
}
}
return y;
return y;
}
}
function getAmountOut(uint amountIn, address tokenIn) external view returns (uint) {
function getAmountOut(uint amountIn, address tokenIn) external view returns (uint) {
(uint _reserve0, uint _reserve1) = (reserve0, reserve1);
(uint _reserve0, uint _reserve1) = (reserve0, reserve1);
//amountIn -= amountIn / 10000; // remove fee from amount received
//amountIn -= amountIn / 10000; // remove fee from amount received
return _getAmountOut(amountIn, tokenIn, _reserve0, _reserve1);
return _getAmountOut(amountIn, tokenIn, _reserve0, _reserve1);
}
}
function _getAmountOut(uint amountIn, address tokenIn, uint _reserve0, uint _reserve1) internal view returns (uint) {
function _getAmountOut(uint amountIn, address tokenIn, uint _reserve0, uint _reserve1) internal view returns (uint) {
if (stable) {
if (stable) {
uint xy = _k(_reserve0, _reserve1);
uint xy = _k(_reserve0, _reserve1);
_reserve0 = _reserve0 * 1e18 / decimals0;
_reserve0 = _reserve0 * 1e18 / decimals0;
_reserve1 = _reserve1 * 1e18 / decimals1;
_reserve1 = _reserve1 * 1e18 / decimals1;
(uint reserveA, uint reserveB) = tokenIn == token0 ? (_reserve0, _reserve1) : (_reserve1, _reserve0);
(uint reserveA, uint reserveB) = tokenIn == token0 ? (_reserve0, _reserve1) : (_reserve1, _reserve0);
amountIn = tokenIn == token0 ? amountIn * 1e18 / decimals0 : amountIn * 1e18 / decimals1;
amountIn = tokenIn == token0 ? amountIn * 1e18 / decimals0 : amountIn * 1e18 / decimals1;
uint y = reserveB - _get_y(amountIn+reserveA, xy, reserveB);
uint y = reserveB - _get_y(amountIn+reserveA, xy, reserveB);
return y * (tokenIn == token0 ? decimals1 : decimals0) / 1e18;
return y * (tokenIn == token0 ? decimals1 : decimals0) / 1e18;
} else {
} else {
(uint reserveA, uint reserveB) = tokenIn == token0 ? (_reserve0, _reserve1) : (_reserve1, _reserve0);
(uint reserveA, uint reserveB) = tokenIn == token0 ? (_reserve0, _reserve1) : (_reserve1, _reserve0);
return amountIn * reserveB / (reserveA + amountIn);
return amountIn * reserveB / (reserveA + amountIn);
}
}
}
}
function _k(uint x, uint y) internal view returns (uint) {
function _k(uint x, uint y) internal view returns (uint) {
if (stable) {
if (stable) {
uint _x = x * 1e18 / decimals0;
uint _x = x * 1e18 / decimals0;
uint _y = y * 1e18 / decimals1;
uint _y = y * 1e18 / decimals1;
uint _a = (_x * _y) / 1e18;
uint _a = (_x * _y) / 1e18;
uint _b = ((_x * _x) / 1e18 + (_y * _y) / 1e18);
uint _b = ((_x * _x) / 1e18 + (_y * _y) / 1e18);
return _a * _b / 1e18; // x3y+y3x >= k
return _a * _b / 1e18; // x3y+y3x >= k
} else {
} else {
return x * y; // xy >= k
return x * y; // xy >= k
}
}
}
}
function _mint(address dst, uint amount) internal {
function _mint(address dst, uint amount) internal {
totalSupply += amount;
totalSupply += amount;
balanceOf[dst] += amount;
balanceOf[dst] += amount;
emit Transfer(address(0), dst, amount);
emit Transfer(address(0), dst, amount);
}
}
function _burn(address dst, uint amount) internal {
function _burn(address dst, uint amount) internal {
totalSupply -= amount;
totalSupply -= amount;
balanceOf[dst] -= amount;
balanceOf[dst] -= amount;
emit Transfer(dst, address(0), amount);
emit Transfer(dst, address(0), amount);
}
}
function approve(address spender, uint amount) external returns (bool) {
function approve(address spender, uint amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
emit Approval(msg.sender, spender, amount);
return true;
return true;
}
}
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external {
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external {
require(deadline >= block.timestamp, 'BaseV1: EXPIRED');
require(deadline >= block.timestamp, 'BaseV1: EXPIRED');
DOMAIN_SEPARATOR = keccak256(
DOMAIN_SEPARATOR = keccak256(
abi.encode(
abi.encode(
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
keccak256(bytes(name)),
keccak256(bytes(name)),
keccak256('1'),
keccak256('1'),
block.chainid,
block.chainid,
address(this)
address(this)
)
)
);
);
bytes32 digest = keccak256(
bytes32 digest = keccak256(
abi.encodePacked(
abi.encodePacked(
'\x19\x01',
'\x19\x01',
DOMAIN_SEPARATOR,
DOMAIN_SEPARATOR,
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
)
)
);
);
address recoveredAddress = ecrecover(digest, v, r, s);
address recoveredAddress = ecrecover(digest, v, r, s);
require(recoveredAddress != address(0) && recoveredAddress == owner, 'BaseV1: INVALID_SIGNATURE');
require(recoveredAddress != address(0) && recoveredAddress == owner, 'BaseV1: INVALID_SIGNATURE');
allowance[owner][spender] = value;
allowance[owner][spender] = value;
emit Approval(owner, spender, value);
emit Approval(owner, spender, value);
}
}
function transfer(address dst, uint amount) external returns (bool) {
function transfer(address dst, uint amount) external returns (bool) {
_transferTokens(msg.sender, dst, amount);
_transferTokens(msg.sender, dst, amount);
return true;
return true;
}
}
function transferFrom(address src, address dst, uint amount) external returns (bool) {
function transferFrom(address src, address dst, uint amount) external returns (bool) {
address spender = msg.sender;
address spender = msg.sender;
uint spenderAllowance = allowance[src][spender];
uint spenderAllowance = allowance[src][spender];
if (spender != src && spenderAllowance != type(uint).max) {
if (spender != src && spenderAllowance != type(uint).max) {
uint newAllowance = spenderAllowance - amount;
uint newAllowance = spenderAllowance - amount;
allowance[src][spender] = newAllowance;
allowance[src][spender] = newAllowance;
emit Approval(src, spender, newAllowance);
emit Approval(src, spender, newAllowance);
}
}
_transferTokens(src, dst, amount);
_transferTokens(src, dst, amount);
return true;
return true;
}
}
function _transferTokens(address src, address dst, uint amount) internal {
function _transferTokens(address src, address dst, uint amount) internal {
balanceOf[src] -= amount;
balanceOf[src] -= amount;
balanceOf[dst] += amount;
balanceOf[dst] += amount;
emit Transfer(src, dst, amount);
emit Transfer(src, dst, amount);
}
}
function _safeTransfer(address token,address to,uint256 value) internal {
function _safeTransfer(address token,address to,uint256 value) internal {
require(token.code.length > 0);
require(token.code.length > 0);
(bool success, bytes memory data) =
(bool success, bytes memory data) =
token.call(abi.encodeWithSelector(erc20.transfer.selector, to, value));
token.call(abi.encodeWithSelector(erc20.transfer.selector, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))));
require(success && (data.length == 0 || abi.decode(data, (bool))));
}
}
}
}
contract BaseV1Factory {
contract BaseV1Factory {
bool public isPaused;
bool public isPaused;
address public pauser;
address public pauser;
address public pendingPauser;
address public pendingPauser;
mapping(address => mapping(address => mapping(bool => address))) public getPair;
mapping(address => mapping(address => mapping(bool => address))) public getPair;
address[] public allPairs;
address[] public allPairs;
mapping(address => bool) public isPair; // simplified check if its a pair, given that `stable` flag might not be available in peripherals
mapping(address => bool) public isPair; // simplified check if its a pair, given that `stable` flag might not be available in peripherals
address internal _temp0;
address internal _temp0;
address internal _temp1;
address internal _temp1;
bool internal _temp;
bool internal _temp;
event PairCreated(address indexed token0, address indexed token1, bool stable, address pair, uint);
event PairCreated(address indexed token0, address indexed token1, bool stable, address pair, uint);
constructor() {
constructor() {
pauser = msg.sender;
pauser = msg.sender;
isPaused = false;
isPaused = false;
}
}
function allPairsLength() external view returns (uint) {
function allPairsLength() external view returns (uint) {
return allPairs.length;
return allPairs.length;
}
}
function setPauser(address _pauser) external {
function setPauser(address _pauser) external {
require(msg.sender == pauser);
require(msg.sender == pauser);
pendingPauser = _pauser;
pendingPauser = _pauser;
}
}
function acceptPauser() external {
function acceptPauser() external {
require(msg.sender == pendingPauser);
require(msg.sender == pendingPauser);
pauser = pendingPauser;
pauser = pendingPauser;
}
}
function setPause(bool _state) external {
function setPause(bool _state) external {
require(msg.sender == pauser);
require(msg.sender == pauser);
isPaused = _state;
isPaused = _state;
}
}
function pairCodeHash() external pure returns (bytes32) {
function pairCodeHash() external pure returns (bytes32) {
return keccak256(type(BaseV1Pair).creationCode);
return keccak256(type(BaseV1Pair).creationCode);
}
}
function getInitializable() external view returns (address, address, bool) {
function getInitializable() external view returns (address, address, bool) {
return (_temp0, _temp1, _temp);
return (_temp0, _temp1, _temp);
}
}
function createPair(address tokenA, address tokenB, bool stable) external returns (address pair) {
function createPair(address tokenA, address tokenB, bool stable) external returns (address pair) {
require(tokenA != tokenB, 'IA'); // BaseV1: IDENTICAL_ADDRESSES
require(tokenA != tokenB, 'IA'); // BaseV1: IDENTICAL_ADDRESSES
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'ZA'); // BaseV1: ZERO_ADDRESS
require(token0 != address(0), 'ZA'); // BaseV1: ZERO_ADDRESS
require(getPair[token0][token1][stable] == address(0), 'PE'); // BaseV1: PAIR_EXISTS - single check is sufficient
require(getPair[token0][token1][stable] == address(0), 'PE'); // BaseV1: PAIR_EXISTS - single check is sufficient
bytes32 salt = keccak256(abi.encodePacked(token0, token1, stable)); // notice salt includes stable as well, 3 parameters
bytes32 salt = keccak256(abi.encodePacked(token0, token1, stable)); // notice salt includes stable as well, 3 parameters
(_temp0, _temp1, _temp) = (token0, token1, stable);
(_temp0, _temp1, _temp) = (token0, token1, stable);
pair = address(new BaseV1Pair{salt:salt}());
pair = address(new BaseV1Pair{salt:salt}());
getPair[token0][token1][stable] = pair;
getPair[token0][token1][stable] = pair;
getPair[token1][token0][stable] = pair; // populate mapping in the reverse direction
getPair[token1][token0][stable] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
allPairs.push(pair);
isPair[pair] = true;
isPair[pair] = true;
emit PairCreated(token0, token1, stable, pair, allPairs.length);
emit PairCreated(token0, token1, stable, pair, allPairs.length);
}
}
}
}