-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/utxo_caching
- Loading branch information
Showing
113 changed files
with
3,684 additions
and
1,257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Decoding | ||
|
||
Be sure to read the [prerequisites](./index.md#prerequisites-for-decodingencoding) to decoding. | ||
|
||
Decoding is done via the [`ABIDecoder`](https://docs.rs/fuels/latest/fuels/core/codec/struct.ABIDecoder.html): | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/codec/src/lib.rs:decoding_example}} | ||
``` | ||
|
||
First into a [`Token`](https://docs.rs/fuels/latest/fuels/types/enum.Token.html), then via the [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html) trait, into the desired type. | ||
|
||
If the type came from [`abigen!`](../abigen/index.md) (or uses the [`::fuels::macros::TryFrom`](https://docs.rs/fuels/latest/fuels/macros/derive.TryFrom.html) derivation) then you can also use `try_into` to convert bytes into a type that implements both [`Parameterize`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html) and [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html): | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/codec/src/lib.rs:decoding_example_try_into}} | ||
``` | ||
|
||
Under the hood, [`try_from_bytes`](https://docs.rs/fuels/latest/fuels/core/codec/fn.try_from_bytes.html) is being called, which does what the preceding example did. | ||
|
||
## Configuring the decoder | ||
|
||
The decoder can be configured to limit its resource expenditure: | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/codec/src/lib.rs:configuring_the_decoder}} | ||
``` | ||
|
||
<!-- TODO: Add a link once a release is made --> | ||
<!-- https://docs.rs/fuels/latest/fuels/core/codec/struct.DecoderConfig.html --> | ||
For an explanation of each configuration value visit the `DecoderConfig`. | ||
|
||
<!-- TODO: add a link once a release is made --> | ||
<!-- https://docs.rs/fuels/latest/fuels/core/codec/struct.DecoderConfig.html --> | ||
The default values for the `DecoderConfig` are: | ||
|
||
```rust,ignore | ||
{{#include ../../../packages/fuels-core/src/codec/abi_decoder.rs:default_decoder_config}} | ||
``` | ||
|
||
## Configuring the decoder for contract/script calls | ||
|
||
You can also configure the decoder used to decode the return value of the contract method: | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/contracts/src/lib.rs:contract_decoder_config}} | ||
``` | ||
|
||
The same method is available for script calls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Encoding | ||
|
||
Be sure to read the [prerequisites](./index.md#prerequisites-for-decodingencoding) to encoding. | ||
|
||
Encoding is done via the [`ABIEncoder`](https://docs.rs/fuels/latest/fuels/core/codec/struct.ABIEncoder.html): | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/codec/src/lib.rs:encoding_example}} | ||
``` | ||
|
||
Note that the return type of `encode` is `UnresolvedBytes`. The encoding cannot be finished until we know at which memory address this data is to be loaded. If you don't use heap types (`::std::vec::Vec`, `::fuels::types::Bytes`, `::std::string::String`), then you can safely `.resolve(0)` to get the encoded bytes. | ||
|
||
There is also a shortcut-macro that can encode multiple types which implement [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html): | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/codec/src/lib.rs:encoding_example_w_macro}} | ||
``` | ||
|
||
> Note: | ||
> The above example will call `.resolve(0)`. Don't use it if you're encoding heap types. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Codec | ||
|
||
Encoding and decoding are done as per [the fuel spec](https://specs.fuel.network/master/abi/argument-encoding.html). To this end, `fuels` makes use of the [ABIEncoder](https://docs.rs/fuels/latest/fuels/core/codec/struct.ABIEncoder.html) and the [ABIDecoder](https://docs.rs/fuels/latest/fuels/core/codec/struct.ABIDecoder.html). | ||
|
||
## Prerequisites for decoding/encoding | ||
|
||
To encode a type, you must first convert it into a [`Token`](https://docs.rs/fuels/latest/fuels/types/enum.Token.html). This is commonly done by implementing the [Tokenizable](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html) trait. | ||
|
||
To decode, you also need to provide a [`ParamType`](https://docs.rs/fuels/latest/fuels/types/param_types/enum.ParamType.html) describing the schema of the type in question. This is commonly done by implementing the [Parameterize](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html) trait. | ||
|
||
All types generated by the [`abigen!`](../abigen/index.md) macro implement both the [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html) and [`Parameterize`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html) traits. | ||
|
||
`fuels` also contains implementations for: | ||
|
||
- [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html) for the `fuels`-owned types listed [here](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html#implementors) as well as [for some foreign types](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html#foreign-impls) (such as `u8`, `u16`, `std::vec::Vec<T: Tokenizable>`, etc.). | ||
- [`Parameterize`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html) for the `fuels`-owned types listed [here](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html#implementors) as well as [for some foreign types](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html#foreign-impls) (such as `u8`, `u16`, `std::vec::Vec<T: Parameterize>`, etc.). | ||
|
||
## Deriving the traits | ||
|
||
Both [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html) and [`Parameterize`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html) can be derived for `struct`s and `enum`s if all inner types implement the derived traits: | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/macros/src/lib.rs:deriving_traits}} | ||
``` | ||
|
||
> Note: | ||
> Deriving [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html) on `enum`s requires that all variants also implement [`Parameterize`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html). | ||
### Tweaking the derivation | ||
|
||
#### Changing 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`. | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/macros/src/lib.rs:deriving_traits_paths}} | ||
``` | ||
|
||
#### Generating no-std code | ||
|
||
If you want `no-std` generated code: | ||
|
||
```rust,ignore | ||
{{#include ../../../examples/macros/src/lib.rs:deriving_traits_nostd}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.