Skip to content

Commit

Permalink
add flow-edges from users with trusted tokens to the organizations th…
Browse files Browse the repository at this point in the history
…at trust them.
  • Loading branch information
jaensen committed May 30, 2023
1 parent 18cc005 commit 5950ab8
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/safe_db/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap, HashSet};

use crate::types::{edge::EdgeDB, Address, Edge, Safe, U256};

Expand Down Expand Up @@ -31,6 +31,45 @@ impl DB {

fn compute_edges(&mut self) {
let mut edges = vec![];

// token address -> orga addresses
let mut organization_accepted_tokens: HashMap<Address, HashSet<Address>> = HashMap::new();

// Build a map from token address to orga addresses that accept this token
for (_, safe) in &self.safes {
for (send_to, percentage) in &safe.limit_percentage {
if percentage == &0 {
continue;
}

let receiver_safe = self.safes.get(send_to).unwrap();
if receiver_safe.organization {
//println!("user {} can send {} token to orga {}", user, safe.token_address, send_to);
organization_accepted_tokens.entry(safe.token_address).or_default().insert(*send_to);
}
}
}

// Find all safes that have a non-zero balance of tokens that are accepted by an organization
for (user, safe) in &self.safes {
for (token, balance) in &safe.balances {
if balance == &U256::from(0) {
continue;
}
organization_accepted_tokens.get(token).map(|organizations| {
for organization in organizations {
// Add the balance as capacity from 'user' to 'organization'
edges.push(Edge {
from: *user,
to: *organization,
token: *token,
capacity: *balance,
});
}
});
}
}

for (user, safe) in &self.safes {
// trust connections
for (send_to, percentage) in &safe.limit_percentage {
Expand Down

0 comments on commit 5950ab8

Please sign in to comment.