Blockchain technology is generally used to build transaction systems, and it is necessary to ensure that the transaction information is authentic, traceable, and tamper proof. The information of each transaction is confirmed and stored in a block,
which is encrypted through hash technology to ensure that the information is not tampered with. These blocks form a chain in chronological order. Each node maintains complete blockchain information, and the information of individual nodes is damaged without affecting the blockchain information. This type of information recording method is called distributed ledger.
What is blockchain? In short, it is a special type of distributed database. Firstly, the main function of blockchain is to store information. Any information that needs to be saved can be written to or read from the blockchain, so it is a database.
Secondly, anyone can set up a server, join the blockchain network, and become a node. In the world of blockchain, there is no central node, and each node is equal and stores the entire database. You can write/read data to any node, as all nodes will eventually synchronize to ensure blockchain consistency.
/**
销毁方法
* @param to to地址
* @return amount0
* @return amount1
* @notice 应该从执行重要安全检查的合同中调用此低级功能
*/
// this low-level function should be called from a contract which performs important safety checks
function burn(address to)
external
lock
returns (uint256 amount0, uint256 amount1)
{
//获取`储备量0`,`储备量1`
(uint112 _reserve0, uint112 _reserve1, ) = getReserves(); // gas savings
//带入变量
address _token0 = token0; // gas savings
address _token1 = token1; // gas savings
//获取当前合约在token0合约内的余额
uint256 balance0 = IERC20(_token0).balanceOf(address(this));
//获取当前合约在token1合约内的余额
uint256 balance1 = IERC20(_token1).balanceOf(address(this));
//从当前合约的balanceOf映射中获取当前合约自身的流动性数量
uint256 liquidity = balanceOf[address(this)];
//返回铸造费开关
bool feeOn = _mintFee(_reserve0, _reserve1);
//获取totalSupply,必须在此处定义,因为totalSupply可以在mintFee中更新
uint256 _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
//amount0 = 流动性数量 * 余额0 / totalSupply 使用余额确保按比例分配
amount0 = liquidity.mul(balance0) / _totalSupply; // using balances ensures pro-rata distribution
//amount1 = 流动性数量 * 余额1 / totalSupply 使用余额确保按比例分配
amount1 = liquidity.mul(balance1) / _totalSupply; // using balances ensures pro-rata distribution
//确认amount0和amount1都大于0
require(
amount0 > 0 && amount1 > 0,
"UniswapV2: INSUFFICIENT_LIQUIDITY_BURNED"
);
//销毁当前合约内的流动性数量
_burn(address(this), liquidity);
//将amount0数量的_token0发送给to地址
_safeTransfer(_token0, to, amount0);
//将amount1数量的_token1发送给to地址
_safeTransfer(_token1, to, amount1);
//更新balance0
balance0 = IERC20(_token0).balanceOf(address(this));
//更新balance1
balance1 = IERC20(_token1).balanceOf(address(this));
//更新储备量
_update(balance0, balance1, _reserve0, _reserve1);
//如果铸造费开关为true, k值 = 储备0 * 储备1
if (feeOn) kLast = uint256(reserve0).mul(reserve1); // reserve0 and reserve1 are up-to-date
//触发销毁事件
emit Burn(msg.sender, amount0, amount1, to);
}