Cannot use from_uint in the I128 from sway_libs

This script failed with an error Calling private library method from_uint is not allowed.

  --> /Users/alexey/projects/fuel/sway-lend/sway-scripts/functions_test_script/src/main.sw:45:33
   |
43 | 
44 | 
45 |     res = I128::from(u128_10) / I128::from_uint(u128_lower_max_u64);
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Calling private library method from_uint is not allowed.
46 |     assert(res == I128::neg_from(u128_10));
   |
____

  Aborting due to 1 error.
Error: Failed to compile functions_test_script
14 Likes

Version of sway_libs
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.4.1" }

3 Likes

By the way, when I copied the standard locally to my computer and imported it using a relative path, it worked, maybe the author should make a small fix and it will work

3 Likes

After I downloaded sway_libs locally I started to study the tests. I modified each of the Integer tests this way (added a check to make sure the test passes)

 async fn runs_i128_test_script() {
        let path_to_bin = "test_projects/signed_i128/out/debug/i128_test.bin
        let wallet = launch_provider_and_get_wallet();

        let instance = Testi128::new(wallet, path_to_bin);

        let _result = instance.main().call().await;
        assert_eq!(_result.is_err(), false);
    }

And that’s what we got

I think there is some bug in the libraries from i8 to i256 that interferes with these numbers

3 Likes

By logging it was found out that the test crashes on the first asert because the numbers are not equal

[
    "I128 { underlying: U128 { upper: 1, lower: 1 } }",
    "I128 { underlying: U128 { upper: 2, lower: 2 } }",
]
3 Likes

Also wanted to note that the standard I128 lacks some methods that would simplify working with it. I have created a library in which I implement the missing methods in my opinion. So far it looks like this

library int;
use sway_libs::i128::I128;
use std::u128::U128;

impl I128 {
    pub fn from_u64(value: u64) -> I128 {
        Self {
            underlying: U128::from((0, value))
        }
    }

    pub fn as_u64(self) -> u64 {
        if self.underlying < Self::indent() {revert(0)} else {self.underlying.as_u64().unwrap()}
    }

    pub fn flip(self) -> Self {
        self * Self {
            underlying: Self::indent() - self.underlying,
        }
    }
}

impl I128{
    pub fn ge(self, other: Self) -> bool {
        self > other || self == other
    }

    pub fn le(self, other: Self) -> bool {
        self < other || self == other
    }

    pub fn zero() -> I128 {
        Self::from_u64(0)
    }
}
3 Likes

Will make this public, hope that solves your problem.

3 Likes

Investigating this, should be solved soon.

3 Likes

We would appreciate it if you opened a PR in the sway-libs repo so we could add these functions.

3 Likes

Type of change

  • Bug fix
  • New feature

Changes

The following changes have been made:

  • added functions

fn from_u64(value: u64) β†’ I128
fn as_u64(self) β†’ u64
fn flip(self) β†’ Self
fn ge(self, other: Self) β†’ bool
fn le(self, other: Self) β†’ bool
fn zero() β†’ I128

  • A test success check has been added for the int

Notes

I also need to add to these changes all the same but for the other numbers except I128, as well as cover everything with tests.
Please check the pull request and leave your comments if everything is ok and after that I will update the pull request.

3 Likes

Are there any updates?