-
Notifications
You must be signed in to change notification settings - Fork 8
Add vaults protocol-fee endpoint #166
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
Open
zahorodnyi
wants to merge
2
commits into
dev
Choose a base branch
from
feat/protocol-fee-vaults-api
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
.sqlx/query-1eb14658b5b541fa5926f8bf7fc7843f8ca196c0ddd214a5dbba1bfb69f4c1ea.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| use sqlx::PgPool; | ||
|
|
||
| use crate::api::utils::{format_hex, format_offer_id, format_satoshis}; | ||
|
|
||
| use super::dto::{ProtocolFeeVaultDto, ProtocolFeeVaultsResponse}; | ||
|
|
||
| struct HarvestableProtocolFeeVaultRow { | ||
| offer_id: i64, | ||
| txid: Vec<u8>, | ||
| vout: i32, | ||
| amount: i64, | ||
| created_at_height: i64, | ||
| updated_at_height: i64, | ||
| borrower_nft_asset_id: Vec<u8>, | ||
| protocol_fee_keeper_asset_id: Vec<u8>, | ||
| } | ||
|
|
||
| impl From<HarvestableProtocolFeeVaultRow> for ProtocolFeeVaultDto { | ||
| fn from(row: HarvestableProtocolFeeVaultRow) -> Self { | ||
| Self { | ||
| offer_id: format_offer_id(row.offer_id), | ||
| txid: format_hex(row.txid), | ||
| vout: row.vout as u32, | ||
| amount: format_satoshis(row.amount), | ||
| borrower_nft_asset: format_hex(row.borrower_nft_asset_id), | ||
| protocol_fee_keeper_asset: format_hex(row.protocol_fee_keeper_asset_id), | ||
| created_at_height: row.created_at_height as u64, | ||
| updated_at_height: row.updated_at_height as u64, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[tracing::instrument(name = "Fetching unspent protocol-fee vaults", skip(db))] | ||
| pub async fn fetch_unspent_protocol_fee_vaults( | ||
| db: &PgPool, | ||
| principal_asset_id: Vec<u8>, | ||
| ) -> Result<ProtocolFeeVaultsResponse, sqlx::Error> { | ||
| let rows = sqlx::query_as!( | ||
| HarvestableProtocolFeeVaultRow, | ||
| r#" | ||
| SELECT | ||
| v.offer_id, | ||
| v.txid, | ||
| v.vout, | ||
| v.amount, | ||
| v.created_at_height, | ||
| v.updated_at_height, | ||
| o.borrower_nft_asset_id, | ||
| o.protocol_fee_keeper_asset_id | ||
| FROM offer_vaults v | ||
| JOIN offers o ON o.id = v.offer_id | ||
| WHERE v.vault_type = 'protocol_fee' | ||
| AND v.is_finalized = true | ||
| AND v.spent_txid IS NULL | ||
| AND o.principal_asset_id = $1 | ||
| ORDER BY v.amount DESC, v.id DESC | ||
| "#, | ||
| principal_asset_id, | ||
| ) | ||
| .fetch_all(db) | ||
| .await?; | ||
|
|
||
| let count = rows.len() as u64; | ||
| let total_amount: i64 = rows.iter().map(|row| row.amount).sum(); | ||
| let items = rows.into_iter().map(ProtocolFeeVaultDto::from).collect(); | ||
|
|
||
| Ok(ProtocolFeeVaultsResponse { | ||
| items, | ||
| count, | ||
| total_amount: format_satoshis(total_amount), | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| use serde::Deserialize; | ||
| use utoipa::{IntoParams, ToSchema}; | ||
|
|
||
| #[derive(Deserialize, IntoParams, ToSchema)] | ||
| #[into_params(parameter_in = Query)] | ||
| pub struct ProtocolFeeVaultsQuery { | ||
| #[param(example = "020202…")] | ||
| pub principal_asset: String, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, ToSchema)] | ||
| pub struct ProtocolFeeVaultDto { | ||
| #[schema(example = "1")] | ||
| pub offer_id: String, | ||
| pub txid: String, | ||
| pub vout: u32, | ||
| #[schema(example = "1000")] | ||
| pub amount: String, | ||
| pub borrower_nft_asset: String, | ||
| pub protocol_fee_keeper_asset: String, | ||
| pub created_at_height: u64, | ||
| pub updated_at_height: u64, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, ToSchema)] | ||
| pub struct ProtocolFeeVaultsResponse { | ||
| pub items: Vec<ProtocolFeeVaultDto>, | ||
| pub count: u64, | ||
| #[schema(example = "1500")] | ||
| pub total_amount: String, | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{ProtocolFeeVaultDto, ProtocolFeeVaultsResponse}; | ||
|
|
||
| #[test] | ||
| fn protocol_fee_vaults_response_serializes_expected_shape() { | ||
| let response = ProtocolFeeVaultsResponse { | ||
| items: vec![ProtocolFeeVaultDto { | ||
| offer_id: "1".to_string(), | ||
| txid: "aabb".to_string(), | ||
| vout: 0, | ||
| amount: "1000".to_string(), | ||
| borrower_nft_asset: "0102".to_string(), | ||
| protocol_fee_keeper_asset: "0304".to_string(), | ||
| created_at_height: 10, | ||
| updated_at_height: 10, | ||
| }], | ||
| count: 1, | ||
| total_amount: "1000".to_string(), | ||
| }; | ||
|
|
||
| let json = serde_json::to_value(&response).expect("serialize"); | ||
| assert_eq!(json["count"], 1); | ||
| assert_eq!(json["total_amount"], "1000"); | ||
| assert_eq!(json["items"][0]["borrower_nft_asset"], "0102"); | ||
| assert_eq!(json["items"][0]["protocol_fee_keeper_asset"], "0304"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use axum::{ | ||
| Json, | ||
| extract::{Query, State}, | ||
| }; | ||
|
|
||
| use crate::api::openapi::ErrorResponse; | ||
| use crate::api::utils::parse_filter_hex; | ||
| use crate::api::{ApiError, AppState}; | ||
|
|
||
| use super::dto::{ProtocolFeeVaultsQuery, ProtocolFeeVaultsResponse}; | ||
|
|
||
| #[utoipa::path( | ||
| get, | ||
| path = "/vaults/protocol-fee", | ||
| tag = "vaults", | ||
| params(ProtocolFeeVaultsQuery), | ||
| responses( | ||
| (status = 200, description = "Unspent, finalized protocol-fee vaults for the asset, sorted by amount desc", body = ProtocolFeeVaultsResponse), | ||
| (status = 400, description = "Invalid principal_asset hex", body = ErrorResponse), | ||
| (status = 500, description = "Internal server error", body = ErrorResponse), | ||
| ) | ||
| )] | ||
| #[tracing::instrument(name = "Getting unspent protocol-fee vaults", skip(state, query))] | ||
| pub async fn list_protocol_fee_vaults( | ||
| State(state): State<Arc<AppState>>, | ||
| Query(query): Query<ProtocolFeeVaultsQuery>, | ||
| ) -> Result<Json<ProtocolFeeVaultsResponse>, ApiError> { | ||
| let asset_bytes = parse_filter_hex(&query.principal_asset) | ||
| .ok_or_else(|| ApiError::BadRequest("Invalid principal_asset hex".to_string()))?; | ||
|
|
||
| let response = super::db::fetch_unspent_protocol_fee_vaults(&state.db, asset_bytes).await?; | ||
|
|
||
| Ok(Json(response)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| pub(crate) mod db; | ||
| pub(crate) mod dto; | ||
| pub(crate) mod handlers; | ||
| mod routes; | ||
|
|
||
| pub use routes::routes; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use axum::{Router, routing::get}; | ||
|
|
||
| use crate::api::AppState; | ||
|
|
||
| use super::handlers; | ||
|
|
||
| pub fn routes() -> Router<Arc<AppState>> { | ||
| Router::new().nest( | ||
| "/vaults", | ||
| Router::new().route("/protocol-fee", get(handlers::list_protocol_fee_vaults)), | ||
| ) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.