using anyhow::Result to solve multiple error type issue

This commit is contained in:
mfahampshire
2023-07-21 10:34:08 +02:00
parent 627fe72cf6
commit ea8057ed93
4 changed files with 12 additions and 11 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ async fn main() -> anyhow::Result<()> {
loop {
// listen out for incoming requests from mixnet, parse and match them
let request: (RequestTypes, AnonymousSenderTag) =
listen_and_parse_request(&mut client).await?;
listen_and_parse_request(&mut client).await?;
// grab sender_tag from parsed request for anonymous replies
let return_recipient: AnonymousSenderTag = request.1;
match request.0 {
+2 -2
View File
@@ -16,7 +16,7 @@ pub async fn offline_sign(
to: AccountId,
client: &mut MixnetClient,
sp_address: Recipient,
) -> Result<String, std::io::Error> {
) -> anyhow::Result<String> {
let denom: Denom = DEFAULT_DENOM.parse().unwrap();
let signer = DirectSecp256k1HdWallet::from_mnemonic(DEFAULT_PREFIX, mnemonic.clone());
let signer_address = signer.try_derive_accounts().unwrap()[0].address().clone();
@@ -97,7 +97,7 @@ pub async fn send_tx(
base58_tx: String,
sp_address: Recipient,
client: &mut MixnetClient,
) -> Result<(String, bool), std::io::Error> {
) -> anyhow::Result<(String, bool)> {
let broadcast_request = crate::BroadcastRequest {
base58_tx_bytes: base58_tx,
};
+8 -7
View File
@@ -1,4 +1,3 @@
use anyhow::Result;
use cosmrs::{tendermint, AccountId};
use nym_sdk::mixnet::{
AnonymousSenderTag, MixnetClient, MixnetClientBuilder, ReconstructedMessage, StoragePaths,
@@ -64,7 +63,9 @@ pub async fn create_client(config_path: PathBuf) -> MixnetClient {
// parse returned response from service: ignore empty SURB data packets + parse incoming message to struct or error
// we know we are expecting JSON here but an irl helper would parse conditionally on bytes / string incoming
pub async fn listen_and_parse_response(client: &mut MixnetClient) -> Result<ResponseTypes, std::io::Error> {
pub async fn listen_and_parse_response(
client: &mut MixnetClient
) -> anyhow::Result<ResponseTypes> {
let mut message: Vec<ReconstructedMessage> = Vec::new();
// get the actual message - discard the empty vec sent along with the SURB topup request
@@ -79,9 +80,9 @@ pub async fn listen_and_parse_response(client: &mut MixnetClient) -> Result<Resp
// parse vec<u8> -> JSON String
let mut parsed = String::new();
if let Some(r) = message.iter().next() {
parsed = String::from_utf8(r.message.clone()).unwrap();
parsed = String::from_utf8(r.message.clone())?;
}
let sp_response: crate::ResponseTypes = serde_json::from_str(&parsed).unwrap();
let sp_response: crate::ResponseTypes = serde_json::from_str(&parsed)?;
Ok(sp_response)
}
@@ -89,7 +90,7 @@ pub async fn listen_and_parse_response(client: &mut MixnetClient) -> Result<Resp
// we know we are expecting JSON here but an irl helper would parse conditionally on bytes / string incoming
pub async fn listen_and_parse_request(
client: &mut MixnetClient,
) -> Result<(RequestTypes, AnonymousSenderTag), std::io::Error> {
) -> anyhow::Result<(RequestTypes, AnonymousSenderTag)> {
let mut message: Vec<ReconstructedMessage> = Vec::new();
// get the actual message - discard the empty vec sent along with the SURB topup request
@@ -104,9 +105,9 @@ pub async fn listen_and_parse_request(
// parse vec<u8> -> JSON String
let mut parsed = String::new();
if let Some(r) = message.iter().next() {
parsed = String::from_utf8(r.message.clone()).unwrap();
parsed = String::from_utf8(r.message.clone())?;
}
let client_request: crate::RequestTypes = serde_json::from_str(&parsed).unwrap();
let client_request: crate::RequestTypes = serde_json::from_str(&parsed)?;
// get the sender_tag for anon reply
let return_recipient = message[0].sender_tag.unwrap();
+1 -1
View File
@@ -15,7 +15,7 @@ pub async fn get_sequence(
) -> Result<crate::SequenceRequestResponse, NyxdError> {
// get signer information
let sequence = broadcaster.get_sequence(&signer_address).await?;
let chain_id: tendermint::chain::Id = broadcaster.get_chain_id().await?;
let chain_id: tendermint::chain::Id = broadcaster.get_chain_id().await?; // unwrap();
Ok(crate::SequenceRequestResponse {
account_number: sequence.account_number,
sequence: sequence.sequence,