pallet_crowdloan_rewards/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.

//! # Crowdloan Rewards Pallet
//!
//! This pallet is DEPRECATED, the remaining code handle only unclaimed rewards.
//!
//! ## Payout Mechanism
//!
//! The current payout mechanism requires contributors to claim their payouts. Because they are
//! paying the transaction fees for this themselves, they can do it as often as every block, or
//! wait and claim the entire thing once it is fully vested. We could consider auto payouts if we
//! want.

#![cfg_attr(not(feature = "std"), no_std)]

pub use crate::weights::WeightInfo;
use frame_support::pallet;
pub use pallet::*;

#[cfg(any(test, feature = "runtime-benchmarks"))]
mod benchmarks;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;

#[pallet]
pub mod pallet {
	use super::*;
	use frame_support::{
		pallet_prelude::*,
		traits::{Currency, ExistenceRequirement::AllowDeath, WithdrawReasons},
		PalletId,
	};
	use frame_system::pallet_prelude::*;
	use parity_scale_codec::{Decode, Encode};
	use sp_core::crypto::AccountId32;
	use sp_runtime::traits::{
		AccountIdConversion, AtLeast32BitUnsigned, BlockNumberProvider, Saturating, Verify,
	};
	use sp_runtime::{MultiSignature, Perbill};
	use sp_std::collections::btree_map::BTreeMap;
	use sp_std::vec;
	use sp_std::vec::Vec;
	#[pallet::pallet]
	#[pallet::without_storage_info]
	// The crowdloan rewards pallet
	pub struct Pallet<T>(PhantomData<T>);

	pub const PALLET_ID: PalletId = PalletId(*b"Crowdloa");

	// The wrapper around which the reward changing message needs to be wrapped
	pub const WRAPPED_BYTES_PREFIX: &[u8] = b"<Bytes>";
	pub const WRAPPED_BYTES_POSTFIX: &[u8] = b"</Bytes>";

	/// Configuration trait of this pallet.
	#[pallet::config]
	pub trait Config: frame_system::Config {
		/// The overarching event type
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
		/// Checker for the reward vec, is it initalized already?
		type Initialized: Get<bool>;
		/// Percentage to be payed at initialization
		#[pallet::constant]
		type InitializationPayment: Get<Perbill>;
		// Max number of contributors that can be inserted at once in initialize_reward_vec
		#[pallet::constant]
		type MaxInitContributors: Get<u32>;
		/// The minimum contribution to which rewards will be paid.
		type MinimumReward: Get<BalanceOf<Self>>;
		/// A fraction representing the percentage of proofs
		/// that need to be presented to change a reward address through the relay keys
		#[pallet::constant]
		type RewardAddressRelayVoteThreshold: Get<Perbill>;
		/// The currency in which the rewards will be paid (probably the parachain native currency)
		type RewardCurrency: Currency<Self::AccountId>;
		/// The AccountId type contributors used on the relay chain.
		type RelayChainAccountId: Parameter
			//TODO these AccountId32 bounds feel a little extraneous. I wonder if we can remove them.
			+ Into<AccountId32>
			+ From<AccountId32>
			+ Ord
			+ sp_runtime::serde::Serialize
			+ for<'a> sp_runtime::serde::Deserialize<'a>;

		// The origin that is allowed to change the reward address with relay signatures
		type RewardAddressChangeOrigin: EnsureOrigin<Self::RuntimeOrigin>;

		/// Network Identifier to be appended into the signatures for reward address change/association
		/// Prevents replay attacks from one network to the other
		#[pallet::constant]
		type SignatureNetworkIdentifier: Get<&'static [u8]>;

		// The origin that is allowed to change the reward address with relay signatures
		type RewardAddressAssociateOrigin: EnsureOrigin<Self::RuntimeOrigin>;

		/// The type that will be used to track vesting progress
		type VestingBlockNumber: AtLeast32BitUnsigned
			+ Parameter
			+ Default
			+ Into<BalanceOf<Self>>
			+ sp_runtime::serde::Serialize
			+ for<'a> sp_runtime::serde::Deserialize<'a>;

