-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
204 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "proxy-bonding" | ||
authors.workspace = true | ||
documentation.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license-file.workspace = true | ||
readme.workspace = true | ||
repository.workspace = true | ||
version.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
frame-system.workspace = true | ||
frame-support.workspace = true | ||
sp-runtime.workspace = true | ||
polimec-common.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use crate::pallet::{Pallet, Config}; | ||
|
||
impl<T: Config> Pallet<T> { | ||
|
||
pub fn bond_on_behalf_of(account: T::AccountId, amount: T::Balance) -> Result<(), &'static str> { | ||
let bond = T::NativeToken::hold(account.clone(), amount)?; | ||
T::NativeToken::transfer(account, T::ProxyBonding::account_id(), bond)?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
mod functions; | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*, DefaultNoBound, traits::fungible}; | ||
use frame_system::pallet_prelude::*; | ||
use sp_runtime::traits::{CheckedAdd, One}; | ||
use polimec_common::ProvideAssetPrice; | ||
|
||
|
||
type Balance = u128; | ||
|
||
/// Configure the pallet by specifying the parameters and types on which it depends. | ||
#[pallet::config] | ||
pub trait Config: frame_system::Config { | ||
/// Because this pallet emits events, it depends on the runtime's definition of an event. | ||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; | ||
|
||
/// A type representing the weights required by the dispatchables of this pallet. | ||
type WeightInfo: crate::weights::WeightInfo; | ||
|
||
/// The pallet giving access to this chain's native token | ||
type NativeToken: fungible::Inspect<Self::AccountId, Balance=Balance> + fungible::MutateHold<Self::AccountId, Balance=Balance>; | ||
|
||
/// Method to get the price of an asset like USDT or PLMC. Likely to come from an oracle | ||
type PriceProvider: ProvideAssetPrice<AssetId = u32, Price = Self::Price>; | ||
} | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::event] | ||
#[pallet::generate_deposit(pub(super) fn deposit_event)] | ||
pub enum Event<T: Config> { | ||
/// We usually use passive tense for events. | ||
SomethingStored { block_number: BlockNumberFor<T>, who: T::AccountId }, | ||
} | ||
|
||
#[pallet::error] | ||
pub enum Error<T> { | ||
/// Error names should be descriptive. | ||
NoneValue, | ||
/// Errors should have helpful documentation associated with them. | ||
StorageOverflow, | ||
} | ||
|
||
#[pallet::hooks] | ||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {} | ||
|
||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
/// An example dispatchable that takes a singles value as a parameter, writes the value to | ||
/// storage and emits an event. This function must be dispatched by a signed extrinsic. | ||
#[pallet::call_index(0)] | ||
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] | ||
pub fn do_something(origin: OriginFor<T>, bn: u32) -> DispatchResultWithPostInfo { | ||
// Check that the extrinsic was signed and get the signer. | ||
// This function will return an error if the extrinsic is not signed. | ||
// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_origin/index.html> | ||
let who = ensure_signed(origin)?; | ||
|
||
// Convert the u32 into a block number. This is possible because the set of trait bounds | ||
// defined in [`frame_system::Config::BlockNumber`]. | ||
let block_number: BlockNumberFor<T> = bn.into(); | ||
|
||
// Update storage. | ||
<Something<T>>::put(CompositeStruct { block_number }); | ||
|
||
// Emit an event. | ||
Self::deposit_event(Event::SomethingStored { block_number, who }); | ||
|
||
// Return a successful [`DispatchResultWithPostInfo`] or [`DispatchResult`]. | ||
Ok(().into()) | ||
} | ||
|
||
/// An example dispatchable that may throw a custom error. | ||
#[pallet::call_index(1)] | ||
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().reads_writes(1,1))] | ||
pub fn cause_error(origin: OriginFor<T>) -> DispatchResultWithPostInfo { | ||
let _who = ensure_signed(origin)?; | ||
|
||
// Read a value from storage. | ||
match <Something<T>>::get() { | ||
// Return an error if the value has not been set. | ||
None => Err(Error::<T>::NoneValue)?, | ||
Some(mut old) => { | ||
// Increment the value read from storage; will error in the event of overflow. | ||
old.block_number = old | ||
.block_number | ||
.checked_add(&One::one()) | ||
// ^^ equivalent is to: | ||
// .checked_add(&1u32.into()) | ||
// both of which build a `One` instance for the type `BlockNumber`. | ||
.ok_or(Error::<T>::StorageOverflow)?; | ||
// Update the value in storage with the incremented result. | ||
<Something<T>>::put(old); | ||
// Explore how you can rewrite this using | ||
// [`frame_support::storage::StorageValue::mutate`]. | ||
Ok(().into()) | ||
}, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters