Custom Balances Pallet 작성

코와->코어·2022년 7월 3일
0

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,
        ) {
                // check the origin === root
                ensure_root(origin)?;
                let who = T::LookUp::lookup(who)?;
                let existential_deposit = T::ExistentialDeposit::get();

                // get current target and his balance
                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};

                // calculate new free/reseved balance > existential deposit
                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)
                })?;

                // change total issuance
                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));
                }

                // trigger deposit event
                Self::deposit_event(Event::BalacneSet {who, free:new_free, reseved:new_reserved });
                Ok(().into())
            }
  • set_balance_creating(), set_balance_killing()
// Storage: System Account (r:1 w:1)
	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))
	}
	// Storage: System Account (r:1 w:1)
	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 {
            // usable balance 가져오기
            self.account(whoe.borrow()).usable(Reasons::Misc)

        }
  • self.account:는 어디에 선언되어있는거?

mutate_account()

// 명하
        // account의 balance 값을 업데이트. 기존에 있는 계정인지 헤크해야 함
        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는 또 어디서 튀어나온거
profile
풀스택 웹개발자👩‍💻✨️

0개의 댓글