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
This commit is contained in:
Krzysztof Jelski
2020-12-08 13:25:30 +01:00
committed by GitHub
parent c29f66c7c5
commit beccc2ed9b
7 changed files with 177 additions and 1 deletions
@@ -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: <T extends Codec> (typeName: keyof InterfaceTypes) => Option<T>;
let optionOf: <T extends Codec> (value: T)=> Option<T>;
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('');
});
});
@@ -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<Bounty>[], Option<Bytes>[]]): 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<DeriveBounties> {
return memo(instanceId, (): Observable<DeriveBounties> => api.query.treasury.bountyCount().pipe(
switchMap(() => api.query.treasury.bounties.keys()),
switchMap((keys: StorageKey[]) => {
return combineLatest([
api.query.treasury.bounties.multi<Option<Bounty>>(extractIds(keys)),
api.query.treasury.bountyDescriptions.multi<Option<Bytes>>(extractIds(keys))
]
).pipe(map(parseResult));
})));
}
@@ -2,3 +2,4 @@
// SPDX-License-Identifier: Apache-2.0
export * from './proposals';
export * from './bounties';