Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e committed Oct 11, 2023
2 parents e5a7fb9 + 626334a commit cb36603
Show file tree
Hide file tree
Showing 40 changed files with 1,159 additions and 688 deletions.
17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ readme = "README.md"
license = "Apache-2.0"
repository = "https://github.com/FuelLabs/fuels-rs"
rust-version = "1.72.1"
version = "0.48.0"
version = "0.49.0"

[workspace.dependencies]
Inflector = "0.11.4"
Expand Down Expand Up @@ -67,6 +67,7 @@ tokio = { version = "1.33.0", default-features = false }
trybuild = "1.0.85"
uint = { version = "0.9.5", default-features = false }
which = { version = "4.4.2", default-features = false }
zeroize = "1.6.0"

# Dependencies from the `fuel-core` repository:
fuel-core = { version = "0.20.6", default-features = false }
Expand All @@ -84,10 +85,10 @@ fuel-types = { version = "0.35.4", default-features = false }
fuel-vm = "0.35.4"

# Workspace projects
fuels = { version = "0.48.0", path = "./packages/fuels" }
fuels-accounts = { version = "0.48.0", path = "./packages/fuels-accounts", default-features = false }
fuels-code-gen = { version = "0.48.0", path = "./packages/fuels-code-gen", default-features = false }
fuels-core = { version = "0.48.0", path = "./packages/fuels-core", default-features = false }
fuels-macros = { version = "0.48.0", path = "./packages/fuels-macros", default-features = false }
fuels-programs = { version = "0.48.0", path = "./packages/fuels-programs", default-features = false }
fuels-test-helpers = { version = "0.48.0", path = "./packages/fuels-test-helpers", default-features = false }
fuels = { version = "0.49.0", path = "./packages/fuels" }
fuels-accounts = { version = "0.49.0", path = "./packages/fuels-accounts", default-features = false }
fuels-code-gen = { version = "0.49.0", path = "./packages/fuels-code-gen", default-features = false }
fuels-core = { version = "0.49.0", path = "./packages/fuels-core", default-features = false }
fuels-macros = { version = "0.49.0", path = "./packages/fuels-macros", default-features = false }
fuels-programs = { version = "0.49.0", path = "./packages/fuels-programs", default-features = false }
fuels-test-helpers = { version = "0.49.0", path = "./packages/fuels-test-helpers", default-features = false }
2 changes: 1 addition & 1 deletion docs/src/calling-contracts/call-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The parameters for a contract call are:
3. Gas forwarded
<!-- call_params:example:end -->

