Coins pallet (#399)

* initial implementation

* add function to get a balance of an account

* add support for multiple coins

* rename pallet to "coins-pallet"

* replace balances, assets and tokens pallet with coins pallet in runtime

* add total supply info

* update client side for new Coins pallet

* handle fees

* bug fixes

* Update FeeAccount test

* Fmt

* fix pr comments

* remove extraneous Imbalance type

* Minor tweaks

---------

Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
akildemir
2023-10-19 13:22:21 +03:00
committed by GitHub
parent 3255c0ace5
commit fdfce9e207
32 changed files with 535 additions and 445 deletions

View File

@@ -6,12 +6,11 @@ pub mod pallet {
use sp_std::vec::Vec;
use frame_system::pallet_prelude::*;
use frame_support::{
pallet_prelude::*,
traits::{Currency, tokens::ExistenceRequirement},
};
use frame_support::pallet_prelude::*;
use serai_primitives::{NetworkId, Amount, PublicKey};
use serai_primitives::*;
use coins_pallet::{Config as CoinsConfig, Pallet as Coins};
use validator_sets_pallet::{
primitives::{Session, ValidatorSet},
@@ -29,9 +28,8 @@ pub mod pallet {
#[pallet::config]
pub trait Config:
frame_system::Config + VsConfig + SessionConfig<ValidatorId = PublicKey>
frame_system::Config + CoinsConfig + VsConfig + SessionConfig<ValidatorId = PublicKey>
{
type Currency: Currency<Self::AccountId, Balance = u64>;
}
#[pallet::pallet]
@@ -98,10 +96,8 @@ pub mod pallet {
#[pallet::weight((0, DispatchClass::Operational))] // TODO
pub fn stake(origin: OriginFor<T>, #[pallet::compact] amount: u64) -> DispatchResult {
let signer = ensure_signed(origin)?;
// Serai accounts are solely public keys. Accordingly, there's no harm to letting accounts
// die. They'll simply be re-instantiated later
// AllowDeath accordingly to not add additional requirements (and therefore annoyances)
T::Currency::transfer(&signer, &Self::account(), amount, ExistenceRequirement::AllowDeath)?;
let balance = Balance { coin: Coin::Serai, amount: Amount(amount) };
Coins::<T>::transfer_internal(&signer, &Self::account(), balance)?;
Self::add_stake(&signer, amount);
Ok(())
}
@@ -112,8 +108,8 @@ pub mod pallet {
pub fn unstake(origin: OriginFor<T>, #[pallet::compact] amount: u64) -> DispatchResult {
let signer = ensure_signed(origin)?;
Self::remove_stake(&signer, amount)?;
// This should never be out of funds as there should always be stakers. Accordingly...
T::Currency::transfer(&Self::account(), &signer, amount, ExistenceRequirement::KeepAlive)?;
let balance = Balance { coin: Coin::Serai, amount: Amount(amount) };
Coins::<T>::transfer_internal(&Self::account(), &signer, balance)?;
Ok(())
}