-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: ETCM-7811 native token data source (#40)
- Loading branch information
1 parent
b668d84
commit ee79311
Showing
19 changed files
with
671 additions
and
8 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
mainchain-follower/db-sync-follower/src/native_token/mod.rs
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,72 @@ | ||
use crate::db_model::{Address, NativeTokenAmount, SlotNumber}; | ||
use crate::metrics::McFollowerMetrics; | ||
use crate::observed_async_trait; | ||
use async_trait::async_trait; | ||
use main_chain_follower_api::{DataSourceError, NativeTokenManagementDataSource, Result}; | ||
use sidechain_domain::*; | ||
use sqlx::PgPool; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub struct NativeTokenManagementDataSourceImpl { | ||
pub pool: PgPool, | ||
pub metrics_opt: Option<McFollowerMetrics>, | ||
} | ||
|
||
observed_async_trait!( | ||
impl NativeTokenManagementDataSource for NativeTokenManagementDataSourceImpl { | ||
async fn get_total_native_token_transfer( | ||
&self, | ||
after_block: Option<McBlockHash>, | ||
to_block: McBlockHash, | ||
native_token_policy_id: PolicyId, | ||
native_token_asset_name: AssetName, | ||
illiquid_supply_address: MainchainAddress, | ||
) -> Result<sidechain_domain::NativeTokenAmount> { | ||
if after_block == Some(to_block.clone()) { | ||
return Ok(NativeTokenAmount(0).into()); | ||
} | ||
|
||
let (after_slot , to_slot) = futures::try_join!( | ||
get_after_slot(after_block, &self.pool), | ||
get_to_slot(to_block, &self.pool) | ||
)?; | ||
|
||
let total_transfer = crate::db_model::get_total_native_tokens_transfered( | ||
&self.pool, | ||
after_slot, | ||
to_slot, | ||
crate::db_model::Asset { | ||
policy_id: native_token_policy_id.into(), | ||
asset_name: native_token_asset_name.into(), | ||
}, | ||
Address(illiquid_supply_address.to_string()), | ||
) | ||
.await?; | ||
|
||
Ok(total_transfer.unwrap_or(NativeTokenAmount(0)).into()) | ||
} | ||
} | ||
); | ||
|
||
async fn get_after_slot(after_block: Option<McBlockHash>, pool: &PgPool) -> Result<SlotNumber> { | ||
match after_block { | ||
None => Ok(SlotNumber(0)), | ||
Some(after_block) => Ok(crate::db_model::get_block_by_hash(pool, after_block.clone()) | ||
.await? | ||
.ok_or(DataSourceError::ExpectedDataNotFound(format!( | ||
"Lower bound block {after_block} not found when querying for native token transfers" | ||
)))? | ||
.slot_no), | ||
} | ||
} | ||
|
||
async fn get_to_slot(to_block: McBlockHash, pool: &PgPool) -> Result<SlotNumber> { | ||
Ok(crate::db_model::get_block_by_hash(pool, to_block.clone()) | ||
.await? | ||
.ok_or(DataSourceError::ExpectedDataNotFound(format!( | ||
"Upper bound block {to_block} not found when querying for native token transfers" | ||
)))? | ||
.slot_no) | ||
} |
90 changes: 90 additions & 0 deletions
90
mainchain-follower/db-sync-follower/src/native_token/tests.rs
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,90 @@ | ||
use super::NativeTokenManagementDataSourceImpl; | ||
use main_chain_follower_api::NativeTokenManagementDataSource; | ||
use sidechain_domain::{AssetName, MainchainAddress, McBlockHash, PolicyId}; | ||
use sqlx::PgPool; | ||
use std::str::FromStr; | ||
|
||
fn native_token_policy_id() -> PolicyId { | ||
PolicyId::from_hex_unsafe("6c969320597b755454ff3653ad09725d590c570827a129aeb4385526") | ||
} | ||
|
||
fn native_token_asset_name() -> AssetName { | ||
AssetName::from_hex_unsafe("546573744275647a507265766965775f3335") | ||
} | ||
|
||
fn illiquid_supply_address() -> MainchainAddress { | ||
MainchainAddress::from_str("addr_test1wrhvtvx3f0g9wv9rx8kfqc60jva3e07nqujk2cspekv4mqs9rjdvz") | ||
.unwrap() | ||
} | ||
|
||
fn block_hash(i: u32) -> McBlockHash { | ||
McBlockHash::from_str(&format!( | ||
"b00000000000000000000000000000000000000000000000000000000000000{i}" | ||
)) | ||
.unwrap() | ||
} | ||
|
||
pub fn genesis_hash() -> McBlockHash { | ||
block_hash(0) | ||
} | ||
|
||
fn make_source(pool: PgPool) -> NativeTokenManagementDataSourceImpl { | ||
NativeTokenManagementDataSourceImpl { pool, metrics_opt: None } | ||
} | ||
|
||
#[sqlx::test(migrations = "./testdata/native-token/migrations")] | ||
async fn defaults_to_zero_when_there_are_no_transfers(pool: PgPool) { | ||
let source = make_source(pool); | ||
let after_block = None; | ||
let to_block = genesis_hash(); | ||
let result = source | ||
.get_total_native_token_transfer( | ||
after_block, | ||
to_block, | ||
native_token_policy_id(), | ||
native_token_asset_name(), | ||
illiquid_supply_address(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(result.0, 0) | ||
} | ||
|
||
#[sqlx::test(migrations = "./testdata/native-token/migrations")] | ||
async fn gets_sum_of_all_transfers_when_queried_up_to_latest_block(pool: PgPool) { | ||
let source = make_source(pool); | ||
let after_block = None; | ||
let to_block = block_hash(5); | ||
let result = source | ||
.get_total_native_token_transfer( | ||
after_block, | ||
to_block, | ||
native_token_policy_id(), | ||
native_token_asset_name(), | ||
illiquid_supply_address(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(result.0, 11 + 12 + 13 + 14) | ||
} | ||
|
||
#[sqlx::test(migrations = "./testdata/native-token/migrations")] | ||
async fn gets_sum_of_transfers_in_range(pool: PgPool) { | ||
let source = make_source(pool); | ||
let after_block = Some(block_hash(1)); | ||
let to_block = block_hash(5); | ||
let result = source | ||
.get_total_native_token_transfer( | ||
after_block, | ||
to_block, | ||
native_token_policy_id(), | ||
native_token_asset_name(), | ||
illiquid_supply_address(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(result.0, 12 + 13 + 14) | ||
} |
Oops, something went wrong.