diff --git a/backend/examples/summa_solvency_flow.rs b/backend/examples/summa_solvency_flow.rs index 76a0861d..c69da5bc 100644 --- a/backend/examples/summa_solvency_flow.rs +++ b/backend/examples/summa_solvency_flow.rs @@ -1,11 +1,7 @@ #![feature(generic_const_exprs)] use std::{error::Error, fs::File, io::BufReader, io::Write}; -use ethers::{ - abi::{encode, Token}, - types::{Bytes, U256}, - utils::keccak256, -}; +use ethers::types::{Bytes, U256}; use serde_json::{from_reader, to_string_pretty}; use summa_backend::{ @@ -38,43 +34,12 @@ async fn main() -> Result<(), Box> { ) .unwrap(); - // Retrieve hashed addresses using the `keccak256` method. - let address_hashes = address_ownership_client - .get_ownership_proofs() - .iter() - .map(|x| keccak256(encode(&[Token::String(x.cex_address.clone())]))) - .collect::>(); - // Dispatch the proof of address ownership. // the `dispatch_proof_of_address_ownership` function sends a transaction to the Summa contract. address_ownership_client .dispatch_proof_of_address_ownership() - .await - .unwrap(); + .await?; - // 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 - .ownership_proof_by_address(*address_hash) - .call() - .await - .unwrap(); - - assert_ne!(registered, U256::from(0)); - } println!("1. Ownership proofs are submitted successfully!"); // 2. Submit solvency proof @@ -98,21 +63,13 @@ async fn main() -> Result<(), Box> { .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();); + round.dispatch_solvency_proof().await?; println!("2. Solvency proof is submitted successfully!"); // 3. Generate Inclusion Proof // - // In a production setup, the CEX should first dispatch the solvency proof to update the Merkle sum tree's root before generating any inclusion proofs. - // Otherwise, users might distrust the provided `root_hash` in the inclusion proof, as it hasn't been published on-chain. + // Generate and export the inclusion proof for the specified user to a JSON file. let inclusion_proof = round.get_proof_of_inclusion(USER_INDEX).unwrap(); let filename = format!("user_{}_proof.json", USER_INDEX); @@ -142,6 +99,7 @@ async fn main() -> Result<(), Box> { // Verify the `leaf_hash` from the proof file. // It's assumed that both `user_name` and `balances` are provided by the CEX. + // The `balances` represent the user's balances on the CEX at `snapshot_time`. let user_name = "dxGaEAii".to_string(); let balances = vec![11888, 41163]; @@ -166,11 +124,7 @@ async fn main() -> Result<(), Box> { .collect(); // Get `mst_root` from contract. the `mst_root` is disptached by CEX with specific time `snapshot_time`. - let mst_root = summa_contract - .mst_roots(snapshot_time) - .call() - .await - .unwrap(); + let mst_root = summa_contract.mst_roots(snapshot_time).call().await?; // Match the `mst_root` with the `root_hash` derived from the proof. assert_eq!(mst_root, public_inputs[1]); @@ -178,8 +132,7 @@ async fn main() -> Result<(), Box> { // Validate the inclusion proof using the contract verifier. let verification_result = summa_contract .verify_inclusion_proof(proof, public_inputs, snapshot_time) - .await - .unwrap(); + .await?; println!( "4. Verifying the proof on contract veirifer for User #{}: {}", diff --git a/backend/src/contracts/signer.rs b/backend/src/contracts/signer.rs index 5b4f3ccc..96ebd653 100644 --- a/backend/src/contracts/signer.rs +++ b/backend/src/contracts/signer.rs @@ -5,10 +5,7 @@ use ethers::{ signers::{LocalWallet, Signer}, types::Address, }; -use serde_json::Value; -use std::{ - error::Error, fs::File, io::BufReader, path::Path, str::FromStr, sync::Arc, time::Duration, -}; +use std::{str::FromStr, sync::Arc, time::Duration}; use super::generated::summa_contract::{AddressOwnershipProof, Asset}; @@ -41,30 +38,6 @@ impl SummaSigner { } } - pub fn get_deployment_address>( - path: P, - chain_id: u64, - ) -> Result> { - // Open file in RO mode with buffer - let file = File::open(path)?; - let reader = BufReader::new(file); - - // Read the JSON contents of the file - let payload: Value = serde_json::from_reader(reader)?; - - // Retrieve the contract address from the deployments.json file - let deployment_address: &Value = &payload.as_object().unwrap() - [chain_id.to_string().as_str()] - .as_object() - .unwrap()["address"]; - - let summa_address: &str = deployment_address.as_str().unwrap(); - - let address: Address = summa_address.parse().unwrap(); - - Ok(address) - } - pub async fn submit_proof_of_address_ownership( &self, address_ownership_proofs: Vec, @@ -72,9 +45,10 @@ impl SummaSigner { let submit_proof_of_address_ownership = &self .summa_contract .submit_proof_of_address_ownership(address_ownership_proofs); - let tx = submit_proof_of_address_ownership.send().await.unwrap(); + let tx = submit_proof_of_address_ownership.send().await?; - tx.await.unwrap(); + // Wait for the pending transaction to be mined + tx.await?; Ok(()) } @@ -89,9 +63,10 @@ impl SummaSigner { let submit_proof_of_solvency_call = &self .summa_contract .submit_proof_of_solvency(mst_root, assets, proof, timestamp); - let tx = submit_proof_of_solvency_call.send().await.unwrap(); + let tx = submit_proof_of_solvency_call.send().await?; - tx.await.unwrap(); + // Wait for the pending transaction to be mined + tx.await?; Ok(()) } diff --git a/backend/src/tests.rs b/backend/src/tests.rs index faf41cee..20d1a5d1 100644 --- a/backend/src/tests.rs +++ b/backend/src/tests.rs @@ -132,7 +132,7 @@ mod test { .dispatch_proof_of_address_ownership() .await; - assert_eq!(ownership_submitted_result.is_ok(), true); + assert!(ownership_submitted_result.is_ok()); let logs = summa_contract .address_ownership_proof_submitted_filter()