Skip to content

Commit

Permalink
Merge pull request #4 from zianksm/refactor-move-errors
Browse files Browse the repository at this point in the history
move errors tu one single source of truth
  • Loading branch information
zianksm authored Jun 13, 2023
2 parents a454a7e + 9743d0a commit 7ef109f
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 56 deletions.
55 changes: 48 additions & 7 deletions dhatu/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
use crate::{
registrar::{key_manager::prelude::KeypairGenerationError, signer::TxBuilderError},
tx::extrinsics::prelude::{
calldata::ToPayloadError, reserve::FundsReserveError, CallbackExecutorError,
},
types::MandalaClientErorr,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -32,3 +25,51 @@ pub enum Error {
#[error("error when signing transaction : {0}")]
SignTransactionError(#[from] TxBuilderError),
}

#[derive(thiserror::Error, Debug)]
pub enum KeypairGenerationError {
#[error("{0}")]
PublicAddress(String),

#[error("fail to generate mnemonic phrase with {0}")]
MnemonicPhrase(String),

#[error("{0}")]
PrivateKey(String),

#[error("{0}")]
Recover(String),
}

#[derive(thiserror::Error, Debug)]
pub enum MandalaClientErorr {
#[error("connection Error : {0}")]
Connection(#[from] subxt::Error),
}

#[derive(thiserror::Error, Debug)]
pub enum FundsReserveError {
#[error("{0}")]
RpcError(#[from] subxt::error::Error),

#[error("account does not exist!")]
NonExistentAccount,
}

#[derive(thiserror::Error, Debug)]
pub enum CallbackExecutorError {
#[error("{0}")]
InvalidUrl(String),
}

#[derive(thiserror::Error, Debug)]
pub enum ToPayloadError {
#[error("{0}")]
AddressError(String),
}

#[derive(thiserror::Error, Debug)]
pub enum TxBuilderError {
#[error("{0}")]
SignErorr(#[from] subxt::Error),
}
16 changes: 1 addition & 15 deletions dhatu/src/registrar/key_manager/keypair.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::Error;
use crate::error::{Error, KeypairGenerationError};
use futures::FutureExt;
use sp_core::{crypto::Ss58Codec, sr25519::Pair, Pair as PairTraits};
use std::str::FromStr;
Expand Down Expand Up @@ -47,20 +47,6 @@ impl Keypair {
&self.keypair
}
}
#[derive(thiserror::Error, Debug)]
pub enum KeypairGenerationError {
#[error("{0}")]
PublicAddress(String),

#[error("fail to generate mnemonic phrase with {0}")]
MnemonicPhrase(String),

#[error("{0}")]
PrivateKey(String),

#[error("{0}")]
Recover(String),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PublicAddress(pub(crate) String);
Expand Down
2 changes: 1 addition & 1 deletion dhatu/src/registrar/key_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod prelude {

use prelude::*;

use crate::{error::Error, };
use crate::{error::{Error, KeypairGenerationError}, };

/// represent a keypair manager.
pub struct KeyManager;
Expand Down
6 changes: 0 additions & 6 deletions dhatu/src/registrar/signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ pub(crate) trait WrappedExtrinsic<T: EncodeAsFields> {

pub(crate) struct TxBuilder;

#[derive(thiserror::Error,Debug)]
pub enum TxBuilderError{
#[error("{0}")]
SignErorr(#[from] subxt::Error),
}

impl TxBuilder {
/// create a new unsigned transaction from a transaction payload
pub fn unsigned<T: EncodeAsFields>(
Expand Down
7 changes: 1 addition & 6 deletions dhatu/src/tx/extrinsics/callback_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ use serde::Serialize;


use super::prelude::enums::{ExtrinsicStatus, Hash};
use crate::error::Error;
use crate::error::{Error, CallbackExecutorError};

#[cfg(feature = "tokio")]
pub struct Executor {
http_connection_pool: reqwest::Client,
}

#[derive(thiserror::Error, Debug)]
pub enum CallbackExecutorError {
#[error("{0}")]
InvalidUrl(String),
}

pub struct Url(pub(crate) reqwest::Url);

Expand Down
10 changes: 1 addition & 9 deletions dhatu/src/tx/extrinsics/funds_reserve/reserve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@



use crate::error::Error;
use crate::error::{Error, FundsReserveError};
use crate::registrar::key_manager::prelude::PublicAddress;
use crate::registrar::signer::WrappedExtrinsic;
use crate::tx::extrinsics::prelude::{
Expand All @@ -16,14 +16,6 @@ use subxt::{tx::PairSigner};

use crate::{registrar::key_manager::prelude::PrivateKey};

#[derive(thiserror::Error, Debug)]
pub enum FundsReserveError {
#[error("{0}")]
RpcError(#[from] subxt::error::Error),

#[error("account does not exist!")]
NonExistentAccount,
}

#[derive(Clone)]
pub struct FundsReserve {
Expand Down
7 changes: 2 additions & 5 deletions dhatu/src/tx/extrinsics/transaction_constructor/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use subxt::utils::{AccountId32, MultiAddress};



use crate::error::ToPayloadError;

use super::{
traits::{ScaleEncodeable, ToContractPayload, ValidateHash},
transfer_nft_contract::{
Expand Down Expand Up @@ -40,11 +42,6 @@ impl From<NftTransferAgrs> for CallData<TransferNFT> {
}
}

#[derive(thiserror::Error, Debug)]
pub enum ToPayloadError {
#[error("{0}")]
AddressError(String),
}

impl<T> ToContractPayload for CallData<T> {
fn to_payload(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use parity_scale_codec::Encode;
use subxt::utils::AccountId32;

use crate::{tx::extrinsics::{
prelude::calldata::{CallData, ToPayloadError},
prelude::calldata::{CallData, },
transaction_constructor::{
calldata::ContractCall,
traits::{ScaleEncodeable, ToContractPayload, },
},
}, registrar::signer::WrappedExtrinsic};
}, registrar::signer::WrappedExtrinsic, error::ToPayloadError};

pub(crate) struct NftTransferAgrs {
function_selector: String,
Expand Down
7 changes: 2 additions & 5 deletions dhatu/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use subxt::{
};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};

use crate::error::MandalaClientErorr;

pub(crate) type MandalaConfig = SubstrateConfig;
pub(crate) type NodeClient = OnlineClient<MandalaConfig>;
pub(crate) type Extrinsic = SubmittableExtrinsic<MandalaConfig, NodeClient>;
Expand Down Expand Up @@ -91,11 +93,6 @@ impl From<TransactionProgress> for MandalaTransactionProgress {
#[derive(Clone)]
pub struct MandalaClient(pub(crate) OnlineClient<MandalaConfig>);

#[derive(thiserror::Error, Debug)]
pub enum MandalaClientErorr {
#[error("connection Error : {0}")]
Connection(#[from] subxt::Error),
}

impl MandalaClient {
pub(crate) fn inner(&self) -> &OnlineClient<MandalaConfig> {
Expand Down

0 comments on commit 7ef109f

Please sign in to comment.