From beccc2ed9b6b269fd1a7ce11b8b9c84389be9d81 Mon Sep 17 00:00:00 2001 From: Krzysztof Jelski Date: Tue, 8 Dec 2020 13:25:30 +0100 Subject: [PATCH] Derive for bounties and descriptions (#2925) * WIP on bounties derive. * wip * Some basic tests for derive bounties * Simplify bounties derive and mocks * Get bounties and descriptions * Allow for empty bounties. Refactor tests. * Move tests to test directory * Attempt to remove duplication * Sort imports * Reorganize tests * Change returned type --- .../api-derive/src/treasury/bounties.spec.ts | 62 +++++++++++++++++++ packages/api-derive/src/treasury/bounties.ts | 42 +++++++++++++ packages/api-derive/src/treasury/index.ts | 1 + packages/api-derive/src/types.ts | 9 ++- packages/api-derive/test/bountyFactory.ts | 31 ++++++++++ packages/api-derive/test/bytesFactory.ts | 15 +++++ packages/api-derive/test/helpers.ts | 18 ++++++ 7 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 packages/api-derive/src/treasury/bounties.spec.ts create mode 100644 packages/api-derive/src/treasury/bounties.ts create mode 100644 packages/api-derive/test/bountyFactory.ts create mode 100644 packages/api-derive/test/bytesFactory.ts create mode 100644 packages/api-derive/test/helpers.ts diff --git a/packages/api-derive/src/treasury/bounties.spec.ts b/packages/api-derive/src/treasury/bounties.spec.ts new file mode 100644 index 0000000000..a06553dec4 --- /dev/null +++ b/packages/api-derive/src/treasury/bounties.spec.ts @@ -0,0 +1,62 @@ +// Copyright 2017-2020 @polkadot/api authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { ApiInterfaceRx } from '@polkadot/api/types'; +import type { Bytes, Option, StorageKey } from '@polkadot/types'; +import type { Bounty, BountyIndex } from '@polkadot/types/interfaces'; +import type { Codec, InterfaceTypes } from '@polkadot/types/types'; + +import { of } from 'rxjs'; + +import { bounties } from '@polkadot/api-derive/treasury/bounties'; + +import { BountyFactory } from '../../test/bountyFactory'; +import { BytesFactory } from '../../test/bytesFactory'; +import { createApiWithAugmentations } from '../../test/helpers'; + +const DEFAULT_PROPOSER = '5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM'; + +describe('bounties derive', () => { + let storageKey: (index: number) => StorageKey; + let defaultBounty: () => Bounty; + let emptyOption: (typeName: keyof InterfaceTypes) => Option; + let optionOf: (value: T)=> Option; + let bountyIndex: (index: number) => BountyIndex; + let bytes: (value: string) => Bytes; + + beforeAll(() => { + const api = createApiWithAugmentations(); + + ({ bountyIndex, defaultBounty, emptyOption, optionOf, storageKey } = new BountyFactory(api)); + ({ bytes } = new BytesFactory(api.registry)); + }); + + it('combines bounties with descriptions', async () => { + const mockApi = { + query: { + treasury: { + bounties: { + keys: () => of([storageKey(0), storageKey(1), storageKey(2)]), + multi: () => of([optionOf(defaultBounty()), emptyOption('Bounty'), optionOf(defaultBounty())]) + }, + bountyCount: () => of(bountyIndex(3)), + bountyDescriptions: { + multi: () => of([ + optionOf(bytes('make polkadot even better')), + optionOf(bytes('this will be totally ignored')), + emptyOption('Bytes') + ]) + } + } + } + } as unknown as ApiInterfaceRx; + + const result = await bounties('', mockApi)().toPromise(); + + expect(result).toHaveLength(2); + expect(result[0].bounty.proposer.toString()).toEqual(DEFAULT_PROPOSER); + expect(result[0].description).toEqual('make polkadot even better'); + expect(result[1].bounty.proposer.toString()).toEqual(DEFAULT_PROPOSER); + expect(result[1].description).toEqual(''); + }); +}); diff --git a/packages/api-derive/src/treasury/bounties.ts b/packages/api-derive/src/treasury/bounties.ts new file mode 100644 index 0000000000..9c44e306f7 --- /dev/null +++ b/packages/api-derive/src/treasury/bounties.ts @@ -0,0 +1,42 @@ +// Copyright 2017-2020 @polkadot/api authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Observable } from 'rxjs'; +import type { ApiInterfaceRx } from '@polkadot/api/types'; +import type { DeriveBounties } from '@polkadot/api-derive/types'; +import type { Bytes, Option, StorageKey } from '@polkadot/types'; +import type { Bounty } from '@polkadot/types/interfaces'; +import type { Codec } from '@polkadot/types/types'; + +import { combineLatest } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '@polkadot/api-derive/util'; + +function parseResult ([maybeBounties, maybeDescriptions]: [Option[], Option[]]): DeriveBounties { + const bounties: DeriveBounties = []; + + maybeBounties.forEach((bounty, index) => { + if (bounty.isSome) { + bounties.push({ bounty: bounty.unwrap(), description: maybeDescriptions[index].unwrapOrDefault().toUtf8() }); + } + }); + + return bounties; +} + +function extractIds (keys: StorageKey[]): Codec[] { + return keys.map(({ args: [id] }: StorageKey) => id); +} + +export function bounties (instanceId: string, api: ApiInterfaceRx): () => Observable { + return memo(instanceId, (): Observable => api.query.treasury.bountyCount().pipe( + switchMap(() => api.query.treasury.bounties.keys()), + switchMap((keys: StorageKey[]) => { + return combineLatest([ + api.query.treasury.bounties.multi>(extractIds(keys)), + api.query.treasury.bountyDescriptions.multi>(extractIds(keys)) + ] + ).pipe(map(parseResult)); + }))); +} diff --git a/packages/api-derive/src/treasury/index.ts b/packages/api-derive/src/treasury/index.ts index f970d924cc..d7bfd2a3d0 100644 --- a/packages/api-derive/src/treasury/index.ts +++ b/packages/api-derive/src/treasury/index.ts @@ -2,3 +2,4 @@ // SPDX-License-Identifier: Apache-2.0 export * from './proposals'; +export * from './bounties'; diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index b22ccc866b..36612b307d 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, 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, Hash, Index, Proposal, ProposalIndex, SetIndex, SocietyVote, StrikeCount, TreasuryProposal, Votes, VouchingStatus } from '@polkadot/types/interfaces'; export * from './accounts/types'; export * from './chain/types'; @@ -127,3 +127,10 @@ export interface VoterPosition { } export type DeriveVoterPositions = Record; + +export interface DeriveBounty { + bounty: Bounty; + description: string; +} + +export type DeriveBounties = DeriveBounty[]; diff --git a/packages/api-derive/test/bountyFactory.ts b/packages/api-derive/test/bountyFactory.ts new file mode 100644 index 0000000000..fd15982aa6 --- /dev/null +++ b/packages/api-derive/test/bountyFactory.ts @@ -0,0 +1,31 @@ +// Copyright 2017-2020 @polkadot/api authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { ApiPromise } from '@polkadot/api'; +import { Option, StorageKey } from '@polkadot/types'; +import { Bounty, BountyIndex } from '@polkadot/types/interfaces'; +import { Codec, Constructor, InterfaceTypes, Registry } from '@polkadot/types/types'; + +export class BountyFactory { + readonly #api: ApiPromise; + readonly #registry: Registry; + + constructor (api: ApiPromise) { + this.#api = api; + this.#registry = this.#api.registry; + } + + public storageKey = (index: number): StorageKey => new StorageKey(this.#registry, this.#api.query.treasury.bounties.key(this.bountyIndex(index))); + + public bountyIndex = (index: number): BountyIndex => this.#registry.createType('BountyIndex', index); + + public defaultBounty = (): Bounty => this.#registry.createType('Bounty'); + + public optionOf = (value: T): Option => { + const typeName = this.#registry.getClassName(value.constructor as Constructor); + + return new Option(this.#registry, typeName as keyof InterfaceTypes, value); + }; + + public emptyOption = (typeName: keyof InterfaceTypes): Option => new Option(this.#registry, typeName); +} diff --git a/packages/api-derive/test/bytesFactory.ts b/packages/api-derive/test/bytesFactory.ts new file mode 100644 index 0000000000..3bfabcb1d2 --- /dev/null +++ b/packages/api-derive/test/bytesFactory.ts @@ -0,0 +1,15 @@ +// Copyright 2017-2020 @polkadot/api authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { Bytes } from '@polkadot/types'; +import { Registry } from '@polkadot/types/types'; + +export class BytesFactory { + #registry: Registry; + + constructor (registry: Registry) { + this.#registry = registry; + } + + public bytes = (value: string): Bytes => this.#registry.createType('Bytes', value); +} diff --git a/packages/api-derive/test/helpers.ts b/packages/api-derive/test/helpers.ts new file mode 100644 index 0000000000..492f7fa447 --- /dev/null +++ b/packages/api-derive/test/helpers.ts @@ -0,0 +1,18 @@ +// Copyright 2017-2020 @polkadot/api authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { ApiPromise, WsProvider } from '@polkadot/api'; +import { Metadata } from '@polkadot/metadata'; +import metaStatic from '@polkadot/metadata/static'; +import { TypeRegistry } from '@polkadot/types'; + +export function createApiWithAugmentations (): ApiPromise { + const registry = new TypeRegistry(); + const metadata = new Metadata(registry, metaStatic); + + const api = new ApiPromise({ provider: new WsProvider('ws://', false) }); + + api.injectMetadata(metadata, true); + + return api; +}