forked from Moonsong-Labs/storage-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding payment stream creation event to indexer (Moonsong-Labs#213
) * adding payment stream creation event to indexer * rustfmt * handle payment stream payments to the provider (increase total_amount_paid) * update schema; fix up.sql for payment stream * fix total_amount and total_amount_paid naming incoherency * fix diesel related errors * fix handlers for payment stream * fix clippy * add tick to the payment stream events * add TODO comment * update the typescript files for the PaymentStreamCharged event * pnpm fmt * generate typescript types * fix charged tick and add charged_at_tick value * some fixes * generate type for typescript * fix test * fix test again * fix: 🩹 Make it so `last_chargeable_tick` is always set correctly and not left as 0 when user is without funds * chore: 🏷️ Update `api-augment` * chore: 🏷️ Update `api-augment` * genearte types --------- Co-authored-by: Facundo Farall <[email protected]>
- Loading branch information
1 parent
93e424a
commit a72381d
Showing
18 changed files
with
325 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
client/indexer-db/migrations/2024-10-01-112655_create_paymentstream/down.sql
This file contains 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,2 @@ | ||
-- Drop the paymentstream table | ||
DROP TABLE IF EXISTS paymentstream; |
14 changes: 14 additions & 0 deletions
14
client/indexer-db/migrations/2024-10-01-112655_create_paymentstream/up.sql
This file contains 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 @@ | ||
CREATE TABLE paymentstream ( | ||
id SERIAL PRIMARY KEY, | ||
account VARCHAR NOT NULL, | ||
provider VARCHAR NOT NULL, | ||
total_amount_paid NUMERIC(38, 0) NOT NULL DEFAULT 0, | ||
last_tick_charged BIGINT NOT NULL DEFAULT 0, | ||
charged_at_tick BIGINT NOT NULL DEFAULT 0 | ||
); | ||
|
||
-- Create an index on the account column for faster lookups | ||
CREATE INDEX idx_paymentstream_account ON paymentstream(account); | ||
|
||
-- Create an index on the provider column for faster lookups | ||
CREATE INDEX idx_paymentstream_provider ON paymentstream(provider); |
This file contains 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 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,76 @@ | ||
use bigdecimal::BigDecimal; | ||
use diesel::prelude::*; | ||
use diesel_async::RunQueryDsl; | ||
|
||
use crate::{schema::paymentstream, DbConnection}; | ||
|
||
#[derive(Debug, Queryable, Insertable, Selectable)] | ||
#[diesel(table_name = paymentstream)] | ||
pub struct PaymentStream { | ||
// postgres attributed ID for this payment stream | ||
pub id: i32, | ||
// Account ID of the payer | ||
pub account: String, | ||
// ID of the payee (msp or bsp) | ||
pub provider: String, | ||
// Total amount already paid to this provider from this account for this payment stream | ||
pub total_amount_paid: BigDecimal, | ||
// The last tick for which the payment stream has recorded a payment | ||
pub last_tick_charged: i64, | ||
// The tick at which the payment actually happened | ||
pub charged_at_tick: i64, | ||
} | ||
|
||
impl PaymentStream { | ||
pub async fn create<'a>( | ||
conn: &mut DbConnection<'a>, | ||
account: String, | ||
provider: String, | ||
) -> Result<Self, diesel::result::Error> { | ||
let ps = diesel::insert_into(paymentstream::table) | ||
.values(( | ||
paymentstream::account.eq(account), | ||
paymentstream::provider.eq(provider), | ||
)) | ||
.returning(PaymentStream::as_select()) | ||
.get_result(conn) | ||
.await?; | ||
Ok(ps) | ||
} | ||
|
||
pub async fn get<'a>( | ||
conn: &mut DbConnection<'a>, | ||
account: String, | ||
provider: String, | ||
) -> Result<Self, diesel::result::Error> { | ||
// Looking by a payment stream by provider and the account associated | ||
let ps = paymentstream::table | ||
.filter( | ||
paymentstream::account | ||
.eq(account) | ||
.and(paymentstream::provider.eq(provider)), | ||
) | ||
.first(conn) | ||
.await?; | ||
Ok(ps) | ||
} | ||
|
||
pub async fn update_total_amount<'a>( | ||
conn: &mut DbConnection<'a>, | ||
ps_id: i32, | ||
new_total_amount: BigDecimal, | ||
last_tick_charged: i64, | ||
charged_at_tick: i64, | ||
) -> Result<(), diesel::result::Error> { | ||
diesel::update(paymentstream::table) | ||
.filter(paymentstream::id.eq(ps_id)) | ||
.set(( | ||
paymentstream::total_amount_paid.eq(new_total_amount), | ||
paymentstream::last_tick_charged.eq(last_tick_charged), | ||
paymentstream::charged_at_tick.eq(charged_at_tick), | ||
)) | ||
.execute(conn) | ||
.await?; | ||
Ok(()) | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.