Skip to content

Commit ee1d98f

Browse files
authored
fix: unappliedSlashesAtActiveEra in staking/progress (#1868)
* fix: unappliedSlashesAtActiveEra in staking/progress * add check for slash empty & updated tests
1 parent 53a7dbb commit ee1d98f

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

src/services/pallets/PalletsStakingProgressService.spec.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const validatorsAt = () =>
6060
const forceEraAt = () => Promise.resolve().then(() => polkadotRegistry.createType('Forcing', 'NotForcing'));
6161

6262
const unappliedSlashesEntries = () => {
63-
return Promise.resolve([['5640', []]]);
63+
return Promise.resolve([['5640', {}]]);
6464
};
6565

6666
const validatorCountAt = () => Promise.resolve().then(() => polkadotRegistry.createType('u32', 197));
@@ -122,24 +122,22 @@ const mockApi = {
122122
at: (_hash: Hash) => mockHistoricApi,
123123
} as unknown as ApiPromise;
124124

125-
const unappliedSlashes = [
126-
{
127-
validator: '5CD2Q2EnKaKvjWza3ufMxaXizBTTDgm9kPB3DCZ4VA9j7Ud6',
128-
own: '0',
129-
others: [['5GxDBrTuFgCAN49xrpRFWJiA969R2Ny5NnTa8cSPBh8hWHY9', '6902377436592']],
130-
reporters: [],
131-
payout: '345118871829',
132-
toJSON: function () {
133-
return {
134-
validator: this.validator,
135-
own: this.own,
136-
others: this.others.map(([account, amount]) => ({ account, amount })),
137-
reporters: this.reporters,
138-
payout: this.payout,
139-
};
140-
},
125+
const unappliedSlashes = {
126+
validator: '5CD2Q2EnKaKvjWza3ufMxaXizBTTDgm9kPB3DCZ4VA9j7Ud6',
127+
own: '0',
128+
others: [['5GxDBrTuFgCAN49xrpRFWJiA969R2Ny5NnTa8cSPBh8hWHY9', '6902377436592']],
129+
reporters: [],
130+
payout: '345118871829',
131+
toJSON: function () {
132+
return {
133+
validator: this.validator,
134+
own: this.own,
135+
others: this.others.map(([account, amount]) => ({ account, amount })),
136+
reporters: this.reporters,
137+
payout: this.payout,
138+
};
141139
},
142-
];
140+
};
143141
const unappliedSlashesEntriesUnappliedSlashes = () => {
144142
return Promise.resolve([['5640', unappliedSlashes]]);
145143
};
@@ -477,6 +475,7 @@ describe('PalletStakingProgressService', () => {
477475
const palletStakingProgressServiceUnappliedSlashes = new PalletsStakingProgressService('polkadot');
478476

479477
jest.spyOn(ApiPromiseRegistry, 'getApi').mockImplementation(() => mockApiUnappliedSlashes);
478+
480479
expect(
481480
sanitizeNumbers(
482481
await palletStakingProgressServiceUnappliedSlashes.derivePalletStakingProgress(blockHash789629),

src/services/pallets/PalletsStakingProgressService.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,12 @@ export class PalletsStakingProgressService extends AbstractService {
118118
activeEra: activeEra.toString(10),
119119
forceEra: forceEra.toJSON(),
120120
nextSessionEstimate: nextSession.toString(10),
121-
unappliedSlashes: unappliedSlashesAtActiveEra.toString()
122-
? unappliedSlashesAtActiveEra[0][1].map((slash) => slash.toJSON())
123-
: ([] as AnyJson[]),
121+
unappliedSlashes:
122+
Array.isArray(unappliedSlashesAtActiveEra) && unappliedSlashesAtActiveEra.length > 0
123+
? unappliedSlashesAtActiveEra.map(([_key, slash]) =>
124+
slash && Object.keys(slash).length > 0 ? slash.toJSON() : {},
125+
)
126+
: ([] as AnyJson[]),
124127
validatorSet: validators && validators.length ? validators.map((accountId) => accountId.toString()) : [],
125128
};
126129

src/services/test-helpers/responses/pallets/stakingProgress789629.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"activeEra": "49",
77
"forceEra": "NotForcing",
88
"nextSessionEstimate": "791868",
9-
"unappliedSlashes": [],
9+
"unappliedSlashes": [{}],
1010
"nextActiveEraEstimate": "803868",
1111
"electionStatus": {
1212
"status": {

src/services/test-helpers/responses/pallets/stakingProgressPostAhm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"activeEra": "49",
77
"forceEra": "NotForcing",
88
"nextSessionEstimate": "789999",
9-
"unappliedSlashes": [],
9+
"unappliedSlashes": [{}],
1010
"nextActiveEraEstimate": "-20369001",
1111
"electionStatus": {
1212
"status": {

0 commit comments

Comments
 (0)