Skip to content

Commit 7a84637

Browse files
feat: use withdraw instead of slash when contract is billed (#460)
1 parent dd1581c commit 7a84637

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

substrate-node/pallets/pallet-smart-contract/src/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use frame_support::{
99
pallet_prelude::DispatchResult,
1010
traits::{
1111
Currency, EnsureOrigin, ExistenceRequirement, ExistenceRequirement::KeepAlive, Get,
12-
LockableCurrency, WithdrawReasons,
12+
LockableCurrency, OnUnbalanced, WithdrawReasons,
1313
},
1414
transactional,
1515
weights::Pays,
@@ -51,7 +51,7 @@ pub mod pallet {
5151
use frame_support::{
5252
dispatch::DispatchResultWithPostInfo,
5353
log,
54-
traits::{Currency, Get, LockIdentifier, LockableCurrency},
54+
traits::{Currency, Get, LockIdentifier, LockableCurrency, OnUnbalanced},
5555
};
5656
use frame_system::pallet_prelude::*;
5757
use sp_core::H256;
@@ -64,6 +64,8 @@ pub mod pallet {
6464

6565
pub type BalanceOf<T> =
6666
<<T as Config>::Currency as Currency<<T as system::Config>::AccountId>>::Balance;
67+
pub type NegativeImbalanceOf<T> =
68+
<<T as Config>::Currency as Currency<<T as system::Config>::AccountId>>::NegativeImbalance;
6769

6870
pub const GRID_LOCK_ID: LockIdentifier = *b"gridlock";
6971

@@ -165,6 +167,8 @@ pub mod pallet {
165167
{
166168
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
167169
type Currency: LockableCurrency<Self::AccountId>;
170+
/// Handler for the unbalanced decrement when slashing (burning collateral)
171+
type Burn: OnUnbalanced<NegativeImbalanceOf<Self>>;
168172
type StakingPoolAccount: Get<Self::AccountId>;
169173
type BillingFrequency: Get<u64>;
170174
type DistributionFrequency: Get<u16>;
@@ -1297,11 +1301,18 @@ impl<T: Config> Pallet<T> {
12971301
- existential_deposit_requirement;
12981302
}
12991303

1300-
<T as Config>::Currency::slash(&twin.account_id, amount_to_burn);
1304+
let to_burn = T::Currency::withdraw(
1305+
&twin.account_id,
1306+
amount_to_burn,
1307+
WithdrawReasons::FEE,
1308+
ExistenceRequirement::KeepAlive,
1309+
)?;
1310+
T::Burn::on_unbalanced(to_burn);
13011311
Self::deposit_event(Event::TokensBurned {
13021312
contract_id: contract.contract_id,
13031313
amount: amount_to_burn,
13041314
});
1315+
13051316
Ok(().into())
13061317
}
13071318

substrate-node/pallets/pallet-smart-contract/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ use weights;
194194
impl pallet_smart_contract::Config for TestRuntime {
195195
type Event = Event;
196196
type Currency = Balances;
197+
type Burn = ();
197198
type StakingPoolAccount = StakingPoolAccount;
198199
type BillingFrequency = BillingFrequency;
199200
type DistributionFrequency = DistributionFrequency;

substrate-node/runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ impl pallet_smart_contract::Config for Runtime {
401401
type RestrictedOrigin = EnsureRootOrCouncilApproval;
402402
type MaxDeploymentDataLength = MaxDeploymentDataLength;
403403
type MaxNodeContractPublicIps = MaxNodeContractPublicIPs;
404+
type Burn = ();
404405
}
405-
// type Tfgrid = TfgridModule;
406406

407407
impl pallet_tft_bridge::Config for Runtime {
408408
type Event = Event;

0 commit comments

Comments
 (0)