diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 38fd55b8a1..5362bb4236 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -51,7 +51,6 @@ - [`Address`](./types/address.md) - [`ContractId`](./types/contract-id.md) - [`AssetId`](./types/asset-id.md) - - [Converting native types](./types/conversion.md) - [`Bech32`](./types/bech32.md) - [Structs and enums](./types/custom_types.md) - [`String`](./types/string.md) @@ -60,6 +59,7 @@ - [`B512`](./types/B512.md) - [`EvmAddress`](./types/evm_address.md) - [Vectors](./types/vectors.md) + - [Converting types](./types/conversion.md) - [Codec](./codec/index.md) - [Encoding](./codec/encoding.md) - [Decoding](./codec/decoding.md) diff --git a/docs/src/types/conversion.md b/docs/src/types/conversion.md index f376e7abc6..b5984de832 100644 --- a/docs/src/types/conversion.md +++ b/docs/src/types/conversion.md @@ -1,7 +1,214 @@ -# Converting native types +# Converting Types + +Below you can find examples for common type conversions: + +- [Convert Between Native Types](#convert-between-native-types) +- [Convert to `Bytes32`](#convert-to-bytes32) +- [Convert to `Address`](#convert-to-address) +- [Convert to `ContractId`](#convert-to-contractid) +- [Convert to `Identity`](#convert-to-identity) +- [Convert to `AssetId`](#convert-to-assetid) +- [Convert to `Bech32`](#convert-to-bech32) +- [Convert to `str`](#convert-to-str) +- [Convert to `Bits256`](#convert-to-bits256) +- [Convert to `Bytes`](#convert-to-bytes) +- [Convert to `B512`](#convert-to-b512) +- [Convert to `EvmAddress`](#convert-to-evmaddress) + +## Convert Between Native Types 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: ```rust,ignore {{#include ../../../examples/types/src/lib.rs:type_conversion}} ``` + +## Convert to `Bytes32` + +Convert a `[u8; 32]` array to `Bytes32`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:array_to_bytes32}} +``` + +Convert a hex string to `Bytes32`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:hex_string_to_bytes32}} +``` + +## Convert to `Address` + +Convert a `[u8; 32]` array to an `Address`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:array_to_address}} +``` + +Convert a `Bech32` address to an `Address`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:bech32_to_address}} +``` + +Convert a wallet to an `Address`: + +```rust,ignore +{{#include ../../../examples/wallets/src/lib.rs:wallet_to_address}} +``` + +Convert a hex string to an `Address`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:hex_string_to_address}} +``` + +## Convert to `ContractId` + +Convert a `[u8; 32]` array to to `ContractId`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:array_to_contract_id}} +``` + +Convert a hex string to a `ContractId`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:string_to_contract_id}} +``` + +Convert a contract instance to a `ContractId`: + +```rust,ignore +{{#include ../../../packages/fuels/tests/logs.rs:instance_to_contract_id}} +``` + +## Convert to `Identity` + +Convert an `Address` to an `Identity`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:address_to_identity}} +``` + +Convert a `ContractId` to an `Identity`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:contract_id_to_identity}} +``` + +## Convert to `AssetId` + +Convert a `[u8; 32]` array to an `AssetId`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:array_to_asset_id}} +``` + +Convert a hex string to an `AssetId`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:string_to_asset_id}} +``` + +## Convert to `Bech32` + +Convert a `[u8; 32]` array to a `Bech32` address: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:array_to_bech32}} +``` + +Convert `Bytes32` to a `Bech32` address: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:bytes32_to_bech32}} +``` + +Convert a string to a `Bech32` address: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:str_to_bech32}} +``` + +Convert an `Address` to a `Bech32` address: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:address_to_bech32}} +``` + +## Convert to `str` + +Convert a `ContractId` to a `str`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:contract_id_to_str}} +``` + +Convert an `Address` to a `str`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:address_to_str}} +``` + +Convert an `AssetId` to a `str`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:asset_id_to_str}} +``` + +Convert `Bytes32` to a `str`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:bytes32_to_str}} +``` + +## Convert to `Bits256` + +Convert a hex string to `Bits256`: + +```rust,ignore +{{#include ../../../packages/fuels-core/src/types/core/bits.rs:hex_str_to_bits256}} +``` + +Convert a `ContractId` to `Bits256`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:contract_id_to_bits256}} +``` + +Convert an `Address` to `Bits256`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:address_to_bits256}} +``` + +Convert an `AssetId` to `Bits256`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:asset_id_to_bits256}} +``` + +## Convert to `Bytes` + +Convert a string to `Bytes`: + +```rust,ignore +{{#include ../../../packages/fuels-core/src/types/core/bytes.rs:hex_string_to_bytes32}} +``` + +## Convert to `B512` + +Convert two hex strings to `B512`: + +```rust,ignore +{{#include ../../../packages/fuels/tests/types_contracts.rs:b512_example}} +``` + +## Convert to `EvmAddress` + +Convert a `Bits256` address to an `EvmAddress`: + +```rust,ignore +{{#include ../../../examples/types/src/lib.rs:b256_to_evm_address}} +``` diff --git a/examples/types/src/lib.rs b/examples/types/src/lib.rs index 70494aee02..322395e4d2 100644 --- a/examples/types/src/lib.rs +++ b/examples/types/src/lib.rs @@ -5,6 +5,7 @@ mod tests { #[cfg(feature = "fuel-core-lib")] use fuels::prelude::Config; use fuels::prelude::Result; + use fuels::types::{Bits256, EvmAddress, Identity}; #[tokio::test] async fn bytes32() -> Result<()> { @@ -21,13 +22,17 @@ mod tests { assert_eq!([0u8; 32], *b256); // From a `[u8; 32]`. + // ANCHOR: array_to_bytes32 let my_slice = [1u8; 32]; let b256 = Bytes32::new(my_slice); + // ANCHOR_END: array_to_bytes32 assert_eq!([1u8; 32], *b256); // From a hex string. + // ANCHOR: hex_string_to_bytes32 let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000"; let b256 = Bytes32::from_str(hex_str)?; + // ANCHOR_END: hex_string_to_bytes32 assert_eq!([0u8; 32], *b256); // ANCHOR_END: bytes32 @@ -39,6 +44,10 @@ mod tests { assert_eq!(hex_str[2..], b256_string); assert_eq!(hex_str, b256_hex_string); + // ANCHOR: bytes32_to_str + let _str_from_bytes32: &str = b256.to_string().as_str(); + // ANCHOR_END: bytes32_to_str + Ok(()) } #[tokio::test] @@ -56,15 +65,36 @@ mod tests { assert_eq!([0u8; 32], *address); // From a `[u8; 32]`. + // ANCHOR: array_to_address let my_slice = [1u8; 32]; let address = Address::new(my_slice); + // ANCHOR_END: array_to_address assert_eq!([1u8; 32], *address); // From a string. + // ANCHOR: hex_string_to_address let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000"; let address = Address::from_str(hex_str)?; + // ANCHOR_END: hex_string_to_address assert_eq!([0u8; 32], *address); // ANCHOR_END: address + + // ANCHOR: address_to_identity + let _identity_from_address = Identity::Address(address); + // ANCHOR_END: address_to_identity + + // ANCHOR: address_to_str + let _str_from_address: &str = address.to_string().as_str(); + // ANCHOR_END: address_to_str + + // ANCHOR: address_to_bits256 + let bits_256 = Bits256(address.into()); + // ANCHOR_END: address_to_bits256 + + // ANCHOR: b256_to_evm_address + let _evm_address = EvmAddress::from(bits_256); + // ANCHOR_END: b256_to_evm_address + Ok(()) } #[tokio::test] @@ -73,26 +103,36 @@ mod tests { use fuels::types::{bech32::Bech32Address, Address, Bytes32}; // New from HRP string and a hash + // ANCHOR: array_to_bech32 let hrp = "fuel"; let my_slice = [1u8; 32]; let _bech32_address = Bech32Address::new(hrp, my_slice); + // ANCHOR_END: array_to_bech32 // Note that you can also pass a hash stored as Bytes32 to new: + // ANCHOR: bytes32_to_bech32 let my_hash = Bytes32::new([1u8; 32]); let _bech32_address = Bech32Address::new(hrp, my_hash); + // ANCHOR_END: bytes32_to_bech32 // From a string. + // ANCHOR: str_to_bech32 let address = "fuel1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsx2mt2"; let bech32_address = Bech32Address::from_str(address)?; + // ANCHOR_END: str_to_bech32 assert_eq!([0u8; 32], *bech32_address.hash()); // From Address + // ANCHOR: address_to_bech32 let plain_address = Address::new([0u8; 32]); let bech32_address = Bech32Address::from(plain_address); + // ANCHOR_END: address_to_bech32 assert_eq!([0u8; 32], *bech32_address.hash()); // Convert to Address + // ANCHOR: bech32_to_address let _plain_address: Address = bech32_address.into(); + // ANCHOR_END: bech32_to_address // ANCHOR_END: bech32 @@ -113,13 +153,17 @@ mod tests { assert_eq!([0u8; 32], *asset_id); // From a `[u8; 32]`. + // ANCHOR: array_to_asset_id let my_slice = [1u8; 32]; let asset_id = AssetId::new(my_slice); + // ANCHOR_END: array_to_asset_id assert_eq!([1u8; 32], *asset_id); // From a string. + // ANCHOR: string_to_asset_id let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000"; let asset_id = AssetId::from_str(hex_str)?; + // ANCHOR_END: string_to_asset_id assert_eq!([0u8; 32], *asset_id); // ANCHOR_END: asset_id Ok(()) @@ -139,15 +183,28 @@ mod tests { assert_eq!([0u8; 32], *contract_id); // From a `[u8; 32]`. + // ANCHOR: array_to_contract_id let my_slice = [1u8; 32]; let contract_id = ContractId::new(my_slice); + // ANCHOR_END: array_to_contract_id assert_eq!([1u8; 32], *contract_id); // From a string. + // ANCHOR: string_to_contract_id let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000"; let contract_id = ContractId::from_str(hex_str)?; + // ANCHOR_END: string_to_contract_id assert_eq!([0u8; 32], *contract_id); // ANCHOR_END: contract_id + + // ANCHOR: contract_id_to_identity + let _identity_from_contract_id = Identity::ContractId(contract_id); + // ANCHOR_END: contract_id_to_identity + + // ANCHOR: contract_id_to_str + let _str_from_contract_id: &str = contract_id.to_string().as_str(); + // ANCHOR_END: contract_id_to_str + Ok(()) } @@ -162,6 +219,19 @@ mod tests { assert_eq!([1u8; 32], *asset_id); // ANCHOR_END: type_conversion + + // ANCHOR: asset_id_to_str + let _str_from_asset_id: &str = asset_id.to_string().as_str(); + // ANCHOR_END: asset_id_to_str + + // ANCHOR: contract_id_to_bits256 + let _contract_id_to_bits_256 = Bits256(contract_id.into()); + // ANCHOR_END: contract_id_to_bits256 + + // ANCHOR: asset_id_to_bits256 + let _asset_id_to_bits_256 = Bits256(asset_id.into()); + // ANCHOR_END: asset_id_to_bits256 + Ok(()) } diff --git a/examples/wallets/src/lib.rs b/examples/wallets/src/lib.rs index fd3fdfb59b..e12905797f 100644 --- a/examples/wallets/src/lib.rs +++ b/examples/wallets/src/lib.rs @@ -303,6 +303,11 @@ mod tests { let wallet_config = WalletsConfig::new_multiple_assets(num_wallets, assets); let wallets = launch_custom_provider_and_get_wallets(wallet_config, None, None).await?; // ANCHOR_END: custom_assets_wallet_short + + // ANCHOR: wallet_to_address + let wallet_unlocked = WalletUnlocked::new_random(None); + let address: Address = wallet_unlocked.address().into(); + // ANCHOR_END: wallet_to_address Ok(()) } diff --git a/packages/fuels-core/src/types/core/bits.rs b/packages/fuels-core/src/types/core/bits.rs index e6b051243c..eefc5e5c30 100644 --- a/packages/fuels-core/src/types/core/bits.rs +++ b/packages/fuels-core/src/types/core/bits.rs @@ -108,9 +108,11 @@ mod tests { assert_eq!(bits256.0, [1u8; 32]); // With the `0x0` prefix + // ANCHOR: hex_str_to_bits256 let hex_str = "0x0101010101010101010101010101010101010101010101010101010101010101"; let bits256 = Bits256::from_hex_str(hex_str)?; + // ANCHOR_END: hex_str_to_bits256 assert_eq!(bits256.0, [1u8; 32]); // ANCHOR_END: from_hex_str diff --git a/packages/fuels-core/src/types/core/bytes.rs b/packages/fuels-core/src/types/core/bytes.rs index b064dfd5fd..02e6dc6519 100644 --- a/packages/fuels-core/src/types/core/bytes.rs +++ b/packages/fuels-core/src/types/core/bytes.rs @@ -50,9 +50,11 @@ mod tests { assert_eq!(bytes.0, vec![1u8; 32]); // With the `0x0` prefix + // ANCHOR: hex_string_to_bytes32 let hex_str = "0x0101010101010101010101010101010101010101010101010101010101010101"; let bytes = Bytes::from_hex_str(hex_str)?; + // ANCHOR_END: hex_string_to_bytes32 assert_eq!(bytes.0, vec![1u8; 32]); // ANCHOR_END: bytes_from_hex_str diff --git a/packages/fuels/tests/logs.rs b/packages/fuels/tests/logs.rs index f602eb343d..746e9b96bc 100644 --- a/packages/fuels/tests/logs.rs +++ b/packages/fuels/tests/logs.rs @@ -767,7 +767,9 @@ async fn test_script_logs_with_contract_logs() -> Result<()> { format!("{:?}", [1, 2, 3]), ]; + // ANCHOR: instance_to_contract_id let contract_id: ContractId = contract_instance.id().into(); + // ANCHOR_END: instance_to_contract_id // ANCHOR: external_contract_ids let response = script_instance