BSEX交易所開(kāi)發(fā)穩(wěn)定版丨BSEX交易所系統(tǒng)開(kāi)發(fā)實(shí)現(xiàn)技術(shù)方案及詳情丨源碼功能
什么是DAp,pDApp應(yīng)用需要滿足那些條件
什么是Dapp?
DApp是Decentralized Application的縮寫,譯為:分散式的應(yīng)用程序。App我們都知道,我們?cè)谥悄苁謾C(jī)上安裝的應(yīng)用程序也就是App。而DApp比App多了一個(gè)‘D’,‘D’的意思是分散式的。所以,它的意思是分散式的應(yīng)用程序/去中心化的應(yīng)用程序。
DApp是一種互聯(lián)網(wǎng)應(yīng)用程序,與傳統(tǒng)的App最大的區(qū)別是:DApp運(yùn)行在去中心化的網(wǎng)絡(luò)上,也就是區(qū)塊鏈網(wǎng)絡(luò)中。網(wǎng)絡(luò)中不存在中心化的節(jié)點(diǎn)可以完整的控制DApp。
Dapp(去中心化應(yīng)用程序)是一種開(kāi)源的應(yīng)用程序,開(kāi)發(fā)流程:I35需求7O98開(kāi)發(fā)O7I8,自動(dòng)運(yùn)行,將其數(shù)據(jù)存儲(chǔ)在區(qū)塊鏈上,以密碼令牌的形式激勵(lì),并以顯示有價(jià)值證明的協(xié)議進(jìn)行操作。
function addLiquidityETH(//添加流動(dòng)性,其中一個(gè)幣種是eth
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,//eth最小輸入量;對(duì)應(yīng)的Desired在msg.value
address to,
uint deadline
)external virtual override payable ensure(deadline)returns(uint amountToken,uint amountETH,uint liquidity){
//調(diào)用內(nèi)部方法_addLiquidity獲取到兩個(gè)幣實(shí)際所需要的數(shù)量
//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轉(zhuǎn)代幣數(shù)量
IWETH(WETH).deposit{value:amountETH}();//調(diào)用weth的兌換方法,通過(guò)eth換weth
assert(IWETH(WETH).transfer(pair,amountETH));//給pair轉(zhuǎn)weth數(shù)量
liquidity=IUniswapV2Pair(pair).mint(to);//調(diào)用pair的mint方法,會(huì)有添加的lp數(shù)量返回
//refund dust eth,if any
//如果傳入的eth數(shù)量,大于實(shí)際所需的eth數(shù)量,將剩余的eth返還給用戶
if(msg.value>amountETH)TransferHelper.safeTransferETH(msg.sender,msg.value-amountETH);
}
//****REMOVE LIQUIDITY****
function removeLiquidity(//移除流動(dòng)性,該方法需要先將lp代幣授權(quán)給路由合約,才能代扣lp
address tokenA,
address tokenB,
uint liquidity,//移除lp的數(shù)量,轉(zhuǎn)入lp得另外兩個(gè)幣
uint amountAMin,//A的最小輸出量
uint amountBMin,//B的最小輸出量
address to,//接收兩個(gè)幣的地址
uint deadline
)public virtual override ensure(deadline)returns(uint amountA,uint amountB){
address pair=UniswapV2Library.pairFor(factory,tokenA,tokenB);//獲取pair地址
IUniswapV2Pair(pair).transferFrom(msg.sender,pair,liquidity);//將lp轉(zhuǎn)到pair地址
//send liquidity to pair
(uint amount0,uint amount1)=IUniswapV2Pair(pair).burn(to);//調(diào)用pair的burn方法,內(nèi)部會(huì)將兩個(gè)幣的數(shù)量轉(zhuǎn)給to,返回值就是兩個(gè)代幣的輸出數(shù)量
(address token0,)=UniswapV2Library.sortTokens(tokenA,tokenB);//通過(guò)排序確認(rèn)兩個(gè)amountA/B
(amountA,amountB)=tokenA==token0?(amount0,amount1):(amount1,amount0);
//校驗(yàn)A/B的輸出量需要小于參數(shù)中要求的最小量,否則交易失敗
require(amountA>=amountAMin,'UniswapV2Router:INSUFFICIENT_A_AMOUNT');
require(amountB>=amountBMin,'UniswapV2Router:INSUFFICIENT_B_AMOUNT');
}
function removeLiquidityETH(//移除流動(dòng)性(其中一個(gè)返還幣是ETH),該方法需要先將lp代幣授權(quán)給路由合約,才能代扣lp
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,//eth最小輸出量
address to,
uint deadline
)public virtual override ensure(deadline)returns(uint amountToken,uint amountETH){
//調(diào)用上面的removeLiquidity方法,傳入的是WETH
(amountToken,amountETH)=removeLiquidity(
token,
WETH,
liquidity,
amountTokenMin,
amountETHMin,
address(this),//注意!接收幣的地址是路由
deadline
);
//將代幣轉(zhuǎn)給to
TransferHelper.safeTransfer(token,to,amountToken);
IWETH(WETH).withdraw(amountETH);//將weth轉(zhuǎn)換成eth
TransferHelper.safeTransferETH(to,amountETH);//將eth轉(zhuǎn)給to
}