Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify for benchmarks #64

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions pallets/farming/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const USDT: u128 = 2;
const W3G_DECIMALS: u128 = 1_000_000_000_000_000_000;
const USDT_DECIMALS: u128 = 1_000_000;

// Helper to assert last event.
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

fn setup<T: Config>() -> DispatchResult {
let alice: T::AccountId = account("alice", 0, 0);
let bob: T::AccountId = account("bob", 0, 0);
Expand Down Expand Up @@ -51,14 +56,21 @@ fn setup<T: Config>() -> DispatchResult {
benchmarks! {
set_admin {
let alice: T::AccountId = account("alice", 0, 0);
}: _(RawOrigin::Root,alice)
}: _(RawOrigin::Root,alice.clone())
verify {
assert_eq!(Admin::<T>::get(), Some(alice));
}

create_pool {
let alice: T::AccountId = account("alice", 0, 0);
let bob: T::AccountId = account("bob", 0, 0);
setup::<T>()?;
assert_ok!(Farming::<T>::set_admin(RawOrigin::Root.into(),alice.clone()));
}: _(RawOrigin::Signed(alice),T::BlockNumber::from(1u32),T::BlockNumber::from(10u32),T::BlockNumber::from(10u32),W3G,USDT,10 * USDT_DECIMALS)
verify {
assert_eq!(NextPoolId::<T>::get(), 1);
assert_last_event::<T>(Event::<T>::PoolCreated(0).into());
}

staking {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -75,7 +87,15 @@ benchmarks! {
10 * USDT_DECIMALS,
));
System::<T>::set_block_number(T::BlockNumber::from(10u32));
}: _(RawOrigin::Signed(bob),0,10 * W3G_DECIMALS)
}: _(RawOrigin::Signed(bob.clone()),0,10 * W3G_DECIMALS)
verify {
assert_eq!(AccountPoolIdLocked::<T>::get((bob.clone(), 0)), Some(StakingInfo {
staking_balance: 10 * W3G_DECIMALS,
is_claimed: false,
}));
assert_eq!(Pools::<T>::get(0).unwrap().total_locked, 10 * W3G_DECIMALS);
assert_last_event::<T>(Event::<T>::Staking(bob, 0, 10 * W3G_DECIMALS).into());
}

claim {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -98,7 +118,20 @@ benchmarks! {
10 * W3G_DECIMALS,
));
System::<T>::set_block_number(T::BlockNumber::from(30u32));
}: _(RawOrigin::Signed(bob),0)
}: _(RawOrigin::Signed(bob.clone()),0)
verify {
assert_eq!(AccountPoolIdLocked::<T>::get((bob.clone(), 0)), Some(StakingInfo {
staking_balance: 10 * W3G_DECIMALS,
is_claimed: true,
}));
assert_eq!(Pools::<T>::get(0).unwrap().total_locked, 10 * W3G_DECIMALS);
assert_last_event::<T>(Event::<T>::Claim(
bob,
0,
10 * W3G_DECIMALS,
10 * USDT_DECIMALS,
).into());
}

force_claim {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -120,7 +153,11 @@ benchmarks! {
0,
10 * W3G_DECIMALS,
));
}: _(RawOrigin::Signed(alice),bob,0)
}: _(RawOrigin::Signed(alice.clone()),bob,0)
verify {
assert_eq!(AccountPoolIdLocked::<T>::get((alice, 0)), None);
assert_eq!(Pools::<T>::get(0).unwrap().total_locked, 0);
}

impl_benchmark_test_suite!(Farming, crate::mock::new_test_ext(), crate::mock::Test);
}
47 changes: 43 additions & 4 deletions pallets/launchpad/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const USDT: u128 = 2;
const W3G_DECIMALS: u128 = 1_000_000_000_000_000_000;
const USDT_DECIMALS: u128 = 1_000_000;

// Helper to assert last event.
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

