bond curve

  • bixia1994
  • 更新于 2024-09-22 17:46
  • 阅读 422

Certainly!I'llrewritetheexplanationoftheapproximationusedinthe_ethToTokensfunctioninmarkdownformat:BondingCurveApproximationinDoju

Certainly! I'll rewrite the explanation of the approximation used in the _ethToTokens function in markdown format:

Bonding Curve Approximation in Doju Contract

The _ethToTokens function uses an approximation to calculate the number of tokens to mint for a given ETH amount. Let's break down this approximation:

// Bonding curve formula to calculate how many tokens to mint for given ETH
    function _ethToTokens(uint256 ethAmount, uint256 currentSupply) internal pure returns (uint256) {
        uint256 k = currentSupply * currentSupply;
        uint256 newSupply = sqrt(k + 2 * ethAmount * 1e18);
        return newSupply - currentSupply;
    }

The Approximation Formula

The core approximation used is: $$ x_2 \approx \sqrt{x_1^2 + 2\text{ETH}} $$ Where:

x2 is the new supply after buying tokens

x1 is the current supply

ETH is the amount of Ether used to buy tokens

Derivation of the Approximation

    // Bonding curve formula to calculate how many tokens to mint for given ETH
    function _ethToTokens(uint256 ethAmount, uint256 currentSupply) internal pure returns (uint256) {
        uint256 k = currentSupply * currentSupply;
        uint256 newSupply = sqrt(k + 2 * ethAmount * 1e18);
        return newSupply - currentSupply;
    }

% Basic Bonding Curve Equation

$$ x \cdot y = k $$

% Price as a Function of Supply

$$ y = \frac{k}{x} $$

% Integral for Token Purchase

$$ \text{Cost} = \int_{x_1}^{x_2} \frac{k}{x} dx $$

% Solving the Integral

$$ \text{Cost} = k \cdot \ln\left(\frac{x_2}{x_1}\right) $$

% Rearranging for New Supply

$$ x_2 = x_1 \cdot e^{\frac{\text{ETH}}{k}} $$

% Taylor Series Approximation

$$ e^x \approx 1 + x $$

% Applying the Approximation

$$ x_2 \approx x_1 \cdot \left(1 + \frac{\text{ETH}}{k}\right) $$

% Squaring Both Sides

$$ x_2^2 \approx x_1^2 \cdot \left(1 + \frac{2\text{ETH}}{k} + \left(\frac{\text{ETH}}{k}\right)^2\right) $$

% Simplifying

$$ x_2^2 \approx x_1^2 + 2\text{ETH} $$

% Final Approximation

$$ x_2 \approx \sqrt{x_1^2 + 2\text{ETH}} $$

Sell part

    // Bonding curve formula to calculate how much ETH to return for given tokens
    function _tokensToEth(uint256 tokenAmount) internal view returns (uint256) {
        uint256 currentSupply = totalSupply;
        uint256 k = currentSupply * currentSupply;
        uint256 newSupply = currentSupply - tokenAmount;
        uint256 newK = newSupply * newSupply;
        return (k - newK) / (2 * 1e18);
    }

Implementation Details

In the contract, $$k = x1^2$$ (currentSupply currentSupply)

A factor of 1e18 is added for precision: 2 ethAmount 1e18

Advantages

Computationally efficient (avoids expensive exponential calculations)

Reasonably accurate for small to moderate purchases

Maintains essential properties of the bonding curve (price increases with supply)

Potential Limitations

  1. May become less accurate for very large purchases

Could lead to slight discrepancies between buying and selling

Mathematical Intuition

The $$√(x1^2 + 2*ETH)$$ formula creates a curve that closely mimics the ideal exponential curve

As ETH increases, the new supply increases, but at a decreasing rate, reflecting the rising price

This approximation allows the contract to implement a bonding curve mechanism efficiently while maintaining the core principle that token price increases as more tokens are minted. It's a practical compromise between mathematical accuracy and computational efficiency in the context of smart contract execution.

