Expose queryMulti on api (#909)

* Expose queryMulti on api

* Convert contract fees to queryMulti
This commit is contained in:
Jaco Greeff
2019-05-15 10:41:48 +02:00
committed by GitHub
parent bd22da687e
commit 3557657034
5 changed files with 96 additions and 41 deletions
+7 -7
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Observable, combineLatest } from 'rxjs';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
@@ -12,12 +12,12 @@ import { drr } from '../util/drr';
export function fees (api: ApiInterface$Rx) {
return (): Observable<DerivedFees> => {
return (combineLatest([
api.query.balances.creationFee(),
api.query.balances.existentialDeposit(),
api.query.balances.transactionBaseFee(),
api.query.balances.transactionByteFee(),
api.query.balances.transferFee()
return (api.queryMulti([
api.query.balances.creationFee,
api.query.balances.existentialDeposit,
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]) => ({
creationFee,
+14 -26
View File
@@ -2,43 +2,31 @@
// 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 { ApiInterface$Rx } from '@polkadot/api/types';
import { DerivedContractFees } from '../types';
import BN from 'bn.js';
import { Observable, combineLatest, of } from 'rxjs';
import { Observable } 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<Codec>, Observable<Codec>>): Observable<BN> {
return fn
? fn() as any as Observable<BN>
: of(new BN(0));
}
/**
* Get the balances for all intentions and their nominators
* Retrieves all the contract fees
*/
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<DerivedContractFees> => {
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)
return (api.queryMulti([
api.query.contract.callBaseFee,
api.query.contract.contractFee,
api.query.contract.createBaseFee,
api.query.contract.creationFee,
api.query.contract.rentByteFee,
api.query.contract.rentDepositOffset,
api.query.contract.transactionBaseFee,
api.query.contract.transactionByteFee,
api.query.contract.transferFee,
api.query.contract.tombstoneDeposit
]) as any as Observable<Array<BN>>).pipe(
map(([callBaseFee, contractFee, createBaseFee, creationFee, rentByteFee, rentDepositOffset, transactionBaseFee, transactionByteFee, transferFee, tombstoneDeposit]) => {
return {
+10 -2
View File
@@ -6,8 +6,8 @@ import ApiPromise from '@polkadot/api/promise/Api';
import { BlockNumber } from '@polkadot/types';
import { WsProvider } from '@polkadot/rpc-provider';
// const WS = 'ws://127.0.0.1:9944/';
const WS = 'wss://poc3-rpc.polkadot.io/';
const WS = 'ws://127.0.0.1:9944/';
// const WS = 'wss://poc3-rpc.polkadot.io/';
describe.skip('derive e2e', () => {
let api: ApiPromise;
@@ -39,4 +39,12 @@ describe.skip('derive e2e', () => {
}
});
});
it('retrieves the fees (api.queryMulti)', (done) => {
return api.derive.balances.fees((fees) => {
console.error('fees', JSON.stringify(fees));
done();
});
});
});