let serde_json do more of the work

This commit is contained in:
Bryan Stitt 2023-06-21 10:18:36 -07:00
parent 5504062f47
commit 06ce95c386
3 changed files with 25 additions and 27 deletions

@ -82,23 +82,21 @@ pub async fn user_deposits_get(
.await?;
// Return the response, all except the user ...
let mut response = HashMap::new();
let receipts = receipts
.into_iter()
.map(|x| {
let mut out = HashMap::new();
out.insert("amount", serde_json::Value::String(x.amount.to_string()));
out.insert("chain_id", serde_json::Value::Number(x.chain_id.into()));
out.insert("tx_hash", serde_json::Value::String(x.tx_hash));
// TODO: log_index
out
json!({
"amount": x.amount,
"chain_id": x.chain_id,
"tx_hash": x.tx_hash,
})
})
.collect::<Vec<_>>();
response.insert(
"user",
json!(format!("{:?}", Address::from_slice(&user.address))),
);
response.insert("deposits", json!(receipts));
let response = json!({
"user": Address::from_slice(&user.address),
"deposits": receipts,
});
Ok(Json(response).into_response())
}

@ -68,17 +68,19 @@ pub async fn get_keys_as_subuser(
// Now return the list
let response_json = json!({
"subuser": format!("{:?}", Address::from_slice(&subuser.address)),
"subuser": Address::from_slice(&subuser.address),
"rpc_keys": rpc_key_entities
.into_iter()
.flat_map(|(rpc_key, rpc_owner)| {
match rpc_owner {
Some(inner_rpc_owner) => {
let mut tmp = HashMap::new();
tmp.insert("rpc-key", serde_json::Value::String(Ulid::from(rpc_key.secret_key).to_string()));
tmp.insert("rpc-owner", serde_json::Value::String(format!("{:?}", Address::from_slice(&inner_rpc_owner.address))));
tmp.insert("role", serde_json::Value::String(format!("{:?}", secondary_user_entities.get(&rpc_key.id).unwrap().role))); // .to_string() returns ugly "'...'"
Some(tmp)
let x = json!({
"rpc-key": Ulid::from(rpc_key.secret_key),
"rpc-owner": Address::from_slice(&inner_rpc_owner.address),
// TODO: prettier serialize for role
"role": format!("{:?}", secondary_user_entities.get(&rpc_key.id).unwrap().role),
});
Some(x)
},
None => {
// error!("Found RPC secret key with no user!".to_owned());
@ -148,16 +150,16 @@ pub async fn get_subusers(
// Now return the list
let response_json = json!({
"caller": format!("{:?}", Address::from_slice(&user.address)),
"caller": Address::from_slice(&user.address),
"rpc_key": rpc_key,
"subusers": subusers
.into_iter()
.map(|subuser| {
let mut tmp = HashMap::new();
// .encode_hex()
tmp.insert("address", serde_json::Value::String(format!("{:?}", Address::from_slice(&subuser.address))));
tmp.insert("role", serde_json::Value::String(format!("{:?}", secondary_user_entities.get(&subuser.id).unwrap().role)));
json!(tmp)
json!({
"address": Address::from_slice(&subuser.address),
// TODO: prettier serialize for role
"role": format!("{:?}", secondary_user_entities.get(&subuser.id).unwrap().role),
})
})
.collect::<Vec::<_>>(),
});

@ -36,9 +36,7 @@ impl JsonRpcId {
pub fn to_raw_value(self) -> Box<RawValue> {
// TODO: is this a good way to do this? we should probably use references
match self {
Self::None => {
to_raw_value(&json!(None::<Option<()>>)).expect("null id should always work")
}
Self::None => Default::default(),
Self::Number(x) => {
serde_json::from_value(json!(x)).expect("number id should always work")
}