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

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

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