Skip to content
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
22 changes: 19 additions & 3 deletions e2e/capacity/capacity_rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import '@frequency-chain/api-augment';
import { KeyringPair } from '@polkadot/keyring/types';
import { u64, u16 } from '@polkadot/types';
import assert from 'assert';
import { AddProviderPayload, ExtrinsicHelper } from '../scaffolding/extrinsicHelpers';
import { AddProviderPayload, Extrinsic, ExtrinsicHelper } from '../scaffolding/extrinsicHelpers';
import {
createKeys,
createMsaAndProvider,
generateDelegationPayload,
signPayloadSr25519,
DOLLARS,
getOrCreateGraphChangeSchema,
stakeToProvider,
} from '../scaffolding/helpers';
import { FeeDetails } from '@polkadot/types/interfaces';
import { getFundingSource } from '../scaffolding/funding';
import { getUnifiedPublicKey } from '@frequency-chain/ethereum-utils';
import { firstValueFrom, tap } from 'rxjs';

const FUNDS_AMOUNT: bigint = 50n * DOLLARS;
const FUNDS_AMOUNT: bigint = 200n * DOLLARS;
const STAKED_AMOUNT: bigint = 50n * DOLLARS;
let fundingSource: KeyringPair;

describe('Capacity RPC', function () {
Expand All @@ -37,6 +40,7 @@ describe('Capacity RPC', function () {
'CapacityProvider',
FUNDS_AMOUNT
);
await stakeToProvider(fundingSource, capacityProviderKeys, capacityProvider, STAKED_AMOUNT);
defaultPayload = {
authorizedMsaId: capacityProvider,
schemaIds: [schemaId],
Expand Down Expand Up @@ -96,19 +100,31 @@ describe('Capacity RPC', function () {
addProviderPayload
);
const tx = ExtrinsicHelper.api.tx.frequencyTxPayment.payWithCapacity(insideTx);
const signedTx = await firstValueFrom(tx.signAsync(capacityProviderKeys));
const signedHex = signedTx.toHex();
// it's important to submit a signed extrinsic to the rpc to get an accurate estimate due to the actual length of extrinsic and etc.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const feeDetails: FeeDetails = await ExtrinsicHelper.apiPromise.rpc.frequencyTxPayment.computeCapacityFeeDetails(
tx.toHex(),
signedHex,
null
);
assert.notEqual(feeDetails, undefined, 'should have returned a feeDetails');
assert.notEqual(feeDetails.inclusionFee, undefined, 'should have returned a partialFee');
assert(feeDetails.inclusionFee.isSome, 'should have returned a partialFee');
const { baseFee, lenFee, adjustedWeightFee } = feeDetails.inclusionFee.toJSON() as any;
const estimatedCapacity = BigInt(baseFee + lenFee + adjustedWeightFee);

const extrinsic = new Extrinsic(() => insideTx, capacityProviderKeys, ExtrinsicHelper.api.events.msa.MsaCreated);
const { eventMap } = await extrinsic.payWithCapacity();
const callCapacityCost = (eventMap['capacity.CapacityWithdrawn'].data as any).amount.toBigInt();

// payWithCapacity costs will fluctuate, so just checking that they are valid positive numbers
assert(baseFee > 1, 'The base fee appears to be wrong.');
assert(lenFee > 1, 'The len fee appears to be wrong.');
assert(adjustedWeightFee > 1, 'The adjusted weight fee appears to be wrong');
assert(
estimatedCapacity - callCapacityCost < 20_000,
'The estimated capacity and the actual capacity are not within threshold'
);
});

it('Returns nothing when requesting capacity cost of a non-capacity transaction', async function () {
Expand Down
6 changes: 3 additions & 3 deletions pallets/frequency-tx-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ impl<T: Config> Pallet<T> {
/// Compute the capacity fee details for a transaction.
/// # Arguments
/// * `runtime_call` - The runtime call to be dispatched.
/// * `weight` - The weight of the transaction.
/// * `overhead_weight` - The overhead weight associated with capacity transactions.
/// * `len` - The length of the transaction.
///
/// # Returns
/// `FeeDetails` - The fee details for the transaction.
pub fn compute_capacity_fee_details(
runtime_call: &<T as Config>::RuntimeCall,
dispatch_weight: &Weight,
overhead_weight: &Weight,
len: u32,
) -> FeeDetails<BalanceOf<T>> {
let calls = T::CapacityCalls::get_inner_calls(runtime_call)
Expand All @@ -286,7 +286,7 @@ impl<T: Config> Pallet<T> {

let mut fees = FeeDetails { inclusion_fee: None, tip: Zero::zero() };
if !calls_weight_sum.is_zero() {
if let Some(weight) = calls_weight_sum.checked_add(dispatch_weight) {
if let Some(weight) = calls_weight_sum.checked_add(overhead_weight) {
let weight_fee = Self::weight_to_fee(weight);
let len_fee = Self::length_to_fee(len);
let base_fee = Self::weight_to_fee(CAPACITY_EXTRINSIC_BASE_WEIGHT);
Expand Down
16 changes: 8 additions & 8 deletions runtime/frequency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: Cow::Borrowed("frequency"),
impl_name: Cow::Borrowed("frequency"),
authoring_version: 1,
spec_version: 178,
spec_version: 179,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand All @@ -654,7 +654,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: Cow::Borrowed("frequency-testnet"),
impl_name: Cow::Borrowed("frequency"),
authoring_version: 1,
spec_version: 178,
spec_version: 179,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -1950,16 +1950,16 @@ sp_api::impl_runtime_apis! {

// if the call is wrapped in a batch, we need to get the weight of the outer call
// and use that to compute the fee with the inner call's stable weight(s)
let dispatch_weight = match &uxt.function {
RuntimeCall::FrequencyTxPayment(pallet_frequency_tx_payment::Call::pay_with_capacity { .. }) |
RuntimeCall::FrequencyTxPayment(pallet_frequency_tx_payment::Call::pay_with_capacity_batch_all { .. }) => {
<<Block as BlockT>::Extrinsic as GetDispatchInfo>::get_dispatch_info(&uxt).call_weight
},
let capacity_overhead_weight = match &uxt.function {
RuntimeCall::FrequencyTxPayment(pallet_frequency_tx_payment::Call::pay_with_capacity { .. }) =>
<() as pallet_frequency_tx_payment::WeightInfo>::pay_with_capacity(),
RuntimeCall::FrequencyTxPayment(pallet_frequency_tx_payment::Call::pay_with_capacity_batch_all { calls, .. }) =>
<() as pallet_frequency_tx_payment::WeightInfo>::pay_with_capacity_batch_all(calls.len() as u32),
_ => {
Weight::zero()
}
};
FrequencyTxPayment::compute_capacity_fee_details(&uxt.function, &dispatch_weight, len)
FrequencyTxPayment::compute_capacity_fee_details(&uxt.function, &capacity_overhead_weight, len)
}
}

Expand Down