		/// The notion of time that will be used for vesting. Probably
		/// either the relay chain or sovereign chain block number.
		type VestingBlockProvider: BlockNumberProvider<BlockNumber = Self::VestingBlockNumber>;

		type WeightInfo: WeightInfo;
	}

	pub type BalanceOf<T> = <<T as Config>::RewardCurrency as Currency<
		<T as frame_system::Config>::AccountId,
	>>::Balance;

	/// Type alias for contributor data: (relay_account, optional_native_account, reward)
	pub type ContributorData<T> = (
		<T as Config>::RelayChainAccountId,
		Option<<T as frame_system::Config>::AccountId>,
		BalanceOf<T>,
	);

	/// Stores info about the rewards owed as well as how much has been vested so far.
	/// For a primer on this kind of design, see the recipe on compounding interest
	/// https://substrate.dev/recipes/fixed-point.html#continuously-compounding
	#[derive(Default, Clone, Encode, Decode, RuntimeDebug, PartialEq, scale_info::TypeInfo)]
	#[scale_info(skip_type_params(T))]
	pub struct RewardInfo<T: Config> {
		pub total_reward: BalanceOf<T>,
		pub claimed_reward: BalanceOf<T>,
		pub contributed_relay_addresses: Vec<T::RelayChainAccountId>,
	}

	#[pallet::call]
	impl<T: Config> Pallet<T> {
		/// Associate a native rewards_destination identity with a crowdloan contribution.
		///
		/// The caller needs to provide the unassociated relay account and a proof to succeed
		/// with the association
		/// The proof is nothing but a signature over the reward_address using the relay keys
		#[pallet::call_index(0)]
		#[pallet::weight(T::WeightInfo::associate_native_identity())]
		pub fn associate_native_identity(
			origin: OriginFor<T>,
			reward_account: T::AccountId,
			relay_account: T::RelayChainAccountId,
			proof: MultiSignature,
		) -> DispatchResultWithPostInfo {
			// Check that the origin is the one able to asociate the reward addrss
			T::RewardAddressChangeOrigin::ensure_origin(origin)?;

			// Check the proof:
			// 1. Is signed by an actual unassociated contributor
			// 2. Signs a valid native identity
			// Check the proof. The Proof consists of a Signature of the rewarded account with the
			// claimer key

			// The less costly checks will go first

			// The relay account should be unassociated
			let mut reward_info = UnassociatedContributions::<T>::get(&relay_account)
				.ok_or(Error::<T>::NoAssociatedClaim)?;

			// We ensure the relay chain id wast not yet associated to avoid multi-claiming
			// We dont need this right now, as it will always be true if the above check is true
			ensure!(
				ClaimedRelayChainIds::<T>::get(&relay_account).is_none(),
				Error::<T>::AlreadyAssociated
			);

			// For now I prefer that we dont support providing an existing account here
			ensure!(
				AccountsPayable::<T>::get(&reward_account).is_none(),
				Error::<T>::AlreadyAssociated
			);

			// b"<Bytes>" "SignatureNetworkIdentifier" + "new_account" + b"</Bytes>"
			let mut payload = WRAPPED_BYTES_PREFIX.to_vec();
			payload.append(&mut T::SignatureNetworkIdentifier::get().to_vec());
			payload.append(&mut reward_account.encode());
			payload.append(&mut WRAPPED_BYTES_POSTFIX.to_vec());

			// Check the signature
			Self::verify_signatures(
				vec![(relay_account.clone(), proof)],
				reward_info.clone(),
				payload,
			)?;

			// Make the first payment
			let first_payment = T::InitializationPayment::get() * reward_info.total_reward;

			T::RewardCurrency::transfer(
				&PALLET_ID.into_account_truncating(),
				&reward_account,
				first_payment,
				AllowDeath,
			)?;

			Self::deposit_event(Event::InitialPaymentMade(
				reward_account.clone(),
				first_payment,
			));

			reward_info.claimed_reward = first_payment;

			// Insert on payable
			AccountsPayable::<T>::insert(&reward_account, &reward_info);

			// Remove from unassociated
			<UnassociatedContributions<T>>::remove(&relay_account);

			// Insert in mapping
			ClaimedRelayChainIds::<T>::insert(&relay_account, ());

			// Emit Event
			Self::deposit_event(Event::NativeIdentityAssociated(
				relay_account,
				reward_account,
				reward_info.total_reward,
			));

			Ok(Default::default())
		}

