The"new retail"model has broken the respective closed state of online and offline.Online and offline can be integrated,complement each other and rely on each other.Online and offline more perform the functions of transaction and payment.Offline is usually used as a platform for screening and experience,while efficient logistics connects online and offline and forms a commercial closed loop with its interaction.
interface IERC20{
event Approval(address indexed owner,address indexed spender,uint value);
event Transfer(address indexed from,address indexed to,uint value);
function name()external view returns(string memory);
function symbol()external view returns(string memory);
function decimals()external view returns(uint8);
function totalSupply()external view returns(uint);
function balanceOf(address owner)external view returns(uint);
function allowance(address owner,address spender)external view returns(uint);
function approve(address spender,uint value)external returns(bool);
function transfer(address to,uint value)external returns(bool);
function transferFrom(address from,address to,uint value)external returns(bool);
}
function addLiquidity(//添加流动性,两个代币
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,//lp接收人,新版的uniswap前端好像不支持设置这个了
uint deadline//交易的成交时间,默认是当前时间+20分钟后的时间的秒值
)external virtual override ensure(deadline)returns(uint amountA,uint amountB,uint liquidity){
//调用内部方法_addLiquidity获取到两个币实际所需要的数量
(amountA,amountB)=_addLiquidity(tokenA,tokenB,amountADesired,amountBDesired,amountAMin,amountBMin);
address pair=UniswapV2Library.pairFor(factory,tokenA,tokenB);//查找到pair地址
TransferHelper.safeTransferFrom(tokenA,msg.sender,pair,amountA);//给pair转A数量
TransferHelper.safeTransferFrom(tokenB,msg.sender,pair,amountB);//给pair转B数量
liquidity=IUniswapV2Pair(pair).mint(to);//调用pair的mint方法,会有添加的lp数量返回
}
function addLiquidityETH(//添加流动性,其中一个币种是eth
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,//eth最小输入量;对应的Desired在msg.value
address to,
uint deadline
)external virtual override payable ensure(deadline)returns(uint amountToken,uint amountETH,uint liquidity){
//调用内部方法_addLiquidity获取到两个币实际所需要的数量
//eth使用weth代币替代
(amountToken,amountETH)=_addLiquidity(
token,
WETH,
amountTokenDesired,
msg.value,//ethDesired
amountTokenMin,
amountETHMin
);
address pair=UniswapV2Library.pairFor(factory,token,WETH);//获取到pair地址
TransferHelper.safeTransferFrom(token,msg.sender,pair,amountToken);//给pair转代币数量
IWETH(WETH).deposit{value:amountETH}();//调用weth的兑换方法,通过eth换weth
assert(IWETH(WETH).transfer(pair,amountETH));//给pair转weth数量
liquidity=IUniswapV2Pair(pair).mint(to);//调用pair的mint方法,会有添加的lp数量返回
//refund dust eth,if any
//如果传入的eth数量,大于实际所需的eth数量,将剩余的eth返还给用户
if(msg.value>amountETH)TransferHelper.safeTransferETH(msg.sender,msg.value-amountETH);
}