Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: move methods from Account to ViewOnlyAccount #1540

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/custom-transactions/transaction-builders.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ As we have used coins that require a signature, we have to add the signer to the

> **Note** The signature is not created until the transaction is finalized with `build(&provider)`

We need to do one more thing before we stop thinking about transaction inputs. Executing the transaction also incurs a fee that is paid with the base asset. Our base asset inputs need to be large enough so that the total amount covers the transaction fee and any other operations we are doing. The `Account` trait lets us use `adjust_for_fee()` for adjusting the transaction inputs if needed to cover the fee. The second argument to `adjust_for_fee()` is the total amount of the base asset that we expect our transaction to spend regardless of fees. In our case, this is the **ask_amount** we are transferring to the predicate.
We need to do one more thing before we stop thinking about transaction inputs. Executing the transaction also incurs a fee that is paid with the base asset. Our base asset inputs need to be large enough so that the total amount covers the transaction fee and any other operations we are doing. The `ViewOnlyAccount` trait lets us use `adjust_for_fee()` for adjusting the transaction inputs if needed to cover the fee. The second argument to `adjust_for_fee()` is the total amount of the base asset that we expect our transaction to spend regardless of fees. In our case, this is the **ask_amount** we are transferring to the predicate.

```rust,ignore
{{#include ../../../examples/cookbook/src/lib.rs:custom_tx_adjust}}
Expand Down
2 changes: 1 addition & 1 deletion examples/cookbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use std::{str::FromStr, time::Duration};

use fuels::{
accounts::{predicate::Predicate, wallet::WalletUnlocked, Account, ViewOnlyAccount},
accounts::{predicate::Predicate, wallet::WalletUnlocked, ViewOnlyAccount},
prelude::Result,
test_helpers::{setup_single_asset_coins, setup_test_provider},
types::{
Expand Down
26 changes: 12 additions & 14 deletions packages/fuels-accounts/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,6 @@ pub trait ViewOnlyAccount: std::fmt::Debug + Send + Sync + Clone {

self.try_provider()?.get_spendable_resources(filter).await
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait Account: ViewOnlyAccount {
/// Returns a vector consisting of `Input::Coin`s and `Input::Message`s for the given
/// asset ID and amount. The `witness_index` is the position of the witness (signature)
/// in the transaction's list of witnesses. In the validation process, the node will
/// use the witness at this index to validate the coins returned by this method.
async fn get_asset_inputs_for_amount(
&self,
asset_id: AssetId,
amount: u64,
excluded_coins: Option<Vec<CoinTypeId>>,
) -> Result<Vec<Input>>;

/// Returns a vector containing the output coin and change output given an asset and amount
fn get_asset_outputs_for_amount(
Expand All @@ -122,6 +108,15 @@ pub trait Account: ViewOnlyAccount {
]
}

/// Returns a vector consisting of `Input::Coin`s and `Input::Message`s for the given
/// asset ID and amount.
async fn get_asset_inputs_for_amount(
&self,
asset_id: AssetId,
amount: u64,
excluded_coins: Option<Vec<CoinTypeId>>,
) -> Result<Vec<Input>>;

/// Add base asset inputs to the transaction to cover the estimated fee.
/// Requires contract inputs to be at the start of the transactions inputs vec
/// so that their indexes are retained
Expand Down Expand Up @@ -155,7 +150,10 @@ pub trait Account: ViewOnlyAccount {

Ok(())
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait Account: ViewOnlyAccount {
// Add signatures to the builder if the underlying account is a wallet
fn add_witnesses<Tb: TransactionBuilder>(&self, _tb: &mut Tb) -> Result<()> {
Ok(())
Expand Down
8 changes: 3 additions & 5 deletions packages/fuels-accounts/src/impersonated_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl ImpersonatedAccount {
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl ViewOnlyAccount for ImpersonatedAccount {
fn address(&self) -> &Bech32Address {
self.address()
Expand All @@ -37,12 +38,7 @@ impl ViewOnlyAccount for ImpersonatedAccount {
fn try_provider(&self) -> Result<&Provider> {
self.provider.as_ref().ok_or_else(try_provider_error)
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl Account for ImpersonatedAccount {
/// Returns a vector consisting of `Input::Coin`s and `Input::Message`s for the given
/// asset ID and amount.
async fn get_asset_inputs_for_amount(
&self,
asset_id: AssetId,
Expand All @@ -56,7 +52,9 @@ impl Account for ImpersonatedAccount {
.map(Input::resource_signed)
.collect::<Vec<Input>>())
}
}

impl Account for ImpersonatedAccount {
fn add_witnesses<Tb: TransactionBuilder>(&self, tb: &mut Tb) -> Result<()> {
tb.add_signer(self.clone())?;

Expand Down
8 changes: 4 additions & 4 deletions packages/fuels-accounts/src/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Predicate {
}

#[cfg(feature = "std")]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl ViewOnlyAccount for Predicate {
fn address(&self) -> &Bech32Address {
self.address()
Expand All @@ -109,11 +110,7 @@ impl ViewOnlyAccount for Predicate {
fn try_provider(&self) -> Result<&Provider> {
self.provider.as_ref().ok_or_else(try_provider_error)
}
}

#[cfg(feature = "std")]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl Account for Predicate {
async fn get_asset_inputs_for_amount(
&self,
asset_id: AssetId,
Expand All @@ -130,3 +127,6 @@ impl Account for Predicate {
.collect::<Vec<Input>>())
}
}

#[cfg(feature = "std")]
impl Account for Predicate {}
34 changes: 21 additions & 13 deletions packages/fuels-accounts/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl Wallet {
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl ViewOnlyAccount for Wallet {
fn address(&self) -> &Bech32Address {
self.address()
Expand All @@ -86,6 +87,20 @@ impl ViewOnlyAccount for Wallet {
fn try_provider(&self) -> Result<&Provider> {
self.provider.as_ref().ok_or_else(try_provider_error)
}

async fn get_asset_inputs_for_amount(
&self,
asset_id: AssetId,
amount: u64,
excluded_coins: Option<Vec<CoinTypeId>>,
) -> Result<Vec<Input>> {
Ok(self
.get_spendable_resources(asset_id, amount, excluded_coins)
.await?
.into_iter()
.map(Input::resource_signed)
.collect::<Vec<Input>>())
}
}

impl WalletUnlocked {
Expand Down Expand Up @@ -190,6 +205,7 @@ impl WalletUnlocked {
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl ViewOnlyAccount for WalletUnlocked {
fn address(&self) -> &Bech32Address {
self.wallet.address()
Expand All @@ -198,28 +214,20 @@ impl ViewOnlyAccount for WalletUnlocked {
fn try_provider(&self) -> Result<&Provider> {
self.provider.as_ref().ok_or_else(try_provider_error)
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl Account for WalletUnlocked {
/// Returns a vector consisting of `Input::Coin`s and `Input::Message`s for the given
/// asset ID and amount. The `witness_index` is the position of the witness (signature)
/// in the transaction's list of witnesses. In the validation process, the node will
/// use the witness at this index to validate the coins returned by this method.
async fn get_asset_inputs_for_amount(
&self,
asset_id: AssetId,
amount: u64,
excluded_coins: Option<Vec<CoinTypeId>>,
) -> Result<Vec<Input>> {
Ok(self
.get_spendable_resources(asset_id, amount, excluded_coins)
.await?
.into_iter()
.map(Input::resource_signed)
.collect::<Vec<Input>>())
self.wallet
.get_asset_inputs_for_amount(asset_id, amount, excluded_coins)
.await
}
}

impl Account for WalletUnlocked {
fn add_witnesses<Tb: TransactionBuilder>(&self, tb: &mut Tb) -> Result<()> {
tb.add_signer(self.clone())?;

Expand Down
2 changes: 1 addition & 1 deletion wasm-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ crate-type = ['cdylib']
fuels = { workspace = true }
fuels-core = { workspace = true }
getrandom = { version = "0.2.11", features = ["js"] }
wasm-bindgen-test = "0.3.39"
wasm-bindgen-test = "0.3.45"
Loading