		/// Change reward account by submitting proofs from relay accounts
		///
		/// The number of valid proofs needs to be bigger than 'RewardAddressRelayVoteThreshold'
		/// The account to be changed needs to be submitted as 'previous_account'
		/// Origin must be RewardAddressChangeOrigin
		#[pallet::call_index(1)]
		#[pallet::weight(T::WeightInfo::change_association_with_relay_keys(proofs.len() as u32))]
		pub fn change_association_with_relay_keys(
			origin: OriginFor<T>,
			reward_account: T::AccountId,
			previous_account: T::AccountId,
			proofs: Vec<(T::RelayChainAccountId, MultiSignature)>,
		) -> DispatchResultWithPostInfo {
			// Check that the origin is the one able to change the reward addrss
			T::RewardAddressChangeOrigin::ensure_origin(origin)?;

			// For now I prefer that we dont support providing an existing account here
			ensure!(
				AccountsPayable::<T>::get(&reward_account).is_none(),
				Error::<T>::AlreadyAssociated
			);

			// To avoid replay attacks, we make sure the payload contains the previous address too
			// I am assuming no rational user will go back to a previously changed reward address
			// b"<Bytes>" + "SignatureNetworkIdentifier" + new_account" + "previous_account" + b"</Bytes>"
			let mut payload = WRAPPED_BYTES_PREFIX.to_vec();
			payload.append(&mut T::SignatureNetworkIdentifier::get().to_vec());
			payload.append(&mut reward_account.encode());
			payload.append(&mut previous_account.encode());
			payload.append(&mut WRAPPED_BYTES_POSTFIX.to_vec());

			// Get the reward info for the account to be changed
			let reward_info = AccountsPayable::<T>::get(&previous_account)
				.ok_or(Error::<T>::NoAssociatedClaim)?;

			Self::verify_signatures(proofs, reward_info.clone(), payload)?;

			// Remove fromon payable
			AccountsPayable::<T>::remove(&previous_account);

			// Insert on payable
			AccountsPayable::<T>::insert(&reward_account, &reward_info);

			// Emit Event
			Self::deposit_event(Event::RewardAddressUpdated(
				previous_account,
				reward_account,
			));

			Ok(Default::default())
		}

		/// Collect whatever portion of your reward are currently vested.
		#[pallet::call_index(2)]
		#[pallet::weight(T::WeightInfo::claim())]
		pub fn claim(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
			let payee = ensure_signed(origin)?;
			let initialized = <Initialized<T>>::get();
			ensure!(initialized, Error::<T>::RewardVecNotFullyInitializedYet);
			// Calculate the veted amount on demand.
			let mut info =
				AccountsPayable::<T>::get(&payee).ok_or(Error::<T>::NoAssociatedClaim)?;
			ensure!(
				info.claimed_reward < info.total_reward,
				Error::<T>::RewardsAlreadyClaimed
			);

			// Get the current block used for vesting purposes
			let now = T::VestingBlockProvider::current_block_number();

			// Substract the first payment from the vested amount
			let first_paid = T::InitializationPayment::get() * info.total_reward;

			// To calculate how much could the user have claimed already
			let payable_period = now.saturating_sub(<InitVestingBlock<T>>::get());

			// How much should the contributor have already claimed by this block?
			// By multiplying first we allow the conversion to integer done with the biggest number
			let period = EndVestingBlock::<T>::get() - InitVestingBlock::<T>::get();
			let should_have_claimed = if period == 0u32.into() {
				// Pallet is configured with a zero vesting period.
				info.total_reward - first_paid
			} else {
				(info.total_reward - first_paid).saturating_mul(payable_period.into())
					/ period.into()
			};

			// If the period is bigger than whats missing to pay, then return whats missing to pay
			let payable_amount = if should_have_claimed >= (info.total_reward - first_paid) {
				info.total_reward.saturating_sub(info.claimed_reward)
			} else {
				should_have_claimed + first_paid - info.claimed_reward
			};

			info.claimed_reward = info.claimed_reward.saturating_add(payable_amount);
			AccountsPayable::<T>::insert(&payee, &info);

			// This pallet controls an amount of funds and transfers them to each of the contributors
			//TODO: contributors should have the balance locked for tranfers but not for democracy
			T::RewardCurrency::transfer(
				&PALLET_ID.into_account_truncating(),
				&payee,
				payable_amount,
				AllowDeath,
			)?;
			// Emit event
			Self::deposit_event(Event::RewardsPaid(payee, payable_amount));
			Ok(Default::default())
		}

