我在计算初始tokenSupply参数时遇到了困难,这是初始化Bancor风格的键合曲线契约所需的两个参数之一。
公式:
P = sent Ether in Wei
R = reserve ratio
M = Slope
tokenSupply = (P/(R*M))^R假设:
P = 1 * 10^18
R = .333333
M = .0025
N = Scaling Factor = 1/1,000,000这似乎很好:
tokenSupply = 1*10^18 / ((N)333,333 * (N)2500) * N现在:
tokenSupply = ( 1*10^18 / (N)833 )^R 但第一:
我能将未标度的魏除以比例乘积(N)833,并假定商具有相同的标度因子N?
.#2:
我怎么把这个商数提高到R呢?(假设R的初始标度因子,333,333是不可能的),我怎么知道定标因子是什么结果tokenSupply?(我不知道如何处理缩放指数)。谢谢
我正在使用SafeMath来处理溢出
资料来源:https://blog.relevant.community/bonding-curves-in-depth-intuition-parametrization-d3905a681e0a https://en.wikipedia.org/wiki/Fixed-point_算术
发布于 2019-01-27 06:41:46
我能否将未标度的魏除以缩放积(833),并假定商是相同的缩放因子的缩放积?
这个问题对我来说不是很清楚,但是tokenSupply = 1*10^18 / ((N)333,333 * (N)2500) * N似乎错了,因为你在用N*N来缩放分母,但是只将结果乘以N。
如果你是从链上做的,那么就不需要任何缩放因子了,你可以简单地计算P/(R*M)。
如果你是在链上做的,那么你应该用N*N而不是N来乘以结果。另外,请注意,你更好的做法是在你分裂之前而不是之后。
总之,您应该这样做:P*N*N/(R*N*M*N)。
P.S.:在您的数字示例中,您可以使用较小的N值(100 K而不是1米)。
我怎么把这个商数提高到R呢?
存款准备金率必须是0到1之间的值.
如果你是从链上做的,那么只需提高到R的力量。
如果你在链上做这件事,你可以使用班科幂函数,它把指数作为分子和分母的一个元组,然后把R作为333331000000传递给它。
如果您正在执行链上的所有操作,那么我建议您使用班科的power函数“一路走”:
uint256 result;
uint8 precision;
uint256 baseN = P.mul(10000000000);
uint256 baseD = R*M scaled by 10000000000
uint256 expN = R scaled by 1000000
uint256 expD = 1000000
(result, precision) = power(baseN, baseD, expN, expD);此时,可以使用supply计算result >> precision的整数值。
发布于 2020-02-06 11:47:57
Solidity不支持开箱即用的分数数学,所以您需要使用某种库,或者自己实现分数。我建议在您的任务中使用ABDKMathQuad库。下面是代码的外观:
function calculateBancorTotalSupply (
uint etherAmount,
uint reserveRatio,
uint slope,
uint scale)
public pure returns (uint) {
bytes16 scaleQuad = ABDKMathQuad.fromUInt (scale);
bytes16 reserveRatioQuad = ABDKMathQuad.div (
ABDKMathQuad.fromUInt (reserveRatio),
scaleQuad);
bytes16 slopeQuad = ABDKMathQuad.div (
ABDKMathQuad.fromUInt (slope),
scaleQuad);
return ABDKMathQuad.toUInt (
ABDKMathQuad.pow_2 (
ABDKMathQuad.mul (
ABDKMathQuad.log_2 (
ABDKMathQuad.div (
ABDKMathQuad.fromUInt (etherAmount),
ABDKMathQuad.mul (reserveRatioQuad, slopeQuad))),
reserveRatioQuad)));
}为
etherAmount = 1000000000000000000
reserveRatio = 333333
slope = 2500
scale = 1000000这段代码给了我10626417,它和谷歌返回相同的数据谷歌返回相同的数据一样。该函数消耗26876 gas (Solity0.5.14,启用优化)。
Bancor的power解决方案,建议 良好振动,返回10625630支出37181气体(在启用优化的情况下,稳定度为0.4.26 )。因此,与ABDKMathQuad解决方案相比,班科的解决方案似乎更不精确,也更昂贵。
下面是我用来测试Bancor解决方案的代码:
function calculateBancorTotalSupply (
uint etherAmount,
uint reserveRatio,
uint slope,
uint scale)
public view returns (uint) {
uint256 result;
uint8 precision;
uint256 baseN = etherAmount * scale * scale;
uint256 baseD = reserveRatio * slope;
uint256 expN = reserveRatio;
uint256 expD = scale;
(result, precision) = power(baseN, baseD, uint32 (expN), uint32 (expD));
return result >> precision;
}https://ethereum.stackexchange.com/questions/66191
复制相似问题