Skip to content

Commit

Permalink
feat: one native token constraint and namada bump
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Nov 12, 2024
1 parent 89eaedc commit 13b2edb
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 34 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ axum-extra = { version = "0.9.3", features = ["query"] }
chrono = { version = "0.4.30", features = ["serde"] }
async-trait = "0.1.73"
anyhow = "1.0.75"
namada_core = { git = "https://github.com/anoma/namada", tag = "v0.44.0" }
namada_sdk = { git = "https://github.com/anoma/namada", tag = "v0.44.0", default-features = false, features = ["std", "async-send", "download-params"] }
namada_tx = { git = "https://github.com/anoma/namada", tag = "v0.44.0" }
namada_governance = { git = "https://github.com/anoma/namada", tag = "v0.44.0" }
namada_ibc = { git = "https://github.com/anoma/namada", tag = "v0.44.0" }
namada_token = { git = "https://github.com/anoma/namada", tag = "v0.44.0" }
namada_parameters = { git = "https://github.com/anoma/namada", tag = "v0.44.0" }
namada_proof_of_stake = { git = "https://github.com/anoma/namada", tag = "v0.44.0" }
namada_core = { git = "https://github.com/anoma/namada", tag = "v0.45.1" }
namada_sdk = { git = "https://github.com/anoma/namada", tag = "v0.45.1", default-features = false, features = ["std", "async-send", "download-params"] }
namada_tx = { git = "https://github.com/anoma/namada", tag = "v0.45.1" }
namada_governance = { git = "https://github.com/anoma/namada", tag = "v0.45.1" }
namada_ibc = { git = "https://github.com/anoma/namada", tag = "v0.45.1" }
namada_token = { git = "https://github.com/anoma/namada", tag = "v0.45.1" }
namada_parameters = { git = "https://github.com/anoma/namada", tag = "v0.45.1" }
namada_proof_of_stake = { git = "https://github.com/anoma/namada", tag = "v0.45.1" }
tendermint = "0.38.0"
tendermint-config = "0.38.0"
tendermint-rpc = { version = "0.38.0", features = ["http-client"] }
Expand Down
39 changes: 19 additions & 20 deletions chain/src/repository/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ pub fn insert_tokens(
.filter_map(IbcTokenInsertDb::from_token)
.collect::<Vec<_>>();

// TODO: add tests for on conflict:
// - inserting token with the same address twice should NOT throw an error
// - inserting native token with different address should throw an error
diesel::insert_into(token::table)
.values(tokens_db)
.on_conflict_do_nothing()
.on_conflict(token::columns::address)
.do_nothing()
.execute(transaction_conn)
.context("Failed to update tokens in db")?;

diesel::insert_into(ibc_token::table)
.values(ibc_tokens_db)
.on_conflict_do_nothing()
.on_conflict(ibc_token::columns::address)
.do_nothing()
.execute(transaction_conn)
.context("Failed to update ibc tokens in db")?;

Expand Down Expand Up @@ -222,11 +227,8 @@ mod tests {
seed_balance(conn, vec![balance.clone()])?;

let new_amount = Amount::from(NamadaAmount::from_u64(200));
let new_token = Token::Native(Id::Account(
"tnam1q87wtaqqtlwkw927gaff34hgda36huk0kgry692a".to_string(),
));
let new_balance = Balance {
token: new_token.clone(),
token: token.clone(),
amount: new_amount.clone(),
..(balance.clone())
};
Expand All @@ -238,17 +240,7 @@ mod tests {
let queried_balance =
query_balance_by_address(conn, owner.clone(), token.clone())?;

let queried_balance_new = query_balance_by_address(
conn,
owner.clone(),
new_token.clone(),
)?;

assert_eq!(Amount::from(queried_balance.raw_amount), amount);
assert_eq!(
Amount::from(queried_balance_new.raw_amount),
new_amount
);
assert_eq!(Amount::from(queried_balance.raw_amount), new_amount);

anyhow::Ok(())
})
Expand Down Expand Up @@ -318,8 +310,11 @@ mod tests {
let db = TestDb::new();

db.run_test(move |conn| {
let token = Token::Native(Id::Account(
"tnam1q87wtaqqtlwkw927gaff34hgda36huk0kgry692a".to_string(),
));
let fake_balances =
(0..10000).map(|_| Balance::fake()).collect::<Vec<_>>();
(0..10000).map(|_| Balance::fake_with_token(token.clone())).collect::<Vec<_>>();

seed_tokens_from_balance(conn, fake_balances.clone())?;

Expand Down Expand Up @@ -403,8 +398,12 @@ mod tests {
let db = TestDb::new();

db.run_test(|conn| {
let balances =
(0..1000).map(|_| Balance::fake()).collect::<Vec<_>>();
let token = Token::Native(Id::Account(
"tnam1q87wtaqqtlwkw927gaff34hgda36huk0kgry692a".to_string(),
));
let balances = (0..1000)
.map(|_| Balance::fake_with_token(token.clone()))
.collect::<Vec<_>>();

insert_tokens(
conn,
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ At this point only by checking the state of the database. In the future we might

For the API documentation, please refer to the [swagger.yml](../swagger.yml) file.
We generate the client using [OpenApi generator](https://github.com/OpenAPITools/openapi-generator).
You can find the published versions [here](https://www.npmjs.com/package/@anomaorg/namada-indexer-client).
You can find the published versions [here](https://www.npmjs.com/package/@namada/indexer-client).

Graphs/cards thanks to [excalidraw <3](docs_indexer_2024_09_20.excalidraw).
1 change: 1 addition & 0 deletions orm/migrations/2024-04-17-090406_tokens/down.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- This file should undo anything in `up.sql`
DROP INDEX one_native_token;

DROP TABLE IF EXISTS ibc_token;
DROP TABLE IF EXISTS token;
Expand Down
3 changes: 3 additions & 0 deletions orm/migrations/2024-04-17-090406_tokens/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ CREATE TABLE ibc_token (
ibc_trace VARCHAR NOT NULL,
CONSTRAINT fk_ibc_token_token FOREIGN KEY(address) REFERENCES token(address) ON DELETE CASCADE
);

CREATE UNIQUE INDEX one_native_token ON token (token_type)
WHERE token_type = 'native';
18 changes: 16 additions & 2 deletions parameters/src/services/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,21 @@ pub async fn get_parameters(client: &HttpClient) -> anyhow::Result<Parameters> {

let max_block_time = RPC.shell().max_block_time(client).await?;

let apr = rpc::get_staking_rewards_rate(client).await?;
let maybe_apr = rpc::get_staking_rewards_rate(client).await;

// ATM namada throws an error when epoch is 0
// this is a workaround to set APR to 0 in this case
let apr = match maybe_apr {
Ok(apr) => Ok(apr.staking_rewards_rate.to_string()),
Err(e) => {
let epoch = rpc::query_epoch(client).await?;
if epoch.0 == 0 {
Ok("0".to_string())
} else {
Err(e)
}
}
}?;

Ok(Parameters {
unbonding_length: pos_parameters.unbonding_len,
Expand All @@ -95,7 +109,7 @@ pub async fn get_parameters(client: &HttpClient) -> anyhow::Result<Parameters> {
min_num_of_blocks: epoch_duration.min_num_of_blocks,
min_duration: epoch_duration.min_duration.0,
max_block_time: max_block_time.0,
apr: apr.to_string(),
apr,
native_token_address: native_token_address.to_string(),
cubic_slashing_window_length: pos_parameters
.cubic_slashing_window_length,
Expand Down
4 changes: 2 additions & 2 deletions swagger-codegen.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"npmName": "@anomaorg/namada-indexer-client",
"npmVersion": "0.0.27"
"npmName": "@namada/indexer-client",
"npmVersion": "0.0.28"
}

5 changes: 4 additions & 1 deletion webserver/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ impl ApplicationServer {
)
// Server sent events endpoints
.route("/chain/status", get(chain_handlers::chain_status))
.route("/metrics", get(|| async move { metric_handle.render() }))
.route(
"/metrics",
get(|| async move { metric_handle.render() }),
)
.with_state(common_state)
};

Expand Down

0 comments on commit 13b2edb

Please sign in to comment.