		/// Update reward address, proving that the caller owns the current native key
		#[pallet::call_index(3)]
		#[pallet::weight(T::WeightInfo::update_reward_address())]
		pub fn update_reward_address(
			origin: OriginFor<T>,
			new_reward_account: T::AccountId,
		) -> DispatchResultWithPostInfo {
			let signer = ensure_signed(origin)?;

			// Calculate the veted amount on demand.
			let info = AccountsPayable::<T>::get(&signer).ok_or(Error::<T>::NoAssociatedClaim)?;

			// For now I prefer that we dont support providing an existing account here
			ensure!(
				AccountsPayable::<T>::get(&new_reward_account).is_none(),
				Error::<T>::AlreadyAssociated
			);

			// Remove previous rewarded account
			AccountsPayable::<T>::remove(&signer);

			// Update new rewarded acount
			AccountsPayable::<T>::insert(&new_reward_account, &info);

			// Emit event
			Self::deposit_event(Event::RewardAddressUpdated(signer, new_reward_account));

			Ok(Default::default())
		}
	}

	impl<T: Config> Pallet<T> {
		/// The account ID that holds the Crowdloan's funds
		pub fn account_id() -> T::AccountId {
			PALLET_ID.into_account_truncating()
		}
		/// The Account Id's balance
		pub fn pot() -> BalanceOf<T> {
			T::RewardCurrency::free_balance(&Self::account_id())
		}
		/// Verify a set of signatures made with relay chain accounts
		/// We are verifying all the signatures, and then counting
		/// We could do something more efficient like count as we verify
		/// In any of the cases the weight will need to account for all the signatures,
		/// as we dont know beforehand whether they will be valid
		#[allow(clippy::map_entry)] // Cannot use entry API due to ensure! checks before insert
		fn verify_signatures(
			proofs: Vec<(T::RelayChainAccountId, MultiSignature)>,
			reward_info: RewardInfo<T>,
			payload: Vec<u8>,
		) -> DispatchResult {
			// The proofs should
			// 1. be signed by contributors to this address, otherwise they are not counted
			// 2. Signs a valid native identity
			// 3. The sum of the valid proofs needs to be bigger than InsufficientNumberOfValidProofs

			// I use a map here for faster lookups
			let mut voted: BTreeMap<T::RelayChainAccountId, ()> = BTreeMap::new();
			for (relay_account, signature) in proofs {
				// We just count votes that we have not seen
				if !voted.contains_key(&relay_account) {
					// Maybe I should not error here?
					ensure!(
						reward_info
							.contributed_relay_addresses
							.contains(&relay_account),
						Error::<T>::NonContributedAddressProvided
					);

					// I am erroring here as I think it is good to know the reason in the single-case
					// signature
					ensure!(
						signature.verify(payload.as_slice(), &relay_account.clone().into()),
						Error::<T>::InvalidClaimSignature
					);
					voted.insert(relay_account, ());
				}
			}

			// Ensure the votes are sufficient
			ensure!(
				Perbill::from_rational(
					voted.len() as u32,
					reward_info.contributed_relay_addresses.len() as u32
				) >= T::RewardAddressRelayVoteThreshold::get(),
				Error::<T>::InsufficientNumberOfValidProofs
			);
			Ok(())
		}

