Suchen und Verwenden von Solidity-Bibliotheken (dh mathematische Funktionen)

Wie finde und verwende ich vorhandene Solidity-Bibliotheken, wie Sie es vielleicht in Javascript tun?

Zum Beispiel mathematische Funktionen: Wie implementiere ich das Äquivalent von Math.sqrt(x) in einer Solidity-Funktion?

Verwandte: Was macht --libraries in dieser zweiten Frage (falls hier relevant)?

Ich habe mir die Dokumente und Antworten unten angesehen, aber sie schienen dies nicht zu beantworten. Vielen Dank.

Wie importiere ich Solidity-Bibliotheken in Mix?

Kein Gasfehler beim Bereitstellen der Bibliothek

Antworten (2)

Derzeit ist die Standardbibliothek noch nicht vollständig eingerichtet, aber in diesem Fall würden Sie Ihre Verträge mit der Adresse Ihrer Bibliothek verknüpfen (und sie in diesem Fall hoffentlich einer benannten Registrierung zuordnen). Das wird in Zukunft implementiert und es wird viel einfacher sein, wenn es soweit ist.

Bis dahin müssen Sie es jedoch manuell, dh auf die harte Tour, tun.

Eine Sache, die Sie tun würden, wäre, die Quadratwurzelfunktion aus diesem Dapp-Bin zu ziehen (obwohl es sich ehrlich gesagt nicht lohnt, sie an dieser Stelle zu verwenden, da die Festkommavariable noch eingerichtet werden muss, aber wir werden sie für dieses Beispiel verwenden).

https://github.com/ethereum/dapp-bin/blob/master/library/math.sol

Sie würden etwas in der Art von tun

import "math.sol"

contract myContract {
    function f() {
        int a = Math.sqrt(9);
    }
}

Von dort aus ermöglichte Ihnen das Flag --libraries, auf bestimmte Adressen zu verlinken, wobei das Argument der Name der Bibliothek ist, gefolgt von der Adresse. Wenn Sie also die mathematische Bibliothek unter der Adresse 0x123456 haben, würden Sie sie benennen:

solc yourContract.sol --libraries Math: 0x123456
Wirklich lustige Bibliothek, die es wirklich wert ist, verwendet zu werden -- EC-Arithmetik 100% in Solidity!! github.com/jbaylina/ecsol

Meine erweiterte Mathematikbibliothek PRBMath bietet sqrtneben vielen anderen Funktionen . Ich werde meine Implementierung hier für die Nachwelt einfügen, aber sehen Sie sich das verlinkte Repo für den aktuellsten Code an.

/// @notice Calculates the square root of x, rounding down.
/// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
/// @param x The uint256 number for which to calculate the square root.
/// @return result The result as an uint256.
function sqrt(uint256 x) internal pure returns (uint256 result) {
    if (x == 0) {
        return 0;
    }

    // Calculate the square root of the perfect square of a power of two that is the closest to x.
    uint256 xAux = uint256(x);
    result = 1;
    if (xAux >= 0x100000000000000000000000000000000) {
        xAux >>= 128;
        result <<= 64;
    }
    if (xAux >= 0x10000000000000000) {
        xAux >>= 64;
        result <<= 32;
    }
    if (xAux >= 0x100000000) {
        xAux >>= 32;
        result <<= 16;
    }
    if (xAux >= 0x10000) {
        xAux >>= 16;
        result <<= 8;
    }
    if (xAux >= 0x100) {
        xAux >>= 8;
        result <<= 4;
    }
    if (xAux >= 0x10) {
        xAux >>= 4;
        result <<= 2;
    }
    if (xAux >= 0x8) {
        result <<= 1;
    }

    // The operations can never overflow because the result is max 2^127 when it enters this block.
    unchecked {
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1; // Seven iterations should be enough
        uint256 roundedDownResult = x / result;
        return result >= roundedDownResult ? roundedDownResult : result;
    }
}