You can use these to forward coins to a contract. You can configure these parameters by creating an instance of [`CallParameters`](https://docs.rs/fuels/{{versions.fuels}}/fuels/programs/contract/struct.CallParameters.html) and passing it to a chain method called `call_params`.
You can use these to forward coins to a contract. You can configure these parameters by creating an instance of [`CallParameters`](https://docs.rs/fuels/latest/fuels/programs/contract/struct.CallParameters.html) and passing it to a chain method called `call_params`.
<!-- use_call_params:example:end -->

For instance, suppose the following contract that uses Sway's `msg_amount()` to return the amount sent in that transaction.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/connecting/querying.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Querying the blockchain

Once you set up a provider, you can interact with the Fuel blockchain. Here are a few examples of what you can do with a provider; for a more in-depth overview of the API, check the [official provider API documentation](https://docs.rs/fuels/{{versions.fuels}}/fuels/accounts/provider/struct.Provider.html).
Once you set up a provider, you can interact with the Fuel blockchain. Here are a few examples of what you can do with a provider; for a more in-depth overview of the API, check the [official provider API documentation](https://docs.rs/fuels/latest/fuels/accounts/provider/struct.Provider.html).

- [Set up](#set-up)
- [Get all coins from an address](#get-all-coins-from-an-address)
Expand Down
4 changes: 2 additions & 2 deletions docs/src/connecting/short-lived.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ let wallet = launch_provider_and_get_wallet().await;
The `fuel-core-lib` feature allows us to run a `fuel-core` node without installing the `fuel-core` binary on the local machine. Using the `fuel-core-lib` feature flag entails downloading all the dependencies needed to run the fuel-core node.

```rust,ignore
fuels = { version = "0.48.0", features = ["fuel-core-lib"] }
fuels = { version = "0.49.0", features = ["fuel-core-lib"] }
```

### RocksDb

The `rocksdb` is an additional feature that, when combined with `fuel-core-lib`, provides persistent storage capabilities while using `fuel-core` as a library.

```rust,ignore
fuels = { version = "0.48.0", features = ["rocksdb"] }
fuels = { version = "0.49.0", features = ["rocksdb"] }
```
2 changes: 1 addition & 1 deletion docs/src/reference.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# API Reference

For a more in-depth look at the APIs provided by the Fuel Rust SDK, head over to the [official documentation](https://docs.rs/fuels/{{versions.fuels}}/fuels/). In the actual Rust docs, you can see the most up-to-date information about the API, which is synced with the code as it changes.
For a more in-depth look at the APIs provided by the Fuel Rust SDK, head over to the [official documentation](https://docs.rs/fuels/latest/fuels/). In the actual Rust docs, you can see the most up-to-date information about the API, which is synced with the code as it changes.
42 changes: 42 additions & 0 deletions docs/src/types/custom_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,45 @@ Your Rust code would look like this:
```rust,ignore
{{#include ../../../packages/fuels/tests/types_contracts.rs:generic}}
```

### Unused generic type parameters

Sway supports unused generic type parameters when declaring structs/enums:

```Rust
struct SomeStruct<T, K> {
field: u64
}

enum SomeEnum<T, K> {
One: u64
}

```

If you tried the same in Rust you'd get complaints that `T` and `K` must be used or removed. When generating Rust bindings for such types we make use of the [`PhantomData`](https://doc.rust-lang.org/std/marker/struct.PhantomData.html#unused-type-parameters) type. The generated bindings for the above example would look something like this:

```Rust
struct SomeStruct<T, K> {
pub field: u64,
pub _unused_generic_0: PhantomData<T>
pub _unused_generic_1: PhantomData<K>
}

enum SomeEnum<T, K> {
One(u64),
IgnoreMe(PhantomData<T>, PhantomData<K>)
}
```

To lessen the impact to developer experience you may use `SomeStruct::new` to initialize the above structure without bothering with the `PhantomData`s:

```rust,ignore
{{#include ../../../examples/types/src/lib.rs:unused_generics_struct}}
```

If your struct doesn't have any fields we'll also derive `Default`. As for enums all `PhantomData`s are placed inside a new variant called `IgnoreMe` which you'll need to ignore in your matches:

```rust,ignore
{{#include ../../../examples/types/src/lib.rs:unused_generics_enum}}
```
153 changes: 153 additions & 0 deletions examples/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,157 @@ mod tests {
// ANCHOR_END: type_conversion
Ok(())
}

#[tokio::test]
async fn unused_generics() -> Result<()> {
use fuels::prelude::*;
abigen!(Contract(
name = "MyContract",
abi = r#" {
"types": [
{
"typeId": 0,
"type": "()",
"components": [],
"typeParameters": null
},
{
"typeId": 1,
"type": "enum MyEnum",
"components": [
{
"name": "One",
"type": 7,
"typeArguments": null
}
],
"typeParameters": [
3,
2
]
},
{
"typeId": 2,
"type": "generic K",
"components": null,
"typeParameters": null
},
{
"typeId": 3,
"type": "generic T",
"components": null,
"typeParameters": null
},
{
"typeId": 4,
"type": "struct MyStruct",
"components": [
{
"name": "field",
"type": 7,
"typeArguments": null
}
],
"typeParameters": [
3,
2
]
},
{
"typeId": 5,
"type": "u16",
"components": null,
"typeParameters": null
},
{
"typeId": 6,
"type": "u32",
"components": null,
"typeParameters": null
},
{
"typeId": 7,
"type": "u64",
"components": null,
"typeParameters": null
},
{
"typeId": 8,
"type": "u8",
"components": null,
"typeParameters": null
}
],
"functions": [
{
"inputs": [
{
"name": "arg",
"type": 4,
"typeArguments": [
{
"name": "",
"type": 8,
"typeArguments": null
},
{
"name": "",
"type": 5,
"typeArguments": null
}
]
},
{
"name": "arg_2",
"type": 1,
"typeArguments": [
{
"name": "",
"type": 6,
"typeArguments": null
},
{
"name": "",
"type": 7,
"typeArguments": null
}
]
}
],
"name": "test_function",
"output": {
"name": "",
"type": 0,
"typeArguments": null
},
"attributes": null
}
],
"loggedTypes": [],
"messagesTypes": [],
"configurables": []
}"#
));

// ANCHOR: unused_generics_struct
assert_eq!(
<MyStruct<u16, u32>>::new(15),
MyStruct {
field: 15,
_unused_generic_0: std::marker::PhantomData,
_unused_generic_1: std::marker::PhantomData
}
);
// ANCHOR_END: unused_generics_struct

let my_enum = <MyEnum<u32, u64>>::One(15);
// ANCHOR: unused_generics_enum
match my_enum {
MyEnum::One(_value) => {}
MyEnum::IgnoreMe(..) => panic!("Will never receive this variant"),
}
// ANCHOR_END: unused_generics_enum

Ok(())
}
}
1 change: 1 addition & 0 deletions packages/fuels-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rand = { workspace = true, default-features = false }
tai64 = { workspace = true, features = ["serde"] }
thiserror = { workspace = true, default-features = false }
tokio = { workspace = true, features = ["full"] }
zeroize = { workspace = true, features = ["derive"] }

[dev-dependencies]
hex = { workspace = true, default-features = false, features = ["std"] }
Expand Down
13 changes: 9 additions & 4 deletions packages/fuels-accounts/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use fuels_core::{
};
use rand::{CryptoRng, Rng};
use thiserror::Error;
use zeroize::{Zeroize, ZeroizeOnDrop};

use crate::{
accounts_utils::{adjust_inputs, adjust_outputs, calculate_base_amount_with_fee},
Expand Down Expand Up @@ -71,8 +72,11 @@ pub struct Wallet {
/// A `WalletUnlocked` is equivalent to a [`Wallet`] whose private key is known and stored
/// alongside in-memory. Knowing the private key allows a `WalletUlocked` to sign operations, send
/// transactions, and more.
#[derive(Clone, Debug)]
///
/// `private_key` will be zeroed out on calling `lock()` or `drop`ping a `WalletUnlocked`.
#[derive(Clone, Debug, Zeroize, ZeroizeOnDrop)]
pub struct WalletUnlocked {
#[zeroize(skip)]
wallet: Wallet,
pub(crate) private_key: SecretKey,
}
Expand Down Expand Up @@ -118,9 +122,10 @@ impl ViewOnlyAccount for Wallet {
}

impl WalletUnlocked {
/// Lock the wallet by `drop`ping the private key from memory.
pub fn lock(self) -> Wallet {
self.wallet
/// Lock the wallet by securely `zeroize`-ing and `drop`ping the private key from memory.
pub fn lock(mut self) -> Wallet {
self.private_key.zeroize();
self.wallet.clone()
}

// NOTE: Rather than providing a `DerefMut` implementation, we wrap the `set_provider` method
Expand Down
4 changes: 4 additions & 0 deletions packages/fuels-code-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ regex = { workspace = true }
serde_json = { workspace = true }
syn = { workspace = true }

[dev-dependencies]
pretty_assertions = "1.4"


[package.metadata.cargo-machete]
ignored = ["Inflector"]
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,21 @@ pub(crate) fn expand_fn(abi_fun: &FullABIFunction) -> Result<TokenStream> {
};
generator.set_body(body);

Ok(generator.into())
Ok(generator.generate())
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use fuel_abi_types::abi::program::{ABIFunction, ProgramABI, TypeApplication, TypeDeclaration};
use fuel_abi_types::abi::{
full_program::FullABIFunction,
program::{ABIFunction, ProgramABI, TypeApplication, TypeDeclaration},
};
use pretty_assertions::assert_eq;
use quote::quote;

use super::*;
use crate::{error::Result, program_bindings::abigen::bindings::contract::expand_fn};

#[test]
fn test_expand_fn_simple_abi() -> Result<()> {
Expand Down Expand Up @@ -395,13 +400,13 @@ mod tests {

let expected = quote! {
#[doc = "Calls the contract's `HelloWorld` function"]
pub fn HelloWorld(&self, bimbam: bool) -> ::fuels::programs::contract::ContractCallHandler<T, ()> {
pub fn HelloWorld(&self, bimbam: ::core::primitive::bool) -> ::fuels::programs::contract::ContractCallHandler<T, ()> {
::fuels::programs::contract::method_hash(
self.contract_id.clone(),
self.account.clone(),
::fuels::core::codec::resolve_fn_selector(
"HelloWorld",
&[<bool as ::fuels::core::traits::Parameterize>::param_type()]
&[<::core::primitive::bool as ::fuels::core::traits::Parameterize>::param_type()]
),
&[::fuels::core::traits::Tokenizable::into_token(bimbam)],
self.log_decoder.clone(),
Expand Down
Loading

0 comments on commit cb36603

Please sign in to comment.