Skip to content

Commit af7b5e2

Browse files
refactor: use builder pattern to simplify TapAgentContext creation
1 parent 59e5b2c commit af7b5e2

File tree

4 files changed

+32
-60
lines changed

4 files changed

+32
-60
lines changed

crates/tap-agent/src/agent/sender_allocation.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,14 @@ where
468468
escrow_accounts.clone(),
469469
)),
470470
];
471-
let context = TapAgentContext::new(
472-
pgpool.clone(),
473-
allocation_id,
474-
config.indexer_address,
475-
sender,
476-
escrow_accounts.clone(),
477-
);
471+
let context = TapAgentContext::builder()
472+
.pgpool(pgpool.clone())
473+
.allocation_id(allocation_id)
474+
.indexer_address(config.indexer_address)
475+
.sender(sender)
476+
.escrow_accounts(escrow_accounts.clone())
477+
.build();
478+
478479
let latest_rav = context.last_rav().await.unwrap_or_default();
479480
let tap_manager = TapManager::new(
480481
domain_separator.clone(),

crates/tap-agent/src/tap/context.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,19 @@ impl NetworkVersion for Horizon {
152152
/// Context used by [tap_core::manager::Manager] that enables certain helper methods
153153
///
154154
/// This context is implemented for PostgresSQL
155-
#[derive(Clone)]
155+
#[derive(Clone, bon::Builder)]
156156
pub struct TapAgentContext<T> {
157157
pgpool: PgPool,
158+
#[cfg_attr(test, builder(default = crate::test::ALLOCATION_ID_0))]
158159
allocation_id: Address,
160+
#[cfg_attr(test, builder(default = test_assets::TAP_SENDER.1))]
159161
sender: Address,
162+
#[cfg_attr(test, builder(default = crate::test::INDEXER.1))]
160163
indexer_address: Address,
161164
escrow_accounts: Receiver<EscrowAccounts>,
162165
/// We use phantom data as a marker since it's
163166
/// only used to define what methods are available
164167
/// for each type of network
168+
#[builder(default = PhantomData)]
165169
_phantom: PhantomData<T>,
166170
}
167-
168-
/// Allow any [NetworkVersion] to create a new context
169-
impl<T: NetworkVersion> TapAgentContext<T> {
170-
/// Creates a TapContext
171-
pub fn new(
172-
pgpool: PgPool,
173-
allocation_id: Address,
174-
indexer_address: Address,
175-
sender: Address,
176-
escrow_accounts: Receiver<EscrowAccounts>,
177-
) -> Self {
178-
Self {
179-
pgpool,
180-
allocation_id,
181-
indexer_address,
182-
sender,
183-
escrow_accounts,
184-
_phantom: PhantomData,
185-
}
186-
}
187-
}

crates/tap-agent/src/tap/context/rav.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,13 @@ mod test {
326326
use rstest::rstest;
327327
use sqlx::PgPool;
328328
use tap_core::signed_message::Eip712SignedMessage;
329-
use test_assets::{TAP_SENDER as SENDER, TAP_SIGNER as SIGNER};
329+
use test_assets::TAP_SIGNER as SIGNER;
330330
use tokio::sync::watch;
331331

332332
use super::*;
333333
use crate::{
334334
tap::context::NetworkVersion,
335-
test::{CreateRav, ALLOCATION_ID_0, INDEXER},
335+
test::{CreateRav, ALLOCATION_ID_0},
336336
};
337337

338338
#[derive(Debug)]
@@ -351,23 +351,17 @@ mod test {
351351
const VALUE_AGGREGATE: u128 = u128::MAX;
352352

353353
async fn legacy_adapter(pgpool: PgPool) -> TapAgentContext<Legacy> {
354-
TapAgentContext::new(
355-
pgpool,
356-
ALLOCATION_ID_0,
357-
INDEXER.1,
358-
SENDER.1,
359-
watch::channel(EscrowAccounts::default()).1,
360-
)
354+
TapAgentContext::builder()
355+
.pgpool(pgpool)
356+
.escrow_accounts(watch::channel(EscrowAccounts::default()).1)
357+
.build()
361358
}
362359

363360
async fn horizon_adapter(pgpool: PgPool) -> TapAgentContext<Horizon> {
364-
TapAgentContext::new(
365-
pgpool,
366-
ALLOCATION_ID_0,
367-
INDEXER.1,
368-
SENDER.1,
369-
watch::channel(EscrowAccounts::default()).1,
370-
)
361+
TapAgentContext::builder()
362+
.pgpool(pgpool)
363+
.escrow_accounts(watch::channel(EscrowAccounts::default()).1)
364+
.build()
371365
}
372366

373367
/// Insert a single receipt and retrieve it from the database using the adapter.

crates/tap-agent/src/tap/context/receipt.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ mod test {
406406
use tokio::sync::watch::{self, Receiver};
407407

408408
use super::*;
409-
use crate::test::{store_receipt, CreateReceipt, INDEXER, SENDER_2};
409+
use crate::test::{store_receipt, CreateReceipt, SENDER_2};
410410

411411
const ALLOCATION_ID_IRRELEVANT: Address = ALLOCATION_ID_1;
412412

@@ -427,26 +427,20 @@ mod test {
427427
pgpool: PgPool,
428428
escrow_accounts: Receiver<EscrowAccounts>,
429429
) -> TapAgentContext<Legacy> {
430-
TapAgentContext::new(
431-
pgpool.clone(),
432-
ALLOCATION_ID_0,
433-
INDEXER.1,
434-
SENDER.1,
435-
escrow_accounts,
436-
)
430+
TapAgentContext::builder()
431+
.pgpool(pgpool)
432+
.escrow_accounts(escrow_accounts)
433+
.build()
437434
}
438435

439436
async fn horizon_adapter(
440437
pgpool: PgPool,
441438
escrow_accounts: Receiver<EscrowAccounts>,
442439
) -> TapAgentContext<Horizon> {
443-
TapAgentContext::new(
444-
pgpool,
445-
ALLOCATION_ID_0,
446-
INDEXER.1,
447-
SENDER.1,
448-
escrow_accounts,
449-
)
440+
TapAgentContext::builder()
441+
.pgpool(pgpool)
442+
.escrow_accounts(escrow_accounts)
443+
.build()
450444
}
451445

452446
/// Insert a single receipt and retrieve it from the database using the adapter.

0 commit comments

Comments
 (0)