fuel
December 30, 2022, 3:21pm
1
This script failed with an error Calling private library method from_uint is not allowed.
script;
use sway_libs::i128::I128;
use std::u128::U128;
fn main() -> bool {
let u128_one = U128 {
upper: 0,
lower: 1,
};
let u128_two = U128 {
upper: 0,
lower: 2,
};
let one = I128::from(u128_one);
let mut res = one + I128::from(u128_one);
assert(res == I128::from(u128_two));
let u128_10 = U128 {
upper: 0,
This file has been truncated. show original
--> /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
fuel
December 30, 2022, 3:23pm
2
Version of sway_libs
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.4.1" }
3 Likes
fuel
December 30, 2022, 3:38pm
3
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
fuel
December 30, 2022, 4:58pm
4
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
fuel
December 31, 2022, 1:09pm
5
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
fuel
December 31, 2022, 1:11pm
6
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
fuel
January 4, 2023, 12:57am
10
FuelLabs:master
β chlenc:signed-integers-basic-functional-suggestion
opened 12:51AM - 04 Jan 23 UTC
## Type of change
- Bug fix
- New feature
## Changes
The following c⦠hanges have been made to I128 and I256:
- 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 tests
- Added check to test not to be failed
The following changes have been made to I8, I16, I32 and I64:
- added functions
> fn flip(self) -> Self
> fn ge(self, other: Self) -> bool
> fn le(self, other: Self) -> bool
- A tests
- Added check to test not to be failed
## Notes
But my tests are still failing because of the problem I have mentioned before in this topic
https://forum.fuel.network/t/cannot-use-from-uint-in-the-i128-from-sway-libs/1255
Type of change
Changes
The following changes have been made:
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