From 8f8eeccbf43b0f7e3c9469d9d23d984b316a3cb6 Mon Sep 17 00:00:00 2001 From: Doris Benda Date: Tue, 15 Oct 2024 17:58:18 +0300 Subject: [PATCH] Add contract event to database --- ...96052b3c664746400d987b23ee4a58560459d.json | 17 ++++++++++++++ .../migrations/0001_initialize.up.sql | 23 ++++++++----------- backend-rust/src/indexer.rs | 20 ++++++++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 backend-rust/.sqlx/query-da0c0bc6c2a9fdf7ddefaa807d596052b3c664746400d987b23ee4a58560459d.json diff --git a/backend-rust/.sqlx/query-da0c0bc6c2a9fdf7ddefaa807d596052b3c664746400d987b23ee4a58560459d.json b/backend-rust/.sqlx/query-da0c0bc6c2a9fdf7ddefaa807d596052b3c664746400d987b23ee4a58560459d.json new file mode 100644 index 00000000..07e69b73 --- /dev/null +++ b/backend-rust/.sqlx/query-da0c0bc6c2a9fdf7ddefaa807d596052b3c664746400d987b23ee4a58560459d.json @@ -0,0 +1,17 @@ +{ + "db_name": "PostgreSQL", + "query": "INSERT INTO contract_events (\n transaction_index,\n event_index,\n contract_index,\n contract_sub_index\n )\n VALUES (\n $1, $2, $3, $4\n )", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Int8", + "Int8", + "Int8" + ] + }, + "nullable": [] + }, + "hash": "da0c0bc6c2a9fdf7ddefaa807d596052b3c664746400d987b23ee4a58560459d" +} diff --git a/backend-rust/migrations/0001_initialize.up.sql b/backend-rust/migrations/0001_initialize.up.sql index b12d9fe1..e97906eb 100644 --- a/backend-rust/migrations/0001_initialize.up.sql +++ b/backend-rust/migrations/0001_initialize.up.sql @@ -311,8 +311,7 @@ CREATE TABLE contracts( CREATE TABLE module_contract_link_events( -- An index/id for this event (row number). index - BIGINT - SERIAL + BIGSERIAL PRIMARY KEY NOT NULL, -- Event index of the event. @@ -338,18 +337,17 @@ CREATE TABLE module_contract_link_events( -- True if contract gets linked to the given module, false if contract gets unlinked to the given module. is_linked BOOLEAN - NOT NULL, + NOT NULL -- TODO: link_action = int? source = int? -) +); -- Every successful event associated to a contract. -- TODO: add index over the contract (index,subindex) -CREATE TABLE contract_events( +CREATE TABLE contract_events ( -- An index/id for this event (row number). index - BIGINT - SERIAL + BIGSERIAL PRIMARY KEY NOT NULL, -- Transaction index including the event. @@ -367,18 +365,17 @@ CREATE TABLE contract_events( -- Contract subindex that event is associated with. contract_sub_index BIGINT - NOT NULL, + NOT NULL -- TODO: source = int? -) +); -- Every rejected event associated to a contract. -- TODO: add index over the contract (index,subindex) CREATE TABLE contract_reject_events( -- An index/id for this event (row number). index - BIGINT - SERIAL + BIGSERIAL PRIMARY KEY NOT NULL, -- Transaction index including the event. @@ -396,10 +393,10 @@ CREATE TABLE contract_reject_events( -- Contract subindex that event is associated with. contract_sub_index BIGINT - NOT NULL, + NOT NULL -- TODO: source = int? -) +); CREATE OR REPLACE FUNCTION block_added_notify_trigger_function() RETURNS trigger AS $trigger$ DECLARE diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index bddb871a..caf2eaa3 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -1544,6 +1544,26 @@ impl PreparedContractInitialized { ) .execute(tx.as_mut()) .await?; + + // TODO: fix event index. + sqlx::query!( + r#"INSERT INTO contract_events ( + transaction_index, + event_index, + contract_index, + contract_sub_index + ) + VALUES ( + $1, $2, $3, $4 + )"#, + self.tx_index, + 0i64, + self.index, + self.sub_index + ) + .execute(tx.as_mut()) + .await?; + Ok(()) } }