Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: price. #268

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 130 additions & 113 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

## 其他
* [5.1 获取FRA市场信息](#5.1)
* [5.2 获取FRA单价](#5.2)

<h3 id="1.1">1.1 根据交易哈希获取交易</h3>

Expand Down Expand Up @@ -532,122 +533,138 @@
* `GET /api/coins/:id/market_chart`


| 参数 | 类型 | 必传 | 说明 |
|-------------|--------|----|--------------------|
| id | string | Y | 币种 |
| vs_currency | string | N | 计价货币,缺省值为"usd" |
| interval | string | N | 时间间隔,缺省为"daily" |
| days | number | N | 天数,缺省值为7,即列表中有7条数据 |
| 参数 | 类型 | 必传 | 说明 |
|-------------|--------|----|-----------------|
| id | string | Y | 币种 |
| vs_currency | string | N | 计价货币,缺省值为"usd" |
| interval | string | N | 时间间隔,缺省为"daily" |
| days | number | N | 天数,缺省值为7 |

* Request: `/api/coins/findora/market_chart?vs_currency=usd&days=7&interval=daily`
* Response:
```json
{
"code":200,
"message":"",
"data":{
"market_caps":[
[
1718841600000,
12076960.654386723
],
[
1718928000000,
12502620.19204148
],
[
1719014400000,
12518619.67665847
],
[
1719100800000,
12756431.19328877
],
[
1719187200000,
12478551.446567249
],
[
1719273600000,
10902644.116966683
],
[
1719360000000,
10841063.701286633
],
[
1719415382000,
10689314.764516797
]
],
"prices":[
[
1718841600000,
0.0010421953662283902
],
[
1718928000000,
0.0010786349850002864
],
[
1719014400000,
0.001075916897093825
],
[
1719100800000,
0.0011022425089767258
],
[
1719187200000,
0.0010769704462329777
],
[
1719273600000,
0.0009381464216474152
],
[
1719360000000,
0.0009336248576932378
],
[
1719415382000,
0.0009218445536291912
]
],
"total_volumes":[
[
1718841600000,
255031.1386785886
],
[
1718928000000,
77237.26698759367
],
[
1719014400000,
353159.57762952574
],
[
1719100800000,
439593.60549064353
],
[
1719187200000,
430793.7394006403
],
[
1719273600000,
357770.9091104639
],
[
1719360000000,
466249.0112276624
],
[
1719415382000,
480190.8136610133
]
]
}
"market_caps":[
[
1718841600000,
12076960.654386723
],
[
1718928000000,
12502620.19204148
],
[
1719014400000,
12518619.67665847
],
[
1719100800000,
12756431.19328877
],
[
1719187200000,
12478551.446567249
],
[
1719273600000,
10902644.116966683
],
[
1719360000000,
10841063.701286633
],
[
1719415382000,
10689314.764516797
]
],
"prices":[
[
1718841600000,
0.0010421953662283902
],
[
1718928000000,
0.0010786349850002864
],
[
1719014400000,
0.001075916897093825
],
[
1719100800000,
0.0011022425089767258
],
[
1719187200000,
0.0010769704462329777
],
[
1719273600000,
0.0009381464216474152
],
[
1719360000000,
0.0009336248576932378
],
[
1719415382000,
0.0009218445536291912
]
],
"total_volumes":[
[
1718841600000,
255031.1386785886
],
[
1718928000000,
77237.26698759367
],
[
1719014400000,
353159.57762952574
],
[
1719100800000,
439593.60549064353
],
[
1719187200000,
430793.7394006403
],
[
1719273600000,
357770.9091104639
],
[
1719360000000,
466249.0112276624
],
[
1719415382000,
480190.8136610133
]
]
}
```
```

<h3 id="5.2">5.2 获取FRA单价</h3>

* `GET /api/simple/price`


| 参数 | 类型 | 必传 | 说明 |
|---------------|--------|----|------------------|
| ids | string | N | 币种,缺省值为"findora" |
| vs_currencies | string | N | 计价货币,缺省值为"usd" |

* Request: `/api/simple/price?ids=findora&vs_currencies=usd`
* Response:
```json
{
"findora": {
"usd": 0.00092216
}
}
```
5 changes: 4 additions & 1 deletion explorer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::service::v2::block::{
};
use crate::service::v2::claim::{get_claim_by_tx_hash, get_claims};
use crate::service::v2::delegation::{get_delegation_by_tx_hash, get_delegations};
use crate::service::v2::other::{get_address_count, get_market, get_statistics, get_tx_distribute};
use crate::service::v2::other::{
get_address_count, get_market, get_price, get_statistics, get_tx_distribute,
};
use crate::service::v2::prism_evm_to_native::{get_e2n_by_tx_hash, get_e2n_txs};
use crate::service::v2::prism_native_to_evm::{get_n2e_by_tx_hash, get_n2e_txs};
use crate::service::v2::transaction::{get_tx_by_hash, get_txs};
Expand Down Expand Up @@ -84,6 +86,7 @@ async fn main() -> Result<()> {
// asset
.route("/api/assets", get(get_assets))
.route("/api/coins/:id/market_chart", get(get_market))
.route("/api/simple/price", get(get_price))
.layer(cors)
.with_state(app_state);
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
Expand Down
51 changes: 29 additions & 22 deletions explorer/src/service/error.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use std::num::ParseFloatError;

#[derive(Debug)]
pub enum ExplorerError {
Custom(String),
DBError(sqlx::Error),
IOError(std::io::Error),
TomlDeError(toml::de::Error),
HexError(rustc_hex::FromHexError),
ParseUrlError(url::ParseError),
SerdeJsonError(serde_json::Error),
ReqwestError(reqwest::Error),
DBErr(sqlx::Error),
IOErr(std::io::Error),
TomlDeErr(toml::de::Error),
HexErr(rustc_hex::FromHexError),
ParseUrlErr(url::ParseError),
SerdeJsonErr(serde_json::Error),
ReqwestErr(reqwest::Error),
ParseFloatErr(ParseFloatError),
}
impl From<ParseFloatError> for ExplorerError {
fn from(e: ParseFloatError) -> Self {
ExplorerError::ParseFloatErr(e)
}
}

impl From<reqwest::Error> for ExplorerError {
fn from(e: reqwest::Error) -> Self {
ExplorerError::ReqwestError(e)
ExplorerError::ReqwestErr(e)
}
}

impl From<serde_json::Error> for ExplorerError {
fn from(e: serde_json::Error) -> Self {
ExplorerError::SerdeJsonError(e)
ExplorerError::SerdeJsonErr(e)
}
}

Expand All @@ -33,31 +39,31 @@ impl From<String> for ExplorerError {

impl From<url::ParseError> for ExplorerError {
fn from(e: url::ParseError) -> Self {
ExplorerError::ParseUrlError(e)
ExplorerError::ParseUrlErr(e)
}
}

impl From<rustc_hex::FromHexError> for ExplorerError {
fn from(e: rustc_hex::FromHexError) -> Self {
ExplorerError::HexError(e)
ExplorerError::HexErr(e)
}
}

impl From<std::io::Error> for ExplorerError {
fn from(e: std::io::Error) -> Self {
ExplorerError::IOError(e)
ExplorerError::IOErr(e)
}
}

impl From<toml::de::Error> for ExplorerError {
fn from(e: toml::de::Error) -> Self {
ExplorerError::TomlDeError(e)
ExplorerError::TomlDeErr(e)
}
}

impl From<sqlx::Error> for ExplorerError {
fn from(e: sqlx::Error) -> Self {
ExplorerError::DBError(e)
ExplorerError::DBErr(e)
}
}

Expand All @@ -67,13 +73,14 @@ impl IntoResponse for ExplorerError {
fn into_response(self) -> Response {
let err_msg = match self {
ExplorerError::Custom(e) => e,
ExplorerError::DBError(e) => e.to_string(),
ExplorerError::IOError(e) => e.to_string(),
ExplorerError::TomlDeError(e) => e.to_string(),
ExplorerError::HexError(e) => e.to_string(),
ExplorerError::ParseUrlError(e) => e.to_string(),
ExplorerError::SerdeJsonError(e) => e.to_string(),
ExplorerError::ReqwestError(e) => e.to_string(),
ExplorerError::DBErr(e) => e.to_string(),
ExplorerError::IOErr(e) => e.to_string(),
ExplorerError::TomlDeErr(e) => e.to_string(),
ExplorerError::HexErr(e) => e.to_string(),
ExplorerError::ParseUrlErr(e) => e.to_string(),
ExplorerError::SerdeJsonErr(e) => e.to_string(),
ExplorerError::ReqwestErr(e) => e.to_string(),
ExplorerError::ParseFloatErr(e) => e.to_string(),
};

(StatusCode::INTERNAL_SERVER_ERROR, err_msg).into_response()
Expand Down
Loading
Loading