Skip to content

Commit 0242b67

Browse files
authored
Merge pull request #424 from namada-net/refactor-v3
refactor shared structs, api responses
2 parents d273fc1 + 41e6a6d commit 0242b67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2811
-1246
lines changed

chain/src/config.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
use core::fmt;
2-
use std::fmt::Display;
3-
41
use shared::log_config::LogConfig;
52

6-
#[derive(clap::ValueEnum, Clone, Debug, Copy)]
7-
pub enum CargoEnv {
8-
Development,
9-
Production,
10-
}
11-
12-
impl Display for CargoEnv {
13-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14-
write!(f, "{:?}", self)
15-
}
16-
}
17-
183
#[derive(clap::Parser)]
194
pub struct AppConfig {
205
#[clap(long, env)]

chain/src/main.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ async fn crawling_fn(
302302
})
303303
});
304304

305+
let masp_reward_rates = if new_epoch {
306+
namada_service::get_masp_rates(&client)
307+
.await
308+
.into_rpc_error()?
309+
} else {
310+
vec![]
311+
};
312+
305313
tracing::info!(
306314
block = block_height,
307315
txs = block.transactions.len(),
@@ -319,7 +327,8 @@ async fn crawling_fn(
319327
namada_service::query_native_addresses_balance_change(Token::Native(
320328
native_token.clone(),
321329
));
322-
let addresses = block.addresses_with_balance_change(&native_token);
330+
let addresses =
331+
block.addresses_with_balance_change(&native_token, &ibc_tokens);
323332
let all_changed_tokens_supply = addresses
324333
.iter()
325334
.map(|bc| bc.token.clone())
@@ -597,6 +606,11 @@ async fn crawling_fn(
597606
revealed_pks,
598607
)?;
599608

609+
repository::masp::insert_masp_rates(
610+
transaction_conn,
611+
masp_reward_rates,
612+
)?;
613+
600614
if should_update_crawler_state {
601615
repository::crawler_state::upsert_crawler_state(
602616
transaction_conn,
@@ -742,6 +756,10 @@ async fn try_initial_query(
742756
.await
743757
.into_rpc_error()?;
744758

759+
let masp_reward_rates = namada_service::get_masp_rates(client)
760+
.await
761+
.into_rpc_error()?;
762+
745763
let timestamp = DateTimeUtc::now().0.timestamp();
746764

747765
let crawler_state = ChainCrawlerState {
@@ -807,6 +825,11 @@ async fn try_initial_query(
807825
redelegations,
808826
)?;
809827

828+
repository::masp::insert_masp_rates(
829+
transaction_conn,
830+
masp_reward_rates,
831+
)?;
832+
810833
repository::crawler_state::upsert_crawler_state(
811834
transaction_conn,
812835
crawler_state,

chain/src/repository/masp.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use anyhow::Context;
2+
use diesel::upsert::excluded;
3+
use diesel::{ExpressionMethods, PgConnection, RunQueryDsl};
4+
use orm::masp::MaspRewardDataInsertDb;
5+
use orm::schema::masp_rates;
6+
use shared::masp::MaspRewardData;
7+
8+
pub fn insert_masp_rates(
9+
transaction_conn: &mut PgConnection,
10+
masp_reward_data: Vec<MaspRewardData>,
11+
) -> anyhow::Result<()> {
12+
diesel::insert_into(masp_rates::table)
13+
.values(
14+
masp_reward_data
15+
.into_iter()
16+
.map(MaspRewardDataInsertDb::from)
17+
.collect::<Vec<_>>(),
18+
)
19+
.on_conflict(masp_rates::columns::token)
20+
.do_update()
21+
.set((
22+
masp_rates::columns::max_reward_rate
23+
.eq(excluded(masp_rates::columns::max_reward_rate)),
24+
masp_rates::columns::kp_gain
25+
.eq(excluded(masp_rates::columns::kp_gain)),
26+
masp_rates::columns::kd_gain
27+
.eq(excluded(masp_rates::columns::kd_gain)),
28+
masp_rates::columns::locked_amount_target
29+
.eq(excluded(masp_rates::columns::locked_amount_target)),
30+
))
31+
.execute(transaction_conn)
32+
.context("Failed to update masp rates in db")?;
33+
34+
Ok(())
35+
}

chain/src/repository/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod block;
33
pub mod cometbft;
44
pub mod crawler_state;
55
pub mod gov;
6+
pub mod masp;
67
pub mod pgf;
78
pub mod pos;
89
pub mod revealed_pk;

chain/src/services/namada.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use shared::balance::{Amount, Balance, Balances, TokenSupply};
2424
use shared::block::{BlockHeight, Epoch};
2525
use shared::checksums::Checksums;
2626
use shared::id::Id;
27+
use shared::masp::MaspRewardData;
2728
use shared::pos::{
2829
Bond, BondAddresses, Bonds, Redelegation, Unbond, UnbondAddresses, Unbonds,
2930
};
@@ -1106,6 +1107,30 @@ pub async fn get_validator_addresses_at_epoch(
11061107
Ok(validators)
11071108
}
11081109

1110+
pub async fn get_masp_rates(
1111+
client: &HttpClient,
1112+
) -> anyhow::Result<Vec<MaspRewardData>> {
1113+
let operation = || async {
1114+
let masp_rates = rpc::query_masp_reward_tokens(client)
1115+
.await
1116+
.context("Failed to query masp reward tokens")?;
1117+
1118+
let masp_rates = masp_rates.into_iter().map(|data| MaspRewardData {
1119+
address: Id::from(data.address),
1120+
max_reward_rate: data.max_reward_rate.to_string(),
1121+
kp_gain: data.kp_gain.to_string(),
1122+
kd_gain: data.kd_gain.to_string(),
1123+
locked_amount_target: Amount::from(NamadaSdkAmount::from(
1124+
data.locked_amount_target,
1125+
)),
1126+
});
1127+
1128+
Ok(masp_rates.collect())
1129+
};
1130+
1131+
default_retry(operation).await
1132+
}
1133+
11091134
fn to_epoch(epoch: u32) -> NamadaSdkEpoch {
11101135
NamadaSdkEpoch::from(epoch as u64)
11111136
}

governance/src/config.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
use core::fmt;
2-
use std::fmt::Display;
3-
41
use shared::log_config::LogConfig;
52

6-
#[derive(clap::ValueEnum, Clone, Debug, Copy)]
7-
pub enum CargoEnv {
8-
Development,
9-
Production,
10-
}
11-
12-
impl Display for CargoEnv {
13-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14-
write!(f, "{:?}", self)
15-
}
16-
}
17-
183
#[derive(clap::Parser)]
194
pub struct AppConfig {
205
#[clap(long, env)]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- This file should undo anything in `up.sql`
2+
DROP TABLE IF EXISTS masp_rates;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Your SQL goes here
2+
CREATE TABLE masp_rates (
3+
token VARCHAR PRIMARY KEY NOT NULL,
4+
max_reward_rate VARCHAR NOT NULL,
5+
kp_gain VARCHAR NOT NULL,
6+
kd_gain VARCHAR NOT NULL,
7+
locked_amount_target NUMERIC(78, 0) NOT NULL
8+
);

orm/src/blocks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::schema::blocks;
77
#[derive(Insertable, Clone, Queryable, Selectable, Debug)]
88
#[diesel(table_name = blocks)]
99
#[diesel(check_for_backend(diesel::pg::Pg))]
10-
pub struct BlockInsertDb {
10+
pub struct BlockDb {
1111
pub height: i32,
1212
pub hash: Option<String>,
1313
pub app_hash: Option<String>,
@@ -16,7 +16,7 @@ pub struct BlockInsertDb {
1616
pub epoch: Option<i32>,
1717
}
1818

19-
pub type BlockDb = BlockInsertDb;
19+
pub type BlockInsertDb = BlockDb;
2020

2121
impl From<(Block, TendermintBlockResponse)> for BlockInsertDb {
2222
fn from(

orm/src/governance_proposal.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,30 @@ impl From<GovernanceProposalStatus> for GovernanceProposalUpdateStatusDb {
165165
}
166166
}
167167
}
168+
169+
#[derive(Serialize, Queryable, Selectable, Insertable, Clone)]
170+
#[diesel(table_name = governance_proposals)]
171+
#[diesel(check_for_backend(diesel::pg::Pg))]
172+
pub struct GovernanceProposalNoDataDb {
173+
pub id: i32,
174+
pub content: String,
175+
pub kind: GovernanceProposalKindDb,
176+
pub tally_type: GovernanceProposalTallyTypeDb,
177+
pub author: String,
178+
pub start_epoch: i32,
179+
pub end_epoch: i32,
180+
pub activation_epoch: i32,
181+
pub yay_votes: String,
182+
pub nay_votes: String,
183+
pub abstain_votes: String,
184+
pub result: GovernanceProposalResultDb,
185+
}
186+
187+
#[derive(Serialize, Queryable, Selectable, Insertable, Clone)]
188+
#[diesel(table_name = governance_proposals)]
189+
#[diesel(check_for_backend(diesel::pg::Pg))]
190+
pub struct GovernanceProposalData {
191+
pub id: i32,
192+
pub kind: GovernanceProposalKindDb,
193+
pub data: Option<String>,
194+
}

0 commit comments

Comments
 (0)