The universe is the successor of the mobile internet,and the doors of the virtual world and the real world have been opened.The metauniverse may become the new direction of the development of the Internet,
and may also be the next form of the development of the digital economy.The exploration of the universe will promote the deep integration of the real economy and the digital economy,and promote the digital economy to a new stage.
//given some amount of an asset and pair reserves,returns an equivalent amount of the other asset
//添加流动性的时候,通过该方法查询输入A的数量,需要多少个B
function quote(uint amountA,uint reserveA,uint reserveB)internal pure returns(uint amountB){
//判断数量,首次添加流动性,随意定价,不需要查询该方法
require(amountA>0,'UniswapV2Library:INSUFFICIENT_AMOUNT');
require(reserveA>0&&reserveB>0,'UniswapV2Library:INSUFFICIENT_LIQUIDITY');
//B数量=预期输入A的数量*B的储备量/A的储备量;//实际公式就是A/B=reserveA/reserveB,两个币的数量比例一致
amountB=amountA.mul(reserveB)/reserveA;
}
//given an input amount of an asset and pair reserves,returns the maximum output amount of the other asset
//通过精确输入金额,输入币的储备量,输出币的储备量,计算输出币的最大输出量
function getAmountOut(uint amountIn,uint reserveIn,uint reserveOut)internal pure returns(uint amountOut){
require(amountIn>0,'UniswapV2Library:INSUFFICIENT_INPUT_AMOUNT');
require(reserveIn>0&&reserveOut>0,'UniswapV2Library:INSUFFICIENT_LIQUIDITY');
//具体看下面的公式推导,要看该公式,首先要理解uniswap AMM,X*Y=K
uint amountInWithFee=amountIn.mul(997);//手续费都是扣输入额的千三,所以需要去掉千三后才是实际用于交易的金额
uint numerator=amountInWithFee.mul(reserveOut);//套下面公式理解吧!!
uint denominator=reserveIn.mul(1000).add(amountInWithFee);
amountOut=numerator/denominator;