|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +//! Traits for funding sources and sinks in token issuance systems. |
| 19 | +//! |
| 20 | +//! This module provides abstractions for pulling funds (rewards, payments) and returning funds |
| 21 | +//! (burns, slashing) in a way that can be configured differently per runtime. |
| 22 | +//! |
| 23 | +//! Two main patterns: |
| 24 | +//! - **Direct mint/burn**: Traditional approach where funds are created/destroyed on demand |
| 25 | +//! - **Buffer-based**: Funds are pre-minted into a buffer and distributed from there |
| 26 | +
|
| 27 | +use crate::traits::tokens::{fungible, Fortitude, Precision, Preservation}; |
| 28 | +use core::marker::PhantomData; |
| 29 | +use sp_runtime::{DispatchError, DispatchResult}; |
| 30 | + |
| 31 | +/// Trait for requesting funds from an issuance system. |
| 32 | +/// |
| 33 | +/// Implementations can either mint directly or pull from a pre-minted buffer. |
| 34 | +pub trait FundingSource<AccountId, Balance> { |
| 35 | + /// Request funds to be transferred to the beneficiary. |
| 36 | + /// |
| 37 | + /// Returns the actual amount transferred, which may be less than requested |
| 38 | + /// if the source has insufficient funds. |
| 39 | + fn request_funds(beneficiary: &AccountId, amount: Balance) -> Result<Balance, DispatchError>; |
| 40 | +} |
| 41 | + |
| 42 | +/// Trait for returning funds to an issuance system. |
| 43 | +/// |
| 44 | +/// Implementations can either burn directly or return to a buffer for reuse. |
| 45 | +pub trait FundingSink<AccountId, Balance> { |
| 46 | + /// Return funds from the given account back to the issuance system. |
| 47 | + /// |
| 48 | + /// This could mean burning the funds or transferring them to a buffer account. |
| 49 | + fn return_funds(from: &AccountId, amount: Balance) -> DispatchResult; |
| 50 | +} |
| 51 | + |
| 52 | +/// Direct minting implementation of `FundingSource`. |
| 53 | +/// |
| 54 | +/// This implementation mints tokens directly when funds are requested. |
| 55 | +/// Used for traditional mint-on-demand systems (e.g., Kusama). |
| 56 | +/// |
| 57 | +/// # Type Parameters |
| 58 | +/// |
| 59 | +/// * `Currency` - The currency type that implements `Mutate` |
| 60 | +/// * `AccountId` - The account identifier type |
| 61 | +pub struct DirectMint<Currency, AccountId>(PhantomData<(Currency, AccountId)>); |
| 62 | + |
| 63 | +impl<Currency, AccountId> FundingSource<AccountId, Currency::Balance> |
| 64 | + for DirectMint<Currency, AccountId> |
| 65 | +where |
| 66 | + Currency: fungible::Mutate<AccountId>, |
| 67 | + AccountId: Eq, |
| 68 | +{ |
| 69 | + fn request_funds( |
| 70 | + beneficiary: &AccountId, |
| 71 | + amount: Currency::Balance, |
| 72 | + ) -> Result<Currency::Balance, DispatchError> { |
| 73 | + Currency::mint_into(beneficiary, amount)?; |
| 74 | + Ok(amount) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Direct burning implementation of `FundingSink`. |
| 79 | +/// |
| 80 | +/// This implementation burns tokens directly when funds are returned. |
| 81 | +/// Used for traditional burn-on-return systems (e.g., Kusama). |
| 82 | +/// |
| 83 | +/// # Type Parameters |
| 84 | +/// |
| 85 | +/// * `Currency` - The currency type that implements `Mutate` |
| 86 | +/// * `AccountId` - The account identifier type |
| 87 | +pub struct DirectBurn<Currency, AccountId>(PhantomData<(Currency, AccountId)>); |
| 88 | + |
| 89 | +impl<Currency, AccountId> FundingSink<AccountId, Currency::Balance> |
| 90 | + for DirectBurn<Currency, AccountId> |
| 91 | +where |
| 92 | + Currency: fungible::Mutate<AccountId>, |
| 93 | + AccountId: Eq, |
| 94 | +{ |
| 95 | + fn return_funds(from: &AccountId, amount: Currency::Balance) -> DispatchResult { |
| 96 | + Currency::burn_from( |
| 97 | + from, |
| 98 | + amount, |
| 99 | + Preservation::Expendable, |
| 100 | + Precision::Exact, |
| 101 | + Fortitude::Polite, |
| 102 | + )?; |
| 103 | + Ok(()) |
| 104 | + } |
| 105 | +} |
0 commit comments