Skip to content

Commit

Permalink
chore: Safe Swift refactor (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 authored Sep 12, 2024
1 parent 5831cba commit aa97347
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 385 deletions.
1 change: 1 addition & 0 deletions crates/ffi/src/account_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl FFIAccountClient {
config.chain_id.clone(),
config.config.into(),
service,
config.safe,
);
account_client
}
Expand Down
1 change: 1 addition & 0 deletions crates/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod ffi {
pub chain_id: u64,
pub config: FFIConfig,
pub signer_type: String,
pub safe: bool,
}

enum FFIStringResult {
Expand Down
21 changes: 19 additions & 2 deletions crates/yttrium/src/account_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::Config;
use crate::private_key_service::PrivateKeyService;
use crate::sign_service::SignService;
use crate::transaction::send::safe_test;
use crate::transaction::{send::send_transaction, Transaction};
use alloy::primitives::Address;
use alloy::signers::local::PrivateKeySigner;
Expand Down Expand Up @@ -47,6 +48,7 @@ pub struct AccountClient {
chain_id: u64,
config: Config,
signer: Signer,
safe: bool,
}

impl AccountClient {
Expand All @@ -61,6 +63,7 @@ impl AccountClient {
chain_id,
config: config.clone(),
signer: Signer::Native(Arc::new(Mutex::new(sign_service))),
safe: false,
}
}

Expand All @@ -69,6 +72,7 @@ impl AccountClient {
chain_id: u64,
config: Config,
private_key_service: PrivateKeyService,
safe: bool,
) -> Self {
Self {
owner,
Expand All @@ -77,6 +81,7 @@ impl AccountClient {
signer: Signer::PrivateKey(Arc::new(Mutex::new(
private_key_service,
))),
safe,
}
}

Expand All @@ -98,6 +103,7 @@ impl AccountClient {
signer: Signer::PrivateKey(Arc::new(Mutex::new(
private_key_service,
))),
safe: false,
}
}

Expand All @@ -111,6 +117,7 @@ impl AccountClient {
self.chain_id.clone(),
self.config.clone(),
self.signer.clone(),
self.safe,
)
.await
}
Expand All @@ -136,6 +143,7 @@ impl AccountClient {
self.chain_id.clone(),
self.config.clone(),
self.signer.clone(),
self.safe,
)
.await
}
Expand All @@ -162,6 +170,7 @@ impl AccountClient {
chain_id: 0,
config: Config::local(),
signer: Signer::Native(Arc::new(Mutex::new(SignService::mock()))),
safe: false,
}
}
}
Expand All @@ -171,6 +180,7 @@ pub async fn get_address_with_signer(
chain_id: u64,
config: Config,
signer: Signer,
safe: bool,
) -> eyre::Result<String> {
match signer {
Signer::PrivateKey(private_key_service) => {
Expand All @@ -185,6 +195,7 @@ pub async fn get_address_with_signer(
chain_id,
config,
private_key_signer,
safe,
)
.await
}
Expand All @@ -199,11 +210,15 @@ pub async fn get_address_with_private_key_signer(
chain_id: u64,
config: Config,
signer: PrivateKeySigner,
safe: bool,
) -> eyre::Result<String> {
use crate::smart_accounts::simple_account::sender_address::get_sender_address_with_signer;

let sender_address =
get_sender_address_with_signer(config, chain_id, signer).await?;
let sender_address = if safe {
safe_test::get_address(signer, config).await?
} else {
get_sender_address_with_signer(config, chain_id, signer).await?
};

Ok(sender_address.to_string())
}
Expand Down Expand Up @@ -239,6 +254,7 @@ mod tests {
chain_id,
config,
private_key_service,
false,
);

let transaction = Transaction::new_from_strings(
Expand Down Expand Up @@ -278,6 +294,7 @@ mod tests {
chain_id,
config,
private_key_service,
false,
);

let sender_address = account_client.get_address().await?;
Expand Down
24 changes: 21 additions & 3 deletions crates/yttrium/src/transaction/send.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::smart_accounts::safe::Execution;
use crate::transaction::send::simple_account_test::send_transaction_with_signer;
use crate::{
config::Config, transaction::Transaction, user_operation::UserOperationV07,
};
use alloy::signers::local::PrivateKeySigner;
use core::fmt;

mod safe_test;
pub mod safe_test;
pub mod send_tests;
pub mod simple_account_test;

Expand Down Expand Up @@ -50,6 +51,7 @@ pub async fn send_transaction(
chain_id: u64,
config: Config,
signer: Signer,
safe: bool,
) -> eyre::Result<String> {
match signer {
Signer::PrivateKey(private_key_service) => {
Expand All @@ -65,6 +67,7 @@ pub async fn send_transaction(
chain_id,
config,
private_key_signer,
safe,
)
.await
}
Expand All @@ -80,12 +83,27 @@ pub async fn send_transaction_with_private_key_signer(
chain_id: u64,
config: Config,
private_key_signer: PrivateKeySigner,
safe: bool,
) -> eyre::Result<String> {
let signer = private_key_signer;

let user_operation_hash =
let user_operation_hash = if safe {
safe_test::send_transaction(
vec![Execution {
target: transaction.to,
value: transaction.value,
callData: transaction.data,
}],
signer,
None,
None,
config,
)
.await?
} else {
send_transaction_with_signer(transaction, config, chain_id, signer)
.await?;
.await?
};

println!("user_operation_hash: {:?}", user_operation_hash);

Expand Down
Loading

0 comments on commit aa97347

Please sign in to comment.