Files
api/packages/api-derive/src/contracts/fees.ts
T
2021-01-03 11:26:46 +01:00

67 lines
2.4 KiB
TypeScript

// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { Observable } from '@polkadot/x-rxjs';
import type { DeriveContractFees } from '../types';
import BN from 'bn.js';
import { of } from '@polkadot/x-rxjs';
import { map } from '@polkadot/x-rxjs/operators';
import { memo } from '../util';
type ResultV2 = [BN, BN, BN, BN, BN, BN, BN, BN, BN, BN];
// query via constants (current applicable path)
function queryConstants (api: ApiInterfaceRx): Observable<ResultV2> {
return of([
// deprecated
api.consts.contracts.callBaseFee || api.registry.createType('Balance'),
api.consts.contracts.contractFee || api.registry.createType('Balance'),
api.consts.contracts.creationFee || api.registry.createType('Balance'),
api.consts.contracts.transactionBaseFee || api.registry.createType('Balance'),
api.consts.contracts.transactionByteFee || api.registry.createType('Balance'),
api.consts.contracts.transferFee || api.registry.createType('Balance'),
// current
api.consts.contracts.rentByteFee,
api.consts.contracts.rentDepositOffset,
api.consts.contracts.surchargeReward,
api.consts.contracts.tombstoneDeposit
]) as unknown as Observable<ResultV2>;
}
/**
* @name fees
* @returns An object containing the combined results of the queries for
* all relevant contract fees as declared in the substrate chain spec.
* @example
* <BR>
*
* ```javascript
* api.derive.contracts.fees(([creationFee, transferFee]) => {
* console.log(`The fee for creating a new contract on this chain is ${creationFee} units. The fee required to call this contract is ${transferFee} units.`);
* });
* ```
*/
export function fees (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveContractFees> {
return memo(instanceId, (): Observable<DeriveContractFees> => {
return queryConstants(api).pipe(
map(([callBaseFee, contractFee, creationFee, transactionBaseFee, transactionByteFee, transferFee, rentByteFee, rentDepositOffset, surchargeReward, tombstoneDeposit]): DeriveContractFees => ({
callBaseFee,
contractFee,
creationFee,
rentByteFee,
rentDepositOffset,
surchargeReward,
tombstoneDeposit,
transactionBaseFee,
transactionByteFee,
transferFee
}))
);
});
}