流动性质押挖矿矿池系统开发(开发案例)丨流动性质押挖矿矿池系统开发(详细及源码)

简介: ? (amountSpecified - state.amountSpecifiedRemaining, state.amountCalculated) (state.amountCalculated, amountSpecified - state.amountSpecifiedRemaining);

  在进行交易输入/输出的计算时,和流动性的计算一样,也会遇到rounding的问题,处理的原则是:

  当计算output时,使用RoundDown,保证pool不会出现坏账

  当计算input时,使用RoundUp,保证pool不会出现坏账

  当通过input计算P−−√P时,如果P−−√P会减少,那么使用RoundUp,这样可以保证ΔP−−√ΔP被RoundDown,在后续计算output时不会使pool出现坏账。反之如果P−−√P会增大,那么使用RoundDown

  当通过output计算P−−√P时,I35 Develop 7O98 system O7I8 如果P−−√P会减少,那么使用RoundDown,这样可以保证ΔP−−√ΔP被RoundUp,在后续计算input时不会使pool出现坏账。反之如果P−−√P会增大,那么使用RoundUp

  交易收尾阶段

  我们再回到swap函数中循环检查条件:

  while(state.amountSpecifiedRemaining!=0&&state.sqrtPriceX96!=sqrtPriceLimitX96){

  ...

  }

  即通过通过tokenIn是否还有余额来判断是否还需要继续循环,进入下一步的进行交易计算。当tokenIn全部被耗尽后,交易就结束了。当交易结束后,我们还需要做这些事情:

  更新预言机

  更新当前交易对的价格P−−√P,流动性LL

  更新手续费累计值

  扣除用户需要支付的token

  关于手续费,预言机的相关内容,会在其他章节讲解,我们先跳过这部分代码,直接看swap函数的末尾:

 // 确定最终用户支付的 token 数和得到的 token 数
(amount0, amount1) = zeroForOne == exactInput

? (amountSpecified - state.amountSpecifiedRemaining, state.amountCalculated)
: (state.amountCalculated, amountSpecified - state.amountSpecifiedRemaining);

// 扣除用户需要支付的 token
if (zeroForOne) {

// 将 tokenOut 支付给用户,前面说过 tokenOut 记录的是负数
if (amount1 < 0) TransferHelper.safeTransfer(token1, recipient, uint256(-amount1));

uint256 balance0Before = balance0();
// 还是通过回调的方式,扣除用户需要支持的 token
IUniswapV3SwapCallback(msg.sender).uniswapV3SwapCallback(amount0, amount1, data);
// 校验扣除是否成功
require(balance0Before.add(uint256(amount0)) <= balance0(), 'IIA');

} else {

...

}

// 记录日志
emit Swap(msg.sender, recipient, amount0, amount1, state.sqrtPriceX96, state.tick);
// 解除防止重入的锁
slot0.unlocked = true;
}

相关文章
|
6月前
|
区块链
Defi代币质押持币生息系统技术开发/源代码/质押挖矿dapp
Defi代币质押持币生息系统技术开发/源代码/质押挖矿dapp
|
6月前
|
算法 区块链
Defi+NFT质押流动性挖矿系统开发/LP质押挖矿功能开发解析
Defi+NFT质押流动性挖矿系统开发/LP质押挖矿功能开发解析
|
6月前
|
JavaScript 前端开发 区块链
DAPP代币合约流动性质押分红挖矿系统开发丨技术分析
DAPP代币合约流动性质押分红挖矿系统开发丨技术分析
|
机器学习/深度学习 人工智能 安全
PtahDao/ProTradex/Meta2032流动性质押挖矿分红系统开发(NFT质押挖矿开发)丨成熟及源码
 区块链技术由此可以从多方面为企业赋能:提供可靠的共享数据,在各方之间建立信任;消除数据孤岛,即通过去中心化的,在一个网络中共享并支持获许可方访问的账本将数据集成到一个系统中;为数据赋予高度安全性
|
5G 区块链 vr&ar
DAPP智能合约流动性质押挖矿分红开发案例版丨DAPP智能合约流动性质押挖矿分红系统开发(开发案例及源码)
The metauniverse is an immersive digital world created by the combination of virtual reality,augmented reality and the Internet.The connotation and key technologies of the metauniverse require further breaking the space-time limit(5G and the Internet of Things),real immersion(VR),and value transmi
|
人工智能
defi流动性质押挖矿系统丨defi流动性质押挖矿系统开发(dapp开发)丨defi流动性质押挖矿源码版
 Liquidity mining,in short,is a token incentive plan designed to attract liquidity providers(LPs)to provide liquidity for specific transaction pairs/pools on AMM.
NFT流动性质押挖矿开发功能丨NFT流动性质押挖矿系统开发(开发详细)丨NFT流动性质押挖矿系统源码部署
  Liquidity mining is an incentive mechanism to promote the use of DeFi and a new decentralized token distribution mechanism.Most DeFi applications require users to lock tokens into the contract.The larger the amount of lock-in the contract has,the better financial services can be obtained.Liquidity
|
开发工具
defi/nft流动性质押挖矿分红开发正式版,defi/nft流动性质押挖矿分红系统开发(成熟案例及源码)
Liquidity mining usually requires pledge of loan vouchers or LP vouchers. That is to say, instead of pledging the original token to get the reward of liquidity mining, users need to pledge the original token in DeFi first, and then pledge the voucher to the specified contract to get the reward of li
|
人工智能
DAPP流动性矿池挖矿分红开发丨DAPP流动性矿池挖矿分红系统开发详细及逻辑丨DAPP流动性矿池挖矿分行系统源码部署
  Smart contract:Smart contract is a program running on the ETH blockchain,which defines the logic behind the state changes on the blockchain.In abstract,smart contract is a rule that can be executed automatically.In real life,a contract needs to have a special execution role after the contract is f