diff --git a/backend/examples/summa_solvency_flow.rs b/backend/examples/summa_solvency_flow.rs index 76a0861d..bdea1925 100644 --- a/backend/examples/summa_solvency_flow.rs +++ b/backend/examples/summa_solvency_flow.rs @@ -1,8 +1,9 @@ #![feature(generic_const_exprs)] -use std::{error::Error, fs::File, io::BufReader, io::Write}; +use std::{error::Error, fs::File, io::BufReader, io::Write, sync::Arc}; use ethers::{ abi::{encode, Token}, + providers::Provider, types::{Bytes, U256}, utils::keccak256, }; @@ -13,6 +14,7 @@ use summa_backend::{ address_ownership::AddressOwnership, round::{MstInclusionProof, Round}, }, + contracts::signer::SummaSigner, tests::initialize_test_env, }; use summa_solvency::merkle_sum_tree::utils::generate_leaf_hash; @@ -29,14 +31,17 @@ async fn main() -> Result<(), Box> { // // Each CEX prepares its own `signature` CSV file. let signature_csv_path = "src/apis/csv/signatures.csv"; - let mut address_ownership_client = AddressOwnership::new( + // The signer would be using `provider` that shared with `address_ownership` and `round` instances. + let provider = Arc::new(Provider::try_from(anvil.endpoint().as_str())?); + let signer = SummaSigner::new( "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", anvil.chain_id(), - anvil.endpoint().as_str(), + provider, summa_contract.address(), - signature_csv_path, - ) - .unwrap(); + ); + + let mut address_ownership_client = + AddressOwnership::new(signer.clone(), signature_csv_path).unwrap(); // Retrieve hashed addresses using the `keccak256` method. let address_hashes = address_ownership_client @@ -52,19 +57,6 @@ async fn main() -> Result<(), Box> { .await .unwrap(); - // If the `addressHash` isn't found in the `addressOwnershipProofs` mapping of the Summa contract, - // it will return 0; otherwise, it will return a non-zero value. - // - // You can find unregistered address with null bytes as follows: - // - // let unregistered = summa_contract - // .ownership_proof_by_address([0u8; 32]) - // .call() - // .await - // .unwrap(); - // - // assert_eq!(unregistered, 0); - // Verify whether the addresses have been registered within the Summa contract. for address_hash in address_hashes.iter() { let registered = summa_contract @@ -85,28 +77,11 @@ async fn main() -> Result<(), Box> { let params_path = "ptau/hermez-raw-11"; // Using the `round` instance, the solvency proof is dispatched to the Summa contract with the `dispatch_solvency_proof` method. - let mut round = Round::<4, 2, 14>::new( - "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", // anvil account [0] - anvil.chain_id(), - anvil.endpoint().as_str(), - summa_contract.address(), - entry_csv, - asset_csv, - params_path, - 1, - ) - .unwrap(); + let mut round = Round::<4, 2, 14>::new(signer, entry_csv, asset_csv, params_path, 1).unwrap(); // Sends the solvency proof, which should ideally complete without errors. assert_eq!(round.dispatch_solvency_proof().await.unwrap(), ()); - // You can also use the `solvency_proof_submitted_filter` method to check if the solvency proof is submitted. - // println!("{:?}", summa_contract - // .solvency_proof_submitted_filter() - // .query() - // .await - // .unwrap();); - println!("2. Solvency proof is submitted successfully!"); // 3. Generate Inclusion Proof diff --git a/backend/src/apis/address_ownership.rs b/backend/src/apis/address_ownership.rs index 7b56348b..504dbe01 100644 --- a/backend/src/apis/address_ownership.rs +++ b/backend/src/apis/address_ownership.rs @@ -1,5 +1,4 @@ use crate::contracts::{generated::summa_contract::AddressOwnershipProof, signer::SummaSigner}; -use ethers::types::Address; use std::{error::Error, result::Result}; use super::csv_parser::parse_signature_csv; @@ -11,17 +10,14 @@ pub struct AddressOwnership { impl AddressOwnership { pub fn new( - signer_key: &str, - chain_id: u64, - rpc_url: &str, - summa_sc_address: Address, + signer: SummaSigner, signature_csv_path: &str, ) -> Result> { let address_ownership_proofs = parse_signature_csv(signature_csv_path)?; Ok(AddressOwnership { address_ownership_proofs, - signer: SummaSigner::new(signer_key, chain_id, rpc_url, summa_sc_address), + signer, }) } diff --git a/backend/src/apis/round.rs b/backend/src/apis/round.rs index c1f47b45..bb95a3de 100644 --- a/backend/src/apis/round.rs +++ b/backend/src/apis/round.rs @@ -1,7 +1,4 @@ -use ethers::{ - abi::Address, - types::{Bytes, U256}, -}; +use ethers::types::{Bytes, U256}; use halo2_proofs::{ halo2curves::bn256::{Bn256, Fr as Fp, G1Affine}, plonk::{ProvingKey, VerifyingKey}, @@ -79,10 +76,7 @@ where [usize; 2 * (1 + N_ASSETS)]: Sized, { pub fn new( - signer_key: &str, - chain_id: u64, - rpc_url: &str, - summa_sc_address: Address, + signer: SummaSigner, entry_csv_path: &str, asset_csv_path: &str, params_path: &str, @@ -96,7 +90,7 @@ where params_path, ) .unwrap(), - signer: SummaSigner::new(signer_key, chain_id, rpc_url, summa_sc_address), + signer, }) } diff --git a/backend/src/contracts/mod.rs b/backend/src/contracts/mod.rs index a6499aca..55333bf2 100644 --- a/backend/src/contracts/mod.rs +++ b/backend/src/contracts/mod.rs @@ -1,3 +1,3 @@ pub mod generated; pub mod mock; -pub(crate) mod signer; +pub mod signer; diff --git a/backend/src/contracts/signer.rs b/backend/src/contracts/signer.rs index 5b4f3ccc..ddc50917 100644 --- a/backend/src/contracts/signer.rs +++ b/backend/src/contracts/signer.rs @@ -6,15 +6,13 @@ use ethers::{ types::Address, }; use serde_json::Value; -use std::{ - error::Error, fs::File, io::BufReader, path::Path, str::FromStr, sync::Arc, time::Duration, -}; +use std::{error::Error, fs::File, io::BufReader, path::Path, str::FromStr, sync::Arc}; use super::generated::summa_contract::{AddressOwnershipProof, Asset}; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SummaSigner { - summa_contract: Summa, LocalWallet>>, + summa_contract: Summa>, LocalWallet>>, } impl SummaSigner { @@ -22,14 +20,16 @@ impl SummaSigner { /// # Arguments /// * `signer_key` - The private key of wallet that will interact with the chain on behalf of the exchange /// * `chain_id` - The chain id of the network - /// * `rpc_url` - The RPC URL of the network + /// * `provider` - Provider /// * `address` - The address of the Summa contract - pub fn new(signer_key: &str, chain_id: u64, rpc_url: &str, address: Address) -> Self { + pub fn new( + signer_key: &str, + chain_id: u64, + provider: Arc>, + address: Address, + ) -> Self { let wallet: LocalWallet = LocalWallet::from_str(signer_key).unwrap(); - let provider = Provider::::try_from(rpc_url) - .unwrap() - .interval(Duration::from_millis(10u64)); let client = Arc::new(SignerMiddleware::new( provider, wallet.with_chain_id(chain_id), diff --git a/backend/src/tests.rs b/backend/src/tests.rs index faf41cee..8df4a216 100644 --- a/backend/src/tests.rs +++ b/backend/src/tests.rs @@ -104,29 +104,36 @@ pub async fn initialize_test_env() -> ( mod test { use ethers::{ abi::AbiEncode, + providers::Provider, types::{Bytes, U256}, utils::to_checksum, }; + use std::{convert::TryFrom, error::Error, sync::Arc}; use crate::apis::{address_ownership::AddressOwnership, round::Round}; - use crate::contracts::generated::summa_contract::{ - AddressOwnershipProof, AddressOwnershipProofSubmittedFilter, Asset, - SolvencyProofSubmittedFilter, + use crate::contracts::{ + generated::summa_contract::{ + AddressOwnershipProof, AddressOwnershipProofSubmittedFilter, Asset, + SolvencyProofSubmittedFilter, + }, + signer::SummaSigner, }; use crate::tests::initialize_test_env; #[tokio::test] - async fn test_round_features() { + async fn test_round_features() -> Result<(), Box> { let (anvil, cex_addr_1, cex_addr_2, _, summa_contract) = initialize_test_env().await; - let mut address_ownership_client = AddressOwnership::new( + let provider = Arc::new(Provider::try_from(anvil.endpoint().as_str())?); + let signer = SummaSigner::new( "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", anvil.chain_id(), - anvil.endpoint().as_str(), + provider, summa_contract.address(), - "src/apis/csv/signatures.csv", - ) - .unwrap(); + ); + + let mut address_ownership_client = + AddressOwnership::new(signer.clone(), "src/apis/csv/signatures.csv").unwrap(); let ownership_submitted_result = address_ownership_client .dispatch_proof_of_address_ownership() @@ -166,17 +173,8 @@ mod test { let entry_csv = "../zk_prover/src/merkle_sum_tree/csv/entry_16.csv"; let params_path = "ptau/hermez-raw-11"; - let mut round = Round::<4, 2, 14>::new( - "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", // anvil account [0] - anvil.chain_id(), - anvil.endpoint().as_str(), - summa_contract.address(), - entry_csv, - asset_csv, - params_path, - 1, - ) - .unwrap(); + let mut round = + Round::<4, 2, 14>::new(signer, entry_csv, asset_csv, params_path, 1).unwrap(); // Verify solvency proof let mut logs = summa_contract @@ -247,5 +245,6 @@ mod test { assert_eq!(verified, true); drop(anvil); + Ok(()) } }