Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link

Icon LinkCodec

Encoding and decoding are done as per the fuel spec . To this end, fuels makes use of the ABIEncoder Icon Link and the ABIDecoder Icon Link.

Icon LinkPrerequisites for decoding/encoding

To encode a type, you must first convert it into a Token Icon Link. This is commonly done by implementing the Tokenizable Icon Link trait.

To decode, you also need to provide a ParamType Icon Link describing the schema of the type in question. This is commonly done by implementing the Parameterize Icon Link trait.

All types generated by the abigen! macro implement both the Tokenizable Icon Link and Parameterize Icon Link traits.

fuels also contains implementations for:

Icon LinkDeriving the traits

Both Tokenizable Icon Link and Parameterize Icon Link can be derived for structs and enums if all inner types implement the derived traits:

use fuels::macros::{Parameterize, Tokenizable};
 
#[derive(Parameterize, Tokenizable)]
struct MyStruct {
    field_a: u8,
}
 
#[derive(Parameterize, Tokenizable)]
enum SomeEnum {
    A(MyStruct),
    B(Vec<u64>),
}
Icon InfoCircle

Note: Deriving Tokenizable Icon Link on enums requires that all variants also implement Parameterize Icon Link.

Icon LinkTweaking the derivation

Icon LinkChanging the location of imports

The derived code expects that the fuels package is accessible through ::fuels. If this is not the case then the derivation macro needs to be given the locations of fuels::types and fuels::core.

#[derive(Parameterize, Tokenizable)]
#[FuelsCorePath = "fuels_core_elsewhere"]
#[FuelsTypesPath = "fuels_types_elsewhere"]
pub struct SomeStruct {
    field_a: u64,
}

Icon LinkGenerating no-std code

If you want no-std generated code:

use fuels::macros::{Parameterize, Tokenizable};
#[derive(Parameterize, Tokenizable)]
#[NoStd]
pub struct SomeStruct {
    field_a: u64,
}