Skip to content

Commit

Permalink
Merge pull request #41 from oreoslabs/add-decimal
Browse files Browse the repository at this point in the history
Add decimals to assetInfo
  • Loading branch information
whohideonbug authored Jan 9, 2025
2 parents 42f5d09 + 29282bf commit 6309c66
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
18 changes: 18 additions & 0 deletions crates/networking/src/rpc_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub struct AssetBalance {
pub available: String,
pub sequence: Option<u64>,
pub asset_verification: AssetStatus,
pub decimals: Option<u8>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -399,3 +400,20 @@ pub struct SendTransactionResponse {
pub account: String,
pub hash: String,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RpcAssetVerification {
pub status: String,
pub symbol: Option<String>,
pub decimals: Option<u8>,
pub logo_uri: Option<String>,
pub website: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RpcAsset {
pub id: String,
pub verification: RpcAssetVerification,
}
20 changes: 19 additions & 1 deletion crates/networking/src/rpc_handler/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ureq::{Agent, AgentBuilder, Error, Response};

use crate::{
rpc_abi::{
RpcAddTxRequest, RpcAddTxResponse, RpcCreateTxRequest, RpcCreateTxResponse,
RpcAddTxRequest, RpcAddTxResponse, RpcAsset, RpcCreateTxRequest, RpcCreateTxResponse,
RpcExportAccountResponse, RpcGetAccountStatusRequest, RpcGetAccountStatusResponse,
RpcGetAccountTransactionRequest, RpcGetAccountTransactionResponse, RpcGetBalancesRequest,
RpcGetBalancesResponse, RpcGetBlockRequest, RpcGetBlockResponse, RpcGetBlocksRequest,
Expand Down Expand Up @@ -239,6 +239,12 @@ impl RpcHandler {
let resp = self.agent.clone().post(&path).send_json(request);
handle_response(resp)
}

pub fn get_asset(&self, id: String) -> Result<RpcResponse<RpcAsset>, OreoError> {
let path = format!("http://{}/chain/getAsset", self.endpoint);
let resp = self.agent.clone().post(&path).send_json(json!({"id": id}));
handle_response(resp)
}
}

pub fn handle_response<S: Debug + for<'a> Deserialize<'a>>(
Expand Down Expand Up @@ -314,4 +320,16 @@ mod tests {
});
assert!(result.is_ok());
}

#[test]
pub fn get_asset_should_work() {
let rpc_handler = RpcHandler::new("127.0.0.1:8021".into());
let header = rpc_handler.get_latest_block();
println!("chain header {:?}", header);
let result = rpc_handler.get_asset(
"8e36e31d677a47cbd883843a345654c814b1e9ec1e0125bac9031f052ced9174".to_string(),
);
println!("asset info: {:?}", result);
assert!(result.is_ok());
}
}
7 changes: 6 additions & 1 deletion crates/server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ pub async fn get_balances_handler(
confirmations: Some(get_balance.confirmations.unwrap_or(10)),
});
match resp {
Ok(res) => {
Ok(mut res) => {
for item in res.data.balances.iter_mut() {
if let Ok(asset) = shared.rpc_handler.get_asset(item.asset_id.clone()) {
item.decimals = asset.data.verification.decimals;
}
}
let data = match shared.network() {
Testnet::ID => RpcGetBalancesResponse::verified_asset::<Testnet>(res.data),
_ => RpcGetBalancesResponse::verified_asset::<Mainnet>(res.data),
Expand Down

0 comments on commit 6309c66

Please sign in to comment.