Create AccountIndex from number (#304)
This commit is contained in:
@@ -29,8 +29,8 @@ export default class ApiCombined extends ApiCalls {
|
||||
: new BN(0);
|
||||
|
||||
return this.combine(
|
||||
Array(lastIndex.toNumber()).map((_, i): Observable<Array<AccountId> | undefined> =>
|
||||
this.getAccountEnumSet(i)
|
||||
Array(lastIndex.div(ENUMSET_SIZE).toNumber()).map((_, i): Observable<Array<AccountId> | undefined> =>
|
||||
this.getAccountEnumSet(i * ENUMSET_SIZE.toNumber())
|
||||
),
|
||||
(...all: Array<Array<AccountId> | undefined>) => {
|
||||
return all.reduce((result, list = [], outerIndex: number) => {
|
||||
|
||||
@@ -9,9 +9,7 @@ import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import storage from '@polkadot/storage/static';
|
||||
import { AccountId, AccountIndex, Balance, bool as Bool, BlockNumber, Index, Moment, Perbill, PropIndex, ReferendumIndex, u32 } from '@polkadot/types/index';
|
||||
import { ENUMSET_SIZE } from '@polkadot/types/AccountIndex';
|
||||
import { Tuple, U8a, Vector } from '@polkadot/types/codec';
|
||||
import bnToU8a from '@polkadot/util/bn/toU8a';
|
||||
import { Tuple, Vector } from '@polkadot/types/codec';
|
||||
|
||||
import ApiBase from './Base';
|
||||
import { RxProposal, RxProposalDeposits, RxReferendum } from './classes';
|
||||
@@ -48,12 +46,8 @@ export default class ApiQueries extends ApiBase {
|
||||
return this.rawStorage(storage.democracy.nextTally);
|
||||
}
|
||||
|
||||
getAccountEnumSet = (_index: AccountIndex | BN | number): Observable<Array<AccountId> | undefined> => {
|
||||
const index = _index instanceof AccountIndex
|
||||
? _index.toBn().div(ENUMSET_SIZE)
|
||||
: _index;
|
||||
|
||||
return this.rawStorage(storage.balances.enumSet, new U8a(bnToU8a(index, 32, true)));
|
||||
getAccountEnumSet = (index: AccountIndex | BN | number): Observable<Array<AccountId> | undefined> => {
|
||||
return this.rawStorage(storage.balances.enumSet, index);
|
||||
}
|
||||
|
||||
nextAccountEnumSet = (): Observable<AccountIndex | undefined> => {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2018 @polkadot/types 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 AccountIndex from './AccountIndex';
|
||||
|
||||
describe('AccountIndex', () => {
|
||||
it('creates a BN representation', () => {
|
||||
expect(
|
||||
new AccountIndex([0xfd, 17, 18, 19, 20]).toBn().toString()
|
||||
).toEqual('336794129');
|
||||
});
|
||||
|
||||
it('constructs 2-byte from number', () => {
|
||||
expect(
|
||||
new AccountIndex(256 * 1).toString()
|
||||
).toEqual('25GUyk');
|
||||
});
|
||||
|
||||
it('constructs from number', () => {
|
||||
expect(
|
||||
new AccountIndex(new BN(336794129)).toString()
|
||||
).toEqual('3N5RJXxJhgRhxu');
|
||||
});
|
||||
});
|
||||
@@ -14,6 +14,9 @@ import { AnyU8a } from './types';
|
||||
import U8a from './codec/U8a';
|
||||
import u8aToBn from '@polkadot/util/u8a/toBn';
|
||||
import bnToU8a from '@polkadot/util/bn/toU8a';
|
||||
import isNumber from '@polkadot/util/is/number';
|
||||
import isBn from '@polkadot/util/is/bn';
|
||||
import bnToBn from '@polkadot/util/bn/toBn';
|
||||
|
||||
export const ENUMSET_SIZE = new BN(64);
|
||||
|
||||
@@ -21,13 +24,13 @@ export const ENUMSET_SIZE = new BN(64);
|
||||
// for an Account. We extends from U8a which is basically
|
||||
// just a Uint8Array wrapper.
|
||||
export default class AccountIndex extends U8a {
|
||||
constructor (value: AnyU8a = new Uint8Array()) {
|
||||
constructor (value: BN | number | AnyU8a = new Uint8Array()) {
|
||||
super(
|
||||
AccountIndex.decodeAccountIndex(value)
|
||||
);
|
||||
}
|
||||
|
||||
static decodeAccountIndex (value: AnyU8a): Uint8Array {
|
||||
static decodeAccountIndex (value: BN | number | AnyU8a): Uint8Array {
|
||||
if (value instanceof U8a) {
|
||||
return value.raw;
|
||||
} else if (Array.isArray(value)) {
|
||||
@@ -40,6 +43,21 @@ export default class AccountIndex extends U8a {
|
||||
const [offset, length] = AccountIndex.readLength(value);
|
||||
|
||||
return value.subarray(offset, offset + length);
|
||||
} else if (isNumber(value) || isBn(value)) {
|
||||
const index = bnToBn(value);
|
||||
const bitLength = index.ltn(1 << 8)
|
||||
? 8
|
||||
: (
|
||||
index.ltn(1 << 16)
|
||||
? 16
|
||||
: (
|
||||
index.ltn(1 << 32)
|
||||
? 32
|
||||
: 64
|
||||
)
|
||||
);
|
||||
|
||||
return bnToU8a(index, bitLength, true);
|
||||
} else if (isHex(value)) {
|
||||
return hexToU8a(value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user