-
Notifications
You must be signed in to change notification settings - Fork 16
/
bn.ts
33 lines (28 loc) · 1.26 KB
/
bn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2017-2018 @polkadot/client-db-chain authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import BN from 'bn.js';
import { SectionItem } from '@polkadot/params/types';
import { Storage$Key$Value } from '@polkadot/storage/types';
import { BaseDb } from '@polkadot/client-db/types';
import { StorageMethod$Bn } from '../types';
import bnToU8a from '@polkadot/util/bn/toU8a';
import u8aToBn from '@polkadot/util/u8a/toBn';
import createBase from './base';
import creator from '../key';
export default function decodeBn <T> (db: BaseDb, key: SectionItem<T>, bitLength: 32 | 64 | 128): StorageMethod$Bn {
const createKey = creator(key);
const base = createBase<BN | number>(db);
return {
del: (...keyParams: Array<Storage$Key$Value>): void =>
base.del(createKey(keyParams)),
get: (...keyParams: Array<Storage$Key$Value>): BN =>
u8aToBn(
base.get(createKey(keyParams)), true
),
set: (value: BN | number, ...keyParams: Array<Storage$Key$Value>): void =>
base.set(createKey(keyParams), value, bnToU8a(value, bitLength, true)),
onUpdate: (updater: (value: BN | number, raw: Uint8Array) => void): void =>
base.onUpdate(updater)
};
}