I want to mint new tokens to a particular account, I found a function fn mint_into in balances pallet, it is been implemented for fungible assets that's what I understood from the code maybe I am wrong. So what I want to know is that how can I mint new tokens after deploying the blockchain, as tokens are been minted for the validator rewards according to tokenomics, I just want to know how can I mint new tokens according to my needs.
2 Answers
Bruno mentions how to do this with the fungibles::*
traits, but the more common API that people use is simply the Currency
trait.
With Currency
, trait you have access to a few different functions which could do this, but just as an example, you could use:
/// Adds up to `value` to the free balance of `who`. If `who` doesn't exist, it is created.
///
/// Infallible.
fn deposit_creating(who: &AccountId, value: Self::Balance) -> Self::PositiveImbalance;
By dropping the positive imbalance that is returned by this function, you will automatically update the total issuance of your blockchain.
This is what the Staking Pallet does to pay users their staking rewards, and inflate the overall token supply:
/// Actually make a payment to a staker. This uses the currency's reward function
/// to pay the right payee for the given staker account.
fn make_payout(stash: &T::AccountId, amount: BalanceOf<T>) -> Option<PositiveImbalanceOf<T>> {
let dest = Self::payee(stash);
match dest {
RewardDestination::Controller => Self::bonded(stash)
.map(|controller| T::Currency::deposit_creating(&controller, amount)),
RewardDestination::Stash => T::Currency::deposit_into_existing(stash, amount).ok(),
RewardDestination::Staked => Self::bonded(stash)
.and_then(|c| Self::ledger(&c).map(|l| (c, l)))
.and_then(|(controller, mut l)| {
l.active += amount;
l.total += amount;
let r = T::Currency::deposit_into_existing(stash, amount).ok();
Self::update_ledger(&controller, &l);
r
}),
RewardDestination::Account(dest_account) =>
Some(T::Currency::deposit_creating(&dest_account, amount)),
RewardDestination::None => None,
}
}
-
can i declare a default account id balances pallet, to mint new tokens in that particular account? I was doing the same but not able to achieve it, can you tell some work around for this. Mar 24, 2022 at 7:37
-
@Shawn could you elaborate on why you didn't choose a
sudo
call likeset_balance
Mar 24, 2022 at 10:26 -
@ShubhamGupta "default accounts" like what you are describing are not really a pattern we use in Substrate. Instead, you should have intentional accounts for specific purposes generated by the runtime code. I think you should open your own question with your scenario and all the details you can provide, and we can suggest a solution.– Shawn Tabrizi ♦Mar 24, 2022 at 13:46
-
@Purple_Turtle When discussing balance APIs, none of these are "sudo", they are all low level backed APIs. It does not make sense to call extrinsics when you can do things much more efficiently using a specific api design for a scenario. Extrinsics are meant for external users to access and modify the chain.– Shawn Tabrizi ♦Mar 24, 2022 at 13:48
mint_into
without burn_from
will mint new tokens and increase the overall supply.
/// Attempt to increase the `asset` balance of `who` by `amount`.
///
/// If not possible then don't do anything. Possible reasons for failure include:
/// - Minimum balance not met.
/// - Account cannot be created (e.g. because there is no provider reference and/or the asset
/// isn't considered worth anything).
///
/// Since this is an operation which should be possible to take alone, if successful it will
/// increase the overall supply of the underlying token.
fn mint_into(asset: Self::AssetId, who: &AccountId, amount: Self::Balance) -> DispatchResult;
mint_into
is a Rust trait defined in Frame Support. You can see it is being implemented in assets pallet and balances pallet.
You can see this is what teleport uses. The only difference is that teleport also calls burn_from
.
/// Transfer funds from one account into another. The default implementation uses `mint_into`
/// and `burn_from` and may generate unwanted events.
fn teleport(asset: Self::AssetId, source: &AccountId, dest: &AccountId, amount: Self::Balance,)
-
I am curious why can't sudo just call something like
set_balance
and increase the overall supply whilst achieving their desired minting in one call? If burning the excess from some other account isn't a problem? Mar 24, 2022 at 10:25