fn setup<T: Config>() -> DispatchResult {
let alice: T::AccountId = account("alice", 0, 0);
let bob: T::AccountId = account("bob", 0, 0);
Expand Down Expand Up @@ -53,7 +58,11 @@ benchmarks! {
let alice: T::AccountId = account("alice", 0, 0);
setup::<T>()?;
}: _(RawOrigin::Signed(alice),T::BlockNumber::from(10u32),T::BlockNumber::from(10u32),W3G,USDT,10 * W3G_DECIMALS,1 * USDT_DECIMALS)

verify {
assert_eq!(NextPoolId::<T>::get(), 1);
assert!(Pools::<T>::get(0).is_some());
assert_last_event::<T>(Event::<T>::PoolCreated(0).into());
}
buy_token {
let alice: T::AccountId = account("alice", 0, 0);
let bob: T::AccountId = account("bob", 0, 0);
Expand All @@ -68,7 +77,15 @@ benchmarks! {
1 * USDT_DECIMALS,
));
System::<T>::set_block_number(T::BlockNumber::from(10u32));
}: _(RawOrigin::Signed(bob),0,2)
}: _(RawOrigin::Signed(bob.clone()), 0, 2)
verify {
assert_eq!(AccountPoolIdLocked::<T>::get((bob.clone(), 0)), Some(ClaimInfo {
balance: 2 * W3G_DECIMALS,
is_claimed: false,
}));
assert_eq!(Pools::<T>::get(0).unwrap().raise_amount, 8 * W3G_DECIMALS);
assert_last_event::<T>(Event::<T>::BuyToken(bob, 0, 2 * W3G_DECIMALS).into());
}

claim {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -90,7 +107,19 @@ benchmarks! {
2
));
System::<T>::set_block_number(T::BlockNumber::from(21u32));
}: _(RawOrigin::Signed(bob),0)
}: _(RawOrigin::Signed(bob.clone()),0)
verify {
assert_eq!(AccountPoolIdLocked::<T>::get((bob.clone(), 0)), Some(ClaimInfo {
balance: 2 * W3G_DECIMALS,
is_claimed: true,
}));
assert_eq!(Pools::<T>::get(0).unwrap().raise_amount, 8 * W3G_DECIMALS);
assert_last_event::<T>(Event::<T>::Claim(
bob,
0,
2 * W3G_DECIMALS,
).into());
}

owner_claim {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -106,7 +135,17 @@ benchmarks! {
1 * USDT_DECIMALS,
));
System::<T>::set_block_number(T::BlockNumber::from(21u32));
}: _(RawOrigin::Signed(alice),0)
}: _(RawOrigin::Signed(alice.clone()),0)
verify {
assert_eq!(AccountPoolIdLocked::<T>::get((bob.clone(), 0)), None);
assert_eq!(Pools::<T>::get(0).unwrap().raise_amount, 10 * W3G_DECIMALS);
assert_last_event::<T>(Event::<T>::OwnerClaim(
alice,
0,
10 * W3G_DECIMALS,
0,
).into());
}

impl_benchmark_test_suite!(Launchpad, crate::mock::new_test_ext(), crate::mock::Test);
}
53 changes: 46 additions & 7 deletions pallets/marketplace/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const BLOCK: u32 = 1;
type NonFungibleTokenIdOf<T> = <T as web3games_token_non_fungible::Config>::NonFungibleTokenId;
type TokenIdOf<T> = <T as web3games_token_non_fungible::Config>::TokenId;

// Helper to assert last event.
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

pub fn lookup_of_account<T: Config>(
who: T::AccountId,
) -> <<T as frame_system::Config>::Lookup as StaticLookup>::Source {
Expand Down Expand Up @@ -49,23 +54,38 @@ benchmarks! {

set_admin {
let alice: T::AccountId = account("alice", 0, 0);
}: _(RawOrigin::Root,alice)
}: _(RawOrigin::Root,alice.clone())
verify {
assert_eq!(Admin::<T>::get(), Some(alice));
}

set_service_fee_point {
let alice: T::AccountId = account("alice", 0, 0);
assert_ok!(Marketplace::<T>::set_admin(RawOrigin::Root.into(),alice.clone()));
}: _(RawOrigin::Signed(alice),10u8)
verify {
assert_eq!(Point::<T>::get(), 10);
}

create_order {
let alice: T::AccountId = account("alice", 0, 0);
create_non_fungible_token::<T>();
}: _(RawOrigin::Signed(alice),Asset::NonFungibleToken(1, 2),BalanceOf::<T>::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK))
}: _(RawOrigin::Signed(alice.clone()),Asset::NonFungibleToken(1, 2),BalanceOf::<T>::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK))
verify {
assert!(Orders::<T>::contains_key(Asset::NonFungibleToken(1, 2)));
assert_last_event::<T>(Event::<T>::OrderCreated(alice.clone(), Asset::NonFungibleToken(1, 2), Order { creater: alice, price: BalanceOf::<T>::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(1_u32), duration: T::BlockNumber::from(100 * BLOCK) }).into());
}

