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

简介: ? (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;
}

相关文章
|
11月前
|
算法 区块链
Defi+NFT质押流动性挖矿系统开发/LP质押挖矿功能开发解析
Defi+NFT质押流动性挖矿系统开发/LP质押挖矿功能开发解析
|
11月前
|
区块链
Defi代币质押持币生息系统技术开发/源代码/质押挖矿dapp
Defi代币质押持币生息系统技术开发/源代码/质押挖矿dapp
|
11月前
|
JavaScript 前端开发 区块链
DAPP代币合约流动性质押分红挖矿系统开发丨技术分析
DAPP代币合约流动性质押分红挖矿系统开发丨技术分析
|
存储 前端开发 JavaScript
浅谈DAPP智能合约流动性质押挖矿分红系统开发技术分析及代码部署
“Web3.0”是一个用户共建、隐私保护、平台开放的生态体系。相比“Web2.0”,“Web3.0”的目标是实现一个更加开放、零信任或是低信任、无许可的价值互联网时代。其生态体系主要由“Web3.0钱包”、“稳定币”、“公链”、“NFT”、“GameFi”、“去中心化存储”、“Dapp”、“DAO”、“DeFi”构成。
浅谈DAPP智能合约流动性质押挖矿分红系统开发技术分析及代码部署
|
机器学习/深度学习 人工智能 安全
PtahDao/ProTradex/Meta2032流动性质押挖矿分红系统开发(NFT质押挖矿开发)丨成熟及源码
 区块链技术由此可以从多方面为企业赋能:提供可靠的共享数据,在各方之间建立信任;消除数据孤岛,即通过去中心化的,在一个网络中共享并支持获许可方访问的账本将数据集成到一个系统中;为数据赋予高度安全性
|
存储 缓存 算法
流动性质押挖矿分红开发源码版,流动性质押挖矿分红系统开发技术详细及分析
共识模块主要由几个组件组成,世代epoch、提案消息缓存服务msgcache、共识消息处理引擎engine、共识消息验证器verifier、提案消息存储服务forest、投票处理器voter、共识活性服务pacemaker、wal存储wal、节点间共识信息同步服务compensator、各模块相互配合实现maxbft流水线共识算法
|
区块链
智能合约互助公排流动性质押挖矿开发正式版丨智能合约互助公排流动性质押挖矿系统开发(详情开发及源码)
The universe is the successor of the mobile internet,and the doors of the virtual world and the real world have been opened.The metauniverse may become the new direction of the development of the Internet,and may also be the next form of the development of the digital economy.The exploration of the
|
人工智能
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