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

feat: Separate TransactionRequest and TransactionRequestBuilder #605

Merged
merged 17 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

### Features

* [BREAKING] Added `TransactionRequestBuilder` for building `TransactionRequest` (#605).
* Added caching for foreign account code (#597).

### Fixes

* Added Sync Loop to Integration Tests for Small Speedup (#590).

### Changes
Expand Down
20 changes: 12 additions & 8 deletions bin/miden-cli/src/commands/new_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use miden_client::{
crypto::{Digest, FeltRng},
notes::{build_swap_tag, get_input_note_with_id_prefix, NoteType as MidenNoteType},
transactions::{
PaymentTransactionData, SwapTransactionData, TransactionRequest, TransactionResult,
PaymentTransactionData, SwapTransactionData, TransactionRequest, TransactionRequestBuilder,
TransactionResult,
},
Client,
};
Expand Down Expand Up @@ -68,13 +69,14 @@ impl MintCmd {

let target_account_id = parse_account_id(&client, self.target_account_id.as_str()).await?;

let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
fungible_asset,
target_account_id,
(&self.note_type).into(),
client.rng(),
)
.map_err(|err| err.to_string())?;
.map_err(|err| err.to_string())?
.build();

execute_transaction(
&mut client,
Expand Down Expand Up @@ -138,13 +140,14 @@ impl SendCmd {
target_account_id,
);

let transaction_request = TransactionRequest::pay_to_id(
let transaction_request = TransactionRequestBuilder::pay_to_id(
payment_transaction,
self.recall_height,
(&self.note_type).into(),
client.rng(),
)
.map_err(|err| err.to_string())?;
.map_err(|err| err.to_string())?
.build();

execute_transaction(
&mut client,
Expand Down Expand Up @@ -205,12 +208,13 @@ impl SwapCmd {
requested_fungible_asset.into(),
);

let transaction_request = TransactionRequest::swap(
let transaction_request = TransactionRequestBuilder::swap(
swap_transaction.clone(),
(&self.note_type).into(),
client.rng(),
)
.map_err(|err| err.to_string())?;
.map_err(|err| err.to_string())?
.build();

execute_transaction(
&mut client,
Expand Down Expand Up @@ -283,7 +287,7 @@ impl ConsumeNotesCmd {
return Err(format!("No input notes were provided and the store does not contain any notes consumable by {account_id}"));
}

let transaction_request = TransactionRequest::consume_notes(list_of_notes);
let transaction_request = TransactionRequestBuilder::consume_notes(list_of_notes).build();

execute_transaction(
&mut client,
Expand Down
27 changes: 16 additions & 11 deletions crates/rust-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
mock::create_test_client,
rpc::NodeRpcClient,
store::{InputNoteRecord, NoteFilter, Store},
transactions::TransactionRequest,
transactions::TransactionRequestBuilder,
ClientError,
};

Expand Down Expand Up @@ -397,13 +397,14 @@ async fn test_mint_transaction() {
client.sync_state().await.unwrap();

// Test submitting a mint transaction
let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
FungibleAsset::new(faucet.id(), 5u64).unwrap(),
AccountId::from_hex("0x168187d729b31a84").unwrap(),
miden_objects::notes::NoteType::Private,
client.rng(),
)
.unwrap();
.unwrap()
.build();

let transaction = client.new_transaction(faucet.id(), transaction_request).await.unwrap();

Expand All @@ -428,13 +429,14 @@ async fn test_get_output_notes() {
.unwrap();

// Test submitting a mint transaction
let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
FungibleAsset::new(faucet.id(), 5u64).unwrap(),
AccountId::from_hex("0x0123456789abcdef").unwrap(),
miden_objects::notes::NoteType::Private,
client.rng(),
)
.unwrap();
.unwrap()
.build();

//Before executing transaction, there are no output notes
assert!(client.get_output_notes(NoteFilter::All).await.unwrap().is_empty());
Expand Down Expand Up @@ -494,15 +496,16 @@ async fn test_transaction_request_expiration() {
.await
.unwrap();

let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
FungibleAsset::new(faucet.id(), 5u64).unwrap(),
AccountId::from_hex("0x168187d729b31a84").unwrap(),
miden_objects::notes::NoteType::Private,
client.rng(),
)
.unwrap()
.with_expiration_delta(5)
.unwrap();
.unwrap()
.build();

let transaction = client.new_transaction(faucet.id(), transaction_request).await.unwrap();

Expand Down Expand Up @@ -537,13 +540,14 @@ async fn test_import_processing_note_returns_error() {
.unwrap();

// Test submitting a mint transaction
let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
FungibleAsset::new(faucet.id(), 5u64).unwrap(),
account.id(),
miden_objects::notes::NoteType::Private,
client.rng(),
)
.unwrap();
.unwrap()
.build();

let transaction =
client.new_transaction(faucet.id(), transaction_request.clone()).await.unwrap();
Expand All @@ -552,8 +556,9 @@ async fn test_import_processing_note_returns_error() {
let note_id = transaction_request.expected_output_notes().next().unwrap().id();
let note = client.get_input_note(note_id).await.unwrap();

let input = [(note.try_into().unwrap(), None)].into_iter();
let consume_note_request = TransactionRequest::new().with_unauthenticated_input_notes(input);
let input = [(note.try_into().unwrap(), None)];
let consume_note_request =
TransactionRequestBuilder::new().with_unauthenticated_input_notes(input).build();
let transaction = client
.new_transaction(account.id(), consume_note_request.clone())
.await
Expand Down
11 changes: 6 additions & 5 deletions crates/rust-client/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
mod request;
pub use request::{
NoteArgs, PaymentTransactionData, SwapTransactionData, TransactionRequest,
TransactionRequestError, TransactionScriptTemplate,
TransactionRequestBuilder, TransactionRequestError, TransactionScriptTemplate,
};

mod script_builder;
Expand Down Expand Up @@ -910,8 +910,8 @@ mod test {
Felt, FieldElement, Word,
};

use super::{PaymentTransactionData, TransactionRequest};
use crate::mock::create_test_client;
use super::PaymentTransactionData;
use crate::{mock::create_test_client, transactions::TransactionRequestBuilder};

#[tokio::test]
async fn test_transaction_creates_two_notes() {
Expand Down Expand Up @@ -953,7 +953,7 @@ mod test {
.await
.unwrap();
client.sync_state().await.unwrap();
let tx_request = TransactionRequest::pay_to_id(
let tx_request = TransactionRequestBuilder::pay_to_id(
PaymentTransactionData::new(
vec![asset_1, asset_2],
account.id(),
Expand All @@ -963,7 +963,8 @@ mod test {
NoteType::Private,
client.rng(),
)
.unwrap();
.unwrap()
.build();

let tx_result = client.new_transaction(account.id(), tx_request).await.unwrap();
assert!(tx_result
Expand Down
Loading