From 58561380d5b3ac10e7a92fe8a07db0aa77f79843 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 5 Nov 2024 20:45:25 +0700 Subject: [PATCH 01/23] refactor: rename schema to asset --- .../src/db/index/schema/cql/txo_assets_by_stake_table.cql | 6 +++--- .../db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/catalyst-gateway/bin/src/db/index/schema/cql/txo_assets_by_stake_table.cql b/catalyst-gateway/bin/src/db/index/schema/cql/txo_assets_by_stake_table.cql index 19a686f2d53..b879e1f9d60 100644 --- a/catalyst-gateway/bin/src/db/index/schema/cql/txo_assets_by_stake_table.cql +++ b/catalyst-gateway/bin/src/db/index/schema/cql/txo_assets_by_stake_table.cql @@ -6,12 +6,12 @@ CREATE TABLE IF NOT EXISTS txo_assets_by_stake ( slot_no varint, -- slot number the txo was created in. txn smallint, -- Which Transaction in the Slot is the TXO. txo smallint, -- offset in the txo list of the transaction the txo is in. - policy_id blob, -- asset policy hash (id) (28 byte binary hash) - policy_name text, -- name of the policy (UTF8) TODO: https://github.com/input-output-hk/catalyst-voices/issues/1121 + asset_id blob, -- asset policy hash (id) (28 byte binary hash) + asset_name text, -- name of the asset policy (UTF8) (32 bytes) -- None Key Data of the asset. value varint, -- Value of the asset (i128) - PRIMARY KEY (stake_address, slot_no, txn, txo, policy_id, policy_name) + PRIMARY KEY (stake_address, slot_no, txn, txo, asset_id, asset_name) ); diff --git a/catalyst-gateway/bin/src/db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql b/catalyst-gateway/bin/src/db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql index 047567d895d..cd7d560bc11 100644 --- a/catalyst-gateway/bin/src/db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql +++ b/catalyst-gateway/bin/src/db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql @@ -3,8 +3,8 @@ CREATE TABLE IF NOT EXISTS unstaked_txo_assets_by_txn_hash ( -- Primary Key Fields txn_hash blob, -- 32 byte hash of this transaction. txo smallint, -- offset in the txo list of the transaction the txo is in. - policy_id blob, -- asset policy hash (id) (28 byte binary hash) - policy_name text, -- name of the policy (UTF8) + asset_id blob, -- asset policy hash (id) (28 byte binary hash) + asset_name text, -- name of the policy (UTF8) (32 bytes) -- Secondary Location information for the transaction. slot_no varint, -- slot number the txo was created in. @@ -13,5 +13,5 @@ CREATE TABLE IF NOT EXISTS unstaked_txo_assets_by_txn_hash ( -- Value of the asset. value varint, -- Value of the asset (u64) - PRIMARY KEY (txn_hash, txo, policy_id, policy_name) + PRIMARY KEY (txn_hash, txo, asset_id, asset_name) ); From 54d8e5fba6e5d5d6ae5f2c981ecdf22fec255784 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 6 Nov 2024 20:10:54 +0700 Subject: [PATCH 02/23] refactor: vector asset --- .../service/api/cardano/staking/assets_get.rs | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index d1d2b72126e..20ad3f85391 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -99,8 +99,7 @@ struct TxoInfo { /// Whether the TXO was spent. spent_slot_no: Option, /// TXO assets. - // TODO: https://github.com/input-output-hk/catalyst-voices/issues/1121 - assets: HashMap, TxoAssetInfo>, + assets: HashMap, Vec>, } /// Calculate the stake info for a given stake address. @@ -185,12 +184,18 @@ async fn get_txo_by_txn( let entry = txo_info .assets .entry(row.policy_id.clone()) - .or_insert(TxoAssetInfo { - id: row.policy_id, - name: row.policy_name, - amount: num_bigint::BigInt::ZERO, - }); - entry.amount += row.value; + .or_insert_with(Vec::new); + + match entry.iter_mut().find(|x| x.id == row.policy_id) { + Some(item) => item.amount += row.value, + None => { + entry.push(TxoAssetInfo { + id: row.policy_id, + name: row.policy_name, + amount: row.value, + }); + }, + } } let mut txos_by_txn = HashMap::new(); @@ -273,7 +278,7 @@ fn build_stake_info( stake_info.ada_amount += i64::try_from(txo_info.value).map_err(|err| anyhow!(err))?; - for asset in txo_info.assets.into_values() { + for asset in txo_info.assets.into_values().flatten() { stake_info.native_tokens.push(StakedNativeTokenInfo { policy_hash: asset.id.try_into()?, asset_name: asset.name.into(), From 1d34de4cf90b1eb0f12caed522023faaa07956e5 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 6 Nov 2024 20:17:40 +0700 Subject: [PATCH 03/23] chore: rename asset fields --- .../queries/staked_ada/get_assets_by_stake_address.rs | 4 ++-- .../bin/src/service/api/cardano/staking/assets_get.rs | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/catalyst-gateway/bin/src/db/index/queries/staked_ada/get_assets_by_stake_address.rs b/catalyst-gateway/bin/src/db/index/queries/staked_ada/get_assets_by_stake_address.rs index 03d1a16a7dc..a4f29aa1fc7 100644 --- a/catalyst-gateway/bin/src/db/index/queries/staked_ada/get_assets_by_stake_address.rs +++ b/catalyst-gateway/bin/src/db/index/queries/staked_ada/get_assets_by_stake_address.rs @@ -52,9 +52,9 @@ mod result { /// TXO transaction slot number. pub slot_no: num_bigint::BigInt, /// Asset hash. - pub policy_id: Vec, + pub asset_id: Vec, /// Asset name. - pub policy_name: String, + pub asset_name: Vec, /// Asset value. pub value: num_bigint::BigInt, } diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index 20ad3f85391..30496d5281e 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -78,8 +78,7 @@ struct TxoAssetInfo { /// Asset hash. id: Vec, /// Asset name. - // TODO: https://github.com/input-output-hk/catalyst-voices/issues/1121 - name: String, + name: Vec, /// Asset amount. amount: num_bigint::BigInt, } @@ -183,15 +182,15 @@ async fn get_txo_by_txn( let entry = txo_info .assets - .entry(row.policy_id.clone()) + .entry(row.asset_id.clone()) .or_insert_with(Vec::new); - match entry.iter_mut().find(|x| x.id == row.policy_id) { + match entry.iter_mut().find(|x| x.id == row.asset_id) { Some(item) => item.amount += row.value, None => { entry.push(TxoAssetInfo { - id: row.policy_id, - name: row.policy_name, + id: row.asset_id, + name: row.asset_name, amount: row.value, }); }, From 3e0beabffd0ebe57bc57599eeb4b92a6dcc920c0 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 6 Nov 2024 20:28:40 +0700 Subject: [PATCH 04/23] refactor: object mapping structs --- .../bin/src/db/index/block/txo/insert_txo_asset.rs | 12 ++++++------ .../db/index/block/txo/insert_unstaked_txo_asset.rs | 10 +++++----- .../src/service/common/types/cardano/asset_name.rs | 1 - 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs index ba7bbde7c45..b2c15d1a1b7 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs @@ -26,9 +26,9 @@ pub(super) struct Params { /// Transaction Output Offset inside the transaction. txo: i16, /// Policy hash of the asset - policy_id: Vec, + asset_id: Vec, /// Policy name of the asset - policy_name: String, + asset_name: Vec, /// Value of the asset value: num_bigint::BigInt, } @@ -40,16 +40,16 @@ impl Params { /// values. #[allow(clippy::too_many_arguments)] pub(super) fn new( - stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, policy_id: &[u8], - policy_name: &str, value: i128, + stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, asset_id: &[u8], asset_name: &str, + value: i128, ) -> Self { Self { stake_address: stake_address.to_vec(), slot_no: slot_no.into(), txn, txo, - policy_id: policy_id.to_vec(), - policy_name: policy_name.to_owned(), + asset_id: asset_id.to_vec(), + asset_name: asset_name.as_bytes().to_vec(), value: value.into(), } } diff --git a/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs b/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs index 250ca8ae1c3..92f9ceedff8 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs @@ -22,9 +22,9 @@ pub(super) struct Params { /// Transaction Output Offset inside the transaction. txo: i16, /// Policy hash of the asset - policy_id: Vec, + asset_id: Vec, /// Policy name of the asset - policy_name: String, + asset_name: Vec, /// Block Slot Number slot_no: num_bigint::BigInt, /// Transaction Offset inside the block. @@ -40,14 +40,14 @@ impl Params { /// values. #[allow(clippy::too_many_arguments)] pub(super) fn new( - txn_hash: &[u8], txo: i16, policy_id: &[u8], policy_name: &str, slot_no: u64, txn: i16, + txn_hash: &[u8], txo: i16, asset_id: &[u8], asset_name: &str, slot_no: u64, txn: i16, value: i128, ) -> Self { Self { txn_hash: txn_hash.to_vec(), txo, - policy_id: policy_id.to_vec(), - policy_name: policy_name.to_owned(), + asset_id: asset_id.to_vec(), + asset_name: asset_name.as_bytes().to_vec(), slot_no: slot_no.into(), txn, value: value.into(), diff --git a/catalyst-gateway/bin/src/service/common/types/cardano/asset_name.rs b/catalyst-gateway/bin/src/service/common/types/cardano/asset_name.rs index 3f8e1f72cf5..0594a0f968a 100644 --- a/catalyst-gateway/bin/src/service/common/types/cardano/asset_name.rs +++ b/catalyst-gateway/bin/src/service/common/types/cardano/asset_name.rs @@ -65,7 +65,6 @@ impl Example for AssetName { } } -// TODO: https://github.com/input-output-hk/catalyst-voices/issues/1121 impl From> for AssetName { fn from(value: Vec) -> Self { match String::from_utf8(value.clone()) { From fa4724fd7d4880e5ee9d7247cfeea332cfd02424 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 6 Nov 2024 20:44:55 +0700 Subject: [PATCH 05/23] chore: minor rename --- catalyst-gateway/bin/src/db/index/block/txo/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/catalyst-gateway/bin/src/db/index/block/txo/mod.rs b/catalyst-gateway/bin/src/db/index/block/txo/mod.rs index 66bd950822f..aac310ebd1a 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/mod.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/mod.rs @@ -178,10 +178,10 @@ impl TxoInsertQuery { } for asset in txo.non_ada_assets() { - let policy_id = asset.policy().to_vec(); + let asset_id = asset.policy().to_vec(); for policy_asset in asset.assets() { if policy_asset.is_output() { - let policy_name = policy_asset.to_ascii_name().unwrap_or_default(); + let asset_name = policy_asset.to_ascii_name().unwrap_or_default(); let value = policy_asset.any_coin(); if staked { @@ -190,8 +190,8 @@ impl TxoInsertQuery { slot_no, txn, txo_index, - &policy_id, - &policy_name, + &asset_id, + &asset_name, value, ); self.staked_txo_asset.push(params); @@ -199,8 +199,8 @@ impl TxoInsertQuery { let params = insert_unstaked_txo_asset::Params::new( txn_hash, txo_index, - &policy_id, - &policy_name, + &asset_id, + &asset_name, slot_no, txn, value, From b164da48d7fbda0196824cc446b88f5266dd5742 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 6 Nov 2024 20:46:05 +0700 Subject: [PATCH 06/23] fix: update operation cql --- .../bin/src/db/index/block/txo/cql/insert_txo_asset.cql | 8 ++++---- .../db/index/block/txo/cql/insert_unstaked_txo_asset.cql | 8 ++++---- .../db/index/queries/cql/get_assets_by_stake_address.cql | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_txo_asset.cql b/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_txo_asset.cql index 3bdb6342c4b..bcbe796464f 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_txo_asset.cql +++ b/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_txo_asset.cql @@ -5,15 +5,15 @@ INSERT INTO txo_assets_by_stake ( slot_no, txn, txo, - policy_id, - policy_name, + asset_id, + asset_name, value ) VALUES ( :stake_address, :slot_no, :txn, :txo, - :policy_id, - :policy_name, + :asset_id, + :asset_name, :value ); diff --git a/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_unstaked_txo_asset.cql b/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_unstaked_txo_asset.cql index e170a0b46c2..7ca39216614 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_unstaked_txo_asset.cql +++ b/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_unstaked_txo_asset.cql @@ -2,16 +2,16 @@ INSERT INTO unstaked_txo_assets_by_txn_hash ( txn_hash, txo, - policy_id, - policy_name, + asset_id, + asset_name, slot_no, txn, value ) VALUES ( :txn_hash, :txo, - :policy_id, - :policy_name, + :asset_id, + :asset_name, :slot_no, :txn, :value diff --git a/catalyst-gateway/bin/src/db/index/queries/cql/get_assets_by_stake_address.cql b/catalyst-gateway/bin/src/db/index/queries/cql/get_assets_by_stake_address.cql index 128bc81d782..4bf05ce0fc6 100644 --- a/catalyst-gateway/bin/src/db/index/queries/cql/get_assets_by_stake_address.cql +++ b/catalyst-gateway/bin/src/db/index/queries/cql/get_assets_by_stake_address.cql @@ -2,8 +2,8 @@ SELECT txn, txo, slot_no, - policy_id, - policy_name, + asset_id, + asset_name, value FROM txo_assets_by_stake WHERE stake_address = :stake_address From f69a246644e4179ad42fe12c5dee9c750ee0b415 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 6 Nov 2024 21:33:21 +0700 Subject: [PATCH 07/23] fix: schema version --- catalyst-gateway/bin/src/db/index/schema/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalyst-gateway/bin/src/db/index/schema/mod.rs b/catalyst-gateway/bin/src/db/index/schema/mod.rs index 8a219105fe1..2ae10883cd5 100644 --- a/catalyst-gateway/bin/src/db/index/schema/mod.rs +++ b/catalyst-gateway/bin/src/db/index/schema/mod.rs @@ -17,7 +17,7 @@ use crate::{settings::cassandra_db, utils::blake2b_hash::generate_uuid_string_fr /// change accidentally, and is NOT to be used directly to set the schema version of the /// table namespaces. #[allow(dead_code)] -const SCHEMA_VERSION: &str = "08193dfe-698a-8177-bdf8-20c5691a06e7"; +const SCHEMA_VERSION: &str = "8ab4e07a-26c5-84d2-835c-46344fdc0ad5"; /// Keyspace Create (Templated) const CREATE_NAMESPACE_CQL: &str = include_str!("./cql/namespace.cql"); From 4b7991f4cf5667acb4a2d7a641281c074d237e88 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 7 Nov 2024 21:34:53 +0700 Subject: [PATCH 08/23] chore: change asset_id back to policy_id --- .../bin/src/db/index/block/txo/cql/insert_txo_asset.cql | 4 ++-- .../db/index/block/txo/cql/insert_unstaked_txo_asset.cql | 4 ++-- .../bin/src/db/index/block/txo/insert_txo_asset.rs | 6 +++--- .../bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs | 6 +++--- catalyst-gateway/bin/src/db/index/block/txo/mod.rs | 6 +++--- .../db/index/queries/cql/get_assets_by_stake_address.cql | 2 +- .../index/queries/staked_ada/get_assets_by_stake_address.rs | 2 +- .../src/db/index/schema/cql/txo_assets_by_stake_table.cql | 6 +++--- .../db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql | 6 +++--- .../bin/src/service/api/cardano/staking/assets_get.rs | 6 +++--- 10 files changed, 24 insertions(+), 24 deletions(-) diff --git a/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_txo_asset.cql b/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_txo_asset.cql index bcbe796464f..92c9dd0c534 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_txo_asset.cql +++ b/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_txo_asset.cql @@ -5,7 +5,7 @@ INSERT INTO txo_assets_by_stake ( slot_no, txn, txo, - asset_id, + policy_id, asset_name, value ) VALUES ( @@ -13,7 +13,7 @@ INSERT INTO txo_assets_by_stake ( :slot_no, :txn, :txo, - :asset_id, + :policy_id, :asset_name, :value ); diff --git a/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_unstaked_txo_asset.cql b/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_unstaked_txo_asset.cql index 7ca39216614..6045229d69e 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_unstaked_txo_asset.cql +++ b/catalyst-gateway/bin/src/db/index/block/txo/cql/insert_unstaked_txo_asset.cql @@ -2,7 +2,7 @@ INSERT INTO unstaked_txo_assets_by_txn_hash ( txn_hash, txo, - asset_id, + policy_id, asset_name, slot_no, txn, @@ -10,7 +10,7 @@ INSERT INTO unstaked_txo_assets_by_txn_hash ( ) VALUES ( :txn_hash, :txo, - :asset_id, + :policy_id, :asset_name, :slot_no, :txn, diff --git a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs index b2c15d1a1b7..0e38100cb58 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs @@ -26,7 +26,7 @@ pub(super) struct Params { /// Transaction Output Offset inside the transaction. txo: i16, /// Policy hash of the asset - asset_id: Vec, + policy_id: Vec, /// Policy name of the asset asset_name: Vec, /// Value of the asset @@ -40,7 +40,7 @@ impl Params { /// values. #[allow(clippy::too_many_arguments)] pub(super) fn new( - stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, asset_id: &[u8], asset_name: &str, + stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, policy_id: &[u8], asset_name: &str, value: i128, ) -> Self { Self { @@ -48,7 +48,7 @@ impl Params { slot_no: slot_no.into(), txn, txo, - asset_id: asset_id.to_vec(), + policy_id: policy_id.to_vec(), asset_name: asset_name.as_bytes().to_vec(), value: value.into(), } diff --git a/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs b/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs index 92f9ceedff8..02c373ef39e 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs @@ -22,7 +22,7 @@ pub(super) struct Params { /// Transaction Output Offset inside the transaction. txo: i16, /// Policy hash of the asset - asset_id: Vec, + policy_id: Vec, /// Policy name of the asset asset_name: Vec, /// Block Slot Number @@ -40,13 +40,13 @@ impl Params { /// values. #[allow(clippy::too_many_arguments)] pub(super) fn new( - txn_hash: &[u8], txo: i16, asset_id: &[u8], asset_name: &str, slot_no: u64, txn: i16, + txn_hash: &[u8], txo: i16, policy_id: &[u8], asset_name: &str, slot_no: u64, txn: i16, value: i128, ) -> Self { Self { txn_hash: txn_hash.to_vec(), txo, - asset_id: asset_id.to_vec(), + policy_id: policy_id.to_vec(), asset_name: asset_name.as_bytes().to_vec(), slot_no: slot_no.into(), txn, diff --git a/catalyst-gateway/bin/src/db/index/block/txo/mod.rs b/catalyst-gateway/bin/src/db/index/block/txo/mod.rs index aac310ebd1a..0ccf5ec7cef 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/mod.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/mod.rs @@ -178,7 +178,7 @@ impl TxoInsertQuery { } for asset in txo.non_ada_assets() { - let asset_id = asset.policy().to_vec(); + let policy_id = asset.policy().to_vec(); for policy_asset in asset.assets() { if policy_asset.is_output() { let asset_name = policy_asset.to_ascii_name().unwrap_or_default(); @@ -190,7 +190,7 @@ impl TxoInsertQuery { slot_no, txn, txo_index, - &asset_id, + &policy_id, &asset_name, value, ); @@ -199,7 +199,7 @@ impl TxoInsertQuery { let params = insert_unstaked_txo_asset::Params::new( txn_hash, txo_index, - &asset_id, + &policy_id, &asset_name, slot_no, txn, diff --git a/catalyst-gateway/bin/src/db/index/queries/cql/get_assets_by_stake_address.cql b/catalyst-gateway/bin/src/db/index/queries/cql/get_assets_by_stake_address.cql index 4bf05ce0fc6..82d4c605818 100644 --- a/catalyst-gateway/bin/src/db/index/queries/cql/get_assets_by_stake_address.cql +++ b/catalyst-gateway/bin/src/db/index/queries/cql/get_assets_by_stake_address.cql @@ -2,7 +2,7 @@ SELECT txn, txo, slot_no, - asset_id, + policy_id, asset_name, value FROM txo_assets_by_stake diff --git a/catalyst-gateway/bin/src/db/index/queries/staked_ada/get_assets_by_stake_address.rs b/catalyst-gateway/bin/src/db/index/queries/staked_ada/get_assets_by_stake_address.rs index a4f29aa1fc7..09bc8f2df03 100644 --- a/catalyst-gateway/bin/src/db/index/queries/staked_ada/get_assets_by_stake_address.rs +++ b/catalyst-gateway/bin/src/db/index/queries/staked_ada/get_assets_by_stake_address.rs @@ -52,7 +52,7 @@ mod result { /// TXO transaction slot number. pub slot_no: num_bigint::BigInt, /// Asset hash. - pub asset_id: Vec, + pub policy_id: Vec, /// Asset name. pub asset_name: Vec, /// Asset value. diff --git a/catalyst-gateway/bin/src/db/index/schema/cql/txo_assets_by_stake_table.cql b/catalyst-gateway/bin/src/db/index/schema/cql/txo_assets_by_stake_table.cql index b879e1f9d60..da85a1d9323 100644 --- a/catalyst-gateway/bin/src/db/index/schema/cql/txo_assets_by_stake_table.cql +++ b/catalyst-gateway/bin/src/db/index/schema/cql/txo_assets_by_stake_table.cql @@ -6,12 +6,12 @@ CREATE TABLE IF NOT EXISTS txo_assets_by_stake ( slot_no varint, -- slot number the txo was created in. txn smallint, -- Which Transaction in the Slot is the TXO. txo smallint, -- offset in the txo list of the transaction the txo is in. - asset_id blob, -- asset policy hash (id) (28 byte binary hash) - asset_name text, -- name of the asset policy (UTF8) (32 bytes) + policy_id blob, -- asset policy hash (id) (28 byte binary hash) + asset_name blob, -- name of the asset policy (UTF8) (32 bytes) -- None Key Data of the asset. value varint, -- Value of the asset (i128) - PRIMARY KEY (stake_address, slot_no, txn, txo, asset_id, asset_name) + PRIMARY KEY (stake_address, slot_no, txn, txo, policy_id, asset_name) ); diff --git a/catalyst-gateway/bin/src/db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql b/catalyst-gateway/bin/src/db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql index cd7d560bc11..4628e236ec4 100644 --- a/catalyst-gateway/bin/src/db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql +++ b/catalyst-gateway/bin/src/db/index/schema/cql/unstaked_txo_assets_by_txn_hash.cql @@ -3,8 +3,8 @@ CREATE TABLE IF NOT EXISTS unstaked_txo_assets_by_txn_hash ( -- Primary Key Fields txn_hash blob, -- 32 byte hash of this transaction. txo smallint, -- offset in the txo list of the transaction the txo is in. - asset_id blob, -- asset policy hash (id) (28 byte binary hash) - asset_name text, -- name of the policy (UTF8) (32 bytes) + policy_id blob, -- asset policy hash (id) (28 byte binary hash) + asset_name blob, -- name of the policy (UTF8) (32 bytes) -- Secondary Location information for the transaction. slot_no varint, -- slot number the txo was created in. @@ -13,5 +13,5 @@ CREATE TABLE IF NOT EXISTS unstaked_txo_assets_by_txn_hash ( -- Value of the asset. value varint, -- Value of the asset (u64) - PRIMARY KEY (txn_hash, txo, asset_id, asset_name) + PRIMARY KEY (txn_hash, txo, policy_id, asset_name) ); diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index 30496d5281e..f3b9f0fdb9b 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -182,14 +182,14 @@ async fn get_txo_by_txn( let entry = txo_info .assets - .entry(row.asset_id.clone()) + .entry(row.policy_id.clone()) .or_insert_with(Vec::new); - match entry.iter_mut().find(|x| x.id == row.asset_id) { + match entry.iter_mut().find(|x| x.id == row.policy_id) { Some(item) => item.amount += row.value, None => { entry.push(TxoAssetInfo { - id: row.asset_id, + id: row.policy_id, name: row.asset_name, amount: row.value, }); From 73ae09a1dda9bda5b2ca9f55fa789e34337225ce Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 7 Nov 2024 21:39:28 +0700 Subject: [PATCH 09/23] chore: find rename --- .../bin/src/service/api/cardano/staking/assets_get.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index f3b9f0fdb9b..d880b49cfd2 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -185,7 +185,7 @@ async fn get_txo_by_txn( .entry(row.policy_id.clone()) .or_insert_with(Vec::new); - match entry.iter_mut().find(|x| x.id == row.policy_id) { + match entry.iter_mut().find(|item| item.id == row.policy_id) { Some(item) => item.amount += row.value, None => { entry.push(TxoAssetInfo { From e582dddc06448492ceb30a7efab64eb393ff3883 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 7 Nov 2024 21:54:24 +0700 Subject: [PATCH 10/23] fix: schema version --- catalyst-gateway/bin/src/db/index/schema/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalyst-gateway/bin/src/db/index/schema/mod.rs b/catalyst-gateway/bin/src/db/index/schema/mod.rs index 2ae10883cd5..e262e2a0a76 100644 --- a/catalyst-gateway/bin/src/db/index/schema/mod.rs +++ b/catalyst-gateway/bin/src/db/index/schema/mod.rs @@ -17,7 +17,7 @@ use crate::{settings::cassandra_db, utils::blake2b_hash::generate_uuid_string_fr /// change accidentally, and is NOT to be used directly to set the schema version of the /// table namespaces. #[allow(dead_code)] -const SCHEMA_VERSION: &str = "8ab4e07a-26c5-84d2-835c-46344fdc0ad5"; +const SCHEMA_VERSION: &str = "75ae6ac9-ddd8-8472-8a7a-8676d04f8679"; /// Keyspace Create (Templated) const CREATE_NAMESPACE_CQL: &str = include_str!("./cql/namespace.cql"); From 54bf98f2a57220c41fb8ec1dfda104c86306575d Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 25 Nov 2024 16:34:08 +0700 Subject: [PATCH 11/23] fix: i128 --- .../bin/src/service/api/cardano/staking/assets_get.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index d880b49cfd2..e2e1a1395ed 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use anyhow::anyhow; use futures::StreamExt; +use num_traits::ToPrimitive; use poem_openapi::{payload::Json, ApiResponse}; use super::SlotNumber; @@ -80,7 +81,7 @@ struct TxoAssetInfo { /// Asset name. name: Vec, /// Asset amount. - amount: num_bigint::BigInt, + amount: i128, } /// TXO information used when calculating a user's stake info. @@ -186,12 +187,12 @@ async fn get_txo_by_txn( .or_insert_with(Vec::new); match entry.iter_mut().find(|item| item.id == row.policy_id) { - Some(item) => item.amount += row.value, + Some(item) => item.amount += row.value.to_i128().unwrap_or(0), None => { entry.push(TxoAssetInfo { id: row.policy_id, name: row.asset_name, - amount: row.value, + amount: row.value.to_i128().unwrap_or(0), }); }, } @@ -281,7 +282,7 @@ fn build_stake_info( stake_info.native_tokens.push(StakedNativeTokenInfo { policy_hash: asset.id.try_into()?, asset_name: asset.name.into(), - amount: asset.amount.try_into()?, + amount: num_bigint::BigInt::from(asset.amount).try_into()?, }); } From a6fb377601a2fed3b9bfa04e3c27eb8b21c1bdd0 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 25 Nov 2024 16:40:58 +0700 Subject: [PATCH 12/23] feat: asset value from i128 --- .../bin/src/service/api/cardano/staking/assets_get.rs | 4 ++-- .../bin/src/service/common/types/cardano/asset_value.rs | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index e2e1a1395ed..9c4421273cf 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -27,7 +27,7 @@ use crate::{ stake_info::{FullStakeInfo, StakeInfo, StakedNativeTokenInfo}, }, responses::WithErrorResponses, - types::cardano::address::Cip19StakeAddress, + types::cardano::{address::Cip19StakeAddress, asset_value::AssetValue}, }, }; @@ -282,7 +282,7 @@ fn build_stake_info( stake_info.native_tokens.push(StakedNativeTokenInfo { policy_hash: asset.id.try_into()?, asset_name: asset.name.into(), - amount: num_bigint::BigInt::from(asset.amount).try_into()?, + amount: AssetValue::from(asset.amount), }); } diff --git a/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs b/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs index fc858682df1..67644287b6f 100644 --- a/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs +++ b/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs @@ -104,6 +104,12 @@ impl TryFrom for AssetValue { } } +impl From for AssetValue { + fn from(value: i128) -> Self { + Self(value) + } +} + impl Example for AssetValue { fn example() -> Self { Self(EXAMPLE) From cad90d2a5f6794cd75b5452074ff95cc37b5d95d Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 25 Nov 2024 23:23:27 +0700 Subject: [PATCH 13/23] refactor: change &[u8] for asset name --- .../bin/src/db/index/block/txo/insert_txo_asset.rs | 4 ++-- .../bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs | 4 ++-- catalyst-gateway/bin/src/db/index/block/txo/mod.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs index 0e38100cb58..dc802c5bc78 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs @@ -40,7 +40,7 @@ impl Params { /// values. #[allow(clippy::too_many_arguments)] pub(super) fn new( - stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, policy_id: &[u8], asset_name: &str, + stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, policy_id: &[u8], asset_name: &[u8], value: i128, ) -> Self { Self { @@ -49,7 +49,7 @@ impl Params { txn, txo, policy_id: policy_id.to_vec(), - asset_name: asset_name.as_bytes().to_vec(), + asset_name: asset_name.to_vec(), value: value.into(), } } diff --git a/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs b/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs index 02c373ef39e..7436d36d200 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/insert_unstaked_txo_asset.rs @@ -40,14 +40,14 @@ impl Params { /// values. #[allow(clippy::too_many_arguments)] pub(super) fn new( - txn_hash: &[u8], txo: i16, policy_id: &[u8], asset_name: &str, slot_no: u64, txn: i16, + txn_hash: &[u8], txo: i16, policy_id: &[u8], asset_name: &[u8], slot_no: u64, txn: i16, value: i128, ) -> Self { Self { txn_hash: txn_hash.to_vec(), txo, policy_id: policy_id.to_vec(), - asset_name: asset_name.as_bytes().to_vec(), + asset_name: asset_name.to_vec(), slot_no: slot_no.into(), txn, value: value.into(), diff --git a/catalyst-gateway/bin/src/db/index/block/txo/mod.rs b/catalyst-gateway/bin/src/db/index/block/txo/mod.rs index 0ccf5ec7cef..04058003a2d 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/mod.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/mod.rs @@ -181,7 +181,7 @@ impl TxoInsertQuery { let policy_id = asset.policy().to_vec(); for policy_asset in asset.assets() { if policy_asset.is_output() { - let asset_name = policy_asset.to_ascii_name().unwrap_or_default(); + let asset_name = policy_asset.name(); let value = policy_asset.any_coin(); if staked { @@ -191,7 +191,7 @@ impl TxoInsertQuery { txn, txo_index, &policy_id, - &asset_name, + asset_name, value, ); self.staked_txo_asset.push(params); @@ -200,7 +200,7 @@ impl TxoInsertQuery { txn_hash, txo_index, &policy_id, - &asset_name, + asset_name, slot_no, txn, value, From 2e6c4e6e550e2050e33bbbe35fdff45fd65d4ef6 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 25 Nov 2024 23:36:14 +0700 Subject: [PATCH 14/23] refactor: try from asset value --- .../src/db/index/block/txo/insert_txo_asset.rs | 4 ++-- .../bin/src/db/index/block/txo/mod.rs | 8 +------- .../service/api/cardano/staking/assets_get.rs | 18 +++++++++++------- .../common/types/cardano/asset_value.rs | 11 ++++++++--- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs index dc802c5bc78..59d199a6338 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs @@ -40,8 +40,8 @@ impl Params { /// values. #[allow(clippy::too_many_arguments)] pub(super) fn new( - stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, policy_id: &[u8], asset_name: &[u8], - value: i128, + stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, policy_id: &[u8], + asset_name: &[u8], value: i128, ) -> Self { Self { stake_address: stake_address.to_vec(), diff --git a/catalyst-gateway/bin/src/db/index/block/txo/mod.rs b/catalyst-gateway/bin/src/db/index/block/txo/mod.rs index 04058003a2d..f3f08440e60 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/mod.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/mod.rs @@ -197,13 +197,7 @@ impl TxoInsertQuery { self.staked_txo_asset.push(params); } else { let params = insert_unstaked_txo_asset::Params::new( - txn_hash, - txo_index, - &policy_id, - asset_name, - slot_no, - txn, - value, + txn_hash, txo_index, &policy_id, asset_name, slot_no, txn, value, ); self.unstaked_txo_asset.push(params); } diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index 9c4421273cf..c54e9fe8504 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -27,7 +27,7 @@ use crate::{ stake_info::{FullStakeInfo, StakeInfo, StakedNativeTokenInfo}, }, responses::WithErrorResponses, - types::cardano::{address::Cip19StakeAddress, asset_value::AssetValue}, + types::cardano::{address::Cip19StakeAddress, asset_name::AssetName}, }, }; @@ -79,7 +79,7 @@ struct TxoAssetInfo { /// Asset hash. id: Vec, /// Asset name. - name: Vec, + name: AssetName, /// Asset amount. amount: i128, } @@ -175,6 +175,10 @@ async fn get_txo_by_txn( while let Some(row_res) = assets_txos_iter.next().await { let row = row_res?; + + let Some(row_val) = row.value.to_i128() else { + anyhow::bail!("Failed to convert bigint to i128"); + }; let txo_info_key = (row.slot_no.clone(), row.txn, row.txo); let Some(txo_info) = txo_map.get_mut(&txo_info_key) else { @@ -187,12 +191,12 @@ async fn get_txo_by_txn( .or_insert_with(Vec::new); match entry.iter_mut().find(|item| item.id == row.policy_id) { - Some(item) => item.amount += row.value.to_i128().unwrap_or(0), + Some(item) => item.amount += row_val, None => { entry.push(TxoAssetInfo { id: row.policy_id, - name: row.asset_name, - amount: row.value.to_i128().unwrap_or(0), + name: row.asset_name.into(), + amount: row_val, }); }, } @@ -281,8 +285,8 @@ fn build_stake_info( for asset in txo_info.assets.into_values().flatten() { stake_info.native_tokens.push(StakedNativeTokenInfo { policy_hash: asset.id.try_into()?, - asset_name: asset.name.into(), - amount: AssetValue::from(asset.amount), + asset_name: asset.name, + amount: asset.amount.try_into()?, }); } diff --git a/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs b/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs index 67644287b6f..12ad0d62f55 100644 --- a/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs +++ b/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs @@ -104,9 +104,14 @@ impl TryFrom for AssetValue { } } -impl From for AssetValue { - fn from(value: i128) -> Self { - Self(value) +impl TryFrom for AssetValue { + type Error = anyhow::Error; + + fn try_from(value: i128) -> Result { + if !is_valid(value) { + bail!("Invalid Asset Value"); + } + Ok(Self(value)) } } From 85fde9ed95d51e090e10ecc0ecface8319803435 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 25 Nov 2024 23:43:15 +0700 Subject: [PATCH 15/23] fix: import --- .../bin/src/service/api/cardano/staking/assets_get.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index 11d0a4d4c92..196739cad63 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -28,7 +28,7 @@ use crate::{ stake_info::{FullStakeInfo, StakeInfo, StakedNativeTokenInfo}, }, responses::WithErrorResponses, - types::cardano::{address::Cip19StakeAddress, asset_name::AssetName}, + types::cardano::{cip19_stake_address::Cip19StakeAddress, asset_name::AssetName}, }, }; From 4c85eb35de58530daeb1b1f337c0107730766480 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 25 Nov 2024 23:44:05 +0700 Subject: [PATCH 16/23] chore: fmtfix --- .../bin/src/service/api/cardano/staking/assets_get.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index 196739cad63..2ac6a7b96a1 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -28,7 +28,7 @@ use crate::{ stake_info::{FullStakeInfo, StakeInfo, StakedNativeTokenInfo}, }, responses::WithErrorResponses, - types::cardano::{cip19_stake_address::Cip19StakeAddress, asset_name::AssetName}, + types::cardano::{asset_name::AssetName, cip19_stake_address::Cip19StakeAddress}, }, }; @@ -176,7 +176,7 @@ async fn get_txo_by_txn( while let Some(row_res) = assets_txos_iter.next().await { let row = row_res?; - + let Some(row_val) = row.value.to_i128() else { anyhow::bail!("Failed to convert bigint to i128"); }; From b96018b94c69b22fbbb85c2c1bb9d414596a32ad Mon Sep 17 00:00:00 2001 From: Apisit Ritruengroj <38898766+apskhem@users.noreply.github.com> Date: Tue, 26 Nov 2024 14:49:46 +0700 Subject: [PATCH 17/23] Update catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs Co-authored-by: Steven Johnson --- catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs index 59d199a6338..69e547594a7 100644 --- a/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs +++ b/catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs @@ -27,7 +27,7 @@ pub(super) struct Params { txo: i16, /// Policy hash of the asset policy_id: Vec, - /// Policy name of the asset + /// Name of the asset, within the Policy. asset_name: Vec, /// Value of the asset value: num_bigint::BigInt, From c3982b91b385e641441fb845bfd71cdd2c785592 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 26 Nov 2024 14:53:28 +0700 Subject: [PATCH 18/23] revert: i128 to bigint --- .../bin/src/service/api/cardano/staking/assets_get.rs | 10 +++------- .../src/service/common/types/cardano/asset_value.rs | 11 ----------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index 2ac6a7b96a1..f5368d79525 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -82,7 +82,7 @@ struct TxoAssetInfo { /// Asset name. name: AssetName, /// Asset amount. - amount: i128, + amount: num_bigint::BigInt, } /// TXO information used when calculating a user's stake info. @@ -177,10 +177,6 @@ async fn get_txo_by_txn( while let Some(row_res) = assets_txos_iter.next().await { let row = row_res?; - let Some(row_val) = row.value.to_i128() else { - anyhow::bail!("Failed to convert bigint to i128"); - }; - let txo_info_key = (row.slot_no.clone(), row.txn, row.txo); let Some(txo_info) = txo_map.get_mut(&txo_info_key) else { continue; @@ -192,12 +188,12 @@ async fn get_txo_by_txn( .or_insert_with(Vec::new); match entry.iter_mut().find(|item| item.id == row.policy_id) { - Some(item) => item.amount += row_val, + Some(item) => item.amount += row.value, None => { entry.push(TxoAssetInfo { id: row.policy_id, name: row.asset_name.into(), - amount: row_val, + amount: row.value, }); }, } diff --git a/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs b/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs index 48afaefa7c6..2fcf64a82fb 100644 --- a/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs +++ b/catalyst-gateway/bin/src/service/common/types/cardano/asset_value.rs @@ -106,17 +106,6 @@ impl TryFrom for AssetValue { } } -impl TryFrom for AssetValue { - type Error = anyhow::Error; - - fn try_from(value: i128) -> Result { - if !is_valid(value) { - bail!("Invalid Asset Value"); - } - Ok(Self(value)) - } -} - impl Example for AssetValue { fn example() -> Self { Self(EXAMPLE) From 95b0a4ba3c8412ba33b1bb6a25f8b4e9fa43178b Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 26 Nov 2024 16:02:00 +0700 Subject: [PATCH 19/23] fix: unused import --- .../bin/src/service/api/cardano/staking/assets_get.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs index f5368d79525..f92fa93d5d9 100644 --- a/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs +++ b/catalyst-gateway/bin/src/service/api/cardano/staking/assets_get.rs @@ -3,7 +3,6 @@ use std::collections::HashMap; use anyhow::anyhow; use futures::StreamExt; -use num_traits::ToPrimitive; use pallas::ledger::addresses::StakeAddress; use poem_openapi::{payload::Json, ApiResponse}; From 6f6e92963a1ad7d0c3b417565d89c95372b5f93e Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Fri, 29 Nov 2024 15:38:13 +0700 Subject: [PATCH 20/23] feat: api test --- .../types/cardano/cip19_stake_address.rs | 31 ++++++++++++++++++- .../api_tests/get_cardano_assets.hurl | 15 +++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 catalyst-gateway/tests/api_tests/api_tests/get_cardano_assets.hurl diff --git a/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs b/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs index 3ae12fbc3b1..2b8df4582f3 100644 --- a/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs +++ b/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs @@ -42,7 +42,7 @@ pub(crate) const PATTERN: &str = concatcp!( /// Length of the encoded address. const ENCODED_ADDR_LEN: usize = 53; /// Length of the decoded address. -const DECODED_ADDR_LEN: usize = 28; +const DECODED_ADDR_LEN: usize = 29; /// Minimum length pub(crate) const MIN_LENGTH: usize = PROD_STAKE.len() + 1 + ENCODED_ADDR_LEN; /// Minimum length @@ -150,3 +150,32 @@ impl Example for Cip19StakeAddress { Self(EXAMPLE.to_owned()) } } + +#[cfg(test)] +mod tests { + use super::*; + + const VALID_PROD_STAKE_ADDRESS: &str = + "stake1u94ullc9nj9gawc08990nx8hwgw80l9zpmr8re44kydqy9cdjq6rq"; + const VALID_TEST_STAKE_ADDRESS: &str = + "stake_test1uqehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gssrtvn"; + const INVALID_STAKE_ADDRESS: &str = + "invalid1u9nlq5nmuzthw3vhgakfpxyq4r0zl2c0p8uqy24gpyjsa6c3df4h6"; + + #[test] + fn test_valid_stake_address_from_string() { + let stake_address_prod = Cip19StakeAddress::try_from(VALID_PROD_STAKE_ADDRESS.to_string()); + let stake_address_test = Cip19StakeAddress::try_from(VALID_TEST_STAKE_ADDRESS.to_string()); + + assert!(stake_address_prod.is_ok()); + assert!(stake_address_test.is_ok()); + assert_eq!(stake_address_prod.unwrap().0, VALID_PROD_STAKE_ADDRESS); + assert_eq!(stake_address_test.unwrap().0, VALID_TEST_STAKE_ADDRESS); + } + + #[test] + fn test_invalid_stake_address_from_string() { + let stake_address = Cip19StakeAddress::try_from(INVALID_STAKE_ADDRESS.to_string()); + assert!(stake_address.is_err()); + } +} diff --git a/catalyst-gateway/tests/api_tests/api_tests/get_cardano_assets.hurl b/catalyst-gateway/tests/api_tests/api_tests/get_cardano_assets.hurl new file mode 100644 index 00000000000..92bfc53e2ef --- /dev/null +++ b/catalyst-gateway/tests/api_tests/api_tests/get_cardano_assets.hurl @@ -0,0 +1,15 @@ +# Get staked ADA amount: zero assets +GET http://localhost:3030/api/draft/cardano/assets/stake_test1ursne3ndzr4kz8gmhmstu5026erayrnqyj46nqkkfcn0ufss2t7vt +HTTP 200 +{"persistent":{"ada_amount":9809147618,"native_tokens":[],"slot_number":76323283},"volatile":{"ada_amount":0,"native_tokens":[],"slot_number":0}} + +# Get staked ADA amount: single asset +GET http://localhost:3030/api/draft/cardano/assets/stake_test1uq7cnze6az9f8ffjrvkxx4ad77jz088frkhzupxcc7y4x8q5x808s +HTTP 200 +{"persistent":{"ada_amount":8870859858,"native_tokens":[{"amount":3,"asset_name":"GoldRelic","policy_hash":"0x2862c9b33e98096107e2d8b8c072070834db9c91c0d2f3743e75df65"}],"slot_number":76572358},"volatile":{"ada_amount":0,"native_tokens":[],"slot_number":0}} + +# Get staked ADA amount: multiple assets +GET http://localhost:3030/api/draft/cardano/assets/stake_test1ur66dds0pkf3j5tu7py9tqf7savpv7pgc5g3dd74xy0x2vsldf2mx +HTTP 200 +[Asserts] +jsonpath "$.persistent.native_tokens" count == 9 \ No newline at end of file From 7371c5bea4200081d57df7a67ee0ffcbd7526652 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Fri, 29 Nov 2024 15:48:08 +0700 Subject: [PATCH 21/23] chore: cspell fix --- .../bin/src/service/common/types/cardano/cip19_stake_address.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs b/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs index 2b8df4582f3..c3644d2931c 100644 --- a/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs +++ b/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs @@ -155,12 +155,14 @@ impl Example for Cip19StakeAddress { mod tests { use super::*; + /* cSpell:disable */ const VALID_PROD_STAKE_ADDRESS: &str = "stake1u94ullc9nj9gawc08990nx8hwgw80l9zpmr8re44kydqy9cdjq6rq"; const VALID_TEST_STAKE_ADDRESS: &str = "stake_test1uqehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gssrtvn"; const INVALID_STAKE_ADDRESS: &str = "invalid1u9nlq5nmuzthw3vhgakfpxyq4r0zl2c0p8uqy24gpyjsa6c3df4h6"; + /* cSpell:enable */ #[test] fn test_valid_stake_address_from_string() { From 45fae68d3d27daab8147aeb9353d82cf43cff820 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Fri, 29 Nov 2024 15:48:48 +0700 Subject: [PATCH 22/23] chore: cspell fix --- .../src/service/common/types/cardano/cip19_stake_address.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs b/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs index c3644d2931c..f0c869a9878 100644 --- a/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs +++ b/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs @@ -155,14 +155,14 @@ impl Example for Cip19StakeAddress { mod tests { use super::*; - /* cSpell:disable */ + /* cspell: disable */ const VALID_PROD_STAKE_ADDRESS: &str = "stake1u94ullc9nj9gawc08990nx8hwgw80l9zpmr8re44kydqy9cdjq6rq"; const VALID_TEST_STAKE_ADDRESS: &str = "stake_test1uqehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gssrtvn"; const INVALID_STAKE_ADDRESS: &str = "invalid1u9nlq5nmuzthw3vhgakfpxyq4r0zl2c0p8uqy24gpyjsa6c3df4h6"; - /* cSpell:enable */ + /* cspell: enable */ #[test] fn test_valid_stake_address_from_string() { From 1dcdd067bae4698f91763f82e1c6e8bcf42eda42 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Fri, 29 Nov 2024 15:51:04 +0700 Subject: [PATCH 23/23] chore: fmtfix --- .../src/service/common/types/cardano/cip19_stake_address.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs b/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs index f0c869a9878..83f01951003 100644 --- a/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs +++ b/catalyst-gateway/bin/src/service/common/types/cardano/cip19_stake_address.rs @@ -155,14 +155,14 @@ impl Example for Cip19StakeAddress { mod tests { use super::*; - /* cspell: disable */ + // cspell: disable const VALID_PROD_STAKE_ADDRESS: &str = "stake1u94ullc9nj9gawc08990nx8hwgw80l9zpmr8re44kydqy9cdjq6rq"; const VALID_TEST_STAKE_ADDRESS: &str = "stake_test1uqehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gssrtvn"; const INVALID_STAKE_ADDRESS: &str = "invalid1u9nlq5nmuzthw3vhgakfpxyq4r0zl2c0p8uqy24gpyjsa6c3df4h6"; - /* cspell: enable */ + // cspell: enable #[test] fn test_valid_stake_address_from_string() {