cancel_order {
let alice: T::AccountId = account("alice", 0, 0);
create_non_fungible_token::<T>();
assert_ok!(Marketplace::<T>::create_order(RawOrigin::Signed(alice.clone()).into(),Asset::NonFungibleToken(1, 2),BalanceOf::<T>::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK)));
}: _(RawOrigin::Signed(alice),Asset::NonFungibleToken(1, 2))
}: _(RawOrigin::Signed(alice.clone()),Asset::NonFungibleToken(1, 2))
verify {
assert!(!Orders::<T>::contains_key(Asset::NonFungibleToken(1, 2)));
assert!(!Bids::<T>::contains_key(Asset::NonFungibleToken(1, 2)));
assert_last_event::<T>(Event::<T>::OrderCancelled(alice, Asset::NonFungibleToken(1, 2)).into());
}

execute_order {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -78,7 +98,13 @@ benchmarks! {
1000 * W3G,
0,
));
}: _(RawOrigin::Signed(bob),Asset::NonFungibleToken(1, 2))
}: _(RawOrigin::Signed(bob.clone()),Asset::NonFungibleToken(1, 2))
verify {
assert!(!Bids::<T>::contains_key(Asset::NonFungibleToken(1, 2)));
assert!(!Orders::<T>::contains_key(Asset::NonFungibleToken(1, 2)));
assert_last_event::<T>(Event::<T>::OrderExecuted(bob, Asset::NonFungibleToken(1, 2), Order { creater: alice, price: BalanceOf::<T>::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(0_u32), duration: T::BlockNumber::from(100 * BLOCK) }).into());
}


place_bid {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -91,7 +117,11 @@ benchmarks! {
1000 * W3G,
0,
));
}: _(RawOrigin::Signed(bob),Asset::NonFungibleToken(1, 2),BalanceOf::<T>::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK))
}: _(RawOrigin::Signed(bob.clone()),Asset::NonFungibleToken(1, 2),BalanceOf::<T>::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK))
verify {
assert_eq!(Bids::<T>::get(Asset::NonFungibleToken(1, 2)), Some(Order { creater: bob.clone(), price: BalanceOf::<T>::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(1_u32), duration: T::BlockNumber::from(100 * BLOCK) }));
assert_last_event::<T>(Event::<T>::BidCreated(bob.clone(), Asset::NonFungibleToken(1, 2), Order { creater: bob, price: BalanceOf::<T>::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(1_u32), duration: T::BlockNumber::from(100 * BLOCK) }).into());
}

cancel_bid {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -105,7 +135,11 @@ benchmarks! {
0,
));
assert_ok!(Marketplace::<T>::place_bid(RawOrigin::Signed(bob.clone()).into(),Asset::NonFungibleToken(1, 2),BalanceOf::<T>::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK)));
}: _(RawOrigin::Signed(bob),Asset::NonFungibleToken(1, 2))
}: _(RawOrigin::Signed(bob.clone()),Asset::NonFungibleToken(1, 2))
verify {
assert!(!Bids::<T>::contains_key(Asset::NonFungibleToken(1, 2)));
assert_last_event::<T>(Event::<T>::BidCancelled(bob, Asset::NonFungibleToken(1, 2)).into());
}

accept_bid {
let alice: T::AccountId = account("alice", 0, 0);
Expand All @@ -119,7 +153,12 @@ benchmarks! {
0,
));
assert_ok!(Marketplace::<T>::place_bid(RawOrigin::Signed(bob.clone()).into(),Asset::NonFungibleToken(1, 2),BalanceOf::<T>::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK)));
}: _(RawOrigin::Signed(alice),Asset::NonFungibleToken(1, 2))
}: _(RawOrigin::Signed(alice.clone()),Asset::NonFungibleToken(1, 2))
verify {
assert!(!Bids::<T>::contains_key(Asset::NonFungibleToken(1, 2)));
assert!(!Orders::<T>::contains_key(Asset::NonFungibleToken(1, 2)));
assert_last_event::<T>(Event::<T>::BidAccepted(alice, Asset::NonFungibleToken(1, 2), Order { creater: bob, price: BalanceOf::<T>::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(0_u32), duration: T::BlockNumber::from(100 * BLOCK) }).into());
}

impl_benchmark_test_suite!(Marketplace, crate::mock::new_test_ext(), crate::mock::Test);
}