Import from json.
This commit is contained in:
parent
f37b35d6ec
commit
a7e9aefd40
8 changed files with 159 additions and 1 deletions
|
@ -8,4 +8,6 @@ default-run = "server"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
json = "^0.12.4"
|
json = "^0.12.4"
|
||||||
num-bigint = "^0.4.3"
|
num-bigint = "^0.4.3"
|
||||||
|
serde = { version = "1.0.149", features = ["serde_derive"] }
|
||||||
|
serde_json = "1.0.89"
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod graph;
|
pub mod graph;
|
||||||
pub mod io;
|
pub mod io;
|
||||||
|
pub mod safe_db;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
16
src/safe_db/db.rs
Normal file
16
src/safe_db/db.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use crate::types::{Address, Safe};
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct DB {
|
||||||
|
safes: BTreeMap<Address, Safe>,
|
||||||
|
token_owner: BTreeMap<Address, Address>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DB {
|
||||||
|
pub fn new(safes: BTreeMap<Address, Safe>, token_owner: BTreeMap<Address, Address>) -> DB {
|
||||||
|
println!("{} safes, {} tokens", safes.len(), token_owner.len());
|
||||||
|
DB { safes, token_owner }
|
||||||
|
}
|
||||||
|
}
|
2
src/safe_db/mod.rs
Normal file
2
src/safe_db/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
mod db;
|
||||||
|
mod safes_json;
|
115
src/safe_db/safes_json.rs
Normal file
115
src/safe_db/safes_json.rs
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
use crate::types::{Address, Safe};
|
||||||
|
|
||||||
|
use super::db::DB;
|
||||||
|
|
||||||
|
pub fn import_from_safes_json(file: &str) -> DB {
|
||||||
|
let contents = read_to_string(file).unwrap();
|
||||||
|
let db: Safes = serde_json::from_str(&contents).unwrap();
|
||||||
|
|
||||||
|
let mut safes: BTreeMap<Address, Safe> = Default::default();
|
||||||
|
let mut token_owner: BTreeMap<Address, Address> = Default::default();
|
||||||
|
|
||||||
|
for json_safe in &db.safes {
|
||||||
|
let address: Address = json_safe.id.into();
|
||||||
|
let mut s = Safe {
|
||||||
|
organization: json_safe.organization,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
for balance in &json_safe.balances {
|
||||||
|
let token_address: Address = balance.token.id.into();
|
||||||
|
let owner: Address = balance.token.owner.id.into();
|
||||||
|
s.balances.insert(token_address, balance.amount.into());
|
||||||
|
if owner == address {
|
||||||
|
s.token_address = token_address;
|
||||||
|
}
|
||||||
|
token_owner.insert(token_address, owner);
|
||||||
|
}
|
||||||
|
safes.insert(address, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
for json_safe in db.safes {
|
||||||
|
for connection in json_safe.outgoing.iter().chain(json_safe.incoming.iter()) {
|
||||||
|
let send_to: Address = connection.can_send_to_address.into();
|
||||||
|
let user: Address = connection.user_address.into();
|
||||||
|
let limit_percentage: u8 = connection.limit_percentage.parse().unwrap();
|
||||||
|
assert!(limit_percentage <= 100);
|
||||||
|
if send_to != Address::default()
|
||||||
|
&& user != Address::default()
|
||||||
|
&& send_to != user
|
||||||
|
&& limit_percentage > 0
|
||||||
|
{
|
||||||
|
safes
|
||||||
|
.get_mut(&user)
|
||||||
|
.unwrap()
|
||||||
|
.limit_percentage
|
||||||
|
.insert(send_to, limit_percentage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DB::new(safes, token_owner)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct Safes<'a> {
|
||||||
|
block_number: &'a str,
|
||||||
|
safes: Vec<JsonSafe<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct JsonSafe<'a> {
|
||||||
|
id: &'a str,
|
||||||
|
organization: bool,
|
||||||
|
outgoing: Vec<Edge<'a>>,
|
||||||
|
incoming: Vec<Edge<'a>>,
|
||||||
|
balances: Vec<Balance<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct Edge<'a> {
|
||||||
|
limit: Option<&'a str>,
|
||||||
|
limit_percentage: &'a str,
|
||||||
|
can_send_to_address: &'a str,
|
||||||
|
user_address: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct Balance<'a> {
|
||||||
|
amount: &'a str,
|
||||||
|
token: Token<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct Token<'a> {
|
||||||
|
id: &'a str,
|
||||||
|
owner: Owner<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct Owner<'a> {
|
||||||
|
id: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn t1() {
|
||||||
|
import_from_safes_json("/tmp/safes.json");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,11 @@
|
||||||
pub mod address;
|
pub mod address;
|
||||||
pub mod edge;
|
pub mod edge;
|
||||||
|
pub mod safe;
|
||||||
|
pub mod token;
|
||||||
pub mod u256;
|
pub mod u256;
|
||||||
|
|
||||||
pub use address::Address;
|
pub use address::Address;
|
||||||
pub use edge::Edge;
|
pub use edge::Edge;
|
||||||
|
pub use safe::Safe;
|
||||||
|
pub use token::Token;
|
||||||
pub use u256::U256;
|
pub use u256::U256;
|
||||||
|
|
12
src/types/safe.rs
Normal file
12
src/types/safe.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use super::{Address, U256};
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Safe {
|
||||||
|
pub token_address: Address,
|
||||||
|
pub balances: BTreeMap<Address, U256>,
|
||||||
|
/// Limit percentage in "send to" direction
|
||||||
|
pub limit_percentage: BTreeMap<Address, u8>,
|
||||||
|
pub organization: bool,
|
||||||
|
}
|
6
src/types/token.rs
Normal file
6
src/types/token.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
use super::Address;
|
||||||
|
|
||||||
|
pub struct Token {
|
||||||
|
address: Address,
|
||||||
|
owner: Address,
|
||||||
|
}
|
Loading…
Reference in a new issue