Skip to content

Commit

Permalink
Merge branch 'adrian/light-sdk' (#2220)
Browse files Browse the repository at this point in the history
* origin/adrian/light-sdk:
  Adds a method to serialize transactions
  Makes internal struct of `RevealPk` private
  Fixes datetime
  Splits light sdk queries into multiple files
  Renames light sdk crate and adds description
  Adds missing feature for tendermint-rpc in light sdk
  Changelog #2220
  Imports `HttpClient` from `tendermint_rpc`
  Updates types and imports after rebase
  Updates docstrings and fixes clippy
  Some small type simplifications in the reading api
  [feat]: Added rpc functions to light sdk
  Renames `get_msg_to_sign` to `get_sign_bytes`
  [feat]: Small fixes and making stuff public
  First draft of all transactions in light sdk
  Refactors pos txs in separate file. Improved shared functions
  Created light SDK builders.
  First attempt at overview of light-sdk
  • Loading branch information
brentstone committed Dec 29, 2023
2 parents 2067cda + 5a62829 commit c9a1f48
Show file tree
Hide file tree
Showing 21 changed files with 1,992 additions and 19 deletions.
1 change: 1 addition & 0 deletions .changelog/unreleased/SDK/2220-light-sdk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added light sdk ([\#2220](https://github.com/anoma/namada/pull/2220))
103 changes: 87 additions & 16 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ members = [
"apps",
"benches",
"core",
"encoding_spec",
"ethereum_bridge",
"light_sdk",
"macros",
"proof_of_stake",
"sdk",
"shared",
"test_utils",
"tests",
"tx_prelude",
"vm_env",
"macros",
"vp_prelude",
"encoding_spec",
"sdk",
"examples",
]

Expand Down
26 changes: 26 additions & 0 deletions light_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "namada_light_sdk"
description = "A more simple version of the Namada SDK"
resolver = "2"
authors.workspace = true
edition.workspace = true
documentation.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
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

[dependencies]
borsh.workspace = true
borsh-ext.workspace = true
ibc = "0.47.0"
namada_core = {path = "../core"}
namada_sdk = {path = "../sdk"}
prost.workspace = true
tendermint-config.workspace = true
tendermint-rpc = { worskpace = true, features = ["http-client"] }
tokio = {workspace = true, features = ["rt"]}
21 changes: 21 additions & 0 deletions light_sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! The Namada light SDK is a simplified version of the SDK aimed at making
//! interaction with the protocol easier and faster. The library is developed
//! with ease-of-use and interoperability in mind so that it should be possible
//! to wrap it for usage in an FFI context.
//!
//! The [`namada_core`] crate of Namada is also re-exported to allow access to
//! its types.
//!
//! # Structure
//!
//! This SDK is divided into three modules:
//!
//! - 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

pub mod reading;
pub mod transaction;
pub mod writing;
pub use namada_core;
79 changes: 79 additions & 0 deletions light_sdk/src/reading/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use namada_core::types::account::Account;
use namada_core::types::address::Address;
use namada_core::types::key::common;

use super::*;

/// Query token amount of owner.
pub 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()))?;
let rt = Runtime::new().unwrap();
rt.block_on(rpc::get_token_balance(&client, token, owner))
}

/// 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 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()))?;
let rt = Runtime::new().unwrap();
rt.block_on(rpc::known_address(&client, address))
}

/// Query the accunt substorage space of an address
pub 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()))?;
let rt = Runtime::new().unwrap();
rt.block_on(rpc::get_account_info(&client, owner))
}

/// Query if the public_key is revealed
pub 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()))?;
let rt = Runtime::new().unwrap();
rt.block_on(rpc::is_public_key_revealed(&client, owner))
}

/// Query an account substorage at a specific index
pub 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()))?;
let rt = Runtime::new().unwrap();
rt.block_on(rpc::get_public_key_at(&client, owner, index))
}
Loading

0 comments on commit c9a1f48

Please sign in to comment.