Skip to content

Commit

Permalink
feat: bonds start date (#64)
Browse files Browse the repository at this point in the history
* feat: bonds start date

* feat: amounts as numeric new endpoint for merged bonds

* chore: swagger codegen bump
  • Loading branch information
mateuszjasiuk authored Jun 21, 2024
1 parent a01b5c5 commit 5c84c52
Show file tree
Hide file tree
Showing 34 changed files with 308 additions and 57 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ deadpool-diesel = { version = "0.5.0", features = ["postgres"] }
diesel = { version = "2.1.0", features = [
"postgres",
"serde_json",
"numeric"
] }
diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
orm = { path = "orm" }
Expand All @@ -80,3 +81,4 @@ clap-verbosity-flag = "2.1.1"
duration-str = "0.7.1"
fake = { version = "2.9.2", features = ["derive"] }
rand = "0.8.5"
bigdecimal = "0.4.5"
6 changes: 5 additions & 1 deletion chain/src/repository/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ pub fn insert_bonds(
})
.collect::<Vec<_>>(),
)
.on_conflict((bonds::columns::validator_id, bonds::columns::address))
.on_conflict((
bonds::columns::validator_id,
bonds::columns::address,
bonds::columns::start,
))
.do_update()
.set((
bonds::columns::raw_amount.eq(excluded(bonds::columns::raw_amount)),
Expand Down
11 changes: 8 additions & 3 deletions chain/src/services/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ pub async fn query_all_bonds_and_unbonds(
type Source = NamadaSdkAddress;
type Validator = NamadaSdkAddress;
type WithdrawEpoch = NamadaSdkEpoch;
type StartEpoch = NamadaSdkEpoch;

type BondKey = (Source, Validator);
type BondKey = (Source, Validator, StartEpoch);
type BondsMap = HashMap<BondKey, NamadaSdkAmount>;

type UnbondKey = (Source, Validator, WithdrawEpoch);
Expand All @@ -184,7 +185,7 @@ pub async fn query_all_bonds_and_unbonds(
for (bond_id, details) in bonds_and_unbonds {
for bd in details.bonds {
let id = bond_id.clone();
let key = (id.source, id.validator);
let key = (id.source, id.validator, bd.start);

if let Some(record) = bonds.get_mut(&key) {
*record = record.checked_add(bd.amount).unwrap();
Expand All @@ -205,13 +206,16 @@ pub async fn query_all_bonds_and_unbonds(
}
}

// TODO: we can iter in parallel

// Map the types, mostly because we can't add indexer amounts
let bonds = bonds
.into_iter()
.map(|((source, target), amount)| Bond {
.map(|((source, target, start), amount)| Bond {
source: Id::from(source),
target: Id::from(target),
amount: Amount::from(amount),
start: start.0 as Epoch,
})
.collect();

Expand Down Expand Up @@ -348,6 +352,7 @@ pub async fn query_bonds(
source: Id::from(source),
target: Id::from(target),
amount: Amount::from(amount),
start: epoch + pipeline_length,
})
}
})
Expand Down
1 change: 1 addition & 0 deletions orm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ diesel.workspace = true
diesel_migrations.workspace = true
serde.workspace = true
shared.workspace = true
bigdecimal.workspace = true
2 changes: 1 addition & 1 deletion orm/migrations/2024-04-18-102935_init_balances/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE TABLE balances (
id SERIAL PRIMARY KEY,
owner VARCHAR NOT NULL,
token VARCHAR NOT NULL,
raw_amount VARCHAR NOT NULL
raw_amount NUMERIC(78) NOT NULL
);

ALTER TABLE balances ADD UNIQUE (owner, token);
Expand Down
7 changes: 4 additions & 3 deletions orm/migrations/2024-05-08-110347_init_bonds/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ CREATE TABLE bonds (
id SERIAL PRIMARY KEY,
address VARCHAR NOT NULL,
validator_id INT NOT NULL,
raw_amount VARCHAR NOT NULL,
raw_amount NUMERIC(78) NOT NULL,
start INT NOT NULL,
CONSTRAINT fk_validator_id FOREIGN KEY(validator_id) REFERENCES validators(id) ON DELETE CASCADE
);

ALTER TABLE bonds ADD UNIQUE (address, validator_id);
ALTER TABLE bonds ADD UNIQUE (address, validator_id, start);

CREATE INDEX index_bonds_owner ON bonds USING HASH (address);
CREATE INDEX index_bonds_owner ON bonds USING HASH (address);
2 changes: 1 addition & 1 deletion orm/migrations/2024-05-09-042930_init_unbonds/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE TABLE unbonds (
id SERIAL PRIMARY KEY,
address VARCHAR NOT NULL,
validator_id INT NOT NULL,
raw_amount VARCHAR NOT NULL,
raw_amount NUMERIC(78) NOT NULL,
withdraw_epoch INT NOT NULL,
CONSTRAINT fk_validator_id FOREIGN KEY(validator_id) REFERENCES validators(id) ON DELETE CASCADE
);
Expand Down
4 changes: 2 additions & 2 deletions orm/migrations/2024-05-09-131859_pos_rewards/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ CREATE TABLE pos_rewards (
id SERIAL PRIMARY KEY,
owner VARCHAR NOT NULL,
validator_id INT NOT NULL,
raw_amount VARCHAR NOT NULL,
raw_amount NUMERIC(78) NOT NULL,
CONSTRAINT fk_validator_id FOREIGN KEY(validator_id) REFERENCES validators(id) ON DELETE CASCADE
);

ALTER TABLE pos_rewards ADD UNIQUE (owner, validator_id);

CREATE INDEX index_pos_rewards_owner ON pos_rewards USING HASH (owner);
CREATE INDEX index_pos_rewards_owner ON pos_rewards USING HASH (owner);
2 changes: 1 addition & 1 deletion orm/migrations/2024-06-14-110621_gas_price/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

CREATE TABLE gas_price (
token VARCHAR PRIMARY KEY NOT NULL,
amount VARCHAR NOT NULL
amount NUMERIC(78) NOT NULL
);


7 changes: 5 additions & 2 deletions orm/src/balances.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bigdecimal::BigDecimal;
use diesel::{Insertable, Queryable, Selectable};
use shared::balance::Balance;
use std::str::FromStr;

use crate::schema::balances;

Expand All @@ -9,7 +11,7 @@ use crate::schema::balances;
pub struct BalancesInsertDb {
pub owner: String,
pub token: String,
pub raw_amount: String,
pub raw_amount: BigDecimal,
}

pub type BalanceDb = BalancesInsertDb;
Expand All @@ -19,7 +21,8 @@ impl BalancesInsertDb {
Self {
owner: balance.owner.to_string(),
token: balance.token.to_string(),
raw_amount: balance.amount.to_string(),
raw_amount: BigDecimal::from_str(&balance.amount.to_string())
.expect("Invalid amount"),
}
}
}
15 changes: 11 additions & 4 deletions orm/src/bond.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::str::FromStr;

use bigdecimal::BigDecimal;
use diesel::associations::Associations;
use diesel::{Identifiable, Insertable, Queryable, Selectable};
use shared::bond::Bond;
Expand All @@ -11,26 +14,30 @@ use crate::validators::ValidatorDb;
pub struct BondInsertDb {
pub address: String,
pub validator_id: i32,
pub raw_amount: String,
pub raw_amount: BigDecimal,
pub start: i32,
}

#[derive(Identifiable, Clone, Queryable, Selectable, Associations)]
#[derive(Identifiable, Clone, Queryable, Selectable, Associations, Debug)]
#[diesel(table_name = bonds)]
#[diesel(check_for_backend(diesel::pg::Pg))]
#[diesel(belongs_to(ValidatorDb, foreign_key = validator_id))]
pub struct BondDb {
pub id: i32,
pub address: String,
pub validator_id: i32,
pub raw_amount: String,
pub raw_amount: BigDecimal,
pub start: i32,
}

impl BondInsertDb {
pub fn from_bond(bond: Bond, validator_id: i32) -> Self {
Self {
address: bond.source.to_string(),
validator_id,
raw_amount: bond.amount.to_string(),
raw_amount: BigDecimal::from_str(&bond.amount.to_string())
.expect("Invalid amount"),
start: bond.start as i32,
}
}
}
8 changes: 6 additions & 2 deletions orm/src/gas.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::str::FromStr;

use bigdecimal::BigDecimal;
use diesel::{Insertable, Queryable, Selectable};
use shared::gas::GasPrice;

Expand All @@ -18,14 +21,15 @@ pub struct GasDb {
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct GasPriceDb {
pub token: String,
pub amount: String,
pub amount: BigDecimal,
}

impl From<GasPrice> for GasPriceDb {
fn from(value: GasPrice) -> Self {
Self {
token: value.token,
amount: value.amount,
amount: BigDecimal::from_str(&value.amount.to_string())
.expect("Invalid amount"),
}
}
}
18 changes: 18 additions & 0 deletions orm/src/group_by_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::schema::{bonds, validators};
use diesel::allow_columns_to_appear_in_same_group_by_clause;

allow_columns_to_appear_in_same_group_by_clause!(
bonds::address,
validators::id,
validators::namada_address,
validators::voting_power,
validators::max_commission,
validators::commission,
validators::name,
validators::email,
validators::website,
validators::description,
validators::discord_handle,
validators::avatar,
validators::state,
);
1 change: 1 addition & 0 deletions orm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod epoch_crawler_state;
pub mod gas;
pub mod governance_proposal;
pub mod governance_votes;
pub mod group_by_macros;
pub mod migrations;
pub mod parameters;
pub mod pos_rewards;
Expand Down
8 changes: 6 additions & 2 deletions orm/src/pos_rewards.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::str::FromStr;

use bigdecimal::BigDecimal;
use diesel::{Insertable, Queryable, Selectable};
use shared::rewards::Reward;

Expand All @@ -9,7 +12,7 @@ use crate::schema::pos_rewards;
pub struct PosRewardInsertDb {
pub owner: String,
pub validator_id: i32,
pub raw_amount: String,
pub raw_amount: BigDecimal,
}

pub type PoSRewardDb = PosRewardInsertDb;
Expand All @@ -18,7 +21,8 @@ impl PosRewardInsertDb {
pub fn from_reward(reward: Reward, validator_id: i32) -> Self {
Self {
owner: reward.delegation_pair.delegator_address.to_string(),
raw_amount: reward.amount.to_string(),
raw_amount: BigDecimal::from_str(&reward.amount.to_string())
.expect("Invalid amount"),
validator_id,
}
}
Expand Down
11 changes: 6 additions & 5 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ diesel::table! {
id -> Int4,
owner -> Varchar,
token -> Varchar,
raw_amount -> Varchar,
raw_amount -> Numeric,
}
}

Expand All @@ -53,7 +53,8 @@ diesel::table! {
id -> Int4,
address -> Varchar,
validator_id -> Int4,
raw_amount -> Varchar,
raw_amount -> Numeric,
start -> Int4,
}
}

Expand Down Expand Up @@ -94,7 +95,7 @@ diesel::table! {
diesel::table! {
gas_price (token) {
token -> Varchar,
amount -> Varchar,
amount -> Numeric,
}
}

Expand Down Expand Up @@ -155,7 +156,7 @@ diesel::table! {
id -> Int4,
owner -> Varchar,
validator_id -> Int4,
raw_amount -> Varchar,
raw_amount -> Numeric,
}
}

Expand All @@ -172,7 +173,7 @@ diesel::table! {
id -> Int4,
address -> Varchar,
validator_id -> Int4,
raw_amount -> Varchar,
raw_amount -> Numeric,
withdraw_epoch -> Int4,
}
}
Expand Down
10 changes: 7 additions & 3 deletions orm/src/unbond.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::str::FromStr;

use bigdecimal::BigDecimal;
use diesel::associations::Associations;
use diesel::{Identifiable, Insertable, Queryable, Selectable};
use shared::unbond::Unbond;
Expand All @@ -11,7 +14,7 @@ use crate::validators::ValidatorDb;
pub struct UnbondInsertDb {
pub address: String,
pub validator_id: i32,
pub raw_amount: String,
pub raw_amount: BigDecimal,
pub withdraw_epoch: i32,
}

Expand All @@ -23,7 +26,7 @@ pub struct UnbondDb {
pub id: i32,
pub address: String,
pub validator_id: i32,
pub raw_amount: String,
pub raw_amount: BigDecimal,
pub withdraw_epoch: i32,
}

Expand All @@ -32,7 +35,8 @@ impl UnbondInsertDb {
Self {
address: unbond.source.to_string(),
validator_id,
raw_amount: unbond.amount.to_string(),
raw_amount: BigDecimal::from_str(&unbond.amount.to_string())
.expect("Invalid amount"),
withdraw_epoch: unbond.withdraw_at as i32,
}
}
Expand Down
6 changes: 4 additions & 2 deletions orm/src/validators.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::str::FromStr;

use diesel::{AsChangeset, Insertable, Queryable, Selectable};
use diesel::{
sql_types::Nullable, AsChangeset, Insertable, Queryable, Selectable,
};
use serde::{Deserialize, Serialize};
use shared::validator::{Validator, ValidatorState};

Expand Down Expand Up @@ -30,7 +32,7 @@ impl From<ValidatorState> for ValidatorStateDb {
}
}

#[derive(Serialize, Queryable, Selectable, Clone)]
#[derive(Serialize, Queryable, Selectable, Clone, Debug)]
#[diesel(table_name = validators)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct ValidatorDb {
Expand Down
3 changes: 2 additions & 1 deletion parameters/src/services/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use namada_sdk::rpc::{
self, get_token_total_supply, get_total_staked_tokens, query_storage_value,
};
use namada_sdk::token::Amount as NamadaSdkAmount;
use shared::balance::Amount;
use shared::block::Epoch;
use shared::gas::GasPrice;
use shared::parameters::Parameters;
Expand Down Expand Up @@ -83,7 +84,7 @@ pub async fn get_gas_price(client: &HttpClient) -> Vec<GasPrice> {
for (token, gas_cost) in gas_cost_table {
gas_table.push(GasPrice {
token: token.to_string(),
amount: gas_cost.to_string_native(),
amount: Amount::from(gas_cost),
})
}

Expand Down
Loading

0 comments on commit 5c84c52

Please sign in to comment.