Skip to content

Commit

Permalink
fix: set rpc to call for queryInfo in transaction/fee-estimate (parit…
Browse files Browse the repository at this point in the history
…ytech#1170)

* fix: set rpc to call for queryInfo in transaction/fee-estimate

* update docs
  • Loading branch information
TarikGul authored Dec 22, 2022
1 parent 095f57f commit 7334599
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2579,8 +2579,8 @@ components:
type: object
properties:
weight:
type: string
description: Extrinsic weight.
$ref: '#/components/schemas/WeightsV2'
description: Weights represented as WeightsV2 (two dimensional weights). When querying historical blocks that use WeightsV1, the weight will be returned as a weight key that points to a number represented as a string.
class:
type: string
description: Extrinsic class.
Expand Down Expand Up @@ -2898,8 +2898,8 @@ components:
type: object
properties:
weight:
type: string
description: Extrinsic weight.
$ref: '#/components/schemas/WeightsV2'
description: Weights represented as WeightsV2 (two dimensional weights). When querying historical blocks that use WeightsV1, the weight will be returned as a weight key that points to a number represented as a string.
class:
type: string
description: Extrinsic class.
Expand Down Expand Up @@ -3056,6 +3056,15 @@ components:
description: Starting block for unlocking (vesting).
format: unsignedInteger
description: Vesting schedule for an account.
WeightsV2:
type: object
properties:
refTime:
type: string
description: The weight of computational time used based on some reference hardware.
proofSize:
type: string
description: The weight of storage space used by proof of validity.
WinningData:
type: object
properties:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"weight": "195000000",
"class": "Normal",
"partialFee": "149000000"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"weight": {
"proofSize": "0",
"refTime": "133179000"
},
"class": "Normal",
"partialFee": "171607466"
}
51 changes: 47 additions & 4 deletions src/services/transaction/TransactionFeeEstimateService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */

import { ApiPromise } from '@polkadot/api';
import { Hash } from '@polkadot/types/interfaces';

import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { polkadotRegistryV9300 } from '../../test-helpers/registries';
import {
balancesTransferInvalid,
balancesTransferValid,
Expand All @@ -28,11 +30,33 @@ import {
queryInfoBalancesTransfer,
} from '../test-helpers/mock';
import invalidResponse from '../test-helpers/responses/transaction/feeEstimateInvalid.json';
import validResponse from '../test-helpers/responses/transaction/feeEstimateValid.json';
import validRpcResponse from '../test-helpers/responses/transaction/feeEstimateValidRpcCall.json';
import validRuntimeResponse from '../test-helpers/responses/transaction/feeEstimateValidRuntimeCall.json';
import { TransactionFeeEstimateService } from './TransactionFeeEstimateService';

const queryInfoCallAt = () =>
Promise.resolve().then(() =>
polkadotRegistryV9300.createType('RuntimeDispatchInfoV2', {
weight: {
refTime: '133179000',
proofSize: '0',
},
class: 'Normal',
partialFee: '171607466',
})
);

const mockApiAt = {
call: {
transactionPaymentApi: {
queryInfo: queryInfoCallAt,
},
},
};

const mockApi = {
...defaultMockApi,
at: (_hash: Hash) => mockApiAt,
} as unknown as ApiPromise;

const transactionFeeEstimateService = new TransactionFeeEstimateService(
Expand All @@ -41,24 +65,41 @@ const transactionFeeEstimateService = new TransactionFeeEstimateService(

describe('TransactionFeeEstimateService', () => {
describe('fetchTransactionFeeEstimate', () => {
it('works with a valid a transaction', async () => {
it('Works with a valid a transaction', async () => {
expect(
sanitizeNumbers(
await transactionFeeEstimateService.fetchTransactionFeeEstimate(
blockHash789629,
balancesTransferValid
)
)
).toStrictEqual(validResponse);
).toStrictEqual(validRuntimeResponse);
});

it("Should default to the rpc call when the runtime call doesn't exist", async () => {
(mockApiAt.call.transactionPaymentApi.queryInfo as unknown) = undefined;

expect(
sanitizeNumbers(
await transactionFeeEstimateService.fetchTransactionFeeEstimate(
blockHash789629,
balancesTransferValid
)
)
).toStrictEqual(validRpcResponse);

(mockApiAt.call.transactionPaymentApi.queryInfo as unknown) =
queryInfoCallAt;
});

it('catches ApiPromise throws and then throws the correct error format', async () => {
it('Catches ApiPromise throws and then throws the correct error format', async () => {
const err = new Error(
'2: Unable to query dispatch info.: Invalid transaction version'
);
err.stack =
'Error: 2: Unable to query dispatch info.: Invalid transaction version\n ... this is a unit test mock';

(mockApiAt.call.transactionPaymentApi.queryInfo as unknown) = undefined;
(mockApi.rpc.payment as any).queryInfo = () =>
Promise.resolve().then(() => {
throw err;
Expand All @@ -72,6 +113,8 @@ describe('TransactionFeeEstimateService', () => {
).rejects.toStrictEqual(invalidResponse);

(mockApi.rpc.payment as any).queryInfo = queryInfoBalancesTransfer;
(mockApiAt.call.transactionPaymentApi.queryInfo as unknown) =
queryInfoCallAt;
});
});
});
13 changes: 12 additions & 1 deletion src/services/transaction/TransactionFeeEstimateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,20 @@ export class TransactionFeeEstimateService extends AbstractService {
transaction: string
): Promise<RuntimeDispatchInfo> {
const { api } = this;
const apiAt = await api.at(hash);

try {
return await api.rpc.payment.queryInfo(transaction, hash);
if (apiAt.call.transactionPaymentApi.queryInfo) {
const ext = api.registry.createType('Extrinsic', transaction);
const u8a = ext.toU8a();

return await apiAt.call.transactionPaymentApi.queryInfo(
ext,
u8a.length
);
} else {
return await api.rpc.payment.queryInfo(transaction, hash);
}
} catch (err) {
const { cause, stack } = extractCauseAndStack(err);

Expand Down

0 comments on commit 7334599

Please sign in to comment.