Skip to content

Commit

Permalink
added set_gate_faucet extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Frederick authored and atodorov committed Aug 25, 2023
1 parent 1ea82c7 commit 138b347
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pallets/creditcoin/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ benchmarks! {
let root = RawOrigin::Root;
let contract = DeployedContract::default();
}: _(root, contract)


set_gate_faucet {
let root = RawOrigin::Root;
let addr: T::AccountId = lender_account::<T>(false);
}: _(root, addr)
}

//impl_benchmark_test_suite!(Creditcoin, crate::mock::new_test_ext(), crate::mock::Test);
Expand Down
14 changes: 14 additions & 0 deletions pallets/creditcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub mod pallet {
fn set_collect_coins_contract() -> Weight;
fn register_address_v2() -> Weight;
fn set_gate_contract() -> Weight;
fn set_gate_faucet() -> Weight;
}

#[pallet::pallet]
Expand Down Expand Up @@ -236,6 +237,10 @@ pub mod pallet {
#[pallet::getter(fn gate_contract)]
pub type GATEConract<T: Config> = StorageValue<_, DeployedContract, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn gate_faucet_address)]
pub type GATEFaucetAddress<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -1420,5 +1425,14 @@ pub mod pallet {
GATEConract::<T>::put(contract);
Ok(())
}

#[transactional]
#[pallet::call_index(24)]
#[pallet::weight(<T as Config>::WeightInfo::set_gate_faucet())]
pub fn set_gate_faucet(origin: OriginFor<T>, address: T::AccountId) -> DispatchResult {
ensure_root(origin)?;
GATEFaucetAddress::<T>::put(address);
Ok(())
}
}
}
33 changes: 33 additions & 0 deletions pallets/creditcoin/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3279,3 +3279,36 @@ fn set_gate_contract_passes_and_storage_is_updated() {
assert_eq!(stored_contract.chain, Blockchain::Luniverse);
});
}

#[test]
fn set_gate_faucet_fails_with_non_root() {
ExtBuilder::default().build_and_execute(|| {
let acct: AccountId = AccountId::new([0; 32]);

assert_noop!(Creditcoin::set_gate_faucet(Origin::signed(acct.clone()), acct), BadOrigin);
});
}

#[test]
fn gate_faucet_returns_none_when_not_set() {
ExtBuilder::default().build_and_execute(|| {
let gate_faucet = Creditcoin::gate_faucet_address();

assert!(gate_faucet.is_none());
});
}

#[test]
fn set_gate_faucet_passes_and_storage_is_updated() {
ExtBuilder::default().build_and_execute(|| {
let addr: AccountId = AccountId::new([0; 32]);

assert!(Creditcoin::gate_faucet_address().is_none());
assert_ok!(Creditcoin::set_gate_faucet(RawOrigin::Root.into(), addr.clone()));

let faucet_addr: Option<AccountId32> = Creditcoin::gate_faucet_address();

assert!(faucet_addr.is_some());
assert_eq!(faucet_addr.unwrap(), addr)
});
}
4 changes: 4 additions & 0 deletions pallets/creditcoin/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,8 @@ impl<T: frame_system::Config> crate::WeightInfo for WeightInfo<T> {
fn set_gate_contract() -> Weight {
Weight::from_parts(1,1)
}

fn set_gate_faucet() -> Weight {
Weight::from_parts(0,0)
}
}

0 comments on commit 138b347

Please sign in to comment.