Skip to content

Commit

Permalink
feat: use EventDb::modify for INSERT, UPDATE, DELETE operations
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed May 15, 2024
1 parent 8e8617a commit 1c45d5e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 56 deletions.
40 changes: 19 additions & 21 deletions catalyst-gateway/bin/src/event_db/cardano/chain_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ impl EventDB {
&self, slot_no: SlotNumber, network: Network, epoch_no: EpochNumber, block_time: DateTime,
block_hash: BlockHash,
) -> anyhow::Result<()> {
let _rows = self
.query(INSERT_SLOT_INDEX_SQL, &[
&slot_no,
&network.to_string(),
&epoch_no,
&block_time,
&block_hash,
])
.await?;
self.modify(INSERT_SLOT_INDEX_SQL, &[
&slot_no,
&network.to_string(),
&epoch_no,
&block_time,
&block_hash,
])
.await?;

Ok(())
}
Expand Down Expand Up @@ -153,18 +152,17 @@ impl EventDB {

// An insert only happens once when there is no update metadata available
// All future additions are just updates on ended, slot_no and block_hash
let _rows = self
.query(INSERT_UPDATE_STATE_SQL, &[
&i64::try_from(network_id)?,
&last_updated,
&last_updated,
&machine_id,
&slot_no,
&network.to_string(),
&block_hash,
&update,
])
.await?;
self.modify(INSERT_UPDATE_STATE_SQL, &[
&i64::try_from(network_id)?,
&last_updated,
&last_updated,
&machine_id,
&slot_no,
&network.to_string(),
&block_hash,
&update,
])
.await?;

Ok(())
}
Expand Down
23 changes: 11 additions & 12 deletions catalyst-gateway/bin/src/event_db/cardano/cip36_registration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,17 @@ impl EventDB {
&& nonce.is_some()
&& errors_report.is_empty();

let _rows = self
.query(INSERT_VOTER_REGISTRATION_SQL, &[
&tx_id,
&stake_credential,
&encoded_voting_info,
&payment_address,
&nonce,
&metadata_cip36,
&json!(&errors_report),
&is_valid,
])
.await?;
self.modify(INSERT_VOTER_REGISTRATION_SQL, &[
&tx_id,
&stake_credential,
&encoded_voting_info,
&payment_address,
&nonce,
&metadata_cip36,
&json!(&errors_report),
&is_valid,
])
.await?;

Ok(())
}
Expand Down
43 changes: 20 additions & 23 deletions catalyst-gateway/bin/src/event_db/cardano/utxo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,27 @@ impl EventDB {
};
let stake_credential = stake_address.map(|val| val.payload().as_hash().to_vec());

let _rows = self
.query(INSERT_UTXO_SQL, &[
&i32::try_from(index)?,
&tx_hash.as_slice(),
&i64::try_from(tx_out.lovelace_amount())?,
&stake_credential,
&assets,
])
.await?;
self.modify(INSERT_UTXO_SQL, &[
&i32::try_from(index)?,
&tx_hash.as_slice(),
&i64::try_from(tx_out.lovelace_amount())?,
&stake_credential,
&assets,
])
.await?;
}
// update outputs with inputs
for tx_in in tx.inputs() {
let output = tx_in.output_ref();
let output_tx_hash = output.hash();
let out_index = output.index();

let _rows = self
.query(UPDATE_UTXO_SQL, &[
&tx_hash.as_slice(),
&output_tx_hash.as_slice(),
&i32::try_from(out_index)?,
])
.await?;
self.modify(UPDATE_UTXO_SQL, &[
&tx_hash.as_slice(),
&output_tx_hash.as_slice(),
&i32::try_from(out_index)?,
])
.await?;
}

Ok(())
Expand All @@ -71,13 +69,12 @@ impl EventDB {
pub(crate) async fn index_txn_data(
&self, tx_id: &[u8], slot_no: SlotNumber, network: Network,
) -> anyhow::Result<()> {
let _rows = self
.query(INSERT_TXN_INDEX_SQL, &[
&tx_id,
&slot_no,
&network.to_string(),
])
.await?;
self.modify(INSERT_TXN_INDEX_SQL, &[
&tx_id,
&slot_no,
&network.to_string(),
])
.await?;

Ok(())
}
Expand Down

0 comments on commit 1c45d5e

Please sign in to comment.