Skip to content

Commit

Permalink
Eliminate ambiguity between assets/cryptos/liabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
alxkzmn committed Nov 27, 2023
1 parent 9b5e8ad commit 78352ac
Show file tree
Hide file tree
Showing 13 changed files with 904 additions and 851 deletions.
4 changes: 2 additions & 2 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ Without publishing the commitment, users cannot verify their inclusion proof on
In here, we'll introduce you through the process of submitting a commitment using the `Round` to the Summa contract.
The Round serves as the core of the backend in Summa, and we have briefly described it in the Components section.

To initialize the `Round` instance, you'll need paths to the user asset balances CSV file (`entry_16.csv`) and the `ptau/hermez-raw-11` file. The files serve the following purpose:
To initialize the `Round` instance, you'll need paths to the liabilities CSV file (`entry_16.csv`) and the `ptau/hermez-raw-11` file. The files serve the following purpose:

- `entry_16.csv`: contains the username and asset balance entries for each CEX user (necessary to build the commitment). Asset balance column names have the following format: `balance_<ASSET>_<CHAIN>`, where <ASSET> and <CHAIN> are the names of the assets and their corresponding blockchains. <CHAIN> values are the same as in the Address Ownership Proof step;
- `entry_16.csv`: contains the username and liabilities entries for each CEX user (necessary to build the commitment). Liabilities column names have the following format: `balance_<CRYPTOCURRENCY>_<CHAIN>`, where <CRYPTOCURRENCY> and <CHAIN> are the names of the cryptocurrencies and their corresponding blockchains. <CHAIN> values are the same as in the Address Ownership Proof step;
- `ptau/hermez-raw-11`: contains parameters for constructing the zk circuits.

