Skip to content

Commit

Permalink
Merge branch 'grarco/async-light-sdk-rebase' (#2399)
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Jan 19, 2024
2 parents 2e053b4 + d72d32f commit b02bfa4
Show file tree
Hide file tree
Showing 28 changed files with 1,562 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/SDK/2399-async-light-sdk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Both the `reading` and `writing` modules of the light SDK can now be used from
within an async runtime. ([\#2399](https://github.com/anoma/namada/pull/2399))
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion crates/light_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ readme.workspace = true
repository.workspace = true
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
blocking = ["tokio"]

[dependencies]
namada_sdk = { path = "../sdk" }

Expand All @@ -20,4 +25,5 @@ borsh-ext.workspace = true
prost.workspace = true
tendermint-config.workspace = true
tendermint-rpc = { workspace = true, features = ["http-client"] }
tokio = { workspace = true, features = ["rt"] }
tokio = {workspace = true, features = ["rt"], optional = true}
serde_json = "1.0.108"
9 changes: 6 additions & 3 deletions crates/light_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
//!
//! This SDK is divided into three modules:
//!
//! - transaction: contains functions to construct all the transactions
//! - [`transaction`]: contains functions to construct all the transactions
//! currently supported by the protocol
//! - reading: exposes queries to retrieve data from a Namada node
//! - writing: TO BE DONE
//! - [`reading`]: exposes queries to retrieve data from a Namada node
//! - [`writing`]: exposes functions to send data to a Namada node
//!
//! Both the [`reading`] and [`writing`] modules are further divided into a
//! blocking and asynchronous submodules.
pub mod reading;
pub mod transaction;
Expand Down
73 changes: 73 additions & 0 deletions crates/light_sdk/src/reading/asynchronous/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use namada_sdk::account::Account;
use namada_sdk::types::key::common;

use super::*;

/// Query token amount of owner.
pub async fn get_token_balance(
tendermint_addr: &str,
token: &Address,
owner: &Address,
) -> Result<token::Amount, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::get_token_balance(&client, token, owner).await
}

/// Check if the address exists on chain. Established address exists if it
/// has a stored validity predicate. Implicit and internal addresses
/// always return true.
pub async fn known_address(
tendermint_addr: &str,
address: &Address,
) -> Result<bool, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::known_address(&client, address).await
}

/// Query the accunt substorage space of an address
pub async fn get_account_info(
tendermint_addr: &str,
owner: &Address,
) -> Result<Option<Account>, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::get_account_info(&client, owner).await
}

/// Query if the public_key is revealed
pub async fn is_public_key_revealed(
tendermint_addr: &str,
owner: &Address,
) -> Result<bool, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::is_public_key_revealed(&client, owner).await
}

/// Query an account substorage at a specific index
pub async fn get_public_key_at(
tendermint_addr: &str,
owner: &Address,
index: u8,
) -> Result<Option<common::PublicKey>, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::get_public_key_at(&client, owner, index).await
}
43 changes: 43 additions & 0 deletions crates/light_sdk/src/reading/asynchronous/governance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use namada_sdk::governance::parameters::GovernanceParameters;
use namada_sdk::governance::storage::proposal::StorageProposal;
use namada_sdk::governance::utils::Vote;

use super::*;

/// Query proposal by Id
pub async fn query_proposal_by_id(
tendermint_addr: &str,
proposal_id: u64,
) -> Result<Option<StorageProposal>, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::query_proposal_by_id(&client, proposal_id).await
}

/// Get the givernance parameters
pub async fn query_governance_parameters(
tendermint_addr: &str,
) -> Result<GovernanceParameters, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
Ok(rpc::query_governance_parameters(&client).await)
}

/// Get the givernance parameters
pub async fn query_proposal_votes(
tendermint_addr: &str,
proposal_id: u64,
) -> Result<Vec<Vote>, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::query_proposal_votes(&client, proposal_id).await
}
76 changes: 76 additions & 0 deletions crates/light_sdk/src/reading/asynchronous/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::str::FromStr;

use namada_sdk::error::{EncodingError, Error};
use namada_sdk::io::StdIo;
use namada_sdk::queries::RPC;
use namada_sdk::rpc;
use namada_sdk::state::LastBlock;
use namada_sdk::types::address::Address;
use namada_sdk::types::storage::BlockResults;
use namada_sdk::types::token::{self, DenominatedAmount};
use tendermint_config::net::Address as TendermintAddress;
use tendermint_rpc::HttpClient;

pub mod account;
pub mod governance;
pub mod pgf;
pub mod pos;
pub mod tx;

/// Query the address of the native token
pub async fn query_native_token(
tendermint_addr: &str,
) -> Result<Address, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::query_native_token(&client).await
}

/// Query the last committed block, if any.
pub async fn query_block(
tendermint_addr: &str,
) -> Result<Option<LastBlock>, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::query_block(&client).await
}

/// Query the results of the last committed block
pub async fn query_results(
tendermint_addr: &str,
) -> Result<Vec<BlockResults>, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::query_results(&client).await
}

/// Get a properly denominated amount of a token
pub async fn denominate_amount(
tendermint_addr: &str,
amount: u64,
token: &str,
) -> Result<DenominatedAmount, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
let token = Address::decode(token)
.map_err(|e| Error::Encode(EncodingError::Decoding(e.to_string())))?;
Ok(rpc::denominate_amount(
&client,
&StdIo {},
&token,
token::Amount::from(amount),
)
.await)
}
14 changes: 14 additions & 0 deletions crates/light_sdk/src/reading/asynchronous/pgf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use super::*;

/// Check if the given address is a pgf steward.
pub async fn is_steward(
tendermint_addr: &str,
address: &Address,
) -> Result<bool, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
Ok(rpc::is_steward(&client, address).await)
}
Loading

0 comments on commit b02bfa4

Please sign in to comment.