Files
api/packages/api-derive/test/bountyFactory.ts
T
Krzysztof Jelski beccc2ed9b 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
2020-12-08 13:25:30 +01:00

32 lines
1.2 KiB
TypeScript

// 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 = <T extends Codec>(value: T): Option<T> => {
const typeName = this.#registry.getClassName(value.constructor as Constructor<T>);
return new Option<T>(this.#registry, typeName as keyof InterfaceTypes, value);
};
public emptyOption = <T extends Codec>(typeName: keyof InterfaceTypes): Option<T> => new Option<T>(this.#registry, typeName);
}