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

Implement pallet-asset-rewards #3926

Merged
merged 200 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
200 commits
Select commit Hold shift + click to select a range
c2cd9ea
wip staking rewards
liamaharon Mar 22, 2024
a02d843
write brief overview
liamaharon Mar 25, 2024
0ed5f8d
seperate reward pots per pool
liamaharon Mar 25, 2024
5582a73
unused imports
liamaharon Mar 25, 2024
c208636
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Mar 26, 2024
9eeebd5
add pool admins
liamaharon Mar 26, 2024
19cd1d2
create_pool implementation, mock and test set up
liamaharon Mar 28, 2024
b0cd831
update tests
liamaharon Mar 28, 2024
d1bf78f
add check for asset existence
liamaharon Mar 28, 2024
bf1f5c8
wip logic
liamaharon Apr 1, 2024
b921166
unstake tests
liamaharon Apr 1, 2024
60191d5
rename pallet-asset-rewards
liamaharon Apr 1, 2024
31a3c89
stake unstake events
liamaharon Apr 1, 2024
7a15551
add integration test
liamaharon Apr 2, 2024
3742869
add expiry block and improve pool admin calls
liamaharon Apr 2, 2024
13cccba
test reward and expiry adjustment
liamaharon Apr 2, 2024
5f39774
check harvesting in integration test
liamaharon Apr 2, 2024
cd65a37
remove redundant comment
liamaharon Apr 2, 2024
155fde7
add assets to mock
liamaharon Apr 3, 2024
5b47b1d
clean up test
liamaharon Apr 3, 2024
81aeb0c
pure internal functions
liamaharon Apr 3, 2024
90463a8
harvest tests
liamaharon Apr 3, 2024
3660369
add dedicated set_pool_reward_rate_per_block tests
liamaharon Apr 3, 2024
16bc43b
set up pool 0 account with rewards
liamaharon Apr 3, 2024
d5c4659
improve expiry block tests
liamaharon Apr 3, 2024
3b47813
rearrange test
liamaharon Apr 3, 2024
aef1b4b
add deposit extrinsic tests
liamaharon Apr 3, 2024
a4c6067
clean up mock
liamaharon Apr 3, 2024
b8344a3
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 3, 2024
cfb3dfa
update docs
liamaharon Apr 3, 2024
ab8ae7d
Merge branch 'liam-staking-rewards-pallet' of github.com:paritytech/p…
liamaharon Apr 3, 2024
9f2c4fa
test withdraw reward token method
liamaharon Apr 3, 2024
1a22048
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 5, 2024
7078d03
update comment
liamaharon Apr 5, 2024
80c28b8
Merge branch 'liam-staking-rewards-pallet' of github.com:paritytech/p…
liamaharon Apr 5, 2024
679724d
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 5, 2024
740a749
wip benchmarks
liamaharon Apr 5, 2024
a243b05
Merge branch 'liam-staking-rewards-pallet' of github.com:paritytech/p…
liamaharon Apr 5, 2024
1dfde3f
remove stub
liamaharon Apr 6, 2024
138fdbb
stake benchmark
liamaharon Apr 6, 2024
159f63f
unstake benchmark
liamaharon Apr 6, 2024
2fc9c04
harvest rewards benchmark
liamaharon Apr 6, 2024
d6cc5c2
set pool reward rate per block benchmark
liamaharon Apr 6, 2024
b532b50
set pool admin benchmark
liamaharon Apr 6, 2024
898852c
benchmark set pool expiry block
liamaharon Apr 6, 2024
96ac58b
deposit reward tokens benchmark
liamaharon Apr 6, 2024
d2ef188
withdraw reward tokens benchmark
liamaharon Apr 6, 2024
5c8185a
remove unused import
liamaharon Apr 6, 2024
15ced21
reduce code duplication
liamaharon Apr 6, 2024
12257da
commit empty weights file
liamaharon Apr 6, 2024
e1eb1d6
integration into kitchensink
liamaharon Apr 7, 2024
da39270
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 9, 2024
0c807d1
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 10, 2024
48a70eb
remove unnecesary bound
liamaharon Apr 10, 2024
b190e47
rococo set up reward pallet assets
liamaharon Apr 10, 2024
2d196c2
configure permissioned pool creator origin
liamaharon Apr 10, 2024
57b1727
refactor origin success accountid
liamaharon Apr 10, 2024
8d0476b
update pallet benchmarks
liamaharon Apr 10, 2024
9389c47
benchmarks finally working for node-template
liamaharon Apr 11, 2024
ce23c68
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Apr 11, 2024
c3ab001
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_asset…
Apr 11, 2024
6c17137
wip asset-hub-rococo benchmarks
liamaharon Apr 11, 2024
e2f09c8
Merge branch 'liam-staking-rewards-pallet' of github.com:paritytech/p…
liamaharon Apr 11, 2024
7900c4b
asset-hub-rococo benchmarks!
liamaharon Apr 11, 2024
5869590
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 11, 2024
7b3209d
use correct relay treasury location
liamaharon Apr 11, 2024
b1c3668
add to asset-hub-westend
liamaharon Apr 11, 2024
77bd1bc
cleanup
liamaharon Apr 11, 2024
0ef2eaa
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 11, 2024
4a65a38
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 11, 2024
29b59d7
cleaner benchmarkhelper
liamaharon Apr 12, 2024
3d7fc42
Merge branch 'liam-staking-rewards-pallet' of github.com:paritytech/p…
liamaharon Apr 12, 2024
8513adb
feature gate
liamaharon Apr 12, 2024
936bc34
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 12, 2024
ffefda8
update docs
liamaharon Apr 12, 2024
531d766
tidy up
liamaharon Apr 12, 2024
c7e8999
Update substrate/frame/asset-rewards/src/mock.rs
liamaharon Apr 12, 2024
36e1e7d
Update substrate/frame/asset-rewards/src/tests.rs
liamaharon Apr 12, 2024
ae17649
Update substrate/frame/asset-rewards/src/tests.rs
liamaharon Apr 12, 2024
b56af27
kitchensink weightinfo
liamaharon Apr 12, 2024
b46d609
Merge branch 'liam-staking-rewards-pallet' of github.com:paritytech/p…
liamaharon Apr 12, 2024
eaa7fdf
".git/.scripts/commands/fmt/fmt.sh"
Apr 12, 2024
1c1646e
fix doc build
liamaharon Apr 12, 2024
2ec6039
Merge branch 'liam-staking-rewards-pallet' of github.com:paritytech/p…
liamaharon Apr 12, 2024
767af86
address comment
liamaharon Apr 12, 2024
4419133
pure fns
liamaharon Apr 12, 2024
f903ea2
is_zero()
liamaharon Apr 12, 2024
cd77fb3
update doc
liamaharon Apr 12, 2024
a2ec1ee
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 15, 2024
67b23fa
finish merging location refactor
liamaharon Apr 15, 2024
9851b31
Update substrate/frame/asset-rewards/src/lib.rs
liamaharon Apr 15, 2024
7e05c06
Update substrate/frame/asset-rewards/src/lib.rs
liamaharon Apr 15, 2024
a162052
Update substrate/frame/asset-rewards/src/lib.rs
liamaharon Apr 15, 2024
be25a9e
Update substrate/frame/asset-rewards/src/lib.rs
liamaharon Apr 15, 2024
9c9da9a
update comment
liamaharon Apr 15, 2024
c3276cf
Merge branch 'liam-staking-rewards-pallet' of github.com:paritytech/p…
liamaharon Apr 15, 2024
446ae58
wip emulated test
liamaharon Apr 16, 2024
488bcf7
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 16, 2024
bf04e6d
wip emulated test
liamaharon Apr 16, 2024
3a851c8
emulated tests
liamaharon Apr 17, 2024
a4eb51f
use poolinfofor
liamaharon Apr 17, 2024
36a6526
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 17, 2024
1ecc7e2
update comment
liamaharon Apr 17, 2024
af92541
improve call docs
liamaharon Apr 17, 2024
905e102
update call comment
liamaharon Apr 17, 2024
ed9e0dc
simplify core logic
liamaharon Apr 17, 2024
c3434eb
reduce scaling factor
liamaharon Apr 17, 2024
700ec32
add disambiguation comment
liamaharon Apr 17, 2024
21c21ae
add test for same staking and reward asset
liamaharon Apr 17, 2024
8c72832
make admin Option
liamaharon Apr 17, 2024
bb21536
rename createpoolorigin
liamaharon Apr 17, 2024
78f642b
rename who to caller
liamaharon Apr 17, 2024
cfabe6f
create_pool take lifetime
liamaharon Apr 17, 2024
10f9bfc
handle overflow
liamaharon Apr 17, 2024
a307dc5
transfer expendable
liamaharon Apr 17, 2024
9767838
integrity test
liamaharon Apr 17, 2024
653cce7
merge
liamaharon Apr 17, 2024
e8e5088
clippy
liamaharon Apr 19, 2024
34df83f
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 19, 2024
98d705d
clippy
liamaharon Apr 19, 2024
d82175c
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 19, 2024
dc08086
fmt
liamaharon Apr 19, 2024
4edc618
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Apr 24, 2024
1872bc2
merge
liamaharon May 7, 2024
fa5c860
remove unused struct
liamaharon May 7, 2024
d64071e
restore tempfile
liamaharon May 7, 2024
abaecea
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon May 7, 2024
b560be7
merge
liamaharon Jun 26, 2024
819f89b
taplo
liamaharon Jun 26, 2024
6d5554f
apply umbrella patch
liamaharon Jun 26, 2024
de5577e
add freezing
liamaharon Jul 1, 2024
9e56945
merge
liamaharon Jul 1, 2024
a466a49
Merge branch 'master' into liam-staking-rewards-pallet
liamaharon Jul 2, 2024
22e90b1
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Jul 25, 2024
6dadb7a
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Jul 29, 2024
d6c61c8
cargo lock, cargo ubmrella
muharem Jul 29, 2024
8ec56f4
make benchmarks more generic
muharem Jul 29, 2024
872e4bd
fix broken imports
muharem Jul 31, 2024
5459c97
ensure_successful for Consideration types
muharem Jul 31, 2024
eb956c1
add Consideration for pool creation
muharem Jul 31, 2024
3e0fba2
fix integration tests
muharem Jul 31, 2024
9250133
remove unused imports
muharem Jul 31, 2024
656d501
remove duplication from wrokspace members list
muharem Jul 31, 2024
5fcee69
taplo format
muharem Jul 31, 2024
196e6c4
remove unused imports
muharem Jul 31, 2024
dec45b3
fix umbrella crate inclusion order
muharem Jul 31, 2024
78fa44e
mock: fund root account for pool deposit
muharem Aug 1, 2024
c4ff420
pool admin is not optional
muharem Aug 7, 2024
cf8fdf3
checked add on pool id increment
muharem Aug 7, 2024
264bcb4
use fungible::increase_frozen
muharem Aug 7, 2024
3d9b058
runtime api to query the pool creation cost
muharem Aug 7, 2024
d233e35
prdoc
muharem Aug 7, 2024
41d6e44
update cargo toml
muharem Aug 7, 2024
99ec6ba
propogate std feature for pallet-assets-freezer
muharem Aug 7, 2024
94c66ff
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Aug 15, 2024
1cb2428
not optional consideration
muharem Aug 15, 2024
00a17f6
Apply suggestions from code review
muharem Aug 28, 2024
5677471
only staker can harvest rewards
muharem Aug 28, 2024
f0e05ec
optional admin
muharem Aug 28, 2024
672d3e5
admin tests
muharem Aug 28, 2024
cf6197a
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Aug 28, 2024
8f138dd
store pool account id
muharem Aug 28, 2024
65c82f9
fix
muharem Aug 28, 2024
6342783
use checked instead saturating
muharem Aug 28, 2024
e1d3327
update prdoc
muharem Aug 29, 2024
0980aba
fix umbrella
muharem Aug 29, 2024
20e4e49
fix umbrella
muharem Aug 29, 2024
97996e1
remove withdraw_reward_tokens
muharem Aug 29, 2024
4dfa197
restrict pool altering
muharem Aug 29, 2024
ad922e0
docs
muharem Aug 29, 2024
a8240f2
clippy
muharem Aug 29, 2024
021bc9c
use dispatch_time type for expiry parameter
muharem Aug 30, 2024
3a82f5e
Apply suggestions from code review
muharem Sep 12, 2024
8cccdb8
RuntimeFreezeReason config parameter
muharem Sep 12, 2024
ecfc903
review fixes
muharem Sep 12, 2024
eef21d7
remove pool_stakers storage item when it is empty
muharem Sep 12, 2024
0c1cff6
cleanup call
muharem Sep 13, 2024
a6183a5
make admin non optional
muharem Sep 13, 2024
04423f4
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Sep 13, 2024
55ed586
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Sep 13, 2024
8ec81e4
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Sep 13, 2024
0562679
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Sep 13, 2024
949760e
westend send xcm origin includes signed origin
muharem Sep 17, 2024
5611441
unstake and harvest calls permissionless when a pool is expired
muharem Sep 17, 2024
0f72302
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Sep 17, 2024
3035d6a
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_asset…
Sep 17, 2024
1fb27c4
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_asset…
Sep 17, 2024
ecac4b5
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_asset…
Sep 17, 2024
5f8fd33
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Dec 22, 2024
7ffe9d1
fmt
muharem Dec 22, 2024
8b85949
add missing config type
muharem Dec 23, 2024
ed39078
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Jan 14, 2025
e21ae76
fix tests
muharem Jan 14, 2025
bca5544
update pr doc
muharem Jan 14, 2025
c9eb609
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Jan 14, 2025
7a1e113
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Jan 14, 2025
8bd72af
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Jan 14, 2025
7e9cb27
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Jan 14, 2025
2dcea37
Merge remote-tracking branch 'origin/master' into liam-staking-reward…
muharem Jan 15, 2025
e45dee7
Merge branch 'master' into liam-staking-rewards-pallet
muharem Jan 16, 2025
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
Prev Previous commit
Next Next commit
tidy up
  • Loading branch information
liamaharon committed Apr 12, 2024
commit 531d766f07f19c4a1205094c1ac0e16a28b84bd8
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ impl pallet_asset_rewards::Config for Runtime {
AccountId,
TreasurerBodyAccount,
>;
type WeightInfo = weights::pallet_asset_rewards::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = PalletAssetRewardsBenchmarkHelper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod cumulus_pallet_xcmp_queue;
pub mod extrinsic_weights;
pub mod frame_system;
pub mod pallet_asset_conversion;
pub mod pallet_asset_rewards;
pub mod pallet_assets_foreign;
pub mod pallet_assets_local;
pub mod pallet_assets_pool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ impl pallet_asset_rewards::Config for Runtime {
AccountId,
TreasurerBodyAccount,
>;
type WeightInfo = weights::pallet_asset_rewards::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = PalletAssetRewardsBenchmarkHelper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod cumulus_pallet_xcmp_queue;
pub mod extrinsic_weights;
pub mod frame_system;
pub mod pallet_asset_conversion;
pub mod pallet_asset_rewards;
pub mod pallet_assets_foreign;
pub mod pallet_assets_local;
pub mod pallet_assets_pool;
Expand Down
79 changes: 40 additions & 39 deletions substrate/frame/asset-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! Once created, holders of the 'staking asset' can 'stake' them in a corresponding pool, which
//! creates a Freeze on the asset.
//!
//! Once staked, the rewards denominated in 'reward asset' begin accumulating to the staker,
//! Once staked, rewards denominated in 'reward asset' begin accumulating to the staker,
//! proportional to their share of the total staked tokens in the pool.
//!
//! Reward assets pending distribution are held in an account unique to each pool.
Expand Down Expand Up @@ -99,22 +99,25 @@ pub mod benchmarking;
mod mock;
#[cfg(test)]
mod tests;
mod weights;

/// The type of the unique id for each pool.
pub use weights::WeightInfo;

/// Unique id type for each pool.
pub type PoolId = u32;

/// Multiplier to maintain precision when calculating rewards.
pub(crate) const PRECISION_SCALING_FACTOR: u32 = u32::MAX;

/// Convenience type alias for `PoolInfo`.
/// Convenience alias for `PoolInfo`.
pub type PoolInfoFor<T> = PoolInfo<
<T as frame_system::Config>::AccountId,
<T as Config>::AssetId,
<T as Config>::Balance,
BlockNumberFor<T>,
>;

/// A pool staker.
/// The state of a staker in a pool.
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Default, Clone, Decode, Encode, MaxEncodedLen, TypeInfo)]
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
pub struct PoolStakerInfo<Balance> {
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
/// Amount of tokens staked.
Expand All @@ -125,28 +128,28 @@ pub struct PoolStakerInfo<Balance> {
reward_per_token_paid: Balance,
}

/// A staking pool.
/// The state and configuration an incentive pool.
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Clone, Decode, Encode, Default, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
pub struct PoolInfo<AccountId, AssetId, Balance, BlockNumber> {
/// The asset that is staked in this pool.
/// The asset staked in this pool.
staked_asset_id: AssetId,
/// The asset that is distributed as rewards in this pool.
/// The asset distributed as rewards by this pool.
reward_asset_id: AssetId,
/// The amount of tokens distributed per block.
/// The amount of tokens rewarded per block.
reward_rate_per_block: Balance,
/// The block the pool will cease distributing rewards.
expiry_block: BlockNumber,
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
/// The permissioned account that can manage this pool.
admin: AccountId,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess to make it trustless there could be a way of removing the admin, or is it really required to have one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible for a future iteration.. I'll make it an Option in case we want to change it later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muharem maybe still make this an option? Otherwise we will need a migration later.

/// The total amount of tokens staked in this pool.
total_tokens_staked: Balance,
/// Total rewards accumulated per token, up to the last time the rewards were updated.
/// Total rewards accumulated per token, up to the `last_update_block`.
reward_per_token_stored: Balance,
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
/// Last block number the pool was updated. Used when calculating payouts.
/// Last block number the pool was updated.
last_update_block: BlockNumber,
/// The block the pool will cease distributing rewards.
expiry_block: BlockNumber,
/// Permissioned account that can manage this pool.
admin: AccountId,
}
gui1117 marked this conversation as resolved.
Show resolved Hide resolved

#[frame_support::pallet(dev_mode)]
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{
Expand All @@ -167,7 +170,7 @@ pub mod pallet {
/// Overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// The pallet's id, used for deriving its sovereign account ID.
/// The pallet's id, used for deriving pool account IDs.
#[pallet::constant]
type PalletId: Get<PalletId>;
liamaharon marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -190,9 +193,9 @@ pub mod pallet {
+ Mutate<Self::AccountId>;

/// Weight information for extrinsics in this pallet.
// type WeightInfo: WeightInfo;
type WeightInfo: WeightInfo;

/// The benchmarks need a way to create asset ids from u32s.
/// Helper for benchmarking.
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: benchmarking::BenchmarkHelper<Self::AssetId, Self::AccountId>;
}
Expand Down Expand Up @@ -246,7 +249,7 @@ pub mod pallet {
},
/// An account harvested some rewards.
RewardsHarvested {
/// The extrinsic caller.
/// The caller.
who: T::AccountId,
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
/// The staker whos rewards were harvested.
staker: T::AccountId,
Expand All @@ -259,7 +262,7 @@ pub mod pallet {
PoolCreated {
/// The account that created the pool.
creator: T::AccountId,
/// Unique ID for the new pool.
/// The unique ID for the new pool.
pool_id: PoolId,
/// The staking asset.
staked_asset_id: T::AssetId,
Expand All @@ -272,19 +275,14 @@ pub mod pallet {
/// The account allowed to modify the pool.
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
admin: T::AccountId,
},
/// A reward pool was deleted by the admin.
PoolDeleted {
/// The deleted pool id.
pool_id: PoolId,
},
/// A pool reward rate was modified by the admin.
PoolRewardRateModified {
/// The modified pool.
pool_id: PoolId,
/// The new reward rate per block.
new_reward_rate_per_block: T::Balance,
},
/// A pool admin modified by the admin.
/// A pool admin was modified.
PoolAdminModified {
/// The modified pool.
pool_id: PoolId,
Expand All @@ -306,30 +304,24 @@ pub mod pallet {
NotEnoughTokens,
/// An operation was attempted on a non-existent pool.
NonExistentPool,
/// An operation was attempted on a non-existent pool.
/// An operation was attempted for a non-existent staker.
NonExistentStaker,
/// An operation was attempted using a non-existent asset.
/// An operation was attempted with a non-existent asset.
NonExistentAsset,
/// There was an error converting a block number.
BlockNumberConversionError,
/// Expiry block must be in the future.
/// The expiry block must be in the future.
ExpiryBlockMustBeInTheFuture,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn integrity_test() {
// TODO: Proper implementation
}
}

/// Pallet's callable functions.
///
/// Allows optionally specifying an admin account for the pool. By default, the origin is made
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
/// admin.
#[pallet::call]
#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config> Pallet<T> {
/// Create a new reward pool.
#[pallet::call_index(0)]
pub fn create_pool(
origin: OriginFor<T>,
staked_asset_id: Box<T::AssetId>,
Expand Down Expand Up @@ -392,6 +384,7 @@ pub mod pallet {
}

/// Stake tokens in a pool.
#[pallet::call_index(1)]
pub fn stake(origin: OriginFor<T>, pool_id: PoolId, amount: T::Balance) -> DispatchResult {
let caller = ensure_signed(origin)?;

Expand Down Expand Up @@ -419,6 +412,7 @@ pub mod pallet {
}

/// Unstake tokens from a pool.
#[pallet::call_index(2)]
pub fn unstake(
origin: OriginFor<T>,
pool_id: PoolId,
Expand Down Expand Up @@ -453,6 +447,7 @@ pub mod pallet {
}

/// Harvest unclaimed pool rewards for a staker.
#[pallet::call_index(3)]
pub fn harvest_rewards(
origin: OriginFor<T>,
pool_id: PoolId,
Expand Down Expand Up @@ -495,6 +490,7 @@ pub mod pallet {
}

/// Modify a pool reward rate.
#[pallet::call_index(4)]
pub fn set_pool_reward_rate_per_block(
origin: OriginFor<T>,
pool_id: PoolId,
Expand All @@ -521,6 +517,7 @@ pub mod pallet {
}

/// Modify a pool admin.
#[pallet::call_index(5)]
pub fn set_pool_admin(
origin: OriginFor<T>,
pool_id: PoolId,
Expand All @@ -540,6 +537,7 @@ pub mod pallet {
}

/// Modify a expiry block.
muharem marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::call_index(6)]
pub fn set_pool_expiry_block(
origin: OriginFor<T>,
pool_id: PoolId,
Expand Down Expand Up @@ -571,6 +569,7 @@ pub mod pallet {
/// This method is not strictly necessary (tokens could be transferred directly to the
/// pool pot address), but is provided for convenience so manual derivation of the
/// account id is not required.
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::call_index(7)]
pub fn deposit_reward_tokens(
origin: OriginFor<T>,
pool_id: PoolId,
Expand All @@ -590,6 +589,7 @@ pub mod pallet {
}
gui1117 marked this conversation as resolved.
Show resolved Hide resolved

/// Permissioned method to withdraw reward tokens from a pool.
#[pallet::call_index(8)]
pub fn withdraw_reward_tokens(
origin: OriginFor<T>,
pool_id: PoolId,
Expand All @@ -601,6 +601,7 @@ pub mod pallet {

let pool_info = Pools::<T>::get(pool_id).ok_or(Error::<T>::NonExistentPool)?;
ensure!(pool_info.admin == caller, BadOrigin);

T::Assets::transfer(
pool_info.reward_asset_id,
&Self::pool_account_id(&pool_id)?,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should exclude already accumulated rewards? Seems a bit weird otherwise?!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just not let to do it. There will be no rewards for the next block, also same weird for someone who staked before.
If admin set, I would allow only extend the expiration and increase reward per token paid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed that call and restricted other calls the way I proposed above. I have not changed the named for the functions. If we really will have a legit cases for withdrawing or decreasing expiration and reward rate we can bring that back.

Expand Down Expand Up @@ -661,7 +662,7 @@ pub mod pallet {

/// Derives the current reward per token for this pool.
///
/// Helper function for update_pool_rewards. Should not be called directly.
/// This is a helper function for `update_pool_rewards` and should not be called directly.
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
fn reward_per_token(pool_info: &PoolInfoFor<T>) -> Result<T::Balance, DispatchError> {
if pool_info.total_tokens_staked.eq(&0u32.into()) {
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
return Ok(pool_info.reward_per_token_stored)
Expand All @@ -687,7 +688,7 @@ pub mod pallet {

/// Derives the amount of rewards earned by a staker.
///
/// Helper function for update_pool_rewards. Should not be called directly.
/// This is a helper function for `update_pool_rewards` and should not be called directly.
fn derive_rewards(
pool_info: &PoolInfoFor<T>,
staker_info: &PoolStakerInfo<T::Balance>,
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/asset-rewards/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl Config for MockRuntime {
type Assets = NativeAndAssets;
type PalletId = StakingRewardsPalletId;
type PermissionedOrigin = MockPermissionedOrigin;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = AssetRewardsBenchmarkHelper;
}
Expand Down
Loading