mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 09:21:04 +00:00
Use decl_error in common runtime modules (#796)
* use decl_error in common runtime modules * Apply suggestions from code review Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
committed by
Bastian Köcher
parent
5621c18632
commit
f50d0c6505
@@ -18,7 +18,7 @@
|
||||
|
||||
use rstd::prelude::*;
|
||||
use sp_io::{hashing::keccak_256, crypto::secp256k1_ecdsa_recover};
|
||||
use frame_support::{decl_event, decl_storage, decl_module};
|
||||
use frame_support::{decl_event, decl_storage, decl_module, decl_error};
|
||||
use frame_support::weights::SimpleDispatchInfo;
|
||||
use frame_support::traits::{Currency, Get, VestingCurrency};
|
||||
use system::{ensure_root, ensure_none};
|
||||
@@ -102,6 +102,15 @@ decl_event!(
|
||||
}
|
||||
);
|
||||
|
||||
decl_error! {
|
||||
pub enum Error for Module<T: Trait> {
|
||||
/// Invalid Ethereum signature.
|
||||
InvalidEthereumSignature,
|
||||
/// Ethereum address has no claim.
|
||||
SignerHasNoClaim,
|
||||
}
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
// A macro for the Storage trait, and its implementation, for this module.
|
||||
// This allows for type-safe usage of the Substrate storage database, so you can
|
||||
@@ -126,6 +135,8 @@ decl_storage! {
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
type Error = Error<T>;
|
||||
|
||||
/// The Prefix that is used in signed Ethereum messages for this network
|
||||
const Prefix: &[u8] = T::Prefix::get();
|
||||
|
||||
@@ -139,10 +150,10 @@ decl_module! {
|
||||
|
||||
let data = dest.using_encoded(to_ascii_hex);
|
||||
let signer = Self::eth_recover(ðereum_signature, &data)
|
||||
.ok_or("Invalid Ethereum signature")?;
|
||||
.ok_or(Error::<T>::InvalidEthereumSignature)?;
|
||||
|
||||
let balance_due = <Claims<T>>::get(&signer)
|
||||
.ok_or("Ethereum address has no claim")?;
|
||||
.ok_or(Error::<T>::SignerHasNoClaim)?;
|
||||
|
||||
// Check if this claim should have a vesting schedule.
|
||||
if let Some(vs) = <Vesting<T>>::get(&signer) {
|
||||
@@ -415,7 +426,7 @@ mod tests {
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_noop!(
|
||||
Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
|
||||
"Ethereum address has no claim"
|
||||
Error::<Test>::SignerHasNoClaim
|
||||
);
|
||||
assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, None));
|
||||
assert_ok!(Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())));
|
||||
@@ -434,7 +445,7 @@ mod tests {
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_noop!(
|
||||
Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
|
||||
"Ethereum address has no claim"
|
||||
Error::<Test>::SignerHasNoClaim
|
||||
);
|
||||
assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1))));
|
||||
assert_ok!(Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())));
|
||||
@@ -459,7 +470,10 @@ mod tests {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_ok!(Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())));
|
||||
assert_noop!(Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())), "Ethereum address has no claim");
|
||||
assert_noop!(
|
||||
Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())),
|
||||
Error::<Test>::SignerHasNoClaim
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -467,7 +481,10 @@ mod tests {
|
||||
fn non_sender_sig_doesnt_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_noop!(Claims::claim(Origin::NONE, 42, sig(&alice(), &69u64.encode())), "Ethereum address has no claim");
|
||||
assert_noop!(
|
||||
Claims::claim(Origin::NONE, 42, sig(&alice(), &69u64.encode())),
|
||||
Error::<Test>::SignerHasNoClaim
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -475,7 +492,10 @@ mod tests {
|
||||
fn non_claimant_doesnt_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_noop!(Claims::claim(Origin::NONE, 42, sig(&bob(), &69u64.encode())), "Ethereum address has no claim");
|
||||
assert_noop!(
|
||||
Claims::claim(Origin::NONE, 42, sig(&bob(), &69u64.encode())),
|
||||
Error::<Test>::SignerHasNoClaim
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user