Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimistic Project Funding #5162

Open
wants to merge 219 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
219 commits
Select commit Hold shift + click to select a range
8537c9e
Started with the pallet structure
ndkazu Jul 23, 2024
dcaa52c
changed Cargo.toml
ndkazu Jul 23, 2024
b781695
Basic pallet structure
ndkazu Jul 27, 2024
527787b
Types, Storages, and naming
ndkazu Jul 27, 2024
f6b8fa8
Basic pallet structure
ndkazu Jul 27, 2024
c1c442d
Update
ndkazu Jul 27, 2024
674c5f1
Helper functions
ndkazu Jul 27, 2024
8f42432
Spnedings creation from projects list
ndkazu Jul 28, 2024
8667321
Spending status change
ndkazu Jul 28, 2024
5fb830e
Funds unlock and transfer
ndkazu Jul 30, 2024
08a56f5
Added an Event
ndkazu Jul 30, 2024
100c26b
Preparation for testing and minor corrections
ndkazu Jul 30, 2024
2be7fb9
Update substrate/frame/distribution/src/lib.rs
ndkazu Jul 30, 2024
76fe14b
Update substrate/frame/distribution/src/lib.rs
ndkazu Jul 30, 2024
5d0f7ba
Update substrate/frame/distribution/src/lib.rs
ndkazu Jul 30, 2024
503f3d3
Update substrate/frame/distribution/src/lib.rs
ndkazu Jul 30, 2024
aaea1fe
Update substrate/frame/distribution/src/types.rs
ndkazu Jul 30, 2024
01c826c
Update umbrella/Cargo.toml
ndkazu Jul 30, 2024
a305d80
Made some corrections
ndkazu Jul 30, 2024
c4e6ae4
First test
ndkazu Jul 30, 2024
9ba1083
Additional event & testing
ndkazu Jul 30, 2024
0268623
Merge branch 'paritytech:master' into Optimist
ndkazu Jul 30, 2024
dab155d
Update branch
ndkazu Jul 30, 2024
990a2f6
Second test: funds locked
ndkazu Jul 30, 2024
119206a
Minor corrections
ndkazu Jul 30, 2024
c9c9c4f
More tests...
ndkazu Jul 30, 2024
d11cbc1
Corrected some terms
ndkazu Jul 31, 2024
fb73b68
Pallet OPF basic structure. No storage yet.
ndkazu Aug 1, 2024
aa7b249
WhiteListed Projects storage
ndkazu Aug 1, 2024
2406b8f
Helper function for voting
ndkazu Aug 1, 2024
26f3958
modified storage
ndkazu Aug 1, 2024
8e10f21
Adding type for freeze
ndkazu Aug 1, 2024
abde432
Voting action + voter funds hold/release
ndkazu Aug 1, 2024
ad4d675
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 1, 2024
114e430
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 2, 2024
245cf90
New constant added
ndkazu Aug 2, 2024
617ee5e
Helper for rewards calculation & distribution
ndkazu Aug 3, 2024
93e3b31
Updated pallet-distribution tests
ndkazu Aug 3, 2024
5fc4beb
In-code documention, extrinsics, and a few corrections
ndkazu Aug 4, 2024
27524ed
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 4, 2024
3ba0121
Documentation for pallet distribution
ndkazu Aug 4, 2024
82bf13d
Merge branch 'Optimist' of github.com:ndkazu/polkadot-sdk into Optimist
ndkazu Aug 4, 2024
f21ba3c
Documentation edit
ndkazu Aug 4, 2024
8cedc42
Restricted some storages visibility
ndkazu Aug 4, 2024
f95b070
Used checked_add instead of saturating_add
ndkazu Aug 4, 2024
2699fd2
Some corrections
ndkazu Aug 4, 2024
843f558
Logic to be used in pallet-opf hook added
ndkazu Aug 5, 2024
b69cfff
Distribution pallet README
ndkazu Aug 5, 2024
9faf586
README corrections
ndkazu Aug 5, 2024
758e681
README corrections
ndkazu Aug 5, 2024
00fe62b
Remove unfunded projects from whitelist
ndkazu Aug 6, 2024
46ea061
Use on_idle hook in OPF Pallet
ndkazu Aug 6, 2024
876db00
Correction
ndkazu Aug 6, 2024
7445d0f
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 7, 2024
442df1e
Update README.md
ndkazu Aug 7, 2024
238bae5
Update lib.rs
ndkazu Aug 7, 2024
76f31b3
Create README.md
ndkazu Aug 7, 2024
fa8f9c2
User's funds release after rewards calculation
ndkazu Aug 7, 2024
145858c
Benchmarking_begin
ndkazu Aug 8, 2024
1db8e95
Changed Sepnding(s) to Spend(s)
ndkazu Aug 8, 2024
c3ba04e
Changed VotingRoundsNumber to VotingRoundNumber
ndkazu Aug 8, 2024
92eabcd
Correction
ndkazu Aug 8, 2024
6216b5e
Preparing OPF testing
ndkazu Aug 9, 2024
b5c02ab
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 9, 2024
fb76ceb
Stopped using the system block number
ndkazu Aug 10, 2024
945fbb2
corrected block numbers for tests
ndkazu Aug 10, 2024
04c0646
Added events
ndkazu Aug 11, 2024
bdd88c1
First tests and corresponding corrections
ndkazu Aug 11, 2024
6742a58
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 11, 2024
0eb2e5c
Rewards distribution test
ndkazu Aug 11, 2024
d1fac2f
Tests and corrections
ndkazu Aug 11, 2024
7bf608a
Removed dev-mode and some warnings
ndkazu Aug 12, 2024
ba869d6
More OPF testing
ndkazu Aug 12, 2024
bc4c6de
Added benchmarking test suite
ndkazu Aug 12, 2024
32a2296
Extrinsic call not executed in benchmark???
ndkazu Aug 12, 2024
065039e
some corrections
ndkazu Aug 12, 2024
48d5d62
Tests are passing
ndkazu Aug 12, 2024
08c7518
Removed dev mode + clippy & fmt
ndkazu Aug 13, 2024
7b874a1
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 13, 2024
3389e6a
Benchmarking is now working for pallet distribution
ndkazu Aug 13, 2024
0592915
Pallet distribution ready for review
ndkazu Aug 13, 2024
e7956af
Re-wrote distribution benchmark and started opf benchmark
ndkazu Aug 14, 2024
5c25742
Benchmarking done
ndkazu Aug 15, 2024
a8f6d0c
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 15, 2024
ec08801
Fixing CLI errors
ndkazu Aug 15, 2024
d723478
Fixing errors
ndkazu Aug 15, 2024
67ddbb8
Fixing errors
ndkazu Aug 15, 2024
b960b3c
PRdoc
ndkazu Aug 15, 2024
53c31aa
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 15, 2024
ca65abb
Editing the prdoc
ndkazu Aug 15, 2024
b94e3f9
Merge branch 'Optimist' of github.com:ndkazu/polkadot-sdk into Optimist
ndkazu Aug 15, 2024
6a918ad
Editing the prdoc
ndkazu Aug 15, 2024
8ed0b87
Editing the prdoc
ndkazu Aug 15, 2024
2878e5e
Editing the prdoc
ndkazu Aug 15, 2024
ab2ab73
Editing the prdoc->patch instead of minor
ndkazu Aug 15, 2024
40bdd68
Editing the prdoc
ndkazu Aug 15, 2024
d0dac4b
Editing the prdoc
ndkazu Aug 15, 2024
eb1ba63
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 15, 2024
a3be78b
Update prdoc/pr_5162.prdoc
ndkazu Aug 15, 2024
a5f0018
Fix prdoc
ndkazu Aug 15, 2024
39214cc
Merge branch 'Optimist' of github.com:ndkazu/polkadot-sdk into Optimist
ndkazu Aug 15, 2024
fd40298
Fix semver
ndkazu Aug 15, 2024
79a29cc
Fix semver
ndkazu Aug 15, 2024
0c24a47
Corrected README
ndkazu Aug 16, 2024
b5c86d6
Use take instead of get & remove
ndkazu Aug 16, 2024
a3a59a2
Some suggested changes implemented.
ndkazu Aug 16, 2024
1a2139c
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 16, 2024
6bab58b
More OPF tests
ndkazu Aug 17, 2024
bf22c86
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 17, 2024
18fce0f
More tests and some in-code comments
ndkazu Aug 19, 2024
9718231
Merge branch 'Optimist' of github.com:ndkazu/polkadot-sdk into Optimist
ndkazu Aug 19, 2024
1546392
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 19, 2024
8e76dd7
Merge branch 'master' into Optimist
Ank4n Aug 21, 2024
704c2fe
Conviction implemented, and funds unlock extrinsic added. New extrins…
ndkazu Aug 23, 2024
5695675
License added
ndkazu Aug 23, 2024
24cd606
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 23, 2024
7d80bfb
Review related corrections
ndkazu Aug 23, 2024
d9fc656
Merge branch 'Optimist' of github.com:ndkazu/polkadot-sdk into Optimist
ndkazu Aug 23, 2024
baedd94
fix failing benchmark
ndkazu Aug 23, 2024
0d4a587
cargo fmt
ndkazu Aug 23, 2024
0cd321a
New extrinsic benchmark added
ndkazu Aug 23, 2024
73d0f5f
update
ndkazu Aug 23, 2024
1e38a51
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 24, 2024
4bec5f4
Type miss in ReadMe
ndkazu Aug 24, 2024
e1580cd
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 25, 2024
df41d42
Merge branch 'paritytech:master' into Optimist
ndkazu Aug 26, 2024
304a76d
Corrected Distribution pallet based on reviews
ndkazu Aug 27, 2024
bf8ba94
Error handling correction in Distribution pallet
ndkazu Aug 27, 2024
19dc1d9
added some corrections to the OPF pallet
ndkazu Aug 27, 2024
a2ff644
Removed unbounded map iter
ndkazu Aug 27, 2024
980226e
Further improvements in Distribution pallet
ndkazu Aug 27, 2024
ecbf739
Removing use of iter()
ndkazu Aug 28, 2024
b7ab820
Refactored try_vote function and removed iter()
ndkazu Aug 29, 2024
c29ffe8
cargo fmt
ndkazu Aug 29, 2024
c9dcc86
resolving conflict
ndkazu Aug 30, 2024
492a0d3
Merge branch 'master' into Optimist
ndkazu Aug 30, 2024
87d1c1e
Merge branch 'master' into Optimist
ndkazu Aug 31, 2024
8d4657d
Merge branch 'master' into Optimist
ndkazu Sep 3, 2024
5c5b8ad
opf-test correction
ndkazu Sep 3, 2024
98a0ae4
Merge branch 'paritytech:master' into Optimist
ndkazu Sep 5, 2024
98217d9
Renamed variable & removed some unecessary code
ndkazu Sep 6, 2024
77ae32e
Renamed some variables in pallet-opf
ndkazu Sep 6, 2024
8cb1b18
Updates vote_removal and corresponding tests
ndkazu Sep 6, 2024
ed56b2e
Clearing storages after a voting round ended in pallet-opf
ndkazu Sep 6, 2024
47fe97f
Debugging and additional tests
ndkazu Sep 6, 2024
3e2f2a1
cargo +nightly fmt
ndkazu Sep 6, 2024
8dfbcf3
Merge branch 'master' into Optimist
ndkazu Sep 6, 2024
556551d
Merge branch 'master' into Optimist
ndkazu Sep 7, 2024
effa3d9
Merge branch 'master' into Optimist
ndkazu Sep 8, 2024
2b5e0cd
Comments correction
ndkazu Sep 8, 2024
6942a86
Merge branch 'master' into Optimist
ndkazu Sep 9, 2024
1849512
Merge branch 'master' into Optimist
ndkazu Sep 11, 2024
72a8085
Merge branch 'master' into Optimist
ndkazu Sep 12, 2024
bb82af4
Merge branch 'master' into Optimist
ndkazu Sep 13, 2024
d2804b7
Merge branch 'master' into Optimist
ndkazu Sep 13, 2024
c847381
Reverted unwanted changes from cargo fmt
ndkazu Sep 13, 2024
2c0aa9b
Removed last unwanted format change
ndkazu Sep 13, 2024
511e8bc
Merge branch 'master' into Optimist
ndkazu Sep 13, 2024
bddabc8
Merge branch 'master' into Optimist
ndkazu Sep 14, 2024
5a13c98
Merge branch 'master' into Optimist
ndkazu Sep 16, 2024
e02fe4b
Correct import
ndkazu Sep 17, 2024
576a5b3
Merge branch 'master' into Optimist
ndkazu Sep 18, 2024
5760f9b
Merge branch 'master' into Optimist
ndkazu Sep 18, 2024
6e1c9ab
Merge branch 'master' into Optimist
ndkazu Sep 18, 2024
989be08
Corrected the respectives README's
ndkazu Sep 18, 2024
c62e265
toml format
ndkazu Sep 18, 2024
56e8676
rust-feature-propagation
ndkazu Sep 18, 2024
db2506b
Merge branch 'master' into Optimist
ndkazu Sep 18, 2024
12fbf3c
Rust features propagation
ndkazu Sep 18, 2024
c59b159
Merge branch 'master' into Optimist
ndkazu Sep 18, 2024
7412d46
Rust features propagation
ndkazu Sep 18, 2024
18f92fc
toml format
ndkazu Sep 19, 2024
840d64a
toml format
ndkazu Sep 19, 2024
cac2449
format toml
ndkazu Sep 19, 2024
8f0fd86
format toml
ndkazu Sep 19, 2024
4507209
fix umbrella error
ndkazu Sep 19, 2024
fb51be2
Fix Wasm error
ndkazu Sep 19, 2024
eef451c
Merge branch 'master' into Optimist
ndkazu Sep 19, 2024
f38806c
Merge branch 'master' into Optimist
ndkazu Sep 20, 2024
d21f4d8
Update Cargo.toml
ndkazu Sep 20, 2024
936cfd7
Update Cargo.toml
ndkazu Sep 20, 2024
7728824
Update Cargo.toml
ndkazu Sep 20, 2024
38718e5
Update cargo.toml
ndkazu Sep 20, 2024
99b3be4
Merge branch 'master' into Optimist
ndkazu Sep 20, 2024
f079db6
First round of corrections
ndkazu Sep 20, 2024
b61ddc6
Merge branch 'Optimist' of github.com:ndkazu/polkadot-sdk into Optimist
ndkazu Sep 20, 2024
a4edbac
Second round of corrections
ndkazu Sep 20, 2024
635526e
Fix umbrella
ndkazu Sep 20, 2024
fa30adb
Revert some modifications
ndkazu Sep 20, 2024
2cf44db
changed max_weight in opf
ndkazu Sep 20, 2024
fa6a553
Fix format
ndkazu Sep 20, 2024
44fa41a
pallet-distribution corrections
ndkazu Sep 22, 2024
1ea2682
Merge branch 'master' into Optimist
ndkazu Sep 22, 2024
c1014fa
Removed unnecessary condition
ndkazu Sep 22, 2024
94496d1
cargo fmt
ndkazu Sep 23, 2024
e11002a
Merge branch 'master' into Optimist
ndkazu Sep 23, 2024
03b59e0
Updated weight.rs files
ndkazu Sep 23, 2024
b26a798
licenses
ndkazu Sep 23, 2024
37d1bca
Merge branch 'master' into Optimist
ndkazu Sep 23, 2024
96d1731
Merge branch 'master' into Optimist
ndkazu Sep 23, 2024
80cc666
Merge branch 'master' into Optimist
ndkazu Sep 25, 2024
10e9df8
Update substrate/frame/opf/src/lib.rs
ndkazu Sep 25, 2024
064b177
Merge branch 'master' into Optimist
ndkazu Sep 25, 2024
8586c18
Merge branch 'master' into Optimist
ndkazu Sep 26, 2024
93a9e9f
Merge branch 'master' into Optimist
ndkazu Sep 26, 2024
3ea2749
Merge branch 'master' into Optimist
ndkazu Sep 28, 2024
2d17a4e
cargo fmt
ndkazu Sep 28, 2024
5be0c9e
Merge branch 'master' into Optimist
ndkazu Sep 28, 2024
5b9f059
Merge branch 'master' into Optimist
ndkazu Sep 29, 2024
8cb632f
Merge branch 'master' into Optimist
ndkazu Oct 1, 2024
2b5d0e2
Merge branch 'master' into Optimist
ndkazu Oct 2, 2024
9b0a5f4
try to Fix failing benchmark test
ndkazu Oct 3, 2024
e201c53
Merge branch 'Optimist' of github.com:ndkazu/polkadot-sdk into Optimist
ndkazu Oct 3, 2024
228da0a
Use calculated weight instead of max weight in on_idle_function
ndkazu Oct 3, 2024
e8ebcfc
Revert weight changes & limit block consumption to 1/10th
ndkazu Oct 3, 2024
f26d01c
cargo fmt
ndkazu Oct 3, 2024
0aa2708
Merge branch 'master' into Optimist
ndkazu Oct 4, 2024
0dbe4c9
Merge branch 'master' into Optimist
ndkazu Oct 5, 2024
25a3716
Merge branch 'master' into Optimist
ndkazu Oct 6, 2024
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
23 changes: 23 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ members = [
"substrate/frame/core-fellowship",
"substrate/frame/delegated-staking",
"substrate/frame/democracy",
"substrate/frame/distribution",
ndkazu marked this conversation as resolved.
Show resolved Hide resolved
"substrate/frame/election-provider-multi-phase",
"substrate/frame/election-provider-multi-phase/test-staking-e2e",
"substrate/frame/election-provider-support",
Expand Down Expand Up @@ -904,6 +905,7 @@ pallet-default-config-example = { path = "substrate/frame/examples/default-confi
pallet-delegated-staking = { path = "substrate/frame/delegated-staking", default-features = false }
pallet-democracy = { path = "substrate/frame/democracy", default-features = false }
pallet-dev-mode = { path = "substrate/frame/examples/dev-mode", default-features = false }
pallet-distribution = { path = "substrate/frame/distribution", default-features = false }
pallet-election-provider-multi-phase = { path = "substrate/frame/election-provider-multi-phase", default-features = false }
pallet-election-provider-support-benchmarking = { path = "substrate/frame/election-provider-support/benchmarking", default-features = false }
pallet-elections-phragmen = { path = "substrate/frame/elections-phragmen", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions docs/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pallet-scheduler = { workspace = true, default-features = true }
pallet-referenda = { workspace = true, default-features = true }
pallet-broker = { workspace = true, default-features = true }
pallet-babe = { workspace = true, default-features = true }
pallet-distribution = { workspace = true, default-features = true }

# Primitives
sp-io = { workspace = true, default-features = true }
Expand Down
39 changes: 39 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,42 @@ impl pallet_broker::Config for Runtime {
type PriceAdapter = pallet_broker::CenterTargetPrice<Balance>;
}


parameter_types! {
// Id of the treasury
pub const PotId: PalletId = PalletId(*b"py/trsry");

// Time needed after approval to unlock the reward claim
pub const Period:BlockNumber = DAYS;

// Maximum number of whitelisted projects
pub const MaxProjects:u32 = 50;

pub const EpochDurationBlocks: BlockNumber = EPOCH_DURATION_IN_BLOCKS;

}
impl pallet_distribution::Config for Runtime {
type RuntimeEvent = RuntimeEvent;

ndkazu marked this conversation as resolved.
Show resolved Hide resolved
type NativeBalance = Balances;

/// Pot PalletId
type PotId = PotId;

/// A reason for placing a hold on funds.
type RuntimeHoldReason = RuntimeHoldReason;

/// This the minimum required time period between project whitelisting
ndkazu marked this conversation as resolved.
Show resolved Hide resolved
/// and payment/reward_claim from the treasury.
type PaymentPeriod = Period;

/// Maximum number of whitelisted projects
type MaxProjects = MaxProjects;

/// Epoch duration in blocks
type EpochDurationBlocks = EpochDurationBlocks;
}

parameter_types! {
pub const MixnetNumCoverToCurrentBlocks: BlockNumber = 3;
pub const MixnetNumRequestsToCurrentBlocks: BlockNumber = 3;
Expand Down Expand Up @@ -2469,6 +2505,9 @@ mod runtime {

#[runtime::pallet_index(79)]
pub type AssetConversionMigration = pallet_asset_conversion_ops::Pallet<Runtime>;

#[runtime::pallet_index(80)]
pub type Distribution = pallet_distribution::Pallet<Runtime>;
}

/// The address format for describing accounts.
Expand Down
67 changes: 67 additions & 0 deletions substrate/frame/distribution/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[package]
name = "pallet-distribution"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage = "https://substrate.io"
repository.workspace = true
description = "FRAME pallet to distribute funds to whitelisted projects"
readme = "README.md"

[lints]
workspace = true

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

[dependencies]
codec = { workspace = true, default-features = false }
log = { workspace = true }
scale-info = { features = ["derive"], workspace = true , default-features = false }
frame-benchmarking = { optional = true, workspace = true , default-features = false }
frame-support = { workspace = true , default-features = false }
frame-system = { workspace = true , default-features = false }
sp-io = { workspace = true , default-features = false }
sp-runtime = { workspace = true , default-features = false }
sp-core = { workspace = true , default-features = false }

pallet-assets = { workspace = true, default-features = false }
pallet-balances = { workspace = true, default-features = false }
pallet-timestamp = { workspace = true, default-features = false }
pallet-sudo = { workspace = true, default-features = false }
pallet-transaction-payment = { workspace = true, default-features = false }
pallet-transaction-payment-rpc-runtime-api = { workspace = true, default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"log/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"pallet-timestamp/std",
"pallet-sudo/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
"pallet-assets/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-balances/try-runtime",
"sp-runtime/try-runtime",
]
149 changes: 149 additions & 0 deletions substrate/frame/distribution/src/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
pub use super::*;
impl<T: Config> Pallet<T> {


pub fn pot_account() -> T::AccountId{
// Get Pot account
let pot_id = T::PotId::get();
let pot_account: T::AccountId = pot_id.into_account_truncating();
pot_account
}

pub fn get_spending(project_account: T::AccountId) -> Vec<SpendingIndex> {
let mut spendings: Vec<SpendingIndex> = Vec::new();
let value = Some(project_account);
ndkazu marked this conversation as resolved.
Show resolved Hide resolved

for spending in Spendings::<T>::iter() {
let info = spending.1;
if info.whitelisted_project == value {
spendings.push(spending.0);
}
}

spendings
}

/// Series of checks on the Pot, to ensure that we have enough funds
/// before executing a spending
pub fn pot_check(amount: BalanceOf<T>) -> DispatchResult {
ndkazu marked this conversation as resolved.
Show resolved Hide resolved

// Get Pot account
let pot_account: T::AccountId = Self::pot_account();

// Check that the Pot as enough funds for the transfer
ndkazu marked this conversation as resolved.
Show resolved Hide resolved
let balance = T::NativeBalance::balance(&pot_account);
let minimum_balance = T::NativeBalance::minimum_balance();
let remaining_balance = balance.saturating_sub(amount);

ensure!(remaining_balance > minimum_balance, Error::<T>::InsufficientPotReserves);
ensure!(balance > amount, Error::<T>::InsufficientPotReserves);
Ok(())
}


/// Funds transfer from the Pot to a project account
pub fn spending(
amount: BalanceOf<T>,
beneficiary: T::AccountId,
spending_index: u32,
) -> DispatchResult {

// Get Pot account
let pot_account: T::AccountId = Self::pot_account();

//Operate the transfer
let result = T::NativeBalance::transfer(
&pot_account,
&beneficiary,
amount,
Preservation::Preserve,
)
.map_err(|_| Error::<T>::TransferFailed);

Self::process_failed_spending_result(spending_index, result)?;


Ok(())
}

/// Helper function used to change the status of a failed spending
pub fn process_failed_spending_result(
spending_index: u32,
result: Result<BalanceOf<T>, Error<T>>,
) -> Result<BalanceOf<T>, Error<T>> {
match result {
Ok(x) => {
//Change spending status
Spendings::<T>::mutate(spending_index, |val| {
let mut val0 = val.clone().unwrap();
val0.status = SpendingState::Completed;
*val = Some(val0);
});
Ok(x)
},
Err(_e) => {
//Change spending status
Spendings::<T>::mutate(spending_index, |val| {
let mut val0 = val.clone().unwrap();
val0.status = SpendingState::Failed;
*val = Some(val0);
});
Err(Error::<T>::FailedSpendingOperation)
},
}
}

// ToDo in begin_block
// At the beginning of every Epoch, populate the `Spendings` storage from the `Projects` storage (populated by an external process/pallet)
// make sure that there is enough funds before creating a new `SpendingInfo`, and `ProjectInfo`
// corresponding to a created `SpendingInfo` should be removed from the `Projects` storage.
// This is also a good place to lock the funds for created `SpendingInfos`.
// the function will be use in a hook.

pub fn begin_block(now: BlockNumberFor<T>) -> Weight {
let max_block_weight = Weight::from_parts(1000_u64, 0);
ndkazu marked this conversation as resolved.
Show resolved Hide resolved
let epoch = T::EpochDurationBlocks::get();

//We reach the check period
if (now % epoch).is_zero(){
let mut projects = Projects ::<T>::get();

if projects.len() > 0 {

for project in projects.clone(){
// check if the pot has enough fund for the spending
let check = Self::pot_check(project.amount);
ndkazu marked this conversation as resolved.
Show resolved Hide resolved
let _result = match check{

Ok(x) => {
// Create a new spending
let _new_spending = SpendingInfo::<T>::new(project.clone());

// Lock funds for the project
let pot = Self::pot_account();
let _=T::NativeBalance::hold(
ndkazu marked this conversation as resolved.
Show resolved Hide resolved
&HoldReason::FundsLock.into(),
&pot,
project.amount,
);

// remove project from project_list
projects.retain(|value| *value != project);
Ok(x)

},
Err(_e) => Err(Error::<T>::InsufficientPotReserves)
};

}
}

// Update project storage
Projects::<T>::mutate(|val|{
*val = projects;
});
}
max_block_weight
}

}
Loading
Loading