Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate payouts in staking-payouts endpoint #1439

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/services/accounts/AccountsStakingPayoutsService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ describe('AccountsStakingPayoutsService', () => {
it('Should work when the address is a nominator', () => {
const nom = '15j4dg5GzsL1bw2U2AWgeyAk6QTxq43V7ZPbXdAmbVLjvDCK';
const val = '16hzCDgyqnm1tskDccVWqxDVXYDLgdrrpC4Guxu3gPgLe5ib';
const res = stakingPayoutsService['extractExposure'](nom, val, deriveEraExposureParam);
const res = stakingPayoutsService['extractExposure'](nom, val, deriveEraExposureParam, 0);
expect(sanitizeNumbers(res)).toStrictEqual({
nominatorExposure: '21133134966048676',
totalExposure: '21133134966048676',
});
});
it('Should work when the address is a validator', () => {
const val = '16hzCDgyqnm1tskDccVWqxDVXYDLgdrrpC4Guxu3gPgLe5ib';
const res = stakingPayoutsService['extractExposure'](val, val, deriveEraExposureParam);
const res = stakingPayoutsService['extractExposure'](val, val, deriveEraExposureParam, 0);
expect(sanitizeNumbers(res)).toStrictEqual({
nominatorExposure: '0',
totalExposure: '21133134966048676',
Expand Down
67 changes: 56 additions & 11 deletions src/services/accounts/AccountsStakingPayoutsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ interface IEraData {
erasValidatorRewardOption: Option<BalanceOf>;
exposuresWithCommission?: (ICommissionAndLedger & {
validatorId: string;
nominatorIndex: number;
})[];
eraIndex: EraIndex;
}
Expand Down Expand Up @@ -204,13 +205,32 @@ export class AccountsStakingPayoutsService extends AbstractService {
const nominatedExposures = this.deriveNominatedExposures(address, deriveEraExposure);

// Zip the `validatorId` with its associated `commission`, making the data easier to reason
// about downstream
const exposuresWithCommission = nominatedExposures?.map(({ validatorId }, idx) => {
return {
validatorId,
...eraCommissions[idx],
};
});
// about downstream. In this object we added the `nominatorIndex` to account for the rare cases
// where a nominator has multiple nominations (with different stakes) on the same validator and
// at the same era.
const exposuresWithCommission = [];
if (nominatedExposures) {
for (let idx = 0; idx < nominatedExposures.length; idx++) {
let index = 0;
const { validatorId } = nominatedExposures[idx];
const nominatorInstances = nominatedExposures.filter(
(exposure) => exposure.validatorId.toString() === validatorId,
).length;
const exposuresValidatorLen = exposuresWithCommission.filter(
(exposure) => exposure.validatorId.toString() === validatorId,
).length;
if (nominatorInstances > 1) {
index = exposuresValidatorLen;
}
if (eraCommissions[idx]) {
exposuresWithCommission.push({
validatorId,
...eraCommissions[idx],
nominatorIndex: index,
});
}
}
}

return {
deriveEraExposure,
Expand Down Expand Up @@ -382,7 +402,12 @@ export class AccountsStakingPayoutsService extends AbstractService {

// Iterate through validators that this nominator backs and calculate payouts for the era
const payouts: IPayout[] = [];
for (const { validatorId, commission: validatorCommission, validatorLedger } of exposuresWithCommission) {
for (const {
validatorId,
commission: validatorCommission,
validatorLedger,
nominatorIndex,
} of exposuresWithCommission) {
const totalValidatorRewardPoints = deriveEraExposure.validatorIndex
? this.extractTotalValidatorRewardPoints(eraRewardPoints, validatorId, deriveEraExposure.validatorIndex)
: this.extractTotalValidatorRewardPoints(eraRewardPoints, validatorId);
Expand All @@ -391,7 +416,12 @@ export class AccountsStakingPayoutsService extends AbstractService {
continue;
}

const { totalExposure, nominatorExposure } = this.extractExposure(address, validatorId, deriveEraExposure);
const { totalExposure, nominatorExposure } = this.extractExposure(
address,
validatorId,
deriveEraExposure,
nominatorIndex,
);

if (nominatorExposure === undefined) {
// This should not happen once at this point, but here for safety
Expand Down Expand Up @@ -656,7 +686,12 @@ export class AccountsStakingPayoutsService extends AbstractService {
* @param validatorId accountId of a validator's _Stash_ account
* @param deriveEraExposure result of deriveEraExposure
*/
private extractExposure(address: string, validatorId: string, deriveEraExposure: IAdjustedDeriveEraExposure) {
private extractExposure(
address: string,
validatorId: string,
deriveEraExposure: IAdjustedDeriveEraExposure,
nominatorIndex: number,
) {
// Get total stake behind validator
let totalExposure = {} as Compact<u128>;
if (deriveEraExposure.validators[validatorId].total) {
Expand Down Expand Up @@ -688,7 +723,17 @@ export class AccountsStakingPayoutsService extends AbstractService {
? deriveEraExposure.validatorsOverview[address].unwrap().own
: ({} as unknown as Compact<u128>);
} else {
nominatorExposure = exposureAllNominators.find((exposure) => exposure.who.toString() === address)?.value;
// We need to account for the rare cases where a nominator has multiple nominations (with different stakes)
// on the same validator and at the same era.
const nominatorInstancesLen = exposureAllNominators.filter(
(exposure) => exposure.who.toString() === address,
).length;
const nominatorInstances = exposureAllNominators.filter((exposure) => exposure.who.toString() === address);
if (nominatorInstancesLen > 1) {
nominatorExposure = nominatorInstances[nominatorIndex].value;
} else {
nominatorExposure = exposureAllNominators.find((exposure) => exposure.who.toString() === address)?.value;
}
}
return {
totalExposure,
Expand Down
Loading