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

Feat configuring payments pallet kreivo #328

Merged
merged 3 commits into from
Jan 16, 2024
Merged
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
26 changes: 26 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = [
"node",
"runtime/kreivo",
"common"
"common",
]

exclude = ["xcm-emulator"]
Expand Down Expand Up @@ -48,10 +48,10 @@ smallvec = "1.11"
kreivo-runtime = { path = "runtime/kreivo" }
jsonrpsee = { version = "0.16.2"}


# Virto Pallets
pallet-asset-registry = { default-features = false, path = "pallets/asset-registry" }
pallet-burner = { default-features = false, path = "pallets/burner" }
pallet-payments = { default-features = false, path = "pallets/payments" }

runtime-common = { default-features = false, path = "runtime/common" }
kusama-runtime-constants = { default-features = false, path = "runtime/kusama-runtime-constants" }
Expand Down Expand Up @@ -193,7 +193,7 @@ pallet-xcm-benchmarks = { default-features = false, git = "https://github.com/vi

# Dev dependencies
assert_cmd = "2.0"
nix = "0.27.1"
nix = { version = "0.27.1", features = ["signal"] }
tempfile = "3.7"
tokio = { version = "1.32.0", features = ["macros", "time", "parking_lot"] }
wait-timeout = "0.2"
2 changes: 2 additions & 0 deletions pallets/payments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sp-io = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }
log = { workspace = true }

[dev-dependencies]
serde = { workspace = true }
Expand All @@ -30,6 +31,7 @@ sp-keystore = { workspace = true }
pallet-preimage = { workspace = true }
pallet-scheduler= { workspace = true }


