-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement async client trait #173
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,14 @@ license = "MIT" | |
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
async-trait = {version = "0.1.51"} | ||
borsh = "0.9.0" | ||
chrono = "0.4.22" | ||
getrandom = { version = "0.2.7", features = ["js"] } | ||
gloo-utils = { version = "0.1.5", features = ["serde"] } | ||
js-sys = "0.3.60" | ||
masp_primitives = { git = "https://github.com/anoma/masp", rev = "bee40fc465f6afbd10558d12fe96eb1742eee45c" } | ||
namada = {git = "https://github.com/anoma/namada", tag = "v0.11.0", features = ["ferveo-tpke"]} | ||
namada = {git = "https://github.com/anoma/namada", branch = "mateusz/0.11.0", features = ["ferveo-tpke", "async-client"]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is 0.11.0 branch with changes to ?Send |
||
prost = "0.9.0" | ||
prost-types = "0.9.0" | ||
rand = "0.8.5" | ||
|
@@ -27,7 +28,6 @@ serde_json = "1.0" | |
thiserror = "^1" | ||
wasm-bindgen = "0.2.83" | ||
wasm-bindgen-futures = "0.4.33" | ||
tendermint = {git = "https://github.com/heliaxdev/tendermint-rs.git", rev = "87be41b8c9cc2850830f4d8028c1fe1bd9f96284"} | ||
|
||
[dependencies.web-sys] | ||
version = "0.3.4" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use gloo_utils::format::JsValueSerdeExt; | ||
use namada::ledger::queries::RPC; | ||
use namada::types::address::Address; | ||
use namada::types::token::Amount; | ||
use serde::Serialize; | ||
use std::collections::{HashMap, HashSet}; | ||
use std::str::FromStr; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use crate::rpc_client::HttpClient; | ||
|
||
#[wasm_bindgen] | ||
pub struct Query { | ||
client: HttpClient, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl Query { | ||
fn to_js_result<T>(result: T) -> Result<JsValue, JsError> | ||
where | ||
T: Serialize, | ||
{ | ||
match JsValue::from_serde(&result) { | ||
Ok(v) => Ok(v), | ||
Err(e) => Err(JsError::new(&e.to_string())), | ||
} | ||
} | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(url: String) -> Query { | ||
let client = HttpClient::new(url); | ||
Query { client } | ||
} | ||
|
||
pub async fn query_epoch(&self) -> Result<JsValue, JsError> { | ||
let epoch = RPC.shell().epoch(&self.client).await?; | ||
|
||
Query::to_js_result(epoch) | ||
} | ||
|
||
pub async fn query_all_validators(&self) -> Result<JsValue, JsError> { | ||
let validator_addresses = RPC | ||
.vp() | ||
.pos() | ||
.validator_addresses(&self.client, &None) | ||
.await?; | ||
|
||
let mut result: Vec<(Address, Amount)> = Vec::new(); | ||
|
||
for address in validator_addresses.into_iter() { | ||
let total_bonds = RPC | ||
.vp() | ||
.pos() | ||
.validator_stake(&self.client, &address, &None) | ||
.await?; | ||
|
||
result.push((address, total_bonds)); | ||
} | ||
|
||
Query::to_js_result(result) | ||
} | ||
|
||
pub async fn query_my_validators( | ||
&self, | ||
owner_addresses: Box<[JsValue]>, | ||
) -> Result<JsValue, JsError> { | ||
let owner_addresses: Vec<Address> = owner_addresses | ||
.into_iter() | ||
.map(|address| { | ||
let address_str = &(address.as_string().unwrap()[..]); | ||
Address::from_str(address_str).unwrap() | ||
}) | ||
.collect(); | ||
|
||
let mut validators_per_address: HashMap<Address, HashSet<Address>> = HashMap::new(); | ||
|
||
for address in owner_addresses.into_iter() { | ||
let validators = RPC.vp().pos().delegations(&self.client, &address).await?; | ||
|
||
validators_per_address.insert(address, validators); | ||
} | ||
|
||
//TODO: Change to Vec of structs | ||
//Owner, Validator, Amount | ||
let mut result: Vec<(Address, Address, Amount)> = Vec::new(); | ||
|
||
for (owner, validators) in validators_per_address.into_iter() { | ||
for validator in validators.into_iter() { | ||
// let bond_path = &format!("/vp/pos/bond_amount/{}/{}", owner, validator)[..]; | ||
// let total_bonds = abci_query::<Amount>(&self.url, bond_path).await?; | ||
let total_bonds = RPC | ||
.vp() | ||
.pos() | ||
.bond_amount(&self.client, &owner, &validator, &None) | ||
.await?; | ||
|
||
result.push((owner.clone(), validator, total_bonds)); | ||
} | ||
} | ||
|
||
Query::to_js_result(result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
query.query_
does not look nice, but I think we can figure out naming later :)