Skip to content

Commit

Permalink
Stefan/add faucet genesis accounts (#354)
Browse files Browse the repository at this point in the history
* make

* update workflow
  • Loading branch information
mutobui authored Oct 9, 2023
1 parent 84fc09c commit 1e2516b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 30 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ jobs:
- uses: Swatinem/rust-cache@v2

- name: Install make
run: sudo apt-get install -y make

- name: Unit-Test
run: cargo test --release
run: make test

- name: Check Build
run: cargo check --release
run: make check

- name: Check Build for Benchmarking
run: |
mkdir node # Create the "node" directory
pushd node
cargo check --features=runtime-benchmarks --release
make check-benchmark
popd # Return to the previous directory
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
network = devnet-native

.PHONY: build
build:
cargo build -r
cargo build -r --features $(network)

.PHONY: build-tesnet
build-testnet:
cargo build -r --features testnet-native

.PHONY: test
test:
cargo test --workspace --release
cargo test --workspace --release --features $(network)

.PHONY: check
check:
cargo check -r
cargo check -r --features $(network)

.PHONY: run
run:
Expand All @@ -27,15 +29,15 @@ run:

.PHONY: check-benchmark
check-benchmark:
cargo check --release --features runtime-benchmarks
cargo check --release --features runtime-benchmarks,$(network)

.PHONY: check-benchmark-game
check-benchmark-game:
cargo check --release -p pallet-game --features runtime-benchmarks
cargo check --release -p pallet-game --features runtime-benchmarks,$(network)

.PHONY: benchmark
benchmark:
cargo build --release --features runtime-benchmarks
cargo build --release --features runtime-benchmarks,$(network)

.PHONY: clippy
clippy:
Expand Down
23 changes: 21 additions & 2 deletions pallets/pallet-faucet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub mod pallet {
use frame_support::{pallet_prelude::*, BoundedVec};
use frame_system::pallet_prelude::*;

type MaxFundingAccount = ConstU32<3>;

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand All @@ -46,6 +45,10 @@ pub mod pallet {

/// Faucet Amount
type FaucetAmount: Get<BalanceOf<Self>>;

/// Maximum number of fundung accounts
#[pallet::constant]
type MaxFundingAccount: Get<u32>;
}

#[pallet::pallet]
Expand All @@ -54,7 +57,7 @@ pub mod pallet {
/// Holding all the accounts
#[pallet::storage]
pub(super) type GenesisAccounts<T: Config> =
StorageValue<_, BoundedVec<T::AccountId, MaxFundingAccount>, ValueQuery>;
StorageValue<_, BoundedVec<T::AccountId, T::MaxFundingAccount>, ValueQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
Expand Down Expand Up @@ -85,6 +88,7 @@ pub mod pallet {
DontBeGreedy,
PleaseWait,
OutOfFaucet,
ExceedFundingAccounts,
}

#[pallet::call]
Expand Down Expand Up @@ -168,6 +172,21 @@ pub mod pallet {
}
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight({0})]
pub fn new_funding_accounts(origin: OriginFor<T>, accounts: Vec<T::AccountId>) -> DispatchResult {
ensure_root(origin)?;

let funding_account = BoundedVec::<T::AccountId, T::MaxFundingAccount>::try_from(accounts);

if let Ok(accs) = funding_account {
GenesisAccounts::<T>::put(accs);
} else {
return Err(Error::<T>::ExceedFundingAccounts.into())
}
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
3 changes: 2 additions & 1 deletion pallets/pallet-faucet/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use frame_system as system;
use frame_support::dispatch::Vec;
use pallet_balances::AccountData;
pub use pallet_balances::Call as BalancesCall;
use sp_core::{ConstU128, ConstU64, H256, ConstU16};
use sp_core::{ConstU128, ConstU64, H256, ConstU16, ConstU32};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
AccountId32,
Expand Down Expand Up @@ -78,6 +78,7 @@ impl pallet_faucet::Config for Test {
type WeightInfo = ();
type Cache = PalletCache;
type FaucetAmount = FaucetAmount;
type MaxFundingAccount = ConstU32<5>;
}

parameter_types! {
Expand Down
34 changes: 31 additions & 3 deletions pallets/pallet-faucet/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ops::Add;

use crate::{mock::*, Error};
use frame_support::{assert_err, assert_ok, traits::Currency};
use sp_runtime::AccountId32;
use crate::{mock::*, Error, GenesisAccounts};
use frame_support::{assert_err, assert_noop, assert_ok, traits::Currency};
use sp_runtime::{traits::BadOrigin, AccountId32};

#[test]
fn faucet_works() {
Expand Down Expand Up @@ -85,3 +85,31 @@ fn donate_fail() {
);
})
}

#[test]
fn test_new_funding_accounts() {
ExtBuilder::default().build_and_execute(|| {
// Set up test environment
let root = AccountId32::new([0; 32]);
let accounts: Vec<AccountId32> = vec![
AccountId32::new([0; 32]),
AccountId32::new([1; 32]),
AccountId32::new([2; 32]),
];

// Ensure the function fails when called by a non-root account
assert_noop!(
Faucet::new_funding_accounts(RuntimeOrigin::signed(root.clone()), accounts.clone()),
BadOrigin
);

// Ensure the function succeeds when called by a root account
assert_ok!(Faucet::new_funding_accounts(
RuntimeOrigin::root(),
accounts.clone()
));

// Verify that the funding accounts were stored correctly
assert_eq!(GenesisAccounts::<Test>::get(), accounts);
});
}
1 change: 1 addition & 0 deletions runtime/devnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ impl pallet_faucet::Config for Runtime {
type WeightInfo = pallet_faucet::weights::FaucetWeight<Runtime>;
type Cache = PalletCache;
type FaucetAmount = FaucetAmount;
type MaxFundingAccount = ConstU32<3>;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions runtime/testnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ impl pallet_faucet::Config for Runtime {
type WeightInfo = pallet_faucet::weights::FaucetWeight<Runtime>;
type Cache = PalletCache;
type FaucetAmount = FaucetAmount;
type MaxFundingAccount = ConstU32<3>;
}

parameter_types! {
Expand Down
27 changes: 12 additions & 15 deletions support/src/common/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ impl TokenInfo for GafiCurrency {
}

/// Express the native token as u128
///
/// # Examples
///
/// ```
/// use gafi_support::common::{NativeToken::GAKI, unit};
///
/// let balance = 10 * unit(GAKI);
/// assert_eq!(balance, 10_000_000_000_000_000_000);
/// ```
pub fn unit(token: NativeToken) -> u128 {
10u128.saturating_pow(GafiCurrency::token_info(token).decimals.into())
}
Expand Down Expand Up @@ -98,31 +89,37 @@ mod tests {

#[test]
fn test_unit() {
assert_eq!(unit(NativeToken::GAKI), 1_000_000_000_000_000_000);
assert_eq!(unit(NativeToken::GAKI), 1_000_000_000_000);
assert_eq!(unit(NativeToken::GAFI), 10_000_000_000);
}

#[test]
fn test_centi() {
assert_eq!(centi(NativeToken::GAKI), 10_000_000_000_000_000);
assert_eq!(centi(NativeToken::GAKI), 10_000_000_000);
assert_eq!(centi(NativeToken::GAFI), 100_000_000);
}

#[test]
fn test_milli() {
assert_eq!(milli(NativeToken::GAKI), 1_000_000_000_000_000);
assert_eq!(milli(NativeToken::GAKI), 1_000_000_000);
assert_eq!(milli(NativeToken::GAFI), 10_000_000);
}

#[test]
fn test_millicent() {
assert_eq!(millicent(NativeToken::GAKI), 10_000_000_000_000);
assert_eq!(millicent(NativeToken::GAKI), 10_000_000);
assert_eq!(millicent(NativeToken::GAFI), 100_000);
}

#[test]
fn test_microcent() {
assert_eq!(microcent(NativeToken::GAKI), 1_000_000_000_000);
assert_eq!(microcent(NativeToken::GAKI), 1_000_000);
assert_eq!(microcent(NativeToken::GAFI), 10_000);
}

#[test]
fn test_deposit() {
assert_eq!(deposit(5, 100, NativeToken::GAKI), 100_100_000_000_000_000_000);
assert_eq!(deposit(5, 100, NativeToken::GAKI),100_100_000_000_000);
assert_eq!(deposit(5, 100, NativeToken::GAFI),1_001_000_000_000);
}
}

0 comments on commit 1e2516b

Please sign in to comment.