Using the `Round` instance, the solvency proof is dispatched to the Summa contract with the `dispatch_solvency_proof` method.
Expand Down
12 changes: 6 additions & 6 deletions backend/src/apis/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use halo2_proofs::{
use serde::{Deserialize, Serialize};
use std::error::Error;

use crate::contracts::{generated::summa_contract::summa::Asset, signer::SummaSigner};
use crate::contracts::{generated::summa_contract::summa::Cryptocurrency, signer::SummaSigner};
use summa_solvency::{
circuits::{
merkle_sum_tree::MstInclusionCircuit,
Expand Down Expand Up @@ -89,13 +89,13 @@ where
root_sums,
self.snapshot
.mst
.assets()
.cryptocurrencies()
.iter()
.map(|asset| Asset {
asset_name: asset.name.clone(),
chain: asset.chain.clone(),
.map(|cryptocurrency| Cryptocurrency {
name: cryptocurrency.name.clone(),
chain: cryptocurrency.chain.clone(),
})
.collect::<Vec<Asset>>()
.collect::<Vec<Cryptocurrency>>()
.as_slice()
.try_into()
.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion backend/src/contracts/abi/Summa.json

Large diffs are not rendered by default.

1,518 changes: 780 additions & 738 deletions backend/src/contracts/generated/summa_contract.rs

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions backend/src/contracts/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde_json::Value;
use std::{error::Error, fs::File, io::BufReader, path::Path, str::FromStr, sync::Arc};
use tokio::sync::Mutex;

use super::generated::summa_contract::{AddressOwnershipProof, Asset};
use super::generated::summa_contract::{AddressOwnershipProof, Cryptocurrency};
use crate::contracts::generated::summa_contract::Summa;

pub enum AddressInput {
Expand Down Expand Up @@ -108,14 +108,17 @@ impl SummaSigner {
&self,
mst_root: U256,
root_sums: Vec<U256>,
assets: Vec<Asset>,
cryptocurrencies: Vec<Cryptocurrency>,
timestamp: U256,
) -> Result<(), Box<dyn std::error::Error>> {
let lock_guard = self.nonce_lock.lock().await;

let submit_liability_commitment = &self
.summa_contract
.submit_commitment(mst_root, root_sums, assets, timestamp);
let submit_liability_commitment = &self.summa_contract.submit_commitment(
mst_root,
root_sums,
cryptocurrencies,
timestamp,
);

// To prevent nonce collision, we lock the nonce before sending the transaction
let tx = submit_liability_commitment.send().await?;
Expand Down
12 changes: 6 additions & 6 deletions backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mod test {
use crate::apis::{address_ownership::AddressOwnership, round::Round};
use crate::contracts::{
generated::summa_contract::{
AddressOwnershipProof, AddressOwnershipProofSubmittedFilter, Asset,
AddressOwnershipProof, AddressOwnershipProofSubmittedFilter, Cryptocurrency,
LiabilitiesCommitmentSubmittedFilter,
},
signer::{AddressInput, SummaSigner},
Expand Down Expand Up @@ -273,13 +273,13 @@ mod test {
.parse()
.unwrap(),
root_balances: vec![U256::from(556862), U256::from(556862)],
assets: vec![
Asset {
asset_name: "ETH".to_string(),
cryptocurrencies: vec![
Cryptocurrency {
name: "ETH".to_string(),
chain: "ETH".to_string(),
},
Asset {
asset_name: "USDT".to_string(),
Cryptocurrency {
name: "USDT".to_string(),
chain: "ETH".to_string(),
},
],
Expand Down
2 changes: 1 addition & 1 deletion contracts/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Summa Smart Contract

The [Summa smart contract](src/Summa.sol) acts as a registrar for Centralized Exchanges (CEXs) to commit to their liabilities by submitting a Merkle sum tree (MST) root of all the CEX user asset balances. Users can then verify their inclusion into the liabilities commitment, and the public can compare the committed total sums with the assets owned by the CEX onchain.
The [Summa smart contract](src/Summa.sol) acts as a registrar for Centralized Exchanges (CEXs) to commit to their liabilities by submitting a Merkle sum tree (MST) root of all the CEX liabilities owed to its users. Users can then verify their inclusion into the liabilities commitment, and the public can compare the committed total sums with the assets owned by the CEX onchain.

## Features

Expand Down
56 changes: 29 additions & 27 deletions contracts/src/Summa.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ contract Summa is Ownable {
}

/**
* @dev Struct representing an asset owned by the CEX
* @param assetName The name of the asset
* @param chain The name of the chain name where the asset lives (e.g., ETH, BTC)
* @dev Struct identifying a cryptocurrency traded on the CEX
* @param name The name of the cryptocurrency
* @param chain The name of the chain name where the cryptocurrency lives (e.g., ETH, BTC)
*/
struct Asset {
string assetName;
struct Cryptocurrency {
string name;
string chain;
}

/**
* @dev Struct representing a commitment submitted by the CEX.
* @param mstRoot Merkle sum tree root of the CEX's liabilities
* @param rootBalances The total sums of the assets included in the tree
* @param assetChains The chains where the CEX holds the assets included into the tree
* @param assetNames The names of the assets included into the tree
* @param rootBalances The total sums of the liabilities included in the tree
* @param blockchainNames The names of the blockchains where the CEX holds the cryptocurrencies included into the tree
* @param cryptocurrencyNames The names of the cryptocurrencies included into the tree
*/
struct Commitment {
uint256 mstRoot;
uint256[] rootBalances;
string[] assetNames;
string[] assetChains;
string[] cryptocurrencyNames;
string[] blockchainNames;
}

// User inclusion proof verifier
Expand Down Expand Up @@ -78,7 +78,7 @@ contract Summa is Ownable {
uint256 indexed timestamp,
uint256 mstRoot,
uint256[] rootBalances,
Asset[] assets
Cryptocurrency[] cryptocurrencies
);

constructor(IVerifier _inclusionVerifier) {
Expand Down Expand Up @@ -117,49 +117,51 @@ contract Summa is Ownable {
/**
* @dev Submit commitment for a CEX
* @param mstRoot Merkle sum tree root of the CEX's liabilities
* @param rootBalances The total sums of the assets included into the Merkle sum tree
* @param assets The assets included into the Merkle sum tree
* @param rootBalances The total sums of the liabilities included into the Merkle sum tree
* @param cryptocurrencies The cryptocurrencies included into the Merkle sum tree
* @param timestamp The timestamp at which the CEX took the snapshot of its assets and liabilities
*/
function submitCommitment(
uint256 mstRoot,
uint256[] memory rootBalances,
Asset[] memory assets,
Cryptocurrency[] memory cryptocurrencies,
uint256 timestamp
) public onlyOwner {
require(mstRoot != 0, "Invalid MST root");
require(
rootBalances.length == assets.length,
"Root asset sums and asset number mismatch"
rootBalances.length == cryptocurrencies.length,
"Root liabilities sums and liabilities number mismatch"
);
string[] memory assetNames = new string[](assets.length);
string[] memory assetChains = new string[](assets.length);
for (uint i = 0; i < assets.length; i++) {
string[] memory cryptocurrencyNames = new string[](
cryptocurrencies.length
);
string[] memory blockchainNames = new string[](cryptocurrencies.length);
for (uint i = 0; i < cryptocurrencies.length; i++) {
require(
bytes(assets[i].chain).length != 0 &&
bytes(assets[i].assetName).length != 0,
"Invalid asset"
bytes(cryptocurrencies[i].chain).length != 0 &&
bytes(cryptocurrencies[i].name).length != 0,
"Invalid cryptocurrency"
);
require(
rootBalances[i] != 0,
"All root sums should be greater than zero"
);
assetNames[i] = assets[i].assetName;
assetChains[i] = assets[i].chain;
cryptocurrencyNames[i] = cryptocurrencies[i].name;
blockchainNames[i] = cryptocurrencies[i].chain;
}

commitments[timestamp] = Commitment(
mstRoot,
rootBalances,
assetNames,
assetChains
cryptocurrencyNames,
blockchainNames
);

emit LiabilitiesCommitmentSubmitted(
timestamp,
mstRoot,
rootBalances,
assets
cryptocurrencies
);
}

Expand Down
Loading

0 comments on commit 78352ac

Please sign in to comment.