xdefilab / xdefi-governance-token

XDeFi Yield Farming & XDEX on Ethereum
https://farm.xdefi.com
MIT License
12 stars 3 forks source link

pendingXDEX计算,deposit和withdraw #6

Closed tturbulence closed 3 years ago

tturbulence commented 3 years ago

直接把pending 28开(20%立刻解锁,80%进stream)

具体逻辑

原有设计:涉及了pending,然后直接把pending 输入到fund里面,从此刻开始,从0等待解锁。

新逻辑:

pending已经有一部分可以立刻解锁了,而剩下的80%,因为是从pool.lastRewardBlock到block.number累计的,所以仍然是需要有立刻解锁的部分,而且我们仍然需要2个参数:(和https://github.com/xdefilab/xdefi-governance-token/issues/9类似 )

stream.unlockRatio(精度1000)以及stream.kBlock

因为解锁多少,fund进去之后,是需要拆分成2部分的,这个逻辑需要的变量可能是跨域的。可以考虑回传totalpending的时候同时回传block.number.sub(pool.lastRewardBlock) ,然后再去参考stream.unlockRatio(精度1000)以及stream.kBlock去参与计算,这一部分的计算参考Python

··· K = 40 N = 1000 s = 0 M = 70000 m = MK/N mu = 0.001 for i in range(N//K): s+= m(1-mu)**i print(s,M-s) #前面未解锁,后面解锁

化简以上函数为 s - s(1-mu) == m - m(1-0.001)*(N+1) => s = (m - m(1-0.001)**(N+1))/mu

def compute_s(m,N,mu,K): return (m - m*(1-mu)**(N//K))/mu ···

可以在fundstoStream多传一个blockHeightDiff,然后在Stream内部,直接操作未解锁和可解锁余额。

uint256 ONE= 10*18; uint256 instantUnlockPending = totalPending / 5 ; // Safer uint256 remainPending = totalPending.sub(instantUnlockPending); uint256 m = remainPending.mul(stream.kBlock).div(blockHeightDiff); // If underflow m might be 0 uint256 noverk = ONE blockHeightDiff.div(stream.kBlock); // decimal uint256 mu = ONE * stream.unlockRatio.div(1000); // decimal uint256 onesubmu = ONE.sub(mu); // uint256 s = m.mul(ONE.sub(XNUM.bpow(onesubmu,noverk))).div(ONE).div(mu).mul(ONE); uint256 s = m.mul(ONE.sub(XNUM.bpow(onesubmu,noverk))).div(mu); remainPending = remainPending.sub(s); instantUnlockPending = instantUnlockPending.add(s);

至此,Stream被Fund了一笔remainPending等着xHalfLife慢慢解锁,而instantUnlockPending是直接打到Stream的可提取里面的。点击Harvest的人支付了这些转账费用。

xdefilab commented 3 years ago

https://github.com/xdefilab/xdefi-governance-token/pull/13

xdefilab commented 3 years ago

https://github.com/xdefilab/xdefi-governance-token/pull/14 继续做了优化

xdefilab commented 3 years ago

pr14已merge https://github.com/xdefilab/xdefi-governance-token/pull/14