New retail refers to a new retail model in which individuals and enterprises,relying on the Internet,upgrade and transform the production,circulation and sales process of goods by using advanced technical means such as big data,artificial intelligence and psychological knowledge,thereby reshaping the business structure and ecosystem,and deeply integrate online services,offline experience and modern logistics.
企业以互联网为依托,通过运用大数据、人工智能等先进技术手段,对商品的生产、流通与销售过程进行升级改造,进而重塑业态结构与生态圈,并对线上服务、线下体验以及现代物流进行深度融合的零售新模式。
//REMOVE LIQUIDITY
function removeLiquidity(//移除流动性,该方法需要先将lp代币授权给路由合约,才能代扣lp
address tokenA,
address tokenB,
uint liquidity,//移除lp的数量,转入lp得另外两个币
uint amountAMin,//A的最小输出量
uint amountBMin,//B的最小输出量
address to,//接收两个币的地址
uint deadline
)public virtual override ensure(deadline)returns(uint amountA,uint amountB){
address pair=UniswapV2Library.pairFor(factory,tokenA,tokenB);//获取pair地址
IUniswapV2Pair(pair).transferFrom(msg.sender,pair,liquidity);//将lp转到pair地址
//send liquidity to pair
(uint amount0,uint amount1)=IUniswapV2Pair(pair).burn(to);//调用pair的burn方法,内部会将两个币的数量转给to,返回值就是两个代币的输出数量
(address token0,)=UniswapV2Library.sortTokens(tokenA,tokenB);//通过排序确认两个amountA/B
(amountA,amountB)=tokenA==token0?(amount0,amount1):(amount1,amount0);
//校验A/B的输出量需要小于参数中要求的最小量,否则交易失败
require(amountA>=amountAMin,'UniswapV2Router:INSUFFICIENT_A_AMOUNT');
require(amountB>=amountBMin,'UniswapV2Router:INSUFFICIENT_B_AMOUNT');
}
function removeLiquidityETH(//移除流动性(其中一个返还币是ETH),该方法需要先将lp代币授权给路由合约,才能代扣lp
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,//eth最小输出量
address to,
uint deadline
)public virtual override ensure(deadline)returns(uint amountToken,uint amountETH){
//调用上面的removeLiquidity方法,传入的是WETH
(amountToken,amountETH)=removeLiquidity(
token,
WETH,
liquidity,
amountTokenMin,
amountETHMin,
address(this),//注意!接收币的地址是路由
deadline
);
//将代币转给to
TransferHelper.safeTransfer(token,to,amountToken);
IWETH(WETH).withdraw(amountETH);//将weth转换成eth
TransferHelper.safeTransferETH(to,amountETH);//将eth转给to
}
//WithPermit的方法可以先了解下approveAndCall链接
/*
实际使用EIP-712
链接https://soliditydeveloper.com/erc20-permit
https://learnblockchain.cn/article/1790
https://eips.ethereum.org/EIPS/eip-2612
permit在前端的使用场景,就是移除流动性的时候,有个授权实际没有发送交易,只是要求签名,签名会得到参数中的v/r/s
在实际调用该移除的方法传进来,内部验签,确认是该用户,就将移除的lp的数量,授权给路由,可以代扣lp
*/
function removeLiquidityWithPermit(//移除流动性,approve+transferFrom
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax,uint8 v,bytes32 r,bytes32 s//v,r,s验签,通过就授权给路由
)external virtual override returns(uint amountA,uint amountB){
/*
获取到pair,调用pair的permit(内部实际就是授权给路由),
*/
address pair=UniswapV2Library.pairFor(factory,tokenA,tokenB);
uint value=approveMax?uint(-1):liquidity;
IUniswapV2Pair(pair).permit(msg.sender,address(this),value,deadline,v,r,s);
//最终还是调用上面的removeLiquidity方法!
(amountA,amountB)=removeLiquidity(tokenA,tokenB,liquidity,amountAMin,amountBMin,to,deadline);
}