		pub fn complete_initialization(
			lease_ending_block: T::VestingBlockNumber,
		) -> DispatchResultWithPostInfo {
			let initialized = <Initialized<T>>::get();

			// This ensures there was no prior initialization
			ensure!(!initialized, Error::<T>::RewardVecAlreadyInitialized);

			// This ensures the end vesting block (when all funds are fully vested)
			// is bigger than the init vesting block
			ensure!(
				lease_ending_block > InitVestingBlock::<T>::get(),
				Error::<T>::VestingPeriodNonValid
			);

			let current_initialized_rewards = InitializedRewardAmount::<T>::get();

			let reward_difference = Self::pot().saturating_sub(current_initialized_rewards);

			// Ensure the difference is not bigger than the total number of contributors
			ensure!(
				reward_difference < TotalContributors::<T>::get().into(),
				Error::<T>::RewardsDoNotMatchFund
			);

			// Burn the difference
			let imbalance = T::RewardCurrency::withdraw(
				&PALLET_ID.into_account_truncating(),
				reward_difference,
				WithdrawReasons::TRANSFER,
				AllowDeath,
			)
			.expect("Shouldnt fail, as the fund should be enough to burn and nothing is locked");
			drop(imbalance);

			EndVestingBlock::<T>::put(lease_ending_block);

			<Initialized<T>>::put(true);

			Ok(Default::default())
		}

		pub fn initialize_reward_vec(
			rewards: Vec<ContributorData<T>>,
		) -> DispatchResultWithPostInfo {
			let initialized = <Initialized<T>>::get();
			ensure!(!initialized, Error::<T>::RewardVecAlreadyInitialized);

			// Ensure we are below the max number of contributors
			ensure!(
				rewards.len() as u32 <= T::MaxInitContributors::get(),
				Error::<T>::TooManyContributors
			);

			// What is the amount initialized so far?
			let mut current_initialized_rewards = InitializedRewardAmount::<T>::get();

			// Total number of contributors
			let mut total_contributors = TotalContributors::<T>::get();

			let incoming_rewards: BalanceOf<T> = rewards
				.iter()
				.fold(0u32.into(), |acc: BalanceOf<T>, (_, _, reward)| {
					acc + *reward
				});

			// Ensure we dont go over funds
			ensure!(
				current_initialized_rewards + incoming_rewards <= Self::pot(),
				Error::<T>::BatchBeyondFundPot
			);

			for (relay_account, native_account, reward) in &rewards {
				if ClaimedRelayChainIds::<T>::get(relay_account).is_some()
					|| UnassociatedContributions::<T>::get(relay_account).is_some()
				{
					// Dont fail as this is supposed to be called with batch calls and we
					// dont want to stall the rest of the contributions
					Self::deposit_event(Event::InitializedAlreadyInitializedAccount(
						relay_account.clone(),
						native_account.clone(),
						*reward,
					));
					continue;
				}

				if *reward < T::MinimumReward::get() {
					// Don't fail as this is supposed to be called with batch calls and we
					// dont want to stall the rest of the contributions
					Self::deposit_event(Event::InitializedAccountWithNotEnoughContribution(
						relay_account.clone(),
						native_account.clone(),
						*reward,
					));
					continue;
				}

				// If we have a native_account, we make the payment
				let initial_payment = if let Some(native_account) = native_account {
					let first_payment = T::InitializationPayment::get() * (*reward);
					T::RewardCurrency::transfer(
						&PALLET_ID.into_account_truncating(),
						native_account,
						first_payment,
						AllowDeath,
					)?;
					Self::deposit_event(Event::InitialPaymentMade(
						native_account.clone(),
						first_payment,
					));
					first_payment
				} else {
					0u32.into()
				};

				// Calculate the reward info to store after the initial payment has been made.
				let mut reward_info = RewardInfo {
					total_reward: *reward,
					claimed_reward: initial_payment,
					contributed_relay_addresses: vec![relay_account.clone()],
				};

				current_initialized_rewards += *reward - initial_payment;
				total_contributors += 1;

				if let Some(native_account) = native_account {
					if let Some(mut inserted_reward_info) =
						AccountsPayable::<T>::get(native_account)
					{
						inserted_reward_info
							.contributed_relay_addresses
							.append(&mut reward_info.contributed_relay_addresses);
						// the native account has already some rewards in, we add the new ones
						AccountsPayable::<T>::insert(
							native_account,
							RewardInfo {
								total_reward: inserted_reward_info.total_reward
									+ reward_info.total_reward,
								claimed_reward: inserted_reward_info.claimed_reward
									+ reward_info.claimed_reward,
								contributed_relay_addresses: inserted_reward_info
									.contributed_relay_addresses,
							},
						);
					} else {
						// First reward association
						AccountsPayable::<T>::insert(native_account, reward_info);
					}
					ClaimedRelayChainIds::<T>::insert(relay_account, ());
				} else {
					UnassociatedContributions::<T>::insert(relay_account, reward_info);
				}
			}
			InitializedRewardAmount::<T>::put(current_initialized_rewards);
			TotalContributors::<T>::put(total_contributors);

			Ok(Default::default())
		}
	}

