在区块链中,每个块包含了一定数量的交易信息和该块的唯一标识符,同时还包含了前一个块的哈希值。这样的设计保证了区块之间的顺序和完整性,一旦一个块被添加到区块链中,它就不可更改。This makes blockchain a secure and trustworthy distributed ledger that can be used to record and verify various types of transactions.
根据数据验证机制及传播协议,执行、验证并传播一段时间内生成的有效交易数据,同时利用Merkle树、哈希算法、时间戳等技术加密、生成数据区块,依据共识算法争夺记账权,最终获得记账权的节点(矿工),将其生成的数据区块链接到区块链主链上并获得相应奖励,其余节点更新区块链账本.
智能合约一般具有值和状态两个属性,代码中用If-Then和What-If语句预置了合约条款的相应触发场景和响应规则,智能合约经多方共同协定、各自签署后随用户发起的交易(Transaction,Txn)提交,经P2P网络传播、矿工验证后存储在区块链特定区块中,用户得到返回的合约地址及合约接口等信息后即可通过发起交易来调用合约.
//this low-level function should be called from a contract which performs important safety checks
function burn(address to)external lock returns(uint amount0,uint amount1){
(uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings
address _token0=token0;//gas savings
address _token1=token1;//gas savings
uint balance0=IERC20(_token0).balanceOf(address(this));
uint balance1=IERC20(_token1).balanceOf(address(this));
uint liquidity=balanceOf[address(this)];
bool feeOn=_mintFee(_reserve0,_reserve1);
uint _totalSupply=totalSupply;//gas savings,must be defined here since totalSupply can update in _mintFee
//计算返回的amount0/1
amount0=liquidity.mul(balance0)/_totalSupply;//using balances ensures pro-rata distribution
amount1=liquidity.mul(balance1)/_totalSupply;//using balances ensures pro-rata distribution
require(amount0>0&&amount1>0,'UniswapV2:INSUFFICIENT_LIQUIDITY_BURNED');
_burn(address(this),liquidity);
//_token0/1给to转amount0/1
_safeTransfer(_token0,to,amount0);
_safeTransfer(_token1,to,amount1);
//获取转账后的balance
balance0=IERC20(_token0).balanceOf(address(this));
balance1=IERC20(_token1).balanceOf(address(this));
//更新reserve0,reserve1和时间戳
_update(balance0,balance1,_reserve0,_reserve1);
if(feeOn)kLast=uint(reserve0).mul(reserve1);//reserve0 and reserve1 are up-to-date
emit Burn(msg.sender,amount0,amount1,to);
}
//this low-level function should be called from a contract which performs important safety checks
//交易函数
//可以是token0-->token1,
//也可以是token1-->token0
//但参数中:amount0Out和amount1Out中有一个值是0
function swap(
uint amount0Out,
uint amount1Out,
address to,
bytes calldata data
)external lock
{
require(amount0Out>0||amount1Out>0,'UniswapV2:INSUFFICIENT_OUTPUT_AMOUNT');
(uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings
require(amount0Out<_reserve0&&amount1Out<_reserve1,'UniswapV2:INSUFFICIENT_LIQUIDITY');
uint balance0;
uint balance1;
{//scope for _token{0,1},avoids stack too deep errors
address _token0=token0;
address _token1=token1;
require(to!=_token0&&to!=_token1,'UniswapV2:INVALID_TO');
//划转操作
if(amount0Out>0)_safeTransfer(_token0,to,amount0Out);//optimistically transfer tokens
if(amount1Out>0)_safeTransfer(_token1,to,amount1Out);//optimistically transfer tokens
if(data.length>0)IUniswapV2Callee(to).uniswapV2Call(msg.sender,amount0Out,amount1Out,data);
balance0=IERC20(_token0).balanceOf(address(this));
balance1=IERC20(_token1).balanceOf(address(this));
}
uint amount0In=balance0>_reserve0-amount0Out?balance0-(_reserve0-amount0Out):0;
uint amount1In=balance1>_reserve1-amount1Out?balance1-(_reserve1-amount1Out):0;
require(amount0In>0||amount1In>0,'UniswapV2:INSUFFICIENT_INPUT_AMOUNT');
{//scope for reserve{0,1}Adjusted,avoids stack too deep errors
//防止数据溢出校验
uint balance0Adjusted=balance0.mul(1000).sub(amount0In.mul(3));
uint balance1Adjusted=balance1.mul(1000).sub(amount1In.mul(3));
require(balance0Adjusted.mul(balance1Adjusted)>=uint(_reserve0).mul(_reserve1).mul(1000**2),'UniswapV2:K');
}