[features]
default = ["std"]
runtime-benchmarks = [
Expand Down
36 changes: 21 additions & 15 deletions pallets/payments/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use frame_support::{
};

use frame_system::RawOrigin;
use log;
use sp_runtime::Percent;
use sp_std::vec;

// Compare `generic_event` to the last emitted event.
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
Expand All @@ -31,11 +33,10 @@ fn create_and_mint_asset<T: Config>(
sender: &T::AccountId,
beneficiary: &T::AccountId,
asset: &AssetIdOf<T>,
amount: &BalanceOf<T>,
) -> Result<(), BenchmarkError> {
T::BenchmarkHelper::create_asset(asset.clone(), sender.clone(), true, <BalanceOf<T>>::from(1u32));
T::Assets::mint_into(asset.clone(), &sender, <BalanceOf<T>>::from(100000u32))?;
T::Assets::mint_into(asset.clone(), &beneficiary, <BalanceOf<T>>::from(100000u32))?;
T::Assets::mint_into(asset.clone(), &sender, <BalanceOf<T>>::from(10000000u32))?;
T::Assets::mint_into(asset.clone(), &beneficiary, <BalanceOf<T>>::from(10000000u32))?;

Ok(())
}
Expand All @@ -55,9 +56,10 @@ fn create_payment<T: Config>(
BenchmarkError,
> {
let (sender, beneficiary, sender_lookup, beneficiary_lookup) = create_accounts::<T>();
create_and_mint_asset::<T>(&sender, &beneficiary, &asset, &<BalanceOf<T>>::from(100000u32))?;
create_and_mint_asset::<T>(&sender, &beneficiary, &asset)?;

let payment_id: T::PaymentId = Payments::<T>::next_payment_id()?;
log::info!("payment_id: {:?}", payment_id);

let payment_detail = Payments::<T>::create_payment(
&sender,
Expand All @@ -72,6 +74,8 @@ fn create_payment<T: Config>(
// reserve funds for payment
Payments::<T>::reserve_payment_amount(&sender, &beneficiary, payment_detail)?;

log::info!("reserve_payment_amount executed");

// TODO: check storage items

Ok((payment_id, sender, beneficiary, sender_lookup, beneficiary_lookup))
Expand All @@ -88,8 +92,8 @@ mod benchmarks {
fn pay(q: Linear<1, { T::MaxRemarkLength::get() }>) -> Result<(), BenchmarkError> {
let (sender, beneficiary, _, beneficiary_lookup) = create_accounts::<T>();
let asset: AssetIdOf<T> = <AssetIdOf<T>>::zero();
create_and_mint_asset::<T>(&sender, &beneficiary, &asset, &<BalanceOf<T>>::from(100000u32))?;
let amount = <BalanceOf<T>>::from(50_u32);
create_and_mint_asset::<T>(&sender, &beneficiary, &asset)?;
let amount = <BalanceOf<T>>::from(100000_u32);

let remark: Option<BoundedDataOf<T>> = if q == 0 {
None
Expand Down Expand Up @@ -121,10 +125,12 @@ mod benchmarks {

#[benchmark]
fn release() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(50_u32);
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let (payment_id, sender, beneficiary, _, beneficiary_lookup) = create_payment::<T>(&amount, &asset, None)?;

log::info!("beneficiary_lookup: {:?}", beneficiary_lookup);

#[extrinsic_call]
_(RawOrigin::Signed(sender.clone()), beneficiary_lookup, payment_id);

Expand All @@ -134,7 +140,7 @@ mod benchmarks {

#[benchmark]
fn cancel() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(50_u32);
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let (payment_id, sender, beneficiary, sender_lookup, _beneficiary_lookup) =
create_payment::<T>(&amount, &asset, None)?;
Expand All @@ -148,7 +154,7 @@ mod benchmarks {

#[benchmark]
fn request_refund() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(50_u32);
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let (payment_id, sender, beneficiary, _sender_lookup, beneficiary_lookup) =
create_payment::<T>(&amount, &asset, None)?;
Expand All @@ -172,7 +178,7 @@ mod benchmarks {

#[benchmark]
fn dispute_refund() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(50_u32);
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let (payment_id, sender, beneficiary, sender_lookup, beneficiary_lookup) =
create_payment::<T>(&amount, &asset, None)?;
Expand All @@ -193,7 +199,7 @@ mod benchmarks {

#[benchmark]
fn resolve_dispute() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(50_u32);
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let (payment_id, sender, beneficiary, sender_lookup, beneficiary_lookup) =
create_payment::<T>(&amount, &asset, None)?;
Expand Down Expand Up @@ -234,8 +240,8 @@ mod benchmarks {
fn request_payment() -> Result<(), BenchmarkError> {
let (sender, beneficiary, sender_lookup, _beneficiary_lookup) = create_accounts::<T>();
let asset: AssetIdOf<T> = <AssetIdOf<T>>::zero();
create_and_mint_asset::<T>(&sender, &beneficiary, &asset, &<BalanceOf<T>>::from(100000u32))?;
let amount = <BalanceOf<T>>::from(50_u32);
create_and_mint_asset::<T>(&sender, &beneficiary, &asset)?;
let amount = <BalanceOf<T>>::from(100000_u32);

#[extrinsic_call]
_(RawOrigin::Signed(beneficiary.clone()), sender_lookup, asset, amount);
Expand All @@ -248,8 +254,8 @@ mod benchmarks {
fn accept_and_pay() -> Result<(), BenchmarkError> {
let (sender, beneficiary, sender_lookup, beneficiary_lookup) = create_accounts::<T>();
let asset: AssetIdOf<T> = <AssetIdOf<T>>::zero();
create_and_mint_asset::<T>(&sender, &beneficiary, &asset, &<BalanceOf<T>>::from(100000u32))?;
let amount = <BalanceOf<T>>::from(50_u32);
create_and_mint_asset::<T>(&sender, &beneficiary, &asset)?;
let amount = <BalanceOf<T>>::from(100000_u32);
let payment_id: T::PaymentId = Payments::<T>::next_payment_id()?;

assert!(Payments::<T>::request_payment(
Expand Down
21 changes: 12 additions & 9 deletions pallets/payments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ pub mod pallet {
+ MaybeSerializeDeserialize
+ sp_std::fmt::Debug
+ Default
+ From<u64>
+ TypeInfo
+ MaxEncodedLen;

Expand All @@ -117,6 +116,9 @@ pub mod pallet {
/// The overarching hold reason.
type RuntimeHoldReason: From<HoldReason>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;

#[pallet::constant]
type PalletId: Get<PalletId>;

Expand Down Expand Up @@ -270,7 +272,7 @@ pub mod pallet {
/// custom logic and trigger alternate payment flows. the specified
/// amount.
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(<T as Config>::WeightInfo::pay(remark.as_ref().map(|x| x.len() as u32).unwrap_or(0)))]
pub fn pay(
origin: OriginFor<T>,
beneficiary: AccountIdLookupOf<T>,
Expand Down Expand Up @@ -308,7 +310,7 @@ pub mod pallet {
/// Release any created payment, this will transfer the reserved amount
/// from the creator of the payment to the assigned recipient
#[pallet::call_index(1)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(<T as Config>::WeightInfo::release())]
pub fn release(
origin: OriginFor<T>,
beneficiary: AccountIdLookupOf<T>,
Expand All @@ -332,7 +334,7 @@ pub mod pallet {
/// back to creator of the payment. This extrinsic can only be called by
/// the recipient of the payment
#[pallet::call_index(2)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(<T as Config>::WeightInfo::cancel())]
pub fn cancel(
origin: OriginFor<T>,
sender: AccountIdLookupOf<T>,
Expand Down Expand Up @@ -371,7 +373,7 @@ pub mod pallet {
/// the funds after a configured amount of time that the reveiver has to
/// react and opose the request
#[pallet::call_index(3)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(<T as Config>::WeightInfo::request_refund())]
pub fn request_refund(
origin: OriginFor<T>,
beneficiary: AccountIdLookupOf<T>,
Expand Down Expand Up @@ -430,7 +432,7 @@ pub mod pallet {
/// payment to a NeedsReview state The assigned resolver account can
/// then change the state of the payment after review.
#[pallet::call_index(4)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(<T as Config>::WeightInfo::dispute_refund())]
pub fn dispute_refund(
origin: OriginFor<T>,
sender: AccountIdLookupOf<T>,
Expand Down Expand Up @@ -471,7 +473,7 @@ pub mod pallet {
}

#[pallet::call_index(5)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(<T as Config>::WeightInfo::resolve_dispute())]
pub fn resolve_dispute(
origin: OriginFor<T>,
sender: AccountIdLookupOf<T>,
Expand Down Expand Up @@ -500,7 +502,7 @@ pub mod pallet {
// PaymentRequested State and can only be modified by the `accept_and_pay`
// extrinsic.
#[pallet::call_index(6)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(<T as Config>::WeightInfo::request_payment())]
pub fn request_payment(
origin: OriginFor<T>,
sender: AccountIdLookupOf<T>,
Expand All @@ -526,7 +528,7 @@ pub mod pallet {
}

#[pallet::call_index(7)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(<T as Config>::WeightInfo::accept_and_pay())]
pub fn accept_and_pay(
origin: OriginFor<T>,
beneficiary: AccountIdLookupOf<T>,
Expand Down Expand Up @@ -707,6 +709,7 @@ impl<T: Config> Pallet<T> {
.map_err(|_| Error::<T>::ReleaseFailed)?;

Self::try_transfer_fees(sender, payment, fee_sender_recipients, is_dispute)?;

Self::try_transfer_fees(beneficiary, payment, fee_beneficiary_recipients, is_dispute)?;

if let Some((dispute_result, resolver)) = maybe_dispute {
Expand Down
2 changes: 2 additions & 0 deletions pallets/payments/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl pallet_balances::Config for Test {
type MaxFreezes = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
type RuntimeFreezeReason = RuntimeFreezeReason;
}

impl pallet_assets::Config for Test {
Expand Down Expand Up @@ -254,6 +255,7 @@ impl pallet_payments::Config for Test {
type Preimages = ();
type CancelBufferBlockLength = ConstU64<10>;
type PalletsOrigin = OriginCaller;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = BenchmarkHelper;
}
Expand Down
Loading
Loading