Add bounty index to DeriveBounty (#3000)

* Add bounty index to DeriveBounty

* Apply review suggestions
This commit is contained in:
Ewa Kowalska
2020-12-29 15:46:03 +01:00
committed by GitHub
parent fc0148fd41
commit a98b6b9bf8
4 changed files with 26 additions and 12 deletions
@@ -30,12 +30,16 @@ describe('bounties derive', () => {
({ bytes } = new BytesFactory(api.registry));
});
it('creates storage key', function () {
expect(storageKey(194).args[0].eq(194)).toBe(true);
});
it('combines bounties with descriptions', async () => {
const mockApi = {
query: {
treasury: {
bounties: {
keys: () => of([storageKey(0), storageKey(1), storageKey(2)]),
keys: () => of([storageKey(0), storageKey(2), storageKey(3)]),
multi: () => of([optionOf(defaultBounty()), emptyOption('Bounty'), optionOf(defaultBounty())])
},
bountyCount: () => of(bountyIndex(3)),
@@ -55,7 +59,9 @@ describe('bounties derive', () => {
expect(result).toHaveLength(2);
expect(result[0].bounty.proposer.toString()).toEqual(DEFAULT_PROPOSER);
expect(result[0].description).toEqual('make polkadot even better');
expect(result[0].index.eq(0)).toBe(true);
expect(result[1].bounty.proposer.toString()).toEqual(DEFAULT_PROPOSER);
expect(result[1].description).toEqual('');
expect(result[1].index.eq(3)).toBe(true);
});
});
+12 -8
View File
@@ -5,20 +5,23 @@ import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { DeriveBounties } from '@polkadot/api-derive/types';
import type { Bytes, Option } from '@polkadot/types';
import type { Bounty, BountyIndex } from '@polkadot/types/interfaces';
import type { Observable } from '@polkadot/x-rxjs';
import { memo } from '@polkadot/api-derive/util';
import { combineLatest } from '@polkadot/x-rxjs';
import { combineLatest, Observable, of } from '@polkadot/x-rxjs';
import { map, switchMap } from '@polkadot/x-rxjs/operators';
type Result = [Option<Bounty>[], Option<Bytes>[]];
type Result = [Option<Bounty>[], Option<Bytes>[], BountyIndex[]];
function parseResult ([maybeBounties, maybeDescriptions]: Result): DeriveBounties {
function parseResult (api: ApiInterfaceRx, [maybeBounties, maybeDescriptions, ids]: Result): DeriveBounties {
const bounties: DeriveBounties = [];
maybeBounties.forEach((bounty, index) => {
if (bounty.isSome) {
bounties.push({ bounty: bounty.unwrap(), description: maybeDescriptions[index].unwrapOrDefault().toUtf8() });
bounties.push({
bounty: bounty.unwrap(),
description: maybeDescriptions[index].unwrapOrDefault().toUtf8(),
index: ids[index]
});
}
});
@@ -34,14 +37,15 @@ export function bounties (instanceId: string, api: ApiInterfaceRx): () => Observ
bountyBase.bountyCount<BountyIndex>().pipe(
switchMap(() => bountyBase.bounties.keys()),
switchMap((keys): Observable<Result> => {
const ids = keys.map(({ args: [id] }) => id);
const ids = keys.map(({ args: [id] }) => id as BountyIndex);
return combineLatest([
bountyBase.bounties.multi<Option<Bounty>>(ids),
bountyBase.bountyDescriptions.multi<Option<Bytes>>(ids)
bountyBase.bountyDescriptions.multi<Option<Bytes>>(ids),
of(ids)
]);
}),
map(parseResult)
map(([maybeBounties, maybeDescriptions, ids]) => parseResult(api, [maybeBounties, maybeDescriptions, ids]))
)
);
}
+2 -1
View File
@@ -3,7 +3,7 @@
import type BN from 'bn.js';
import type { u32 } from '@polkadot/types';
import type { AccountId, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, Bounty, Hash, Index, Proposal, ProposalIndex, SetIndex, SocietyVote, StrikeCount, TreasuryProposal, Votes, VouchingStatus } from '@polkadot/types/interfaces';
import type { AccountId, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, Bounty, BountyIndex, Hash, Index, Proposal, ProposalIndex, SetIndex, SocietyVote, StrikeCount, TreasuryProposal, Votes, VouchingStatus } from '@polkadot/types/interfaces';
export * from './accounts/types';
export * from './chain/types';
@@ -131,6 +131,7 @@ export type DeriveVoterPositions = Record<string, VoterPosition>;
export interface DeriveBounty {
bounty: Bounty;
description: string;
index: BountyIndex;
}
export type DeriveBounties = DeriveBounty[];
+5 -2
View File
@@ -15,8 +15,11 @@ export class BountyFactory {
this.#registry = this.#api.registry;
}
public storageKey = (index: number): StorageKey =>
new StorageKey(this.#registry, this.#api.query.bounties.bounties.key(this.bountyIndex(index)));
public storageKey = (index: number): StorageKey => {
const key = new StorageKey(this.#registry, this.#api.query.bounties.bounties.key(this.bountyIndex(index)));
return key.setMeta(this.#api.query.bounties.bounties.creator.meta);
};
public bountyIndex = (index: number): BountyIndex =>
this.#registry.createType('BountyIndex', index);