Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ members = [
"frame/lottery",
"frame/membership",
"frame/merkle-mountain-range",
"frame/multi-asset-treasury",
"frame/multisig",
"frame/nicks",
"frame/node-authorization",
Expand Down
34 changes: 34 additions & 0 deletions bin/node/runtime/src/assets_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is part of Substrate.

// Copyright (C) 2018-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Runtime API definition for assets.

use codec::Codec;
use sp_std::vec::Vec;

sp_api::decl_runtime_apis! {
pub trait AssetsApi<AccountId, AssetBalance, AssetId>
where
AccountId: Codec,
AssetBalance: Codec,
AssetId: Codec,
{
/// Returns the list of `AssetId`s and corresponding balance that an `AccountId` has.
fn account_balances(account: AccountId) -> Vec<(AssetId, AssetBalance)>;
}
}
15 changes: 15 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ use sp_runtime::generic::Era;
/// Generated voter bag information.
mod voter_bags;

/// Runtime API definition for assets.
pub mod assets_api;

// Make the WASM binary available.
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
Expand Down Expand Up @@ -2093,6 +2096,18 @@ impl_runtime_apis! {
}
}

impl assets_api::AssetsApi<
Block,
AccountId,
Balance,
u32,
> for Runtime
{
fn account_balances(account: AccountId) -> Vec<(u32, Balance)> {
Assets::account_balances(account)
}
}

impl pallet_contracts::ContractsApi<Block, AccountId, Balance, BlockNumber, Hash> for Runtime
{
fn call(
Expand Down
7 changes: 7 additions & 0 deletions frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,4 +931,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Ok(())
})
}

/// Returns all the non-zero balances for all assets of the given `account`.
pub fn account_balances(account: T::AccountId) -> Vec<(T::AssetId, T::Balance)> {
Asset::<T, I>::iter_keys()
.filter_map(|id| Self::maybe_balance(id, account.clone()).map(|balance| (id, balance)))
.collect::<Vec<_>>()
}
}
79 changes: 74 additions & 5 deletions frame/assets/src/impl_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

//! Implementations for fungibles trait.

use frame_support::traits::tokens::{
Fortitude,
Precision::{self, BestEffort},
Preservation::{self, Expendable},
Provenance::{self, Minted},
use frame_support::traits::{
fungibles::Credit,
tokens::{
Fortitude,
Precision::{self, BestEffort},
Preservation::{self, Expendable},
Provenance::{self, Minted},
},
};

use super::*;
Expand Down Expand Up @@ -258,3 +261,69 @@ impl<T: Config<I>, I: 'static> fungibles::InspectEnumerable<T::AccountId> for Pa
Asset::<T, I>::iter_keys()
}
}

impl<T: Config<I>, I: 'static> fungibles::MutateHold<<T as SystemConfig>::AccountId>
for Pallet<T, I>
{
}

impl<T: Config<I>, I: 'static> fungibles::InspectHold<<T as SystemConfig>::AccountId>
for Pallet<T, I>
{
type Reason = T::HoldIdentifier;

fn total_balance_on_hold(asset: T::AssetId, who: &T::AccountId) -> T::Balance {
Zero::zero()
}
fn reducible_total_balance_on_hold(
asset: T::AssetId,
who: &T::AccountId,
force: Fortitude,
) -> T::Balance {
// // The total balance must never drop below the freeze requirements if we're not forcing:
// let a = Self::account(who);
// let unavailable = if force == Force {
// Self::Balance::zero()
// } else {
// // The freeze lock applies to the total balance, so we can discount the free balance
// // from the amount which the total reserved balance must provide to satisfy it.
// a.frozen.saturating_sub(a.free)
// };
// a.reserved.saturating_sub(unavailable)
Zero::zero()
}
fn balance_on_hold(asset: T::AssetId, reason: &Self::Reason, who: &T::AccountId) -> T::Balance {
// Holds::<T, I>::get(who)
// .iter()
// .find(|x| &x.id == reason)
// .map_or_else(Zero::zero, |x| x.amount)
Zero::zero()
}
fn hold_available(asset: T::AssetId, reason: &Self::Reason, who: &T::AccountId) -> bool {
false
}
}

