-
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
19 changed files
with
514 additions
and
71 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
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,62 @@ | ||
[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 | ||
frame-benchmarking = { workspace = true, optional = true } | ||
sp-runtime.workspace = true | ||
polimec-common.workspace = true | ||
parity-scale-codec.workspace = true | ||
scale-info.workspace = true | ||
|
||
[dev-dependencies] | ||
sp-io.workspace = true | ||
pallet-linear-release.workspace = true | ||
pallet-balances.workspace = true | ||
pallet-assets.workspace = true | ||
|
||
|
||
[features] | ||
default = ["std"] | ||
|
||
std = [ | ||
"frame-system/std", | ||
"frame-support/std", | ||
"sp-runtime/std", | ||
"polimec-common/std", | ||
"parity-scale-codec/std", | ||
"scale-info/std", | ||
"frame-benchmarking?/std", | ||
|
||
"sp-io/std", | ||
"pallet-linear-release/std", | ||
"pallet-balances/std", | ||
"pallet-assets/std" | ||
] | ||
|
||
try-runtime = [ | ||
"frame-support/try-runtime", | ||
"frame-system/try-runtime", | ||
"sp-runtime/try-runtime", | ||
"polimec-common/try-runtime" | ||
] | ||
|
||
runtime-benchmarks = [ | ||
"frame-benchmarking/runtime-benchmarks", | ||
"frame-support/runtime-benchmarks", | ||
"frame-system/runtime-benchmarks", | ||
"sp-runtime/runtime-benchmarks", | ||
"polimec-common/runtime-benchmarks" | ||
] |
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,69 @@ | ||
use crate::{AccountIdOf, AssetId, BalanceOf, Config, HoldReasonOf, Pallet}; | ||
use frame_support::traits::{ | ||
fungible, | ||
fungible::{Inspect, Mutate, MutateHold}, | ||
fungibles, | ||
fungibles::Mutate as FungiblesMutate, | ||
tokens::{Fortitude, Precision, Preservation}, | ||
}; | ||
use polimec_common::ProvideAssetPrice; | ||
use sp_runtime::{ | ||
traits::{AccountIdConversion, CheckedMul, Get}, | ||
FixedPointNumber, | ||
}; | ||
|
||
impl<T: Config> Pallet<T> { | ||
pub fn bond_on_behalf_of( | ||
account: T::AccountId, | ||
bond_amount: BalanceOf<T>, | ||
fee_asset: AssetId, | ||
derivation_path: u32, | ||
hold_reason: HoldReasonOf<T>, | ||
) -> Result<AccountIdOf<T>, &'static str> { | ||
let treasury = T::Treasury::get(); | ||
let bonding_account: AccountIdOf<T> = T::RootId::get().into_sub_account_truncating(derivation_path); | ||
let existential_deposit = <T::BondingToken as fungible::Inspect<T::AccountId>>::minimum_balance(); | ||
|
||
let bonding_token_price = T::PriceProvider::get_decimals_aware_price( | ||
T::BondingTokenId::get(), | ||
T::UsdDecimals::get(), | ||
T::BondingTokenDecimals::get(), | ||
) | ||
.ok_or("Price not available")?; | ||
|
||
let fee_asset_decimals = <T::FeeToken as fungibles::metadata::Inspect<AccountIdOf<T>>>::decimals(fee_asset); | ||
let fee_token_price = | ||
T::PriceProvider::get_decimals_aware_price(fee_asset, T::UsdDecimals::get(), fee_asset_decimals) | ||
.ok_or("Price not available")?; | ||
|
||
let bonded_in_usd = bonding_token_price.saturating_mul_int(bond_amount); | ||
let fee_in_usd = T::FeePercentage::get() * bonded_in_usd; | ||
let fee_in_fee_asset = | ||
fee_token_price.reciprocal().ok_or("Price not available")?.saturating_mul_int(fee_in_usd); | ||
|
||
// Pay the fee from the user to the treasury | ||
T::FeeToken::transfer(fee_asset, &account, &treasury, fee_in_fee_asset, Preservation::Preserve)?; | ||
|
||
// Ensure the sub-account has an ED by the treasury. This will be refunded after all the tokens are unlocked | ||
if T::BondingToken::balance(&bonding_account) < existential_deposit { | ||
T::BondingToken::transfer( | ||
&treasury.clone(), | ||
&bonding_account, | ||
existential_deposit, | ||
Preservation::Preserve, | ||
)?; | ||
} | ||
// Bond the PLMC on behalf of the user | ||
T::BondingToken::transfer_and_hold( | ||
&hold_reason, | ||
&treasury.clone(), | ||
&bonding_account.clone(), | ||
bond_amount, | ||
Precision::Exact, | ||
Preservation::Preserve, | ||
Fortitude::Polite, | ||
)?; | ||
|
||
Ok(bonding_account) | ||
} | ||
} |
Oops, something went wrong.