Skip to content

Commit

Permalink
History endpoint: return the tx history for the authenticated address (
Browse files Browse the repository at this point in the history
  • Loading branch information
naps62 authored Sep 2, 2024
1 parent 73c99c2 commit 99f428f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/api/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn app(jwt_secret: String, state: AppState) -> Router {

let protected_routes = Router::new()
.route("/test", post(test))
.route("/history", post(history))
.route_layer(from_extractor::<Claims>());

let public_routes = Router::new()
Expand Down Expand Up @@ -69,6 +70,17 @@ pub async fn test(State(_state): State<AppState>) -> impl IntoResponse {
Json(json!({"foo": "bar"}))
}

pub async fn history(
State(state): State<AppState>,
Claims { sub: address, .. }: Claims,
) -> ApiResult<impl IntoResponse> {
let addr = alloy_primitives::Address::from_str(&format!("0x{:x}", address)).unwrap();

let history = state.db.history(&addr.into()).await?;

Ok(Json(json!(history)))
}

#[derive(Debug, Serialize, Deserialize)]
pub struct IsWhitelistedResponse {
address: Address,
Expand Down
15 changes: 15 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use diesel_async::{
AsyncConnection, AsyncPgConnection, RunQueryDsl,
};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use models::Txs;
use tokio::sync::mpsc::UnboundedSender;
use tracing::instrument;

Expand Down Expand Up @@ -197,6 +198,20 @@ impl Db {
Ok(res > 0)
}

pub async fn history(&self, address: &Address) -> Result<Vec<Txs>> {
use schema::txs::{self, dsl};
let mut conn = self.pool.get().await?;

Ok(schema::txs::table
.filter(dsl::chain_id.eq(self.chain_id))
.filter(dsl::address.eq(address))
.select((txs::address, txs::hash))
.select(Txs::as_select())
.order(dsl::block_number.asc())
.load(&mut conn)
.await?)
}

pub async fn get_addresses(&self) -> Result<Vec<Address>> {
use schema::accounts::dsl;
let mut conn = self.pool.get().await?;
Expand Down

0 comments on commit 99f428f

Please sign in to comment.