	#[pallet::error]
	pub enum Error<T> {
		/// User trying to associate a native identity with a relay chain identity for posterior
		/// reward claiming provided an already associated relay chain identity
		AlreadyAssociated,
		/// Trying to introduce a batch that goes beyond the limits of the funds
		BatchBeyondFundPot,
		/// First claim already done
		FirstClaimAlreadyDone,
		/// The contribution is not high enough to be eligible for rewards
		RewardNotHighEnough,
		/// User trying to associate a native identity with a relay chain identity for posterior
		/// reward claiming provided a wrong signature
		InvalidClaimSignature,
		/// User trying to claim the first free reward provided the wrong signature
		InvalidFreeClaimSignature,
		/// User trying to claim an award did not have an claim associated with it. This may mean
		/// they did not contribute to the crowdloan, or they have not yet associated a native id
		/// with their contribution
		NoAssociatedClaim,
		/// User trying to claim rewards has already claimed all rewards associated with its
		/// identity and contribution
		RewardsAlreadyClaimed,
		/// Reward vec has already been initialized
		RewardVecAlreadyInitialized,
		/// Reward vec has not yet been fully initialized
		RewardVecNotFullyInitializedYet,
		/// Rewards should match funds of the pallet
		RewardsDoNotMatchFund,
		/// Initialize_reward_vec received too many contributors
		TooManyContributors,
		/// Provided vesting period is not valid
		VestingPeriodNonValid,
		/// User provided a signature from a non-contributor relay account
		NonContributedAddressProvided,
		/// User submitted an unsifficient number of proofs to change the reward address
		InsufficientNumberOfValidProofs,
	}

	#[pallet::storage]
	#[pallet::getter(fn accounts_payable)]
	pub type AccountsPayable<T: Config> =
		StorageMap<_, Blake2_128Concat, T::AccountId, RewardInfo<T>>;
	#[pallet::storage]
	#[pallet::getter(fn claimed_relay_chain_ids)]
	pub type ClaimedRelayChainIds<T: Config> =
		StorageMap<_, Blake2_128Concat, T::RelayChainAccountId, ()>;
	#[pallet::storage]
	#[pallet::getter(fn unassociated_contributions)]
	pub type UnassociatedContributions<T: Config> =
		StorageMap<_, Blake2_128Concat, T::RelayChainAccountId, RewardInfo<T>>;
	#[pallet::storage]
	#[pallet::getter(fn initialized)]
	pub type Initialized<T: Config> = StorageValue<_, bool, ValueQuery, T::Initialized>;

	#[pallet::storage]
	#[pallet::storage_prefix = "InitRelayBlock"]
	#[pallet::getter(fn init_vesting_block)]
	/// Vesting block height at the initialization of the pallet
	pub(crate) type InitVestingBlock<T: Config> =
		StorageValue<_, T::VestingBlockNumber, ValueQuery>;

	#[pallet::storage]
	#[pallet::storage_prefix = "EndRelayBlock"]
	#[pallet::getter(fn end_vesting_block)]
	/// Vesting block height at the initialization of the pallet
	pub(crate) type EndVestingBlock<T: Config> = StorageValue<_, T::VestingBlockNumber, ValueQuery>;

	#[pallet::storage]
	#[pallet::getter(fn init_reward_amount)]
	/// Total initialized amount so far. We store this to make pallet funds == contributors reward
	/// check easier and more efficient
	pub(crate) type InitializedRewardAmount<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

