From 6c0d6832357666b15fd892e6397e9f444ea86ae7 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 7 May 2019 15:53:40 +0200 Subject: [PATCH] Add contract fees (#875) --- packages/api-derive/src/balances/fees.ts | 10 ++-- packages/api-derive/src/contract/fees.ts | 60 +++++++++++++++++++++++ packages/api-derive/src/contract/index.ts | 5 ++ packages/api-derive/src/index.ts | 2 + packages/api-derive/src/types.ts | 13 +++++ 5 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 packages/api-derive/src/contract/fees.ts create mode 100644 packages/api-derive/src/contract/index.ts diff --git a/packages/api-derive/src/balances/fees.ts b/packages/api-derive/src/balances/fees.ts index 49507e568d..230b6efeb3 100644 --- a/packages/api-derive/src/balances/fees.ts +++ b/packages/api-derive/src/balances/fees.ts @@ -16,12 +16,8 @@ export function fees (api: ApiInterface$Rx) { return (combineLatest([ api.query.balances.creationFee(), api.query.balances.existentialDeposit(), - api.query.fees - ? api.query.fees.transactionBaseFee() - : api.query.balances.transactionBaseFee(), - api.query.fees - ? api.query.fees.transactionByteFee() - : api.query.balances.transactionByteFee(), + api.query.balances.transactionBaseFee(), + api.query.balances.transactionByteFee(), api.query.balances.transferFee() ]) as any as Observable<[BN, BN, BN, BN, BN]>).pipe( map(([creationFee, existentialDeposit, transactionBaseFee, transactionByteFee, transferFee]) => ({ @@ -32,6 +28,6 @@ export function fees (api: ApiInterface$Rx) { transferFee })), drr() - ) ; + ); }; } diff --git a/packages/api-derive/src/contract/fees.ts b/packages/api-derive/src/contract/fees.ts new file mode 100644 index 0000000000..fd6d0e0480 --- /dev/null +++ b/packages/api-derive/src/contract/fees.ts @@ -0,0 +1,60 @@ +// Copyright 2017-2019 @polkadot/api-derive authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { ApiInterface$Rx, QueryableStorageFunctionBase } from '@polkadot/api/types'; +import { Codec } from '@polkadot/types/types'; +import { DerivedContractFees } from '../types'; + +import BN from 'bn.js'; +import { Observable, combineLatest, of } from 'rxjs'; +import { map } from 'rxjs/operators'; + +import { drr } from '../util/drr'; + +// TODO The contracts API is still in flux, so some of these may disappear or re-appear or get replaced. Instead of +// failing here, do best effort - if something does not exist, return 0 for the specific value. Should be removed +// when actually stable and no more changes expected +function nonexistentZero (fn: QueryableStorageFunctionBase, Observable>): Observable { + return fn + ? fn() as any as Observable + : of(new BN(0)); +} + +/** + * Get the balances for all intentions and their nominators + */ +export function fees (api: ApiInterface$Rx) { + // TODO Once we have muti support for disjointed queries, add here (so we make a single + // query for all these to the node). + return (): Observable => { + return (combineLatest([ + nonexistentZero(api.query.contract.callBaseFee), + nonexistentZero(api.query.contract.contractFee), + nonexistentZero(api.query.contract.createBaseFee), + nonexistentZero(api.query.contract.creationFee), + nonexistentZero(api.query.contract.rentByteFee), + nonexistentZero(api.query.contract.rentDepositOffset), + nonexistentZero(api.query.contract.transactionBaseFee), + nonexistentZero(api.query.contract.transactionByteFee), + nonexistentZero(api.query.contract.transferFee), + nonexistentZero(api.query.contract.tombstoneDeposit) + ]) as any as Observable>).pipe( + map(([callBaseFee, contractFee, createBaseFee, creationFee, rentByteFee, rentDepositOffset, transactionBaseFee, transactionByteFee, transferFee, tombstoneDeposit]) => { + return { + callBaseFee, + contractFee, + createBaseFee, + creationFee, + rentByteFee, + rentDepositOffset, + transactionBaseFee, + transactionByteFee, + transferFee, + tombstoneDeposit + }; + }), + drr() + ); + }; +} diff --git a/packages/api-derive/src/contract/index.ts b/packages/api-derive/src/contract/index.ts new file mode 100644 index 0000000000..61d0bd6470 --- /dev/null +++ b/packages/api-derive/src/contract/index.ts @@ -0,0 +1,5 @@ +// Copyright 2017-2019 @polkadot/api-derive authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +export * from './fees'; diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index 33852d6f2d..449fa273e2 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -8,6 +8,7 @@ import { ApiInterface$Rx } from '@plugnet/api/types'; import * as accounts from './accounts'; import * as balances from './balances'; import * as chain from './chain'; +import * as contract from './contract'; import * as democracy from './democracy'; import * as session from './session'; import * as staking from './staking'; @@ -37,6 +38,7 @@ export interface Derive { accounts: ReturnTypes; balances: ReturnTypes; chain: ReturnTypes; + contract: ReturnTypes; democracy: ReturnTypes; session: ReturnTypes; staking: ReturnTypes; diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 1d3c804fa7..717d980e7f 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -32,3 +32,16 @@ export type DerivedReferendumVote = { balance: Balance, vote: Vote }; + +export type DerivedContractFees = { + callBaseFee: BN, + contractFee: BN, + createBaseFee: BN, + creationFee: BN, + rentByteFee: BN, + rentDepositOffset: BN, + transactionBaseFee: BN, + transactionByteFee: BN, + transferFee: BN, + tombstoneDeposit: BN +};