impl<T: Config<I>, I: 'static> fungibles::UnbalancedHold<<T as SystemConfig>::AccountId>
for Pallet<T, I>
{
fn set_balance_on_hold(
asset: T::AssetId,
reason: &T::HoldIdentifier,
who: &T::AccountId,
amount: T::Balance,
) -> DispatchResult {
Ok(())
}
}

impl<T: Config<I>, I: 'static> fungibles::BalancedHold<T::AccountId> for Pallet<T, I> {
fn slash(
asset: T::AssetId,
reason: &T::HoldIdentifier,
who: &T::AccountId,
amount: T::Balance,
) -> (Credit<T::AccountId, Self>, T::Balance) {
(<Credit<T::AccountId, Self>>::zero(asset), Zero::zero())
}
}
2 changes: 2 additions & 0 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ pub mod pallet {
+ MaxEncodedLen
+ TypeInfo;

type HoldIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;

/// Max number of items to destroy per `destroy_accounts` and `destroy_approvals` call.
///
/// Must be configured to result in a weight that makes each call fit in a block.
Expand Down
5 changes: 4 additions & 1 deletion frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ fn asset_ids() -> Vec<u32> {
fn basic_minting_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 0, 1, true, 1));
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 1, 1, true, 1));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 1, 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 2, 100));
assert_eq!(Assets::balance(0, 2), 100);
assert_eq!(asset_ids(), vec![0, 999]);
assert_eq!(asset_ids(), vec![0, 1, 999]);
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 1, 1, 100));
assert_eq!(Assets::account_balances(1), vec![(0, 100), (999, 100), (1, 100)]);
});
}

Expand Down
54 changes: 54 additions & 0 deletions frame/multi-asset-treasury/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "pallet-multi-asset-treasury"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME pallet to manage treasury"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = [
"derive",
"max-encoded-len",
] }
impl-trait-for-tuples = "0.2.2"
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", features = ["derive"], optional = true }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
pallet-balances = { version = "4.0.0-dev", default-features = false, path = "../balances" }
pallet-assets = { version = "4.0.0-dev", default-features = false, path = "../assets" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
sp-core = { version = "7.0.0", path = "../../primitives/core" }
sp-io = { version = "7.0.0", path = "../../primitives/io" }

[features]
default = ["std"]
std = [
"frame-benchmarking?/std",
"codec/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"pallet-assets/std",
"scale-info/std",
"serde",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
31 changes: 31 additions & 0 deletions frame/multi-asset-treasury/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Treasury Pallet

The Treasury pallet provides a "pot" of funds that can be managed by stakeholders in the system and
a structure for making spending proposals from this pot.

## Overview

The Treasury Pallet itself provides the pot to store funds, and a means for stakeholders to propose,
approve, and deny expenditures. The chain will need to provide a method (e.g.inflation, fees) for
collecting funds.

By way of example, the Council could vote to fund the Treasury with a portion of the block reward
and use the funds to pay developers.

### Terminology

- **Proposal:** A suggestion to allocate funds from the pot to a beneficiary.
- **Beneficiary:** An account who will receive the funds from a proposal if the proposal is
approved.
- **Deposit:** Funds that a proposer must lock when making a proposal. The deposit will be returned
or slashed if the proposal is approved or rejected respectively.
- **Pot:** Unspent funds accumulated by the treasury pallet.

## Interface

### Dispatchable Functions

General spending/proposal protocol:
- `propose_spend` - Make a spending proposal and stake the required deposit.
- `reject_proposal` - Reject a proposal, slashing the deposit.
- `approve_proposal` - Accept the proposal, returning the deposit.
Loading