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 get_asset_outputs_for_amount, get_asset_inputs_for_amount and adjust_for_fee from Account to ViewOnlyAccount #1498

Merged
merged 9 commits into from
Sep 11, 2024
2 changes: 1 addition & 1 deletion examples/cookbook/src/lib.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ mod tests {
use std::str::FromStr;

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::{
28 changes: 14 additions & 14 deletions packages/fuels-accounts/src/account.rs
Original file line number Diff line number Diff line change
@@ -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(
@@ -122,6 +108,17 @@ 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>>;

/// 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
@@ -155,7 +152,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(())
6 changes: 3 additions & 3 deletions packages/fuels-accounts/src/impersonated_account.rs
Original file line number Diff line number Diff line change
@@ -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()
@@ -37,10 +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(
@@ -56,7 +54,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())?;

8 changes: 4 additions & 4 deletions packages/fuels-accounts/src/predicate.rs
Original file line number Diff line number Diff line change
@@ -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()
@@ -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,
@@ -130,3 +127,6 @@ impl Account for Predicate {
.collect::<Vec<Input>>())
}
}

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

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

/// 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>>())
}
}

impl WalletUnlocked {
@@ -190,6 +209,7 @@ impl WalletUnlocked {
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl ViewOnlyAccount for WalletUnlocked {
fn address(&self) -> &Bech32Address {
self.wallet.address()
@@ -198,10 +218,7 @@ 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
@@ -212,14 +229,13 @@ impl Account for WalletUnlocked {
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())?;

Loading