Skip to content

feat!: add possibility to place a predefined gas amount for predicate #1655

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 20 additions & 8 deletions packages/fuels-core/src/types/transaction_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,8 @@ fn resolve_fuel_inputs(
resource,
code,
data,
} => resolve_predicate_resource(resource, code, data),
gas_used,
} => resolve_predicate_resource(resource, code, data, gas_used),
Input::Contract {
utxo_id,
balance_root,
Expand Down Expand Up @@ -1399,10 +1400,19 @@ fn resolve_predicate_resource(
resource: CoinType,
code: Vec<u8>,
data: Vec<u8>,
gas_used: u64,
) -> Result<FuelInput> {
match resource {
CoinType::Coin(coin) => Ok(create_coin_predicate(coin.asset_id, coin, code, data)),
CoinType::Message(message) => Ok(create_coin_message_predicate(message, code, data)),
CoinType::Coin(coin) => Ok(create_coin_predicate(
coin.asset_id,
coin,
code,
data,
gas_used,
)),
CoinType::Message(message) => {
Ok(create_coin_message_predicate(message, code, data, gas_used))
}
CoinType::Unknown => Err(error_transaction!(
Builder,
"can not resolve `CoinType::Unknown`"
Expand Down Expand Up @@ -1447,14 +1457,15 @@ pub fn create_coin_predicate(
coin: Coin,
code: Vec<u8>,
predicate_data: Vec<u8>,
gas_used: u64,
) -> FuelInput {
FuelInput::coin_predicate(
coin.utxo_id,
coin.owner.into(),
coin.amount,
asset_id,
TxPointer::default(),
0u64,
gas_used,
code,
predicate_data,
)
Expand All @@ -1464,14 +1475,15 @@ pub fn create_coin_message_predicate(
message: Message,
code: Vec<u8>,
predicate_data: Vec<u8>,
gas_used: u64,
) -> FuelInput {
if message.data.is_empty() {
FuelInput::message_coin_predicate(
message.sender.into(),
message.recipient.into(),
message.amount,
message.nonce,
0u64,
gas_used,
code,
predicate_data,
)
Expand All @@ -1481,7 +1493,7 @@ pub fn create_coin_message_predicate(
message.recipient.into(),
message.amount,
message.nonce,
0u64,
gas_used,
message.data,
code,
predicate_data,
Expand Down Expand Up @@ -1551,15 +1563,15 @@ mod tests {
#[test]
fn create_message_coin_predicate_if_data_is_empty() {
assert!(matches!(
create_coin_message_predicate(given_a_message(vec![]), vec![], vec![]),
create_coin_message_predicate(given_a_message(vec![]), vec![], vec![], 0),
FuelInput::MessageCoinPredicate(_)
));
}

#[test]
fn create_message_data_predicate_if_data_is_not_empty() {
assert!(matches!(
create_coin_message_predicate(given_a_message(vec![42]), vec![], vec![]),
create_coin_message_predicate(given_a_message(vec![42]), vec![], vec![], 0),
FuelInput::MessageDataPredicate(_)
));
}
Expand Down
16 changes: 16 additions & 0 deletions packages/fuels-core/src/types/wrappers/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum Input {
resource: CoinType,
code: Vec<u8>,
data: Vec<u8>,
gas_used: u64,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change unless we annotated this previously with the non_exhaustive attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding non_exhaustive also makes this change a breaking change, but prevents breaking changes in the future, was that intended?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in any case, having non_exhaustive only on one enum variant makes it a little annoying for the consumer of this library, so perhaps we can just annotate the whole enum with it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I wanted this to be non breaking. Removed for now. Making the PR draft because I might not need it

},
Contract {
utxo_id: UtxoId,
Expand All @@ -36,6 +37,21 @@ impl Input {
resource,
code,
data,
gas_used: 0,
}
}

pub const fn resource_predicate_with_gas(
Copy link
Contributor

@hal3e hal3e May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is already a breaking change I would add the gas_used directly to the resource_predicate fn

resource: CoinType,
code: Vec<u8>,
data: Vec<u8>,
gas_used: u64,
) -> Self {
Self::ResourcePredicate {
resource,
code,
data,
gas_used,
}
}

Expand Down
Loading