loom_rpc_handler/handler/
pools.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::dto::pagination::Pagination;
use crate::dto::pool::{MarketStats, Pool, PoolClass, PoolDetailsResponse, PoolProtocol, PoolResponse};
use crate::dto::quote::{Filter, QuoteRequest, QuoteResponse};
use alloy_primitives::Address;
use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::Json;
use eyre::ErrReport;
use loom_evm_utils::error_handler::internal_error;
use loom_rpc_state::AppState;
use loom_types_entities::PoolWrapper;
use revm::primitives::Env;
use revm::DatabaseRef;
use std::str::FromStr;

/// Get latest block
///
/// Get the latest block header
#[utoipa::path(
    get,
    path = "/pools",
    tag = "market",
    tags = [],
    params(
        Pagination, Filter
    ),
    responses(
    (status = 200, description = "All available pools", body = PoolResponse),
    )
)]
pub async fn pools<DB: DatabaseRef + Send + Sync + Clone + 'static>(
    State(app_state): State<AppState<DB>>,
    pagination: Query<Pagination>,
    filter: Query<Filter>,
) -> Result<Json<PoolResponse>, (StatusCode, String)> {
    let pools: Vec<(Address, PoolWrapper)> = app_state
        .bc
        .market()
        .read()
        .await
        .pools()
        .iter()
        .filter(|(_, pool)| match &filter.protocol {
            None => true,
            Some(protocol) => pool.pool.get_protocol() == protocol.into(),
        })
        .skip(pagination.start())
        .take(pagination.limit)
        .map(|(address, pool)| (*address, pool.clone()))
        .collect();

    let mut ret = vec![];
    for (pool_address, pool) in pools {
        ret.push(Pool {
            address: pool_address,
            fee: pool.pool.get_fee(),
            tokens: pool.pool.get_tokens(),
            protocol: PoolProtocol::from(pool.pool.get_protocol()),
            pool_class: PoolClass::from(pool.get_class()),
        });
    }
    let total_pools = app_state
        .bc
        .market()
        .read()
        .await
        .pools()
        .iter()
        .filter(|(_, pool)| match &filter.protocol {
            None => true,
            Some(protocol) => pool.pool.get_protocol() == protocol.into(),
        })
        .count();

    Ok(Json(PoolResponse { pools: ret, total: total_pools }))
}

/// Get pool details
///
/// Get pool details
#[utoipa::path(
    get,
    path = "/pools/{address}",
    tag = "market",
    tags = [],
    params(
        ("address" = String, Path, description = "Address of the pool"),
    ),
    responses(
    (status = 200, description = "Pool detail response", body = PoolDetailsResponse),
    )
)]
pub async fn pool<DB: DatabaseRef + Send + Sync + Clone + 'static>(
    State(app_state): State<AppState<DB>>,
    Path(address): Path<String>,
) -> Result<Json<PoolDetailsResponse>, (StatusCode, String)> {
    let address = Address::from_str(&address).map_err(internal_error)?;

    match app_state.bc.market().read().await.pools().get(&address) {
        None => Err((StatusCode::NOT_FOUND, "Pool not found".to_string())),
        Some(pool) => Ok(Json(PoolDetailsResponse {
            address: pool.get_address(),
            pool_class: PoolClass::from(pool.get_class()),
            protocol: PoolProtocol::from(pool.get_protocol()),
            fee: pool.get_fee(),
            tokens: pool.get_tokens(),
        })),
    }
}

/// Market statistics
///
/// Get the latest market statistics
#[utoipa::path(
    get,
    path = "/stats",
    tag = "market",
    tags = [],
    params(
        Pagination, Filter
    ),
    responses(
        (status = 200, description = "Market stats", body = MarketStats),
    )
)]
pub async fn market_stats<DB: DatabaseRef + Send + Sync + Clone + 'static>(
    State(app_state): State<AppState<DB>>,
) -> Result<Json<MarketStats>, (StatusCode, String)> {
    let total_pools = app_state.bc.market().read().await.pools().len();

    Ok(Json(MarketStats { total_pools }))
}

/// Get a quote
///
/// Get quote for a pair of a pool
#[utoipa::path(
    post,
    path = "/pools/{address}/quote",
    tag = "market",
    tags = [],
    params(
        ("address" = String, Path, description = "Address of the pool"),
    ),
    request_body = QuoteRequest,
    responses(
        (status = 200, description = "Market stats", body = QuoteResponse),
    )
)]
pub async fn pool_quote<DB: DatabaseRef<Error = ErrReport> + Send + Sync + Clone + 'static>(
    State(app_state): State<AppState<DB>>,
    Path(address): Path<String>,
    Json(quote_request): Json<QuoteRequest>,
) -> Result<Json<QuoteResponse>, (StatusCode, String)> {
    let address = Address::from_str(&address).map_err(internal_error)?;
    match app_state.bc.market().read().await.pools().get(&address) {
        None => Err((StatusCode::NOT_FOUND, "Pool not found".to_string())),
        Some(pool) => {
            let evm_env = Env::default();
            let quote_result = pool.pool.calculate_out_amount(
                &app_state.bc.market_state().read().await.state_db,
                evm_env,
                &quote_request.token_address_from,
                &quote_request.token_address_to,
                quote_request.amount_in,
            );
            match quote_result {
                Err(err) => Err((StatusCode::INTERNAL_SERVER_ERROR, err.to_string())),
                Ok((out_amount, gas_used)) => Ok(Json(QuoteResponse { out_amount, gas_used })),
            }
        }
    }
}