Skip to content
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

Index scheduled transfer events. #324

Merged
merged 8 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions backend-rust/migrations/0003_release_schedule.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Down migration script for removing the scheduled release table.

-- Drop the scheduled_releases table.
DROP TABLE IF EXISTS scheduled_releases;
31 changes: 31 additions & 0 deletions backend-rust/migrations/0003_release_schedule.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Migration script for adding the scheduled release table.
limemloh marked this conversation as resolved.
Show resolved Hide resolved

-- Every scheduled release on chain.
CREATE TABLE scheduled_releases (
-- An index/id for this scheduled release (row number).
index
BIGINT GENERATED ALWAYS AS IDENTITY
PRIMARY KEY,
-- The index of the transaction creating the scheduled transfer.
transaction_index
BIGINT
NOT NULL
REFERENCES transactions,
-- The account receiving the scheduled transfer.
account_index
BIGINT
NOT NULL
REFERENCES accounts,
-- The scheduled release time.
release_time
TIMESTAMPTZ
NOT NULL,
-- The amount locked in the scheduled release.
amount
BIGINT
NOT NULL
);

-- We typically want to find all scheduled releases for a specific account after a certain time.
-- This index is useful for that.
CREATE INDEX scheduled_releases_idx ON scheduled_releases (account_index, release_time);
limemloh marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions backend-rust/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3873,8 +3873,8 @@ fn decode_value_with_schema(
schema_name: SmartContractSchemaNames,
) -> String {
let Some(schema) = opt_schema else {
// Note: There could be something better displayed than this string if no schema is
// available for decoding at the frontend long-term.
// Note: There could be something better displayed than this string if no schema
// is available for decoding at the frontend long-term.
return format!(
"No embedded {} schema in smart contract available for decoding",
schema_name.kind()
Expand Down
68 changes: 65 additions & 3 deletions backend-rust/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::Context;
use chrono::{DateTime, Utc};
use concordium_rust_sdk::{
base::{contracts_common::to_bytes, smart_contracts::WasmVersion},
common::types::Amount,
common::types::{Amount, Timestamp},
indexer::{async_trait, Indexer, ProcessEvent, TraverseConfig, TraverseError},
smart_contracts::engine::utils::{get_embedded_schema_v0, get_embedded_schema_v1},
types::{
Expand Down Expand Up @@ -1048,6 +1048,8 @@ enum PreparedEvent {
ContractInitialized(PreparedContractInitialized),
/// Contract got updated.
ContractUpdate(Vec<PreparedContractUpdate>),
/// A scheduled transfer got executed.
ScheduledTransfer(PreparedScheduledReleases),
/// No changes in the database was caused by this event.
NoOperation,
}
Expand Down Expand Up @@ -1186,12 +1188,18 @@ impl PreparedEvent {
AccountTransactionEffects::TransferredWithSchedule {
to,
amount,
} => None,
} => Some(PreparedEvent::ScheduledTransfer(PreparedScheduledReleases::prepare(
to.to_string(),
amount,
limemloh marked this conversation as resolved.
Show resolved Hide resolved
)?)),
AccountTransactionEffects::TransferredWithScheduleAndMemo {
to,
amount,
memo,
} => None,
} => Some(PreparedEvent::ScheduledTransfer(PreparedScheduledReleases::prepare(
to.to_string(),
amount,
limemloh marked this conversation as resolved.
Show resolved Hide resolved
)?)),
AccountTransactionEffects::CredentialKeysUpdated {
cred_id,
} => None,
Expand Down Expand Up @@ -1237,6 +1245,7 @@ impl PreparedEvent {
}
Ok(())
}
PreparedEvent::ScheduledTransfer(event) => event.save(tx, tx_idx).await,
PreparedEvent::NoOperation => Ok(()),
}
}
Expand Down Expand Up @@ -1729,3 +1738,56 @@ impl PreparedContractUpdate {
Ok(())
}
}

struct PreparedScheduledReleases {
account_address: String,
release_times: Vec<DateTime<Utc>>,
amounts: Vec<i64>,
}

impl PreparedScheduledReleases {
fn prepare(to: String, scheduled_releases: &[(Timestamp, Amount)]) -> anyhow::Result<Self> {
let capacity = scheduled_releases.len();
let mut release_times: Vec<DateTime<Utc>> = Vec::with_capacity(capacity);
let mut amounts: Vec<i64> = Vec::with_capacity(capacity);
for (timestamp, amount) in scheduled_releases.iter() {
release_times.push(DateTime::<Utc>::try_from(*timestamp)?);
amounts.push(i64::try_from(amount.micro_ccd())?);
}

Ok(Self {
account_address: to,
release_times,
amounts,
})
}

async fn save(
&self,
tx: &mut sqlx::Transaction<'static, sqlx::Postgres>,
transaction_index: i64,
) -> anyhow::Result<()> {
sqlx::query!(
"INSERT INTO scheduled_releases (
transaction_index,
account_index,
release_time,
amount
)
SELECT
$1,
(SELECT index FROM accounts WHERE address = $2),
UNNEST($3::TIMESTAMPTZ[]),
UNNEST($4::BIGINT[])
",
transaction_index,
self.account_address,
&self.release_times,
&self.amounts
)
.execute(tx.as_mut())
.await?;
Ok(())
}
// fn prepare()
}
Loading