Skip to content

Commit

Permalink
chore: compress Subscan failure errors (#2323)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbulat authored Nov 8, 2024
1 parent c400c9d commit 34d2be6
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 160 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"@fortawesome/react-fontawesome": "^0.2.2",
"@ledgerhq/hw-transport-webhid": "^6.29.2",
"@polkadot-api/merkleize-metadata": "^1.1.4",
"@polkadot/api": "^14.0.1",
"@polkadot/rpc-provider": "^14.0.1",
"@polkadot/api": "^14.2.3",
"@polkadot/rpc-provider": "^14.2.3",
"@polkawatch/ddp-client": "^2.0.20",
"@substrate/connect": "^1.1.0",
"@w3ux/extension-assets": "^0.4.0",
Expand Down
139 changes: 77 additions & 62 deletions src/controllers/Subscan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,33 @@ export class SubscanController {
// ------------------------------------------------------

// Handle fetching the various types of payout and set state in one render.
static handleFetchPayouts = async (address: string) => {
if (!this.payoutData[address]) {
const results = await Promise.all([
this.fetchNominatorPayouts(address),
this.fetchPoolClaims(address),
]);
const { payouts, unclaimedPayouts } = results[0];
const poolClaims = results[1];

// Persist results to class.
this.payoutData[address] = {
payouts,
unclaimedPayouts,
poolClaims,
};

document.dispatchEvent(
new CustomEvent('subscan-data-updated', {
detail: {
keys: ['payouts', 'unclaimedPayouts', 'poolClaims'],
},
})
);
static handleFetchPayouts = async (address: string): Promise<void> => {
try {
if (!this.payoutData[address]) {
const results = await Promise.all([
this.fetchNominatorPayouts(address),
this.fetchPoolClaims(address),
]);
const { payouts, unclaimedPayouts } = results[0];
const poolClaims = results[1];

// Persist results to class.
this.payoutData[address] = {
payouts,
unclaimedPayouts,
poolClaims,
};

document.dispatchEvent(
new CustomEvent('subscan-data-updated', {
detail: {
keys: ['payouts', 'unclaimedPayouts', 'poolClaims'],
},
})
);
}
} catch (e) {
// Silently fail request.
}
};

Expand All @@ -99,55 +103,66 @@ export class SubscanController {
payouts: SubscanPayout[];
unclaimedPayouts: SubscanPayout[];
}> => {
const result = await this.makeRequest(this.ENDPOINTS.rewardSlash, {
address,
is_stash: true,
row: 100,
page: 0,
});
try {
const result = await this.makeRequest(this.ENDPOINTS.rewardSlash, {
address,
is_stash: true,
row: 100,
page: 0,
});

const payouts =
result?.list?.filter(
({ block_timestamp }: SubscanPayout) => block_timestamp !== 0
) || [];

let unclaimedPayouts =
result?.list?.filter((l: SubscanPayout) => l.block_timestamp === 0) || [];

// Further filter unclaimed payouts to ensure that payout records of `stash` and
// `validator_stash` are not repeated for an era. NOTE: This was introduced to remove errornous
// data where there were duplicated payout records (with different amounts) for a stash -
// validator - era record. from Subscan.
unclaimedPayouts = unclaimedPayouts.filter(
(u: SubscanPayout) =>
!payouts.find(
(p: SubscanPayout) =>
p.stash === u.stash &&
p.validator_stash === u.validator_stash &&
p.era === u.era
)
);
const payouts =
result?.list?.filter(
({ block_timestamp }: SubscanPayout) => block_timestamp !== 0
) || [];

let unclaimedPayouts =
result?.list?.filter((l: SubscanPayout) => l.block_timestamp === 0) ||
[];

// Further filter unclaimed payouts to ensure that payout records of `stash` and
// `validator_stash` are not repeated for an era. NOTE: This was introduced to remove errornous
// data where there were duplicated payout records (with different amounts) for a stash -
// validator - era record. from Subscan.
unclaimedPayouts = unclaimedPayouts.filter(
(u: SubscanPayout) =>
!payouts.find(
(p: SubscanPayout) =>
p.stash === u.stash &&
p.validator_stash === u.validator_stash &&
p.era === u.era
)
);

return { payouts, unclaimedPayouts };
return { payouts, unclaimedPayouts };
} catch (e) {
// Silently fail request and return empty records.
return { payouts: [], unclaimedPayouts: [] };
}
};

// Fetch pool claims from Subscan, ensuring no payouts have block_timestamp of 0.
static fetchPoolClaims = async (
address: string
): Promise<SubscanPoolClaim[]> => {
const result = await this.makeRequest(this.ENDPOINTS.poolRewards, {
address,
row: 100,
page: 0,
});
if (!result?.list) {
try {
const result = await this.makeRequest(this.ENDPOINTS.poolRewards, {
address,
row: 100,
page: 0,
});
if (!result?.list) {
return [];
}
// Remove claims with a `block_timestamp`.
const poolClaims = result.list.filter(
(l: SubscanPoolClaim) => l.block_timestamp !== 0
);
return poolClaims;
} catch (e) {
// Silently fail request and return empty record.
return [];
}
// Remove claims with a `block_timestamp`.
const poolClaims = result.list.filter(
(l: SubscanPoolClaim) => l.block_timestamp !== 0
);
return poolClaims;
};

// Fetch a page of pool members from Subscan.
Expand Down
Loading

0 comments on commit 34d2be6

Please sign in to comment.