Add new staking types, support derives (#1828)

* Add new staking types

* StakingLedgerTo213 - StakingLedgerTo223

* StakingLedgerTo223 in polkadot (dev)

* Add derive activeEra

* Map Ponts -> RewardPoints

* StakingLedger override CHANGELOG

* Cleanups

* Apparent CC dupes (don't see it, but cleanup useul)

* Cleanup result formatting for indexes

* Map nextElected -> validators

* Fix StakingLedger to current

* Merge CHANGELOG

* Mention derive staking updates

* Mark derive changes as breaking
This commit is contained in:
Jaco Greeff
2020-02-24 16:52:48 +01:00
committed by GitHub
parent 64aa4dcc1c
commit cbc14cc291
14 changed files with 394 additions and 206 deletions
+21 -8
View File
@@ -30,7 +30,7 @@ import { ApiId, BlockHash, ChainProperties, ExtrinsicOrHash, ExtrinsicStatus, He
import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, ChangesTrieConfiguration, Consensus, ConsensusEngineId, Digest, DigestItem, DispatchClass, DispatchInfo, DispatchInfoTo190, Fixed64, H160, H256, H512, Hash, Header, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, LookupSource, LookupTarget, Moment, Origin, Perbill, Percent, Permill, Perquintill, Phantom, PhantomData, PreRuntime, Seal, SealV0, SignedBlock, StorageData, ValidatorId, Weight, WeightMultiplier } from '@polkadot/types/interfaces/runtime';
import { FullIdentification, IdentificationTuple, Keys, SessionIndex, SessionKeys1, SessionKeys2, SessionKeys3, SessionKeys4, SessionKeys5, SessionKeys6 } from '@polkadot/types/interfaces/session';
import { Bid, BidKind, BidKindVouch, SocietyJudgement, SocietyVote, StrikeCount, VouchingStatus } from '@polkadot/types/interfaces/society';
import { CompactAssignments, CompactScore, ElectionCompute, ElectionResult, ElectionStatus, EraIndex, EraPoints, EraRewards, Exposure, Forcing, IndividualExposure, KeyType, MomentOf, Nominations, PhragmenScore, Points, RewardDestination, SlashJournalEntry, SlashingSpans, SlashingSpansTo204, SpanIndex, SpanRecord, StakingLedger, UnappliedSlash, UnappliedSlashOther, UnlockChunk, ValidatorIndex, ValidatorPrefs, ValidatorPrefsTo145, ValidatorPrefsTo196 } from '@polkadot/types/interfaces/staking';
import { CompactAssignments, CompactScore, ElectionCompute, ElectionResult, ElectionStatus, EraIndex, EraPoints, EraRewardPoints, EraRewards, Exposure, Forcing, IndividualExposure, KeyType, MomentOf, Nominations, PhragmenScore, Points, RewardDestination, RewardPoint, SlashJournalEntry, SlashingSpans, SlashingSpansTo204, SpanIndex, SpanRecord, StakingLedger, StakingLedgerNew, StakingLedgerTo223, UnappliedSlash, UnappliedSlashOther, UnlockChunk, ValidatorIndex, ValidatorPrefs, ValidatorPrefsTo145, ValidatorPrefsTo196 } from '@polkadot/types/interfaces/staking';
import { AccountInfo, DigestOf, DispatchError, DispatchErrorModule, DispatchErrorTo198, DispatchResult, DispatchResultOf, DispatchResultTo198, Event, EventId, EventIndex, EventRecord, EventRecordTo76, Key, Phase, RefCount } from '@polkadot/types/interfaces/system';
import { OpenTip, OpenTipFinder, OpenTipTip, TreasuryProposal } from '@polkadot/types/interfaces/treasury';
import { Multiplier } from '@polkadot/types/interfaces/txpayment';
@@ -726,6 +726,13 @@ declare module '@polkadot/types/types/registry' {
VouchingStatus: VouchingStatus;
'Option<VouchingStatus>': Option<VouchingStatus>;
'Vec<VouchingStatus>': Vec<VouchingStatus>;
Points: Points;
'Compact<Points>': Compact<Points>;
'Option<Points>': Option<Points>;
'Vec<Points>': Vec<Points>;
EraPoints: EraPoints;
'Option<EraPoints>': Option<EraPoints>;
'Vec<EraPoints>': Vec<EraPoints>;
CompactAssignments: CompactAssignments;
'Option<CompactAssignments>': Option<CompactAssignments>;
'Vec<CompactAssignments>': Vec<CompactAssignments>;
@@ -745,9 +752,9 @@ declare module '@polkadot/types/types/registry' {
'Compact<EraIndex>': Compact<EraIndex>;
'Option<EraIndex>': Option<EraIndex>;
'Vec<EraIndex>': Vec<EraIndex>;
EraPoints: EraPoints;
'Option<EraPoints>': Option<EraPoints>;
'Vec<EraPoints>': Vec<EraPoints>;
EraRewardPoints: EraRewardPoints;
'Option<EraRewardPoints>': Option<EraRewardPoints>;
'Vec<EraRewardPoints>': Vec<EraRewardPoints>;
EraRewards: EraRewards;
'Option<EraRewards>': Option<EraRewards>;
'Vec<EraRewards>': Vec<EraRewards>;
@@ -772,13 +779,13 @@ declare module '@polkadot/types/types/registry' {
PhragmenScore: PhragmenScore;
'Option<PhragmenScore>': Option<PhragmenScore>;
'Vec<PhragmenScore>': Vec<PhragmenScore>;
Points: Points;
'Compact<Points>': Compact<Points>;
'Option<Points>': Option<Points>;
'Vec<Points>': Vec<Points>;
RewardDestination: RewardDestination;
'Option<RewardDestination>': Option<RewardDestination>;
'Vec<RewardDestination>': Vec<RewardDestination>;
RewardPoint: RewardPoint;
'Compact<RewardPoint>': Compact<RewardPoint>;
'Option<RewardPoint>': Option<RewardPoint>;
'Vec<RewardPoint>': Vec<RewardPoint>;
SlashJournalEntry: SlashJournalEntry;
'Option<SlashJournalEntry>': Option<SlashJournalEntry>;
'Vec<SlashJournalEntry>': Vec<SlashJournalEntry>;
@@ -795,6 +802,12 @@ declare module '@polkadot/types/types/registry' {
SpanRecord: SpanRecord;
'Option<SpanRecord>': Option<SpanRecord>;
'Vec<SpanRecord>': Vec<SpanRecord>;
StakingLedgerTo223: StakingLedgerTo223;
'Option<StakingLedgerTo223>': Option<StakingLedgerTo223>;
'Vec<StakingLedgerTo223>': Vec<StakingLedgerTo223>;
StakingLedgerNew: StakingLedgerNew;
'Option<StakingLedgerNew>': Option<StakingLedgerNew>;
'Vec<StakingLedgerNew>': Vec<StakingLedgerNew>;
StakingLedger: StakingLedger;
'Option<StakingLedger>': Option<StakingLedger>;
'Vec<StakingLedger>': Vec<StakingLedger>;
+71 -71
View File
@@ -12,92 +12,92 @@ import Compact from './Compact';
import Raw from './Raw';
import { compareMap, decodeU8a, typeToConstructor } from './utils';
/** @internal */
function decodeBTreeMapFromU8a<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, u8a: Uint8Array): Map<K, V> {
const output = new Map<K, V>();
const [offset, length] = Compact.decodeU8a(u8a);
const types = [];
for (let i = 0; i < length.toNumber(); i++) {
types.push(KeyClass, ValClass);
}
const values = decodeU8a(registry, u8a.subarray(offset), types);
for (let i = 0; i < values.length; i += 2) {
output.set(values[i] as K, values[i + 1] as V);
}
return output;
}
/** @internal */
function decodeBTreeMapFromMap<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, value: Map<any, any>): Map<K, V> {
const output = new Map<K, V>();
value.forEach((v: any, k: any) => {
let key, val;
try {
key = (k instanceof KeyClass) ? k : new KeyClass(registry, k);
val = (v instanceof ValClass) ? v : new ValClass(registry, v);
} catch (error) {
console.error('Failed to decode BTreeMap key or value:', error.message);
throw error;
}
output.set(key, val);
});
return output;
}
/**
* Decode input to pass into constructor.
*
* @param KeyClass - Type of the map key
* @param ValClass - Type of the map value
* @param value - Value to decode, one of:
* - null
* - undefined
* - hex
* - Uint8Array
* - Map<any, any>, where both key and value types are either
* constructors or decodeable values for their types.
* @param jsonMap
* @internal
*/
function decodeBTreeMap<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, value: Uint8Array | string | Map<any, any>): Map<K, V> {
if (!value) {
return new Map<K, V>();
} else if (isHex(value)) {
return decodeBTreeMap(registry, KeyClass, ValClass, hexToU8a(value));
} else if (isU8a(value)) {
return decodeBTreeMapFromU8a<K, V>(registry, KeyClass, ValClass, u8aToU8a(value));
} else if (value instanceof Map) {
return decodeBTreeMapFromMap<K, V>(registry, KeyClass, ValClass, value);
}
throw new Error('BTreeMap: cannot decode type');
}
export default class BTreeMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements Codec {
public readonly registry: Registry;
private readonly _KeyClass: Constructor<K>;
protected _KeyClass: Constructor<K>;
private readonly _ValClass: Constructor<V>;
protected _ValClass: Constructor<V>;
constructor (registry: Registry, keyType: Constructor<K> | keyof InterfaceTypes, valType: Constructor<V> | keyof InterfaceTypes, rawValue: any) {
const KeyClass = typeToConstructor(registry, keyType);
const ValClass = typeToConstructor(registry, valType);
super(BTreeMap.decodeBTreeMap(registry, KeyClass, ValClass, rawValue));
super(decodeBTreeMap(registry, KeyClass, ValClass, rawValue));
this.registry = registry;
this._KeyClass = KeyClass;
this._ValClass = ValClass;
}
/**
* Decode input to pass into constructor.
*
* @param KeyClass - Type of the map key
* @param ValClass - Type of the map value
* @param value - Value to decode, one of:
* - null
* - undefined
* - hex
* - Uint8Array
* - Map<any, any>, where both key and value types are either
* constructors or decodeable values for their types.
* @param jsonMap
* @internal
*/
private static decodeBTreeMap<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, value: Uint8Array | string | Map<any, any>): Map<K, V> {
if (!value) {
return new Map<K, V>();
} else if (isHex(value)) {
return BTreeMap.decodeBTreeMap(registry, KeyClass, ValClass, hexToU8a(value));
} else if (isU8a(value)) {
return BTreeMap.decodeBTreeMapFromU8a<K, V>(registry, KeyClass, ValClass, u8aToU8a(value));
} else if (value instanceof Map) {
return BTreeMap.decodeBTreeMapFromMap<K, V>(registry, KeyClass, ValClass, value);
}
throw new Error('BTreeMap: cannot decode type');
}
/** @internal */
private static decodeBTreeMapFromU8a<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, u8a: Uint8Array): Map<K, V> {
const output = new Map<K, V>();
const [offset, length] = Compact.decodeU8a(u8a);
const types = [];
for (let i = 0; i < length.toNumber(); i++) {
types.push(KeyClass, ValClass);
}
const values = decodeU8a(registry, u8a.subarray(offset), types);
for (let i = 0; i < values.length; i += 2) {
output.set(values[i] as K, values[i + 1] as V);
}
return output;
}
/** @internal */
private static decodeBTreeMapFromMap<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, value: Map<any, any>): Map<K, V> {
const output = new Map<K, V>();
value.forEach((v: any, k: any) => {
let key, val;
try {
key = (k instanceof KeyClass) ? k : new KeyClass(registry, k);
val = (v instanceof ValClass) ? v : new ValClass(registry, v);
} catch (error) {
console.error('Failed to decode BTreeMap key or value:', error.message);
throw error;
}
output.set(key, val);
});
return output;
}
public static with<K extends Codec, V extends Codec> (keyType: Constructor<K> | keyof InterfaceTypes, valType: Constructor<V> | keyof InterfaceTypes): Constructor<BTreeMap<K, V>> {
return class extends BTreeMap<K, V> {
constructor (registry: Registry, value?: any) {
@@ -2,8 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
const deprecated = {
Points: 'u32',
EraPoints: {
total: 'Points',
individual: 'Vec<Points>'
}
};
export default {
types: {
...deprecated,
CompactAssignments: {
votes1: 'Vec<(AccountId, [CompactScore; 0], AccountId)>',
votes2: 'Vec<(AccountId, [CompactScore; 1], AccountId)>',
@@ -39,9 +48,9 @@ export default {
}
},
EraIndex: 'u32',
EraPoints: {
total: 'Points',
individual: 'Vec<Points>'
EraRewardPoints: {
total: 'RewardPoint',
individual: 'BTreeMap<AccountId, RewardPoint>'
},
EraRewards: {
total: 'u32',
@@ -80,6 +89,7 @@ export default {
'Controller'
]
},
RewardPoint: 'u32',
SlashJournalEntry: {
who: 'AccountId',
amount: 'Balance',
@@ -101,12 +111,21 @@ export default {
slashed: 'Balance',
paidOut: 'Balance'
},
StakingLedger: {
StakingLedgerTo223: {
stash: 'AccountId',
total: 'Compact<Balance>',
active: 'Compact<Balance>',
unlocking: 'Vec<UnlockChunk>'
},
// TODO Enable as default when new staking payouts go live
StakingLedgerNew: {
stash: 'AccountId',
total: 'Compact<Balance>',
active: 'Compact<Balance>',
unlocking: 'Vec<UnlockChunk>',
nextReward: 'EraIndex'
},
StakingLedger: 'StakingLedgerTo223',
UnappliedSlashOther: '(AccountId, Balance)',
UnappliedSlash: {
validator: 'AccountId',
+23 -2
View File
@@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
import { ITuple } from '@polkadot/types/types';
import { Compact, Enum, Struct, Vec } from '@polkadot/types/codec';
import { BTreeMap, Compact, Enum, Struct, Vec } from '@polkadot/types/codec';
import { bool, u128, u16, u32 } from '@polkadot/types/primitive';
import { AccountId, Balance, BlockNumber, Moment, Perbill } from '@polkadot/types/interfaces/runtime';
@@ -60,6 +60,12 @@ export interface EraPoints extends Struct {
readonly individual: Vec<Points>;
}
/** @name EraRewardPoints */
export interface EraRewardPoints extends Struct {
readonly total: RewardPoint;
readonly individual: BTreeMap<AccountId, RewardPoint>;
}
/** @name EraRewards */
export interface EraRewards extends Struct {
readonly total: u32;
@@ -113,6 +119,9 @@ export interface RewardDestination extends Enum {
readonly isController: boolean;
}
/** @name RewardPoint */
export interface RewardPoint extends u32 {}
/** @name SlashingSpans */
export interface SlashingSpans extends Struct {
readonly spanIndex: SpanIndex;
@@ -145,7 +154,19 @@ export interface SpanRecord extends Struct {
}
/** @name StakingLedger */
export interface StakingLedger extends Struct {
export interface StakingLedger extends StakingLedgerTo223 {}
/** @name StakingLedgerNew */
export interface StakingLedgerNew extends Struct {
readonly stash: AccountId;
readonly total: Compact<Balance>;
readonly active: Compact<Balance>;
readonly unlocking: Vec<UnlockChunk>;
readonly nextReward: EraIndex;
}
/** @name StakingLedgerTo223 */
export interface StakingLedgerTo223 extends Struct {
readonly stash: AccountId;
readonly total: Compact<Balance>;
readonly active: Compact<Balance>;
+14 -7
View File
@@ -43,7 +43,8 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [
types: {
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5'
Keys: 'SessionKeys5',
StakingLedger: 'StakingLedgerTo223'
}
},
{
@@ -51,7 +52,8 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [
types: {
// Indices optional, not in transaction
Address: 'AccountId',
Keys: 'SessionKeys5'
Keys: 'SessionKeys5',
StakingLedger: 'StakingLedgerTo223'
}
}
];
@@ -64,7 +66,8 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
BalanceLock: 'BalanceLockTo212',
DispatchError: 'DispatchErrorTo198',
Keys: 'SessionKeys5',
SlashingSpans: 'SlashingSpansTo204'
SlashingSpans: 'SlashingSpansTo204',
StakingLedger: 'StakingLedgerTo223'
}
},
{
@@ -73,7 +76,8 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5',
SlashingSpans: 'SlashingSpansTo204'
SlashingSpans: 'SlashingSpansTo204',
StakingLedger: 'StakingLedgerTo223'
}
},
{
@@ -81,7 +85,8 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
types: {
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5'
Keys: 'SessionKeys5',
StakingLedger: 'StakingLedgerTo223'
}
},
{
@@ -89,7 +94,8 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
types: {
// Indices optional, not in transaction
Address: 'AccountId',
Keys: 'SessionKeys5'
Keys: 'SessionKeys5',
StakingLedger: 'StakingLedgerTo223'
}
}
];
@@ -110,7 +116,8 @@ const TYPES_META: OverrideVersionedType[] = [
BlockNumber: 'u64',
Index: 'u64',
EventRecord: 'EventRecordTo76',
ValidatorPrefs: 'ValidatorPrefsTo145'
ValidatorPrefs: 'ValidatorPrefsTo145',
StakingLedger: 'StakingLedgerTo223'
}
}
];