	#[pallet::storage]
	#[pallet::getter(fn total_contributors)]
	/// Total number of contributors to aid hinting benchmarking
	pub(crate) type TotalContributors<T: Config> = StorageValue<_, u32, ValueQuery>;

	#[pallet::genesis_config]
	pub struct GenesisConfig<T: Config> {
		/// List of contributors with their relay account, optional native account, and reward amount
		pub funded_accounts: Vec<ContributorData<T>>,
		/// Initial vesting block number
		pub init_vesting_block: T::VestingBlockNumber,
		/// End vesting block number
		pub end_vesting_block: T::VestingBlockNumber,
	}

	impl<T: Config> Default for GenesisConfig<T> {
		fn default() -> Self {
			Self {
				funded_accounts: vec![],
				init_vesting_block: Default::default(),
				end_vesting_block: Default::default(),
			}
		}
	}

	#[pallet::genesis_build]
	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
		fn build(&self) {
			// Set the vesting blocks
			InitVestingBlock::<T>::put(self.init_vesting_block.clone());
			EndVestingBlock::<T>::put(self.end_vesting_block.clone());

			let mut total_contributors = 0u32;
			let mut total_rewards = BalanceOf::<T>::default();

			// Process each funded account
			for (relay_account, native_account_opt, reward) in &self.funded_accounts {
				// Skip if reward is less than minimum
				if *reward < T::MinimumReward::get() {
					continue;
				}

				// Calculate the initial payment
				let initial_payment = if native_account_opt.is_some() {
					let payment = T::InitializationPayment::get() * (*reward);
					// Transfer the initial payment to the native account
					if let Some(native_account) = native_account_opt {
						let _ = T::RewardCurrency::transfer(
							&PALLET_ID.into_account_truncating(),
							native_account,
							payment,
							AllowDeath,
						);
					}
					payment
				} else {
					BalanceOf::<T>::default()
				};

				// Create reward info
				let reward_info = RewardInfo {
					total_reward: *reward,
					claimed_reward: initial_payment,
					contributed_relay_addresses: vec![relay_account.clone()],
				};

				// Store the reward info based on whether account is associated
				if let Some(native_account) = native_account_opt {
					AccountsPayable::<T>::insert(native_account, &reward_info);
					ClaimedRelayChainIds::<T>::insert(relay_account, ());
				} else {
					UnassociatedContributions::<T>::insert(relay_account, &reward_info);
				}

				total_contributors += 1;
				total_rewards += *reward - initial_payment;
			}

			// Update total contributors and initialized reward amount
			TotalContributors::<T>::put(total_contributors);
			InitializedRewardAmount::<T>::put(total_rewards);

			// Mark as initialized only if there are funded accounts
			if !self.funded_accounts.is_empty() {
				<Initialized<T>>::put(true);
			}
		}
	}

	#[pallet::event]
	#[pallet::generate_deposit(fn deposit_event)]
	pub enum Event<T: Config> {
		/// The initial payment of InitializationPayment % was paid
		InitialPaymentMade(T::AccountId, BalanceOf<T>),
		/// Someone has proven they made a contribution and associated a native identity with it.
		/// Data is the relay account,  native account and the total amount of _rewards_ that will be paid
		NativeIdentityAssociated(T::RelayChainAccountId, T::AccountId, BalanceOf<T>),
		/// A contributor has claimed some rewards.
		/// Data is the account getting paid and the amount of rewards paid.
		RewardsPaid(T::AccountId, BalanceOf<T>),
		/// A contributor has updated the reward address.
		RewardAddressUpdated(T::AccountId, T::AccountId),
		/// When initializing the reward vec an already initialized account was found
		InitializedAlreadyInitializedAccount(
			T::RelayChainAccountId,
			Option<T::AccountId>,
			BalanceOf<T>,
		),
		/// When initializing the reward vec an already initialized account was found
		InitializedAccountWithNotEnoughContribution(
			T::RelayChainAccountId,
			Option<T::AccountId>,
			BalanceOf<T>,
		),
	}
}