公排互助拆分系統(tǒng)開發(fā)(案例開發(fā)),3M/DAPP拆分公排互助系統(tǒng)開發(fā)(開發(fā)邏輯)及源碼
區(qū)塊鏈技術(shù)可以構(gòu)建一個(gè)高效可靠的價(jià)值傳輸系統(tǒng),推動(dòng)互聯(lián)網(wǎng)成為構(gòu)建社會(huì)信任的網(wǎng)絡(luò)基礎(chǔ)設(shè)施,實(shí)現(xiàn)價(jià)值的有效傳遞,并將此稱為價(jià)值互聯(lián)網(wǎng)。區(qū)塊鏈提供了一種新型的社會(huì)信任機(jī)制,為數(shù)字經(jīng)濟(jì)的發(fā)展奠定了新基石,
什么是智能合約DApp。智能合約DApp是使用區(qū)塊鏈技術(shù)實(shí)現(xiàn)去中心化應(yīng)用(DApp)的核心技術(shù)。所謂智能合約是指以數(shù)字代碼的形式編寫的自動(dòng)執(zhí)行計(jì)算機(jī)程序,A smart contract system that automatically executes and manages the rights and interests of all parties that collaborate with each other.
在區(qū)塊鏈中,每個(gè)塊包含了一定數(shù)量的交易信息和該塊的唯一標(biāo)識(shí)符,同時(shí)還包含了前一個(gè)塊的哈希值。這樣的設(shè)計(jì)保證了區(qū)塊之間的順序和完整性,一旦一個(gè)塊被添加到區(qū)塊鏈中,它就不可更改。This makes blockchain a secure and trustworthy distributed ledger that can be used to record and verify various types of transactions.
//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
//計(jì)算返回的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轉(zhuǎn)amount0/1
_safeTransfer(_token0,to,amount0);
_safeTransfer(_token1,to,amount1);
//獲取轉(zhuǎn)賬后的balance
balance0=IERC20(_token0).balanceOf(address(this));
balance1=IERC20(_token1).balanceOf(address(this));
//更新reserve0,reserve1和時(shí)間戳
_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
//交易函數(shù)
//可以是token0-->token1,
//也可以是token1-->token0
//但參數(shù)中:amount0Out和amount1Out中有一個(gè)值是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');
//劃轉(zhuǎn)操作
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
//防止數(shù)據(jù)溢出校驗(yàn)
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');
}