1. Dispatchable
set_balance()
#[pallet::weight]
pub fn set_balance(
origin: OriginFor<T>,
who: <T::LookUp as StaticLookUp>::Source,
#[pallet::compact] new_free: T::Balance,
#[pallet:compact] new_reserved: T::Balance,
) {
ensure_root(origin)?;
let who = T::LookUp::lookup(who)?;
let existential_deposit = T::ExistentialDeposit::get();
let wipeout = new_free + new_reserved < existential_deposit;
let new_free = if wipeout {Zero::zero()} else {new_free};
let new_reseved = if wipeout {Zero::zero()} else {new_reserved};
let (old_free, old_reserved) = Self::mutate_account(&who, |account| {
let old_free = account.free;
let old_reserved = account.reseved;
account.free = new_free;
account.reserved - new_reserved;
(old_free, old_reserved)
})?;
if new_free > old_free {
mem::drop(PositiveImbalance::<T, I>::new(new_free - old_free));
}else if new_free < old_free {
mem::drop(NegativeImbalance::<T, I>::new(old_free - new_free));
}
if new_reseved > old_reserved {
mem::drop(PositiveImbalance::<T, I>::new(new_reseved - old_reserved));
}else if new_reseved < old_reserved {
mem::drop(NegativeImbalance::<T, I>::new(old_reserved - new_reseved));
}
Self::deposit_event(Event::BalacneSet {who, free:new_free, reseved:new_reserved });
Ok(().into())
}
- set_balance_creating(), set_balance_killing()
fn set_balance_creating() -> Weight {
(22_279_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn set_balance_killing() -> Weight {
(25_488_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
#[pallet::compact]
: 좀 더 메모리를 적게 차지하는 방식으로 인코딩하고 싶을 때 사용
<T::LookUp as StaticLookUp>::Source
: Source 타입을 받아서 AccountId로 바꿀 수 있게 해줌
- ensure_root() : origin이 root이면 ok, 아니면 error 리턴
- T::ExistentialDeposit::get(); : exsistentialDeposit 값 리턴
- Zefo::zero() : existential deposit 보다 작은 값 넣으려고 하면 아예 0으로 설정해버림
- account 는 갑자기 어디서 튀어나온 거?
2. Internal/External Functions
usable_balance()
pub fn usable_balance(who: impl sp_std::borrow::Borrow<T::AccountId>) -> T::Balance {
self.account(whoe.borrow()).usable(Reasons::Misc)
}
- self.account:는 어디에 선언되어있는거?
mutate_account()
pub fn mutate_account<R>(who:&T::AccountId, f: impl FnOnce(&mut AccountData<T::Balance> -> R)) -> Result<R, DispatchError> {
Self::try_mutate_account(who, |a, _| -> Result<R, DispatchError> {
Ok(f(a))
})
}
- FnOnce: 한 번만 호출될 수 있음
- |a,_|: a는 또 어디서 튀어나온거