From a98b6b9bf8fc484eefcb66257037ffd234d72516 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska <30404388+ekowalsk@users.noreply.github.com> Date: Tue, 29 Dec 2020 15:46:03 +0100 Subject: [PATCH] Add bounty index to DeriveBounty (#3000) * Add bounty index to DeriveBounty * Apply review suggestions --- .../api-derive/src/bounties/bounties.spec.ts | 8 +++++++- packages/api-derive/src/bounties/bounties.ts | 20 +++++++++++-------- packages/api-derive/src/types.ts | 3 ++- packages/api-derive/test/bountyFactory.ts | 7 +++++-- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/api-derive/src/bounties/bounties.spec.ts b/packages/api-derive/src/bounties/bounties.spec.ts index aa1d4d40ba..291981f782 100644 --- a/packages/api-derive/src/bounties/bounties.spec.ts +++ b/packages/api-derive/src/bounties/bounties.spec.ts @@ -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); }); }); diff --git a/packages/api-derive/src/bounties/bounties.ts b/packages/api-derive/src/bounties/bounties.ts index ae2f6d1920..5fc8a8c253 100644 --- a/packages/api-derive/src/bounties/bounties.ts +++ b/packages/api-derive/src/bounties/bounties.ts @@ -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[], Option[]]; +type Result = [Option[], Option[], 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().pipe( switchMap(() => bountyBase.bounties.keys()), switchMap((keys): Observable => { - const ids = keys.map(({ args: [id] }) => id); + const ids = keys.map(({ args: [id] }) => id as BountyIndex); return combineLatest([ bountyBase.bounties.multi>(ids), - bountyBase.bountyDescriptions.multi>(ids) + bountyBase.bountyDescriptions.multi>(ids), + of(ids) ]); }), - map(parseResult) + map(([maybeBounties, maybeDescriptions, ids]) => parseResult(api, [maybeBounties, maybeDescriptions, ids])) ) ); } diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 36612b307d..b72de1c50f 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -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; export interface DeriveBounty { bounty: Bounty; description: string; + index: BountyIndex; } export type DeriveBounties = DeriveBounty[]; diff --git a/packages/api-derive/test/bountyFactory.ts b/packages/api-derive/test/bountyFactory.ts index 04b1b885d3..e0bbee8c75 100644 --- a/packages/api-derive/test/bountyFactory.ts +++ b/packages/api-derive/test/bountyFactory.ts @@ -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);