Skip to content

Commit

Permalink
fix: Blockchain API integration (#27)
Browse files Browse the repository at this point in the history
* chore: Blockchain API integration

* chore: fix relative path

* chore: refactor contract builds

* chore: more contracts

* chore: clippy fixes

* chore: fix all clippy errors

* fix: clean deinit submodules
  • Loading branch information
chris13524 authored Sep 20, 2024
1 parent f55f503 commit 6e24481
Show file tree
Hide file tree
Showing 33 changed files with 261 additions and 229 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- run: while ! curl localhost:3000/ping; do sleep 1; done
- run: cargo build --workspace --features=full --all-targets
- run: cargo test --features=full --lib --bins
# - run: cargo clippy --workspace --features=full --all-targets -- -D warnings
- run: cargo clippy --workspace --features=full --all-targets -- -D warnings
- run: cargo +nightly fmt --all -- --check

udeps:
Expand Down
35 changes: 15 additions & 20 deletions crates/ffi/src/account_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FFIAccountClient {
}

let owner_address = config.owner_address.clone();
let chain_id = config.chain_id.clone();
let chain_id = config.chain_id;
let signer_type = config.signer_type.clone();
let signer_id =
format!("{}-{}-{}", signer_type, owner_address, chain_id);
Expand All @@ -40,52 +40,47 @@ impl FFIAccountClient {
let signer_service =
ffi::NativeSignerFFI::new(signer_id.clone());
let sign = signer_service.sign(message);
let result = match sign {
match sign {
ffi::FFIStringResult::Ok(signed_message) => {
Ok(signed_message)
}
ffi::FFIStringResult::Err(error) => {
Err(YttriumError { message: error })
}
};
result
}
});
let owner = address_from_string(&owner_address).unwrap();
let signer = SignService::new(sign_fn, owner);
let account_client = AccountClient::new_with_sign_service(
AccountClient::new_with_sign_service(
config.owner_address.clone(),
config.chain_id.clone(),
config.chain_id,
config.config.into(),
signer,
);
account_client
)
}
SignerType::PrivateKey => {
let private_key_fn = Box::new(move || {
let private_key_service =
ffi::PrivateKeySignerFFI::new(signer_id.clone());
let private_key = private_key_service.private_key();
let result = match private_key {
match private_key {
ffi::FFIStringResult::Ok(private_key) => {
Ok(private_key)
}
ffi::FFIStringResult::Err(error) => {
Err(YttriumError { message: error })
}
};
result
}
});
let owner = address_from_string(&owner_address).unwrap();
let service = PrivateKeyService::new(private_key_fn, owner);
let account_client =
AccountClient::new_with_private_key_service(
config.owner_address.clone(),
config.chain_id.clone(),
config.config.into(),
service,
config.safe,
);
account_client
AccountClient::new_with_private_key_service(
config.owner_address.clone(),
config.chain_id,
config.config.into(),
service,
config.safe,
)
}
};

Expand Down
9 changes: 4 additions & 5 deletions crates/ffi/src/account_client_eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ impl FFI7702AccountClient {
}

let owner_address = config.owner_address.clone();
let chain_id = config.chain_id.clone();
let chain_id = config.chain_id;
let signer_id = format!("{}-{}", owner_address, chain_id);

let sign_fn = Box::new(move |message: String| {
let signer_service = ffi::NativeSignerFFI::new(signer_id.clone());
let sign = signer_service.sign(message);
let result = match sign {
match sign {
ffi::FFIStringResult::Ok(signed_message) => Ok(signed_message),
ffi::FFIStringResult::Err(error) => {
Err(YttriumError { message: error })
}
};
result
}
});

