Minor cleanups, no functionality (#1308)

* Minor cleanups, no functionality

* Shared fee parsing
This commit is contained in:
Jaco Greeff
2019-08-14 15:04:58 +02:00
committed by GitHub
parent fd962327b6
commit a39caccedc
2 changed files with 37 additions and 30 deletions
+34 -29
View File
@@ -11,8 +11,26 @@ import { map } from 'rxjs/operators';
import { drr } from '../util/drr';
type ResultV2 = [BN, BN, BN, BN, BN, BN, BN, BN, BN, BN];
const ZERO = new BN(0);
// parse the result
function parseResult ([callBaseFee, contractFee, createBaseFee, creationFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee, transferFee]: ResultV2): DerivedContractFees {
return {
callBaseFee,
contractFee,
createBaseFee,
creationFee,
rentByteFee,
rentDepositOffset,
tombstoneDeposit,
transactionBaseFee,
transactionByteFee,
transferFee
};
}
// Only for 1.0 support. rentByteFee, rentDepositOffset, tombstoneDeposit are not available in substrate 1.0.
// TODO remove this once 1.0 support is dropped
function queryV1 (api: ApiInterfaceRx): Observable<DerivedContractFees> {
@@ -27,24 +45,19 @@ function queryV1 (api: ApiInterfaceRx): Observable<DerivedContractFees> {
api.query.contract.transferFee
]) as unknown as Observable<BN[]>
).pipe(
map(([callBaseFee, contractFee, createBaseFee, creationFee, transactionBaseFee, transactionByteFee, transferFee]): DerivedContractFees => ({
callBaseFee,
contractFee,
createBaseFee,
creationFee,
rentByteFee: ZERO,
rentDepositOffset: ZERO,
tombstoneDeposit: ZERO,
transactionBaseFee,
transactionByteFee,
transferFee
} as unknown as DerivedContractFees)),
map(([callBaseFee, contractFee, createBaseFee, creationFee, transactionBaseFee, transactionByteFee, transferFee]): DerivedContractFees =>
// We've done this on purpose, i.e. so we can just copy the name/order from the parse above and
// see gaps, in this case those are filled with `ZERO`
parseResult([
callBaseFee, contractFee, createBaseFee, creationFee, ZERO, ZERO, ZERO, transactionBaseFee, transactionByteFee, transferFee
])
),
drr()
);
}
// query via query (early v2, non-current, support to be dropped])
function queryQuery (api: ApiInterfaceRx): Observable<BN[]> {
function queryQuery (api: ApiInterfaceRx): Observable<ResultV2> {
const queryBase = api.query.contracts || api.query.contract;
return api.queryMulti([
@@ -58,11 +71,11 @@ function queryQuery (api: ApiInterfaceRx): Observable<BN[]> {
queryBase.transactionBaseFee,
queryBase.transactionByteFee,
queryBase.transferFee
]) as unknown as Observable<BN[]>;
]) as unknown as Observable<ResultV2>;
}
// query via constants (current applicable path)
function queryConstants (api: ApiInterfaceRx): Observable<BN[]> {
function queryConstants (api: ApiInterfaceRx): Observable<ResultV2> {
return of([
api.consts.contracts.callBaseFee,
api.consts.contracts.contractFee,
@@ -74,7 +87,7 @@ function queryConstants (api: ApiInterfaceRx): Observable<BN[]> {
api.consts.contracts.transactionBaseFee,
api.consts.contracts.transactionByteFee,
api.consts.contracts.transferFee
]) as unknown as Observable<BN[]>;
]) as unknown as Observable<ResultV2>;
}
/**
@@ -101,19 +114,11 @@ export function fees (api: ApiInterfaceRx): () => Observable<DerivedContractFees
? queryConstants(api)
: queryQuery(api)
).pipe(
map(
([callBaseFee, contractFee, createBaseFee, creationFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee, transferFee]): DerivedContractFees => ({
callBaseFee,
contractFee,
createBaseFee,
creationFee,
rentByteFee,
rentDepositOffset,
tombstoneDeposit,
transactionBaseFee,
transactionByteFee,
transferFee
} as unknown as DerivedContractFees)
map(([callBaseFee, contractFee, createBaseFee, creationFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee, transferFee]): DerivedContractFees =>
// We've done this on purpose, i.e. so we can just copy the name/order from the parse above and see gaps
parseResult([
callBaseFee, contractFee, createBaseFee, creationFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee, transferFee
])
),
drr()
);
@@ -3,6 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId } from '@polkadot/types/interfaces';
import { Codec } from '@polkadot/types/types';
import { Observable, combineLatest, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
@@ -16,7 +17,8 @@ import { drr } from '../util/drr';
*/
export function controllers (api: ApiInterfaceRx): () => Observable<[AccountId[], Option<AccountId>[]]> {
return (): Observable<[AccountId[], Option<AccountId>[]]> =>
(api.query.staking.validators() as any as Observable<[AccountId[], any]>)
api.query.staking
.validators<[AccountId[]] & Codec>()
.pipe(
switchMap(([stashIds]): Observable<[AccountId[], Option<AccountId>[]]> =>
combineLatest([