Skip to content

Commit

Permalink
Merge branch 'master' into oleksii/mdbook-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Br1ght0ne authored Sep 28, 2023
2 parents a0960fc + f996863 commit da941b2
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 175 deletions.
6 changes: 1 addition & 5 deletions docs/src/calling-contracts/tx-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ You can configure these parameters by creating an instance of `TxParameters` and

<!-- This section should explain how to use the defauly tx params -->
<!-- tx_params_default:example:start -->
You can also use `TxParameters::default()` to use the default values:
You can also use `TxParameters::default()` to use the default values. If `gas_price` or `gas_limit` is set to `None`, the SDK will use the network's default value:
<!-- tx_params_default:example:end -->

```rust,ignore
{{#include ../../../packages/fuels-core/src/utils/constants.rs:default_tx_parameters}}
```

This way:

```rust,ignore
Expand Down
2 changes: 1 addition & 1 deletion docs/src/connecting/external-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In the code example, we connected a new provider to the Testnet node and created

> **Note:** New wallets on the Testnet will not have any assets! They can be obtained by providing the wallet address to the faucet at
>
>[faucet-beta-3.fuel.network](https://faucet-beta-3.fuel.network)
>[faucet-beta-4.fuel.network](https://faucet-beta-4.fuel.network)
>
> Once the assets have been transferred to the wallet, you can reuse it in other tests by providing the private key!
>
Expand Down
9 changes: 7 additions & 2 deletions examples/cookbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ mod tests {
// ANCHOR_END: transfer_multiple_inout

// ANCHOR: transfer_multiple_transaction
let mut tb =
ScriptTransactionBuilder::prepare_transfer(inputs, outputs, TxParameters::default());
let network_info = provider.network_info().await?;
let mut tb = ScriptTransactionBuilder::prepare_transfer(
inputs,
outputs,
TxParameters::default(),
network_info,
);
wallet_1.sign_transaction(&mut tb);
let tx = tb.build()?;

Expand Down
31 changes: 21 additions & 10 deletions packages/fuels-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,18 @@ pub trait Account: ViewOnlyAccount {
tx_parameters: TxParameters,
) -> Result<(TxId, Vec<Receipt>)> {
let provider = self.try_provider()?;
let network_info = provider.network_info().await?;

let inputs = self.get_asset_inputs_for_amount(asset_id, amount).await?;

let outputs = self.get_asset_outputs_for_amount(to, asset_id, amount);

let consensus_parameters = provider.consensus_parameters();

let tx_builder = ScriptTransactionBuilder::prepare_transfer(inputs, outputs, tx_parameters)
.with_consensus_parameters(consensus_parameters);
let tx_builder = ScriptTransactionBuilder::prepare_transfer(
inputs,
outputs,
tx_parameters,
network_info,
);

// if we are not transferring the base asset, previous base amount is 0
let previous_base_amount = if asset_id == AssetId::default() {
Expand Down Expand Up @@ -233,6 +236,7 @@ pub trait Account: ViewOnlyAccount {
tx_parameters: TxParameters,
) -> std::result::Result<(String, Vec<Receipt>), Error> {
let provider = self.try_provider()?;
let network_info = provider.network_info().await?;

let zeroes = Bytes32::zeroed();
let plain_contract_id: ContractId = to.into();
Expand All @@ -253,17 +257,15 @@ pub trait Account: ViewOnlyAccount {
];

// Build transaction and sign it
let params = provider.consensus_parameters();

let tb = ScriptTransactionBuilder::prepare_contract_transfer(
plain_contract_id,
balance,
asset_id,
inputs,
outputs,
tx_parameters,
)
.with_consensus_parameters(params);
network_info,
);

// if we are not transferring the base asset, previous base amount is 0
let base_amount = if asset_id == AssetId::default() {
Expand Down Expand Up @@ -294,6 +296,7 @@ pub trait Account: ViewOnlyAccount {
tx_parameters: TxParameters,
) -> std::result::Result<(TxId, MessageId, Vec<Receipt>), Error> {
let provider = self.try_provider()?;
let network_info = provider.network_info().await?;

let inputs = self
.get_asset_inputs_for_amount(BASE_ASSET_ID, amount)
Expand All @@ -304,6 +307,7 @@ pub trait Account: ViewOnlyAccount {
amount,
inputs,
tx_parameters,
network_info,
);

let tx = self.add_fee_resources(tb, amount).await?;
Expand All @@ -327,7 +331,7 @@ mod tests {

use fuel_crypto::{Message, SecretKey};
use fuel_tx::{Address, Output};
use fuels_core::types::transaction::Transaction;
use fuels_core::types::{transaction::Transaction, transaction_builders::NetworkInfo};
use rand::{rngs::StdRng, RngCore, SeedableRng};

use super::*;
Expand Down Expand Up @@ -376,6 +380,12 @@ mod tests {
)?;
let wallet = WalletUnlocked::new_from_private_key(secret, None);

let network_info = NetworkInfo {
consensus_parameters: Default::default(),
max_gas_per_tx: 0,
min_gas_price: 0,
gas_costs: Default::default(),
};
// Set up a transaction
let mut tb = {
let input_coin = Input::ResourceSigned {
Expand All @@ -398,6 +408,7 @@ mod tests {
vec![input_coin],
vec![output_coin],
Default::default(),
network_info,
)
};

Expand All @@ -417,7 +428,7 @@ mod tests {
assert_eq!(signature, tx_signature);

// Check if the signature is what we expect it to be
assert_eq!(signature, Signature::from_str("d7027be16db0aada625ac8cd438f9b6187bd74465495ba39511c1ad72b7bb10af4ef582c94cc33433f7a1eb4f2ad21c471473947f5f645e90924ba273e2cee7f")?);
assert_eq!(signature, Signature::from_str("51198e39c541cd3197785fd8add8cdbec3dc5aba7f8fbb23eb09455dd1003a8b78d94f247df8e1577805ea7eebd6d58336393942fd98484609e9e7d6d7a55f28")?);

// Recover the address that signed the transaction
let recovered_address = signature.recover(&message)?;
Expand Down
2 changes: 0 additions & 2 deletions packages/fuels-accounts/src/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ impl Account for Predicate {
previous_base_amount: u64,
) -> Result<Tb::TxType> {
let consensus_parameters = self.try_provider()?.consensus_parameters();
tb = tb.with_consensus_parameters(consensus_parameters);

let new_base_amount =
calculate_base_amount_with_fee(&tb, &consensus_parameters, previous_base_amount)?;

Expand Down
8 changes: 8 additions & 0 deletions packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use fuels_core::{
message_proof::MessageProof,
node_info::NodeInfo,
transaction::Transaction,
transaction_builders::NetworkInfo,
transaction_response::TransactionResponse,
tx_status::TxStatus,
},
Expand Down Expand Up @@ -274,6 +275,13 @@ impl Provider {
self.consensus_parameters
}

pub async fn network_info(&self) -> ProviderResult<NetworkInfo> {
let node_info = self.node_info().await?;
let chain_info = self.chain_info().await?;

Ok(NetworkInfo::new(node_info, chain_info))
}

pub fn chain_id(&self) -> ChainId {
self.consensus_parameters.chain_id
}
Expand Down
2 changes: 0 additions & 2 deletions packages/fuels-accounts/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ impl Account for WalletUnlocked {
previous_base_amount: u64,
) -> Result<Tb::TxType> {
let consensus_parameters = self.try_provider()?.consensus_parameters();
tb = tb.with_consensus_parameters(consensus_parameters);

self.sign_transaction(&mut tb);

let new_base_amount =
Expand Down
Loading

0 comments on commit da941b2

Please sign in to comment.