Skip to content

Instantly share code, notes, and snippets.

@gitpusha
Created April 27, 2021 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitpusha/cf92cb8ca0a8c53fbe34542dbb5031e2 to your computer and use it in GitHub Desktop.
Save gitpusha/cf92cb8ca0a8c53fbe34542dbb5031e2 to your computer and use it in GitHub Desktop.
interface IUniswapV2Pair {
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
event Transfer(address indexed from, address indexed to, uint256 value);
event Mint(address indexed sender, uint256 amount0, uint256 amount1);
event Burn(
address indexed sender,
uint256 amount0,
uint256 amount1,
address indexed to
);
event Swap(
address indexed sender,
uint256 amount0In,
uint256 amount1In,
uint256 amount0Out,
uint256 amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
function MINIMUM_LIQUIDITY() external pure returns (uint256);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves()
external
view
returns (
uint112 reserve0,
uint112 reserve1,
uint32 blockTimestampLast
);
function price0CumulativeLast() external view returns (uint256);
function price1CumulativeLast() external view returns (uint256);
function kLast() external view returns (uint256);
function mint(address to) external returns (uint256 liquidity);
function burn(address to)
external
returns (uint256 amount0, uint256 amount1);
function swap(
uint256 amount0Out,
uint256 amount1Out,
address to,
bytes calldata data
) external;
function skim(address to) external;
function sync() external;
function initialize(address, address) external;
}
library UniswapUtils {
using SafeMath for uint256;
/**
* @notice Returns the current block timestamp within the range of uint32, i.e. [0, 2**32 - 1]
* @return uint32 - block timestamp
*/
function currentBlockTimestamp() internal view returns (uint32) {
return uint32(block.timestamp % 2**32);
}
/**
* @notice Returns sorted token addresses, used to handle return values from pairs sorted in this order
* @param _tokenA - Address of the token A
* @param _tokenB - Address of the token B
* @return token0 - Address of the lower token
* @return token1 - Address of the higher token
*/
function sortTokens(address _tokenA, address _tokenB)
internal
pure
returns (address token0, address token1)
{
require(
_tokenA != _tokenB,
"UniswapUtils#sortTokens: IDENTICAL_ADDRESSES"
);
(token0, token1) = _tokenA < _tokenB
? (_tokenA, _tokenB)
: (_tokenB, _tokenA);
require(token0 != address(0), "UniswapUtils#sortTokens: ZERO_ADDRESS");
}
/**
* @notice Calculates the CREATE2 address for a pair without making any external calls
* @param _factory - Address of the uniswapV2 factory contract
* @param _tokenA - Address of the token A
* @param _tokenB - Address of the token B
* @param _initCodeHash - Bytes32 of the uniswap v2 pair contract unit code hash
* @return pair - Address of the pair
*/
function pairFor(
address _factory,
address _tokenA,
address _tokenB,
bytes32 _initCodeHash
) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(_tokenA, _tokenB);
pair = address(
uint256(
keccak256(
abi.encodePacked(
hex"ff",
_factory,
keccak256(abi.encodePacked(token0, token1)),
_initCodeHash // init code hash
)
)
)
);
}
/**
* @notice Calculates the CREATE2 address for a pair without making any external calls
* @dev Tokens should be in order
* @param _factory - Address of the uniswapV2 factory contract
* @param _token0 - Address of the token 0
* @param _token1 - Address of the token 1
* @param _initCodeHash - Bytes32 of the uniswap v2 pair contract unit code hash
* @return pair - Address of the pair
*/
function pairForSorted(
address _factory,
address _token0,
address _token1,
bytes32 _initCodeHash
) internal pure returns (address pair) {
pair = address(
uint256(
keccak256(
abi.encodePacked(
hex"ff",
_factory,
keccak256(abi.encodePacked(_token0, _token1)),
_initCodeHash // init code hash
)
)
)
);
}
/**
* @notice Given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
* @param _amountIn - uint of the input token's amount
* @param _reserveIn - uint of the input token's reserve
* @param _reserveOut - uint of the output token's reserve
* @return amountOut - Maximum output amount
*/
function getAmountOut(
uint256 _amountIn,
uint256 _reserveIn,
uint256 _reserveOut
) internal pure returns (uint256 amountOut) {
require(
_amountIn > 0,
"UniswapUtils#getAmountOut: INSUFFICIENT_INPUT_AMOUNT"
);
require(
_reserveIn > 0 && _reserveOut > 0,
"UniswapUtils#getAmountOut: INSUFFICIENT_LIQUIDITY"
);
uint256 amountInWithFee = _amountIn.mul(997);
uint256 numerator = amountInWithFee.mul(_reserveOut);
uint256 denominator = _reserveIn.mul(1000).add(amountInWithFee);
amountOut = numerator / denominator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment