@@ -12,7 +12,7 @@ mod tests;
1212
1313use frame_support:: {
1414 decl_error, decl_event, decl_module, decl_storage,
15- dispatch:: DispatchResult ,
15+ dispatch:: { DispatchError , DispatchResult } ,
1616 ensure,
1717 storage:: IterableStorageMap ,
1818 traits:: { Currency , ExistenceRequirement } ,
@@ -42,20 +42,29 @@ pub trait Trait: xpallet_assets::Trait {
4242 /// The overarching event type.
4343 type Event : From < Event < Self > > + Into < <Self as frame_system:: Trait >:: Event > ;
4444
45- ///
45+ /// Get the staked balances of asset miner.
4646 type StakingInterface : StakingInterface < Self :: AccountId , u128 > ;
4747
48- ///
48+ /// Get the possible referral of asset miner.
49+ type GatewayInterface : GatewayInterface < Self :: AccountId > ;
50+
51+ /// Get the treasury account.
4952 type TreasuryAccount : TreasuryAccount < Self :: AccountId > ;
5053
51- ///
54+ /// Generate the reward pot account for mining asset.
5255 type DetermineRewardPotAccount : RewardPotAccountFor < Self :: AccountId , AssetId > ;
5356}
5457
5558pub trait StakingInterface < AccountId , Balance > {
5659 fn staked_of ( who : & AccountId ) -> Balance ;
5760}
5861
62+ impl < AccountId , Balance : Default > StakingInterface < AccountId , Balance > for ( ) {
63+ fn staked_of ( _: & AccountId ) -> Balance {
64+ Default :: default ( )
65+ }
66+ }
67+
5968impl < T : Trait > StakingInterface < <T as frame_system:: Trait >:: AccountId , u128 > for T
6069where
6170 T : xpallet_mining_staking:: Trait ,
@@ -65,23 +74,33 @@ where
6574 }
6675}
6776
77+ pub trait GatewayInterface < AccountId > {
78+ fn referral_of ( who : & AccountId ) -> Option < AccountId > ;
79+ }
80+
81+ impl < AccountId > GatewayInterface < AccountId > for ( ) {
82+ fn referral_of ( _: & AccountId ) -> Option < AccountId > {
83+ None
84+ }
85+ }
86+
6887decl_storage ! {
6988 trait Store for Module <T : Trait > as XMiningAsset {
70- ///
89+ /// Possible reward for the new asset owners that does not have native coins yet.
7190 pub DepositReward get( fn deposit_reward) : BalanceOf <T > = 100_000 . into( ) ;
7291
73- ///
92+ /// Can not claim if the claimer violates the restriction.
7493 pub ClaimRestrictionOf get( fn claim_restriction_of) :
7594 map hasher( twox_64_concat) AssetId => ClaimRestriction <T :: BlockNumber >;
7695
7796 /// External Assets that have the mining rights.
7897 pub MiningPrevilegedAssets get( fn mining_previleged_assets) : Vec <AssetId >;
7998
80- /// Mining weight information of the asset .
99+ /// Mining weight information of the mining assets .
81100 pub AssetLedgers get( fn asset_ledgers) :
82101 map hasher( twox_64_concat) AssetId => AssetLedger <T :: BlockNumber >;
83102
84- /// The map from nominator to the vote weight ledger of all nominees .
103+ /// The map from nominator to the vote weight ledger of all mining assets .
85104 pub MinerLedgers get( fn miner_ledgers) :
86105 double_map hasher( twox_64_concat) T :: AccountId , hasher( twox_64_concat) AssetId
87106 => MinerLedger <T :: BlockNumber >;
@@ -113,24 +132,24 @@ decl_event!(
113132 Balance = BalanceOf <T >,
114133 <T as frame_system:: Trait >:: AccountId ,
115134 {
116- ///
117- Claim ( AccountId , AccountId , Balance ) ,
135+ /// Claimed the asset mining rewards. [claimer, asset_id, amount]
136+ Claim ( AccountId , AssetId , Balance ) ,
118137 }
119138) ;
120139
121140decl_error ! {
122141 /// Error for the staking module.
123142 pub enum Error for Module <T : Trait > {
124143 /// The asset does not have the mining rights.
125- UnprevilegedAsset ,
144+ NotPrevilegedAsset ,
126145 /// Claimer does not have enough Staking locked balance.
127146 InsufficientStaking ,
128147 /// Claimer just did a claim recently, the next frequency limit is not expired.
129148 UnexpiredFrequencyLimit ,
130- /// Asset error.
131- AssetError ,
132149 /// Zero mining weight.
133- ZeroMiningWeight
150+ ZeroMiningWeight ,
151+ /// Balances error.
152+ DispatchError
134153 }
135154}
136155
@@ -140,6 +159,12 @@ impl<T: Trait> From<ZeroMiningWeightError> for Error<T> {
140159 }
141160}
142161
162+ impl < T : Trait > From < DispatchError > for Error < T > {
163+ fn from ( _: DispatchError ) -> Self {
164+ Self :: DispatchError
165+ }
166+ }
167+
143168decl_module ! {
144169 pub struct Module <T : Trait > for enum Call where origin: T :: Origin {
145170 type Error = Error <T >;
@@ -153,7 +178,7 @@ decl_module! {
153178
154179 ensure!(
155180 Self :: mining_previleged_assets( ) . contains( & target) ,
156- Error :: <T >:: UnprevilegedAsset
181+ Error :: <T >:: NotPrevilegedAsset
157182 ) ;
158183
159184 <Self as Claim <T :: AccountId >>:: claim( & sender, & target) ?;
@@ -174,6 +199,12 @@ decl_module! {
174199 restriction. frequency_limit = new;
175200 } ) ;
176201 }
202+
203+ #[ weight = 10 ]
204+ fn set_x_asset_power( origin, asset_id: AssetId , new: FixedAssetPower ) {
205+ ensure_root( origin) ?;
206+ XTypeAssetPowerMap :: insert( asset_id, new) ;
207+ }
177208 }
178209}
179210
@@ -183,6 +214,21 @@ impl<T: Trait> Module<T> {
183214 MinerLedgers :: < T > :: get ( who, asset_id) . last_claim
184215 }
185216
217+ #[ inline]
218+ fn free_balance ( who : & T :: AccountId ) -> BalanceOf < T > {
219+ <T as xpallet_assets:: Trait >:: Currency :: free_balance ( who)
220+ }
221+
222+ #[ inline]
223+ fn transfer ( from : & T :: AccountId , to : & T :: AccountId , value : BalanceOf < T > ) -> DispatchResult {
224+ <T as xpallet_assets:: Trait >:: Currency :: transfer (
225+ from,
226+ to,
227+ value,
228+ ExistenceRequirement :: KeepAlive ,
229+ )
230+ }
231+
186232 /// This rule doesn't take effect if the interval is zero.
187233 fn passed_enough_interval (
188234 who : & T :: AccountId ,
@@ -302,17 +348,14 @@ impl<T: Trait> Module<T> {
302348 Self :: update_asset_mining_weight ( target, current_block) ;
303349 }
304350
351+ /// Gives a tiny reward to the depositor in case of it
352+ /// does not have enough balances to claim the mining reward.
305353 fn issue_deposit_reward ( depositor : & T :: AccountId , target : & AssetId ) -> DispatchResult {
306354 let deposit_reward = Self :: deposit_reward ( ) ;
307355 let reward_pot = T :: DetermineRewardPotAccount :: reward_pot_account_for ( target) ;
308- let reward_pot_balance = <T as xpallet_assets:: Trait >:: Currency :: free_balance ( & reward_pot) ;
309- if reward_pot_balance >= deposit_reward {
310- <T as xpallet_assets:: Trait >:: Currency :: transfer (
311- & reward_pot,
312- depositor,
313- deposit_reward,
314- ExistenceRequirement :: KeepAlive ,
315- ) ?;
356+ let reward_pot_balance = Self :: free_balance ( & reward_pot) ;
357+ if reward_pot_balance >= deposit_reward && Self :: free_balance ( depositor) <= deposit_reward {
358+ Self :: transfer ( & reward_pot, depositor, deposit_reward) ?;
316359 } else {
317360 warn ! ( "asset {}'s reward pot has only {:?}, skipped issuing deposit reward for depositor {:?}" , target, reward_pot_balance, depositor) ;
318361 }
0 commit comments