Skip to content

Commit 53cee39

Browse files
authored
Merge branch 'master' into master
2 parents aa0cb0c + 91c3ae6 commit 53cee39

File tree

37 files changed

+3122
-2171
lines changed

37 files changed

+3122
-2171
lines changed

docs/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
- [`Address`](./types/address.md)
5252
- [`ContractId`](./types/contract-id.md)
5353
- [`AssetId`](./types/asset-id.md)
54-
- [Converting native types](./types/conversion.md)
5554
- [`Bech32`](./types/bech32.md)
5655
- [Structs and enums](./types/custom_types.md)
5756
- [`String`](./types/string.md)
@@ -60,6 +59,7 @@
6059
- [`B512`](./types/B512.md)
6160
- [`EvmAddress`](./types/evm_address.md)
6261
- [Vectors](./types/vectors.md)
62+
- [Converting types](./types/conversion.md)
6363
- [Codec](./codec/index.md)
6464
- [Encoding](./codec/encoding.md)
6565
- [Decoding](./codec/decoding.md)

docs/src/types/conversion.md

Lines changed: 208 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,214 @@
1-
# Converting native types
1+
# Converting Types
2+
3+
Below you can find examples for common type conversions:
4+
5+
- [Convert Between Native Types](#convert-between-native-types)
6+
- [Convert to `Bytes32`](#convert-to-bytes32)
7+
- [Convert to `Address`](#convert-to-address)
8+
- [Convert to `ContractId`](#convert-to-contractid)
9+
- [Convert to `Identity`](#convert-to-identity)
10+
- [Convert to `AssetId`](#convert-to-assetid)
11+
- [Convert to `Bech32`](#convert-to-bech32)
12+
- [Convert to `str`](#convert-to-str)
13+
- [Convert to `Bits256`](#convert-to-bits256)
14+
- [Convert to `Bytes`](#convert-to-bytes)
15+
- [Convert to `B512`](#convert-to-b512)
16+
- [Convert to `EvmAddress`](#convert-to-evmaddress)
17+
18+
## Convert Between Native Types
219

320
You might want to convert between the native types (`Bytes32`, `Address`, `ContractId`, and `AssetId`). Because these types are wrappers on `[u8; 32]`, converting is a matter of dereferencing one and instantiating the other using the dereferenced value. Here's an example:
421

522
```rust,ignore
623
{{#include ../../../examples/types/src/lib.rs:type_conversion}}
724
```
25+
26+
## Convert to `Bytes32`
27+
28+
Convert a `[u8; 32]` array to `Bytes32`:
29+
30+
```rust,ignore
31+
{{#include ../../../examples/types/src/lib.rs:array_to_bytes32}}
32+
```
33+
34+
Convert a hex string to `Bytes32`:
35+
36+
```rust,ignore
37+
{{#include ../../../examples/types/src/lib.rs:hex_string_to_bytes32}}
38+
```
39+
40+
## Convert to `Address`
41+
42+
Convert a `[u8; 32]` array to an `Address`:
43+
44+
```rust,ignore
45+
{{#include ../../../examples/types/src/lib.rs:array_to_address}}
46+
```
47+
48+
Convert a `Bech32` address to an `Address`:
49+
50+
```rust,ignore
51+
{{#include ../../../examples/types/src/lib.rs:bech32_to_address}}
52+
```
53+
54+
Convert a wallet to an `Address`:
55+
56+
```rust,ignore
57+
{{#include ../../../examples/wallets/src/lib.rs:wallet_to_address}}
58+
```
59+
60+
Convert a hex string to an `Address`:
61+
62+
```rust,ignore
63+
{{#include ../../../examples/types/src/lib.rs:hex_string_to_address}}
64+
```
65+
66+
## Convert to `ContractId`
67+
68+
Convert a `[u8; 32]` array to to `ContractId`:
69+
70+
```rust,ignore
71+
{{#include ../../../examples/types/src/lib.rs:array_to_contract_id}}
72+
```
73+
74+
Convert a hex string to a `ContractId`:
75+
76+
```rust,ignore
77+
{{#include ../../../examples/types/src/lib.rs:string_to_contract_id}}
78+
```
79+
80+
Convert a contract instance to a `ContractId`:
81+
82+
```rust,ignore
83+
{{#include ../../../packages/fuels/tests/logs.rs:instance_to_contract_id}}
84+
```
85+
86+
## Convert to `Identity`
87+
88+
Convert an `Address` to an `Identity`:
89+
90+
```rust,ignore
91+
{{#include ../../../examples/types/src/lib.rs:address_to_identity}}
92+
```
93+
94+
Convert a `ContractId` to an `Identity`:
95+
96+
```rust,ignore
97+
{{#include ../../../examples/types/src/lib.rs:contract_id_to_identity}}
98+
```
99+
100+
## Convert to `AssetId`
101+
102+
Convert a `[u8; 32]` array to an `AssetId`:
103+
104+
```rust,ignore
105+
{{#include ../../../examples/types/src/lib.rs:array_to_asset_id}}
106+
```
107+
108+
Convert a hex string to an `AssetId`:
109+
110+
```rust,ignore
111+
{{#include ../../../examples/types/src/lib.rs:string_to_asset_id}}
112+
```
113+
114+
## Convert to `Bech32`
115+
116+
Convert a `[u8; 32]` array to a `Bech32` address:
117+
118+
```rust,ignore
119+
{{#include ../../../examples/types/src/lib.rs:array_to_bech32}}
120+
```
121+
122+
Convert `Bytes32` to a `Bech32` address:
123+
124+
```rust,ignore
125+
{{#include ../../../examples/types/src/lib.rs:bytes32_to_bech32}}
126+
```
127+
128+
Convert a string to a `Bech32` address:
129+
130+
```rust,ignore
131+
{{#include ../../../examples/types/src/lib.rs:str_to_bech32}}
132+
```
133+
134+
Convert an `Address` to a `Bech32` address:
135+
136+
```rust,ignore
137+
{{#include ../../../examples/types/src/lib.rs:address_to_bech32}}
138+
```
139+
140+
## Convert to `str`
141+
142+
Convert a `ContractId` to a `str`:
143+
144+
```rust,ignore
145+
{{#include ../../../examples/types/src/lib.rs:contract_id_to_str}}
146+
```
147+
148+
Convert an `Address` to a `str`:
149+
150+
```rust,ignore
151+
{{#include ../../../examples/types/src/lib.rs:address_to_str}}
152+
```
153+
154+
Convert an `AssetId` to a `str`:
155+
156+
```rust,ignore
157+
{{#include ../../../examples/types/src/lib.rs:asset_id_to_str}}
158+
```
159+
160+
Convert `Bytes32` to a `str`:
161+
162+
```rust,ignore
163+
{{#include ../../../examples/types/src/lib.rs:bytes32_to_str}}
164+
```
165+
166+
## Convert to `Bits256`
167+
168+
Convert a hex string to `Bits256`:
169+
170+
```rust,ignore
171+
{{#include ../../../packages/fuels-core/src/types/core/bits.rs:hex_str_to_bits256}}
172+
```
173+
174+
Convert a `ContractId` to `Bits256`:
175+
176+
```rust,ignore
177+
{{#include ../../../examples/types/src/lib.rs:contract_id_to_bits256}}
178+
```
179+
180+
Convert an `Address` to `Bits256`:
181+
182+
```rust,ignore
183+
{{#include ../../../examples/types/src/lib.rs:address_to_bits256}}
184+
```
185+
186+
Convert an `AssetId` to `Bits256`:
187+
188+
```rust,ignore
189+
{{#include ../../../examples/types/src/lib.rs:asset_id_to_bits256}}
190+
```
191+
192+
## Convert to `Bytes`
193+
194+
Convert a string to `Bytes`:
195+
196+
```rust,ignore
197+
{{#include ../../../packages/fuels-core/src/types/core/bytes.rs:hex_string_to_bytes32}}
198+
```
199+
200+
## Convert to `B512`
201+
202+
Convert two hex strings to `B512`:
203+
204+
```rust,ignore
205+
{{#include ../../../packages/fuels/tests/types_contracts.rs:b512_example}}
206+
```
207+
208+
## Convert to `EvmAddress`
209+
210+
Convert a `Bits256` address to an `EvmAddress`:
211+
212+
```rust,ignore
213+
{{#include ../../../examples/types/src/lib.rs:b256_to_evm_address}}
214+
```

examples/debugging/src/lib.rs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ mod tests {
55
use fuel_abi_types::abi::program::ProgramABI;
66
use fuels::{
77
core::{
8-
codec::{calldata, fn_selector, resolve_fn_selector},
8+
codec::{calldata, fn_selector, resolve_fn_selector, ABIDecoder},
99
traits::Parameterize,
1010
},
11+
macros::abigen,
1112
types::{errors::Result, param_types::ParamType, SizedAsciiString},
1213
};
1314

@@ -69,4 +70,103 @@ mod tests {
6970

7071
Ok(())
7172
}
73+
74+
#[test]
75+
fn decoded_debug_matches_rust_debug() -> Result<()> {
76+
abigen!(Contract(
77+
name = "MyContract",
78+
abi = "packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json"
79+
));
80+
81+
let json_abi_file =
82+
"../../packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json";
83+
let abi_file_contents = std::fs::read_to_string(json_abi_file)?;
84+
85+
let parsed_abi: ProgramABI = serde_json::from_str(&abi_file_contents)?;
86+
87+
let type_lookup = parsed_abi
88+
.types
89+
.into_iter()
90+
.map(|decl| (decl.type_id, decl))
91+
.collect::<HashMap<_, _>>();
92+
93+
let get_first_fn_argument = |fn_name: &str| {
94+
parsed_abi
95+
.functions
96+
.iter()
97+
.find(|abi_fun| abi_fun.name == fn_name)
98+
.expect("should be there")
99+
.inputs
100+
.first()
101+
.expect("should be there")
102+
};
103+
let decoder = ABIDecoder::default();
104+
105+
{
106+
// simple struct with a single generic parameter
107+
let type_application = get_first_fn_argument("struct_w_generic");
108+
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;
109+
110+
let expected_struct = SimpleGeneric {
111+
single_generic_param: 123u64,
112+
};
113+
114+
assert_eq!(
115+
format!("{expected_struct:?}"),
116+
decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 123])?
117+
);
118+
}
119+
{
120+
// struct that delegates the generic param internally
121+
let type_application = get_first_fn_argument("struct_delegating_generic");
122+
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;
123+
124+
let expected_struct = PassTheGenericOn {
125+
one: SimpleGeneric {
126+
single_generic_param: SizedAsciiString::<3>::try_from("abc")?,
127+
},
128+
};
129+
130+
assert_eq!(
131+
format!("{expected_struct:?}"),
132+
decoder.decode_as_debug_str(&param_type, &[97, 98, 99])?
133+
);
134+
}
135+
{
136+
// enum with generic in variant
137+
let type_application = get_first_fn_argument("enum_w_generic");
138+
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;
139+
140+
let expected_enum = EnumWGeneric::B(10u64);
141+
142+
assert_eq!(
143+
format!("{expected_enum:?}"),
144+
decoder.decode_as_debug_str(
145+
&param_type,
146+
&[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 10]
147+
)?
148+
);
149+
}
150+
{
151+
// logged type
152+
let logged_type = parsed_abi
153+
.logged_types
154+
.as_ref()
155+
.expect("has logs")
156+
.first()
157+
.expect("has log");
158+
159+
let param_type =
160+
ParamType::try_from_type_application(&logged_type.application, &type_lookup)?;
161+
162+
let expected_u8 = 1;
163+
164+
assert_eq!(
165+
format!("{expected_u8}"),
166+
decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 1])?
167+
);
168+
}
169+
170+
Ok(())
171+
}
72172
}

0 commit comments

Comments
 (0)