Skip to content

Commit

Permalink
Store net balances in tables and use them in the API (helius-labs#91)
Browse files Browse the repository at this point in the history
* Add balance tables v2

* Intermediate commit
  • Loading branch information
pmantica11 authored Apr 26, 2024
1 parent 98b1dfb commit 6456251
Show file tree
Hide file tree
Showing 17 changed files with 468 additions and 90 deletions.
6 changes: 3 additions & 3 deletions src/api/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::method::get_compressed_balance_by_owner::{
get_compressed_balance_by_owner, GetCompressedBalanceByOwnerRequest,
};
use super::method::get_compressed_token_balances_by_owner::{
get_compressed_token_balances_by_owner, GetCompressedTokenBalancesByOwner,
get_compressed_token_balances_by_owner, GetCompressedTokenBalancesByOwnerRequest,
TokenBalancesResponse,
};
use super::method::get_compression_signatures_for_account::{
Expand Down Expand Up @@ -168,7 +168,7 @@ impl PhotonApi {

pub async fn get_compressed_token_balances_by_owner(
&self,
request: GetCompressedTokenBalancesByOwner,
request: GetCompressedTokenBalancesByOwnerRequest,
) -> Result<TokenBalancesResponse, PhotonApiError> {
get_compressed_token_balances_by_owner(&self.db_conn, request).await
}
Expand Down Expand Up @@ -298,7 +298,7 @@ impl PhotonApi {
},
OpenApiSpec {
name: "getCompressedTokenBalancesByOwner".to_string(),
request: Some(GetCompressedTokenBalancesByOwner::schema().1),
request: Some(GetCompressedTokenBalancesByOwnerRequest::schema().1),
response: TokenBalancesResponse::schema().1,
},
OpenApiSpec {
Expand Down
18 changes: 10 additions & 8 deletions src/api/method/get_compressed_balance_by_owner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::typedefs::serializable_pubkey::SerializablePubkey;
use crate::common::typedefs::unsigned_integer::UnsignedInteger;
use crate::dao::generated::accounts;
use crate::dao::generated::owner_balances;
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, QuerySelect};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
Expand All @@ -22,14 +22,10 @@ pub async fn get_compressed_balance_by_owner(
let context = Context::extract(conn).await?;
let owner = request.owner;

let balances = accounts::Entity::find()
let balances = owner_balances::Entity::find()
.select_only()
.column(accounts::Column::Lamports)
.filter(
accounts::Column::Owner
.eq::<Vec<u8>>(owner.into())
.and(accounts::Column::Spent.eq(false)),
)
.column(owner_balances::Column::Lamports)
.filter(owner_balances::Column::Owner.eq::<Vec<u8>>(owner.into()))
.into_model::<LamportModel>()
.all(conn)
.await?
Expand All @@ -38,6 +34,12 @@ pub async fn get_compressed_balance_by_owner(
.collect::<Result<Vec<u64>, PhotonApiError>>()?;

let total_balance = balances.iter().sum::<u64>();
if total_balance == 0 {
return Err(PhotonApiError::RecordNotFound(format!(
"No balance found for owner: {}",
owner
)));
}

Ok(AccountBalanceResponse {
value: UnsignedInteger(total_balance),
Expand Down
6 changes: 3 additions & 3 deletions src/api/method/get_compressed_token_balances_by_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct TokenBalancesResponse {
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema, Default)]
pub struct GetCompressedTokenBalancesByOwner {
pub struct GetCompressedTokenBalancesByOwnerRequest {
pub owner: SerializablePubkey,
pub mint: Option<SerializablePubkey>,
pub cursor: Option<Base58String>,
Expand All @@ -40,9 +40,9 @@ pub struct GetCompressedTokenBalancesByOwner {

pub async fn get_compressed_token_balances_by_owner(
conn: &DatabaseConnection,
request: GetCompressedTokenBalancesByOwner,
request: GetCompressedTokenBalancesByOwnerRequest,
) -> Result<TokenBalancesResponse, PhotonApiError> {
let GetCompressedTokenBalancesByOwner {
let GetCompressedTokenBalancesByOwnerRequest {
owner,
mint,
cursor,
Expand Down
1 change: 1 addition & 0 deletions src/dao/generated/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Model {
pub seq: Option<i64>,
pub slot_updated: i64,
pub spent: bool,
pub prev_spent: Option<bool>,
#[sea_orm(column_type = "Decimal(Some((20, 0)))")]
pub lamports: Decimal,
#[sea_orm(column_type = "Decimal(Some((20, 0)))", nullable)]
Expand Down
2 changes: 2 additions & 0 deletions src/dao/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub mod prelude;
pub mod account_transactions;
pub mod accounts;
pub mod blocks;
pub mod owner_balances;
pub mod state_trees;
pub mod token_accounts;
pub mod token_owner_balances;
pub mod transactions;
17 changes: 17 additions & 0 deletions src/dao/generated/owner_balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "owner_balances")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub owner: Vec<u8>,
#[sea_orm(column_type = "Decimal(Some((20, 0)))")]
pub lamports: Decimal,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
2 changes: 2 additions & 0 deletions src/dao/generated/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
pub use super::account_transactions::Entity as AccountTransactions;
pub use super::accounts::Entity as Accounts;
pub use super::blocks::Entity as Blocks;
pub use super::owner_balances::Entity as OwnerBalances;
pub use super::state_trees::Entity as StateTrees;
pub use super::token_accounts::Entity as TokenAccounts;
pub use super::token_owner_balances::Entity as TokenOwnerBalances;
pub use super::transactions::Entity as Transactions;
1 change: 1 addition & 0 deletions src/dao/generated/token_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Model {
pub delegate: Option<Vec<u8>>,
pub state: i32,
pub spent: bool,
pub prev_spent: Option<bool>,
#[sea_orm(column_type = "Decimal(Some((20, 0)))")]
pub amount: Decimal,
#[sea_orm(column_type = "Decimal(Some((20, 0)))", nullable)]
Expand Down
19 changes: 19 additions & 0 deletions src/dao/generated/token_owner_balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "token_owner_balances")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub owner: Vec<u8>,
#[sea_orm(primary_key, auto_increment = false)]
pub mint: Vec<u8>,
#[sea_orm(column_type = "Decimal(Some((20, 0)))")]
pub amount: Decimal,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
Loading

0 comments on commit 6456251

Please sign in to comment.