Skip to content

Commit

Permalink
feat!: deprecate U256 and use u256 (#1217)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- `U256` is not supported anymore. If used in sway, the SDK will return
a runtime error.
  • Loading branch information
hal3e committed Nov 29, 2023
1 parent ce7c0b4 commit e6b1d76
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 40 deletions.
25 changes: 20 additions & 5 deletions packages/fuels-code-gen/src/program_bindings/resolved_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ impl TypeResolver {
}

pub(crate) fn resolve(&self, type_application: &FullTypeApplication) -> Result<ResolvedType> {
Self::is_deprecated(type_application.type_decl.type_field.as_str())?;

let resolvers = [
Self::try_as_primitive_type,
Self::try_as_bits256,
Expand Down Expand Up @@ -144,6 +146,15 @@ impl TypeResolver {
.collect()
}

fn is_deprecated(type_field: &str) -> Result<()> {
match type_field {
"struct std::u256::U256" | "struct U256" => {
Err(error!("{} is deprecated. Use `u256` instead.", type_field))
}
_ => Ok(()),
}
}

fn try_as_generic(
&self,
type_application: &FullTypeApplication,
Expand Down Expand Up @@ -228,16 +239,20 @@ impl TypeResolver {

let maybe_resolved = match type_field.as_str() {
"()" => Some(ResolvedType::Unit),
"struct std::u128::U128" | "struct U128" => {
let u128_path = TypePath::new("::core::primitive::u128").expect("to be correct");
Some(ResolvedType::Primitive(u128_path))
}
"u8" | "u16" | "u32" | "u64" | "bool" => {
"bool" | "u8" | "u16" | "u32" | "u64" => {
let path = format!("::core::primitive::{type_field}");
let type_path = TypePath::new(path).expect("to be a valid path");

Some(ResolvedType::Primitive(type_path))
}
"struct std::u128::U128" | "struct U128" => {
let u128_path = TypePath::new("::core::primitive::u128").expect("is correct");
Some(ResolvedType::Primitive(u128_path))
}
"u256" => {
let u256_path = TypePath::new("::fuels::types::U256").expect("is correct");
Some(ResolvedType::Primitive(u256_path))
}
_ => None,
};

Expand Down
1 change: 0 additions & 1 deletion packages/fuels-code-gen/src/program_bindings/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ pub(crate) fn sdk_provided_custom_types_lookup() -> HashMap<TypePath, TypePath>
("std::option::Option", "::core::option::Option"),
("std::result::Result", "::core::result::Result"),
("std::string::String", "::std::string::String"),
("std::u256::U256", "::fuels::types::U256"),
("std::vec::Vec", "::std::vec::Vec"),
(
"std::vm::evm::evm_address::EvmAddress",
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-core/src/codec/function_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn resolve_arg(arg: &ParamType) -> String {
ParamType::U32 => "u32".to_owned(),
ParamType::U64 => "u64".to_owned(),
ParamType::U128 => "s(u64,u64)".to_owned(),
ParamType::U256 => "s(u64,u64,u64,u64)".to_owned(),
ParamType::U256 => "u256".to_owned(),
ParamType::Bool => "bool".to_owned(),
ParamType::B256 => "b256".to_owned(),
ParamType::Unit => "()".to_owned(),
Expand Down
30 changes: 13 additions & 17 deletions packages/fuels/tests/types/contracts/u256/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
contract;

use std::u256::U256;

#[allow(dead_code)]
enum SomeEnum<T> {
A: bool,
B: T,
}

abi MyContract {
fn u256_sum_and_ret(some_u256: U256) -> U256;
fn u256_in_enum_input(some_enum: SomeEnum<U256>);
fn u256_in_enum_output() -> SomeEnum<U256>;
fn u256_sum_and_ret(some_u256: u256) -> u256;
fn u256_in_enum_output() -> SomeEnum<u256>;
fn u256_in_enum_input(some_enum: SomeEnum<u256>);
}

impl MyContract for Contract {
#[allow(deprecated)]
fn u256_sum_and_ret(arg: U256) -> U256 {
arg + U256::from((3, 4, 5, 6))
fn u256_sum_and_ret(arg: u256) -> u256 {
arg + 0x0000000000000003000000000000000400000000000000050000000000000006u256
}

fn u256_in_enum_output() -> SomeEnum<u256> {
SomeEnum::B(
0x0000000000000001000000000000000200000000000000030000000000000004u256,
)
}

#[allow(deprecated)]
fn u256_in_enum_input(some_enum: SomeEnum<U256>) {
fn u256_in_enum_input(some_enum: SomeEnum<u256>) {
if let SomeEnum::B(some_u256) = some_enum {
let expected_u256 = U256::from((2, 3, 4, 5));
require(
some_u256 == expected_u256,
some_u256 == 0x0000000000000002000000000000000300000000000000040000000000000005u256,
"given u256 didn't match the expected u256",
);
} else {
require(false, "enum was not of variant B: u256");
}
}

#[allow(deprecated)]
fn u256_in_enum_output() -> SomeEnum<U256> {
SomeEnum::B(U256::from((1, 2, 3, 4)))
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
predicate;

use std::u256::U256;

#[allow(deprecated)]
fn main(arg: U256) -> bool {
arg == U256::from((10, 11, 12, 13))
fn main(arg: u256) -> bool {
arg == 0x000000000000000a000000000000000b000000000000000c000000000000000du256
}
7 changes: 2 additions & 5 deletions packages/fuels/tests/types/scripts/script_u256/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
script;

use std::u256::U256;

#[allow(deprecated)]
fn main(arg: U256) -> U256 {
arg + U256::from((6, 7, 8, 9))
fn main(arg: u256) -> u256 {
arg + 0x0000000000000006000000000000000700000000000000080000000000000009u256
}
10 changes: 4 additions & 6 deletions packages/fuels/tests/types_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,12 +930,12 @@ async fn strings_must_have_correct_length_custom_types() {
"type": "enum MyEnum",
"components": [
{
"name": "foo",
"name": "Foo",
"type": 1,
"typeArguments": null
},
{
"name": "bar",
"name": "Bar",
"type": 3,
"typeArguments": null
}
Expand Down Expand Up @@ -980,7 +980,7 @@ async fn strings_must_have_correct_length_custom_types() {
let contract_instance = SimpleContract::new(null_contract_id(), wallet);
let _ = contract_instance
.methods()
.takes_enum(MyEnum::bar("fuell".try_into().unwrap()));
.takes_enum(MyEnum::Bar("fuell".try_into().unwrap()));
}

#[tokio::test]
Expand Down Expand Up @@ -1747,17 +1747,15 @@ async fn test_u256() -> Result<()> {
let contract_methods = contract_instance.methods();
{
let arg = u256_from((1, 2, 3, 4));

let actual = contract_methods.u256_sum_and_ret(arg).call().await?.value;

let expected = arg + u256_from((3, 4, 5, 6));

assert_eq!(expected, actual);
}
{
let actual = contract_methods.u256_in_enum_output().call().await?.value;

let expected = SomeEnum::B(u256_from((1, 2, 3, 4)));

assert_eq!(expected, actual);
}
{
Expand Down

0 comments on commit e6b1d76

Please sign in to comment.