let owner = address_from_string(&owner_address).unwrap();
Expand All @@ -48,7 +47,7 @@ impl FFI7702AccountClient {

let account_client = AccountClient::new(
config.owner_address.clone(),
config.chain_id.clone(),
config.chain_id,
config.config.into(),
signer,
);
Expand Down
24 changes: 12 additions & 12 deletions crates/ffi/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use super::ffi;

impl Into<yttrium::config::Endpoint> for ffi::FFIEndpoint {
fn into(self) -> yttrium::config::Endpoint {
impl From<ffi::FFIEndpoint> for yttrium::config::Endpoint {
fn from(val: ffi::FFIEndpoint) -> Self {
yttrium::config::Endpoint {
api_key: self.api_key,
base_url: self.base_url,
api_key: val.api_key,
base_url: val.base_url,
}
}
}

impl Into<yttrium::config::Endpoints> for ffi::FFIEndpoints {
fn into(self) -> yttrium::config::Endpoints {
impl From<ffi::FFIEndpoints> for yttrium::config::Endpoints {
fn from(val: ffi::FFIEndpoints) -> Self {
yttrium::config::Endpoints {
rpc: self.rpc.into(),
bundler: self.bundler.into(),
paymaster: self.paymaster.into(),
rpc: val.rpc.into(),
bundler: val.bundler.into(),
paymaster: val.paymaster.into(),
}
}
}

impl Into<yttrium::config::Config> for ffi::FFIConfig {
fn into(self) -> yttrium::config::Config {
yttrium::config::Config { endpoints: self.endpoints.into() }
impl From<ffi::FFIConfig> for yttrium::config::Config {
fn from(val: ffi::FFIConfig) -> Self {
yttrium::config::Config { endpoints: val.endpoints.into() }
}
}
3 changes: 2 additions & 1 deletion crates/ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#![allow(dead_code, improper_ctypes, clippy::unnecessary_cast)]
use self::account_client::FFIAccountClient;
use self::account_client_eip7702::FFI7702AccountClient;
use swift_bridge;

pub mod account_client;
pub mod account_client_eip7702;
pub mod config;
pub mod error;
pub mod log;

#[allow(non_camel_case_types)]
#[swift_bridge::bridge]
mod ffi {

Expand Down
1 change: 1 addition & 0 deletions crates/yttrium/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.foundry/
2 changes: 1 addition & 1 deletion crates/yttrium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_pimlico_api = []

[dependencies]
# Ethereum
alloy = { git = "https://github.com/alloy-rs/alloy", version = "0.3.2", features = [
alloy = { version = "0.3.6", features = [
"contract",
"network",
"providers",
Expand Down
101 changes: 85 additions & 16 deletions crates/yttrium/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,84 @@ use {
};

fn main() {
// build_contracts();
build_contracts();
}

const CONTRACTS_DIR: &str = "crates/yttrium/safe-smart-account/contracts";

fn build_contracts() {
println!("cargo::rerun-if-changed={CONTRACTS_DIR}");
install_foundry();
compile_contracts(&format!("{CONTRACTS_DIR}/proxies"));
compile_contracts("crates/yttrium/safe-smart-account/contracts/proxies/SafeProxyFactory.sol", );
compile_contracts("crates/yttrium/safe-smart-account/contracts/Safe.sol");
compile_contracts(
"crates/yttrium/safe-smart-account/contracts/libraries/MultiSend.sol",
);
compile_contracts(
"safe-modules/modules/4337/contracts/SafeModuleSetup.sol",
);

{
println!("cargo::rerun-if-changed=safe7579");
let output = Command::new("pnpm")
.current_dir("safe7579")
.args(["install"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
println!("`pnpm install` status: {:?}", output.status);
let stdout = String::from_utf8(output.stdout).unwrap();
println!("`pnpm install` stdout: {stdout:?}");
let stderr = String::from_utf8(output.stderr).unwrap();
println!("`pnpm install` stderr: {stderr:?}");
assert!(output.status.success());
}
compile_contracts_with_args(
"safe7579/src/Safe7579Launchpad.sol",
&["--config-path=safe7579/foundry.toml".to_owned()],
);
compile_contracts_with_args(
"safe7579/src/Safe7579.sol",
&["--config-path=safe7579/foundry.toml".to_owned()],
);

{
println!("cargo::rerun-if-changed=src/contracts");
let output = Command::new("yarn")
.current_dir("src/contracts")
.args(["install"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
println!("`yarn install` status: {:?}", output.status);
let stdout = String::from_utf8(output.stdout).unwrap();
println!("`yarn install` stdout: {stdout:?}");
let stderr = String::from_utf8(output.stderr).unwrap();
println!("`yarn install` stderr: {stderr:?}");
assert!(output.status.success());
}
compile_contracts_with_args(
"src/contracts/contracts/samples/SimpleAccountFactory.sol",
&["--remappings=@openzeppelin=crates/yttrium/src/contracts/node_modules/@openzeppelin".to_owned()],
);
compile_contracts_with_args(
"src/contracts/contracts/samples/SimpleAccount.sol",
&["--remappings=@openzeppelin=crates/yttrium/src/contracts/node_modules/@openzeppelin".to_owned()],
);
compile_contracts_with_args(
"src/contracts/contracts/core/EntryPoint.sol",
&["--remappings=@openzeppelin=crates/yttrium/src/contracts/node_modules/@openzeppelin".to_owned()],
);
// extract_bytecodes();
}

fn format_foundry_dir(path: &str) -> String {
format!(
"{}/../../../../.foundry/{}",
std::env::var("OUT_DIR").unwrap(),
"{}/.foundry/{}",
std::env::var("CARGO_MANIFEST_DIR").unwrap(),
path
)
}
Expand Down Expand Up @@ -52,16 +114,23 @@ fn install_foundry() {
}

fn compile_contracts(contracts_dir: &str) {
compile_contracts_with_args(contracts_dir, &[]);
}

fn compile_contracts_with_args(contracts_dir: &str, args: &[String]) {
println!("cargo::rerun-if-changed={contracts_dir}");
let mut built_args = vec![
"build".to_owned(),
contracts_dir.to_owned(),
"--skip=test".to_owned(),
"--cache-path".to_owned(),
format_foundry_dir("forge/cache"),
"--out".to_owned(),
format_foundry_dir("forge/out"),
];
built_args.extend_from_slice(args);
let output = Command::new(format_foundry_dir("bin/forge"))
.args([
"build",
&format!("--contracts={contracts_dir}"),
"--skip=test",
"--cache-path",
&format_foundry_dir("forge/cache"),
"--out",
&format_foundry_dir("forge/out"),
])
.args(&built_args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
Expand Down
8 changes: 4 additions & 4 deletions crates/yttrium/src/account_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl AccountClient {
pub async fn get_address(&self) -> eyre::Result<String> {
get_address_with_signer(
self.owner.clone(),
self.chain_id.clone(),
self.chain_id,
self.config.clone(),
self.signer.clone(),
self.safe,
Expand All @@ -144,7 +144,7 @@ impl AccountClient {
send_transaction(
transaction,
self.owner.clone(),
self.chain_id.clone(),
self.chain_id,
self.config.clone(),
self.signer.clone(),
self.safe,
Expand Down Expand Up @@ -227,14 +227,14 @@ pub async fn get_address_with_signer(
)
.await
}
Signer::Native(sign_service) => {
Signer::Native(_sign_service) => {
todo!("Implement native signer support")
}
}
}

pub async fn get_address_with_private_key_signer(
owner: String,
_owner: String,
chain_id: u64,
config: Config,
signer: PrivateKeySigner,
Expand Down
24 changes: 12 additions & 12 deletions crates/yttrium/src/bundler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl BundlerClient {
jsonrpc: "2.0".into(),
id: 1,
method: "eth_estimateUserOperationGas".into(),
params: params,
params,
};
println!("req_body: {:?}", serde_json::to_string(&req_body)?);

Expand Down Expand Up @@ -284,22 +284,22 @@ mod tests {
let signature: Bytes = Bytes::new();

UserOperationV07 {
sender: sender.into(),
nonce: nonce.into(),
sender,
nonce,
factory: factory.into(),
factory_data: factory_data.into(),
call_data: call_data.into(),
call_gas_limit: call_gas_limit.into(),
verification_gas_limit: verification_gas_limit.into(),
call_data,
call_gas_limit,
verification_gas_limit,
paymaster_post_op_gas_limit: Some(U256::from(100000)),
paymaster_verification_gas_limit: Some(U256::from(100000)),
pre_verification_gas: pre_verification_gas.into(),
max_fee_per_gas: max_fee_per_gas.into(),
max_priority_fee_per_gas: max_priority_fee_per_gas.into(),
paymaster: paymaster,
paymaster_data: paymaster_data,
pre_verification_gas,
max_fee_per_gas,
max_priority_fee_per_gas,
paymaster,
paymaster_data,
// authorization_list: None,
signature: signature,
signature,
}
};

Expand Down
Loading

0 comments on commit 6e24481

Please sign in to comment.