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: update versions, minor fixes #214

Merged
merged 24 commits into from
Apr 1, 2024
Merged
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
40 changes: 40 additions & 0 deletions docs/beta-4/fuels-rs/packages/fuels-accounts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "fuels-accounts"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
description = "Fuel Rust SDK accounts."

[dependencies]
async-trait = { workspace = true, default-features = false }
bytes = { workspace = true, features = ["serde"] }
chrono = { workspace = true }
elliptic-curve = { workspace = true, default-features = false }
eth-keystore = { workspace = true }
fuel-core = { workspace = true, default-features = false, optional = true }
fuel-core-client = { workspace = true, features = ["default"] }
fuel-crypto = { workspace = true, features = ["random"] }
fuel-tx = { workspace = true }
fuel-types = { workspace = true, features = ["random"] }
fuel-vm = { workspace = true }
fuels-core = { workspace = true }
hex = { workspace = true, default-features = false, features = ["std"] }
itertools = { workspace = true }
rand = { workspace = true, default-features = false }
serde = { workspace = true, default-features = true, features = ["derive"] }
sha2 = { workspace = true, default-features = false }
tai64 = { workspace = true, features = ["serde"] }
thiserror = { workspace = true, default-features = false }
tokio = { workspace = true, features = ["full"] }

[dev-dependencies]
hex = { workspace = true, default-features = false, features = ["std"] }
tempfile = { workspace = true }

[features]
default = ["std"]
std = ["fuels-core/std"]
79 changes: 79 additions & 0 deletions docs/beta-4/fuels-rs/packages/fuels-accounts/src/accounts_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use fuel_tx::{ConsensusParameters, Output, Receipt};
use fuel_types::MessageId;
use fuels_core::{
constants::BASE_ASSET_ID,
types::{
bech32::Bech32Address,
errors::{error, Error, Result},
input::Input,
transaction_builders::TransactionBuilder,
},
};

pub fn extract_message_id(receipts: &[Receipt]) -> Option<MessageId> {
receipts.iter().find_map(|m| m.message_id())
}

pub fn calculate_base_amount_with_fee(
tb: &impl TransactionBuilder,
consensus_params: &ConsensusParameters,
previous_base_amount: u64,
) -> Result<u64> {
let transaction_fee = tb
.fee_checked_from_tx(consensus_params)?
.ok_or(error!(InvalidData, "Error calculating TransactionFee"))?;

let mut new_base_amount = transaction_fee.max_fee() + previous_base_amount;

// If the tx doesn't consume any UTXOs, attempting to repeat it will lead to an
// error due to non unique tx ids (e.g. repeated contract call with configured gas cost of 0).
// Here we enforce a minimum amount on the base asset to avoid this
let is_consuming_utxos = tb
.inputs()
.iter()
.any(|input| !matches!(input, Input::Contract { .. }));
const MIN_AMOUNT: u64 = 1;
if !is_consuming_utxos && new_base_amount == 0 {
new_base_amount = MIN_AMOUNT;
}

Ok(new_base_amount)
}

// Replace the current base asset inputs of a tx builder with the provided ones.
// Only signed resources and coin predicates are replaced, the remaining inputs are kept.
// Messages that contain data are also kept since we don't know who will consume the data.
pub fn adjust_inputs(
tb: &mut impl TransactionBuilder,
new_base_inputs: impl IntoIterator<Item = Input>,
) {
let adjusted_inputs = tb
.inputs()
.iter()
.filter(|input| {
input.contains_data()
|| !matches!(input , Input::ResourceSigned { resource , .. }
| Input::ResourcePredicate { resource, .. } if resource.asset_id() == BASE_ASSET_ID)
})
.cloned()
.chain(new_base_inputs)
.collect();

*tb.inputs_mut() = adjusted_inputs
}

pub fn adjust_outputs(
tb: &mut impl TransactionBuilder,
address: &Bech32Address,
new_base_amount: u64,
) {
let is_base_change_present = tb.outputs().iter().any(|output| {
matches!(output , Output::Change { asset_id , .. }
if asset_id == & BASE_ASSET_ID)
});

if !is_base_change_present && new_base_amount != 0 {
tb.outputs_mut()
.push(Output::change(address.into(), 0, BASE_ASSET_ID));
}
}
Loading
Loading