Blockchain technology can thus empower enterprises in many ways:providing reliable shared data and building trust between all parties;Eliminate data islands,that is,
integrate data into a system through centralized ledgers that are shared in a network and support the access of licensees;Give high security to data;
interface IWETH{
function deposit()external payable;
function transfer(address to,uint value)external returns(bool);
function withdraw(uint)external;
}
contract UniswapV2Router02 is IUniswapV2Router02{
using SafeMath for uint;
address public immutable override factory;
address public immutable override WETH;
//交易时间是否过期,dapp中默认设置时20分钟内成交有效
modifier ensure(uint deadline){
require(deadline>=block.timestamp,'UniswapV2Router:EXPIRED');
_;
}
constructor(address _factory,address _WETH)public{
factory=_factory;
WETH=_WETH;
}
receive()external payable{
assert(msg.sender==WETH);//only accept ETH via fallback from the WETH contract
}
//ADD LIQUIDITY
//添加流动性内部方法,通过该方法计算出两个币的实际所需数量
function _addLiquidity(
address tokenA,//代币地址A
address tokenB,//代币地址B
uint amountADesired,//代币A期望添加量
uint amountBDesired,//代币B期望添加量
uint amountAMin,//代币A最小添加量(这两个min,收益添加的时候可以和Desired一样,二次添加的时候,一般都是小于Desired,具体小多少,算法可以查看uniswap前端代码)
uint amountBMin//代币B最小添加量
)internal virtual returns(uint amountA,uint amountB){//返回值是两个
//create the pair if it doesn't exist yet
//通过factory,查询pair,如果等于0地址,就表示还没有该交易对,调用创建方法
if(IUniswapV2Factory(factory).getPair(tokenA,tokenB)==address(0)){
IUniswapV2Factory(factory).createPair(tokenA,tokenB);//创建交易对
}
//可以先了解下UniswapV2Library中相关方法的意思
//如果查询两个值都是0,首次添加,直接使用期望值
(uint reserveA,uint reserveB)=UniswapV2Library.getReserves(factory,tokenA,tokenB);
if(reserveA==0&&reserveB==0){
(amountA,amountB)=(amountADesired,amountBDesired);//直接使用这两个值,比例就是相互的币价
}else{
//如果