Skip to content

Commit

Permalink
agent: gracefully handle exponential rebates transition
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Aug 2, 2023
1 parent f98074a commit 63e5c93
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions packages/indexer-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ import isEqual from 'lodash.isequal'
import mapValues from 'lodash.mapvalues'
import zip from 'lodash.zip'

// Temporary marker used to signal that the following contract calls have been deprecated in the
// network it appears:
// - channelDisputeEpochs
// - claimRebateRewards
// Once Exponential Rebates have been deployed to all networks, these calls should be removed from
// the code.
const EXPONENTIAL_REBATES_MARKER = -1

type ActionReconciliationContext = [AllocationDecision[], number, number]

const deploymentInList = (
Expand Down Expand Up @@ -257,15 +265,24 @@ export class Agent {

const channelDisputeEpochs: Eventual<NetworkMapped<number>> = timer(
600_000,
).tryMap(
() =>
this.multiNetworks.map(({ network }) =>
network.contracts.staking.channelDisputeEpochs(),
),
{
onError: error =>
this.logger.warn(`Failed to fetch channel dispute epochs`, { error }),
},
).map(() =>
this.multiNetworks.map(async ({ network }) => {
try {
return network.contracts.staking.channelDisputeEpochs()
} catch (error) {
// Disregards `channelDisputeEpochs` value from this point forward.
// TODO: Investigate error to confirm it comes from a reverted call.
this.logger.warn(
'Failed to fetch channel dispute epochs. ' +
'Ignoring claimable allocations for this reconciliation cycle.',
{
error,
protocolNetwork: network.specification.networkIdentifier,
},
)
return EXPONENTIAL_REBATES_MARKER
}
}),
)

const maxAllocationEpochs: Eventual<NetworkMapped<number>> = timer(
Expand Down Expand Up @@ -601,22 +618,22 @@ export class Agent {
currentEpochNumber,
channelDisputeEpochs,
}).tryMap(
async ({ currentEpochNumber, channelDisputeEpochs }) => {
const zipped = this.multiNetworks.zip(
currentEpochNumber,
channelDisputeEpochs,
)

const mapper = async (
{ network }: NetworkAndOperator,
[currentEpochNumber, channelDisputeEpochs]: [number, number],
): Promise<Allocation[]> =>
network.networkMonitor.claimableAllocations(
currentEpochNumber - channelDisputeEpochs,
)

return this.multiNetworks.mapNetworkMapped(zipped, mapper)
},
async ({ currentEpochNumber, channelDisputeEpochs }) =>
this.multiNetworks.mapNetworkMapped(
this.multiNetworks.zip(currentEpochNumber, channelDisputeEpochs),
async (
{ network }: NetworkAndOperator,
[currentEpochNumber, channelDisputeEpochs]: [number, number],
): Promise<Allocation[]> => {
if (channelDisputeEpochs === EXPONENTIAL_REBATES_MARKER) {
return [] // Ignore claimable allocations in Exponential Rebates context
} else {
return network.networkMonitor.claimableAllocations(
currentEpochNumber - channelDisputeEpochs,
)
}
},
),

{
onError: () =>
Expand Down Expand Up @@ -679,8 +696,24 @@ export class Agent {
// Claim rebate pool rewards from finalized allocations
await this.multiNetworks.mapNetworkMapped(
claimableAllocations,
({ network }: NetworkAndOperator, allocations: Allocation[]) =>
network.claimRebateRewards(allocations),
async (
{ network }: NetworkAndOperator,
allocations: Allocation[],
) => {
const protocolNetwork = network.specification.networkIdentifier
if (allocations.length) {
this.logger.debug(
`Claiming rebate rewards for ${allocations.length} allocations`,
{ allocations, protocolNetwork },
)
return network.claimRebateRewards(allocations)
} else {
this.logger.debug(
'Found no allocations to claim rebate rewards for',
{ protocolNetwork },
)
}
},
)

try {
Expand Down

0 comments on commit 63e5c93

Please sign in to comment.