Square Root Function in Doju Contract

Mathematical Foundation

  1. For any positive real numbers a and b:

$$ \frac{a + b}{2} \geq \sqrt{ab} $$

with equality if and only if a = b.

If x > 0 and z > 0, then:

$$ \frac{z + \frac{x}{z}}{2} \geq \sqrt{x} $$

with equality if and only if z = √x.

The Babylonian Method

The core of the algorithm is the iterative formula:

$$ z_{n+1} = \frac{1}{2}\left(z_n + \frac{x}{z_n}\right) $$

where z_n is the n-th approximation of √x, converging to √x.

Intuition

If z_n > √x, then x/z_n < √x

If z_n < √x, then x/z_n > √x

The average of z_n and x/z_n is closer to √x than either value

Convergence

Each iteration brings z_n closer to √x

The method converges quadratically, approximately doubling the correct digits with each iteration

Error Analysis

Let e_n = z_n - √x be the error at step n. Then:

$$ e_{n+1} \approx -\frac{e_n^2}{2\sqrt{x}} $$

This rapid error reduction leads to quick convergence.

Implementation in Solidity

// Square root helper function (for bonding curve math)
    function sqrt(uint256 x) internal pure returns (uint256) {
        uint256 z = (x + 1) / 2; // a = x, b = 1, (a + b) / 2 >= sqrt(ab)
        uint256 y = x;
        while (z &lt; y) { // y = (x/z + z)/2; z = sqrt(x)
            y = z;
            z = (x / z + z) / 2;
        }
        return z;
    }

Initial Guess

$$ z_0 = \frac{x + 1}{2} $$

This ensures z_0 ≥ √x for x ≥ 1.

Termination Condition

The algorithm terminates when:

$$ z_{n+1} \geq z_n $$

At this point, z_n is the integer square root of x, satisfying:

$$ z_n^2 \leq x < (z_n + 1)^2 $$

Certainly! Let's dive deeper into the termination condition of the square root function:

while (z &lt; y) {
            y = z;
            z = (x / z + z) / 2;
        }

The termination condition z < y is crucial for understanding when and why the algorithm stops. Here's a more detailed explanation:

  1. Loop Continuation:

The loop continues as long as the new approximation z is less than the previous approximation y.

Convergence:

As the algorithm progresses, z and y get closer to the actual square root of x.

Integer Arithmetic:

In Solidity, all calculations are done with integer arithmetic. This means that at some point, z will no longer change significantly enough to be different from y after integer division.

Termination Point:

The loop terminates when z >= y. This occurs when:

$$ \frac{x}{z_n} + z_n \leq 2z_n $$

Which is equivalent to:

$$ \frac{x}{z_n} \leq z_n $$

  1. Significance:

When this condition is met, we've found the largest integer z such that:

$$ z^2 \leq x < (z+1)^2 $$

This is precisely the definition of the integer square root.

  1. Precision:

Due to integer arithmetic, this method always finds the floor of the actual square root.

  1. Efficiency:

This termination condition ensures the algorithm stops as soon as it reaches the best integer approximation, avoiding unnecessary iterations.

In LaTeX format, the key condition at termination can be expressed as:

$$ z_n^2 \leq x < (z_n + 1)^2 $$

Where $z_n$ is the final value of z when the loop terminates.

This termination condition elegantly combines efficiency and accuracy, making it ideal for gas-efficient square root calculations in Solidity smart contracts.

Why It Works for Integer Square Roots

Definition: The integer square root of x is the largest integer z such that z² ≤ x.

Convergence: The algorithm approaches √x from above.

Integer arithmetic in Solidity ensures we always work with integers.

The termination condition guarantees we find the largest integer z where z² ≤ x.

This method efficiently approximates the square root using only basic arithmetic operations, making it suitable for gas-efficient computations in Solidity.

点赞 1
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
bixia1994
bixia1994
0x92Fb...C666
learn to code