Skip to content

Commit

Permalink
refactor: Consolidate relayer fillRelay method
Browse files Browse the repository at this point in the history
No change in behaviour, but a but less code duplication between the
normal and sped-up fillRelay code paths.

This is a precursor to supporting messaging.
  • Loading branch information
pxrl committed Oct 4, 2023
1 parent 2da16b5 commit d191cb1
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions src/relayer/Relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,41 +299,33 @@ export class Relayer {
if (this.fullyFilledDeposits[fillKey]) {
this.logger.debug({
at: "Relayer",
message: "Skipping deposits already filled by this relayer",
message: "Skipping deposit already filled by this relayer",
originChainId: deposit.originChainId,
depositId: deposit.depositId,
});
return;
}

this.logger.debug({ at: "Relayer", message: "Filling deposit", deposit, repaymentChainId });

// If deposit has been sped up, call fillRelayWithUpdatedFee instead. This guarantees that the relayer wouldn't
// accidentally double fill due to the deposit hash being different - SpokePool contract will check that the
// original hash with the old fee hasn't been filled.
if (isDepositSpedUp(deposit)) {
this.clients.multiCallerClient.enqueueTransaction({
contract: this.clients.spokePoolClients[deposit.destinationChainId].spokePool, // target contract
chainId: deposit.destinationChainId,
method: "fillRelayWithUpdatedDeposit",
args: buildFillRelayWithUpdatedFeeProps(deposit, repaymentChainId, fillAmount), // props sent with function call.
message: fillAmount.eq(deposit.amount)
? "Relay instantly sent with modified fee πŸš€"
: "Instantly completed relay with modified fee πŸ“«", // message sent to logger.
mrkdwn:
this.constructRelayFilledMrkdwn(deposit, repaymentChainId, fillAmount) +
`Modified relayer fee: ${formatFeePct(deposit.newRelayerFeePct)}%.`, // message details mrkdwn
});
} else {
// Add the fill transaction to the multiCallerClient so it will be executed with the next batch.
this.clients.multiCallerClient.enqueueTransaction({
contract: this.clients.spokePoolClients[deposit.destinationChainId].spokePool, // target contract
chainId: deposit.destinationChainId,
method: "fillRelay", // method called.
args: buildFillRelayProps(deposit, repaymentChainId, fillAmount), // props sent with function call.
message: fillAmount.eq(deposit.amount) ? "Relay instantly sent πŸš€" : "Instantly completed relay πŸ“«", // message sent to logger.
mrkdwn: this.constructRelayFilledMrkdwn(deposit, repaymentChainId, fillAmount), // message details mrkdwn
});
}
const [method, argBuilder, messageModifier] = isDepositSpedUp(deposit)
? ["fillRelayWithUpdatedDeposit", buildFillRelayWithUpdatedFeeProps, "with modified fee "]
: ["fillRelay", buildFillRelayProps, ""];

const message = fillAmount.eq(deposit.amount)
? `Relay instantly sent ${messageModifier}πŸš€`
: `Instantly completed relay ${messageModifier}πŸ“«"`;

this.clients.multiCallerClient.enqueueTransaction({
contract: this.clients.spokePoolClients[deposit.destinationChainId].spokePool,
chainId: deposit.destinationChainId,
method,
args: argBuilder(deposit, repaymentChainId, fillAmount),
message,
mrkdwn: this.constructRelayFilledMrkdwn(deposit, repaymentChainId, fillAmount),
});

// TODO: Revisit in the future when we implement partial fills.
this.fullyFilledDeposits[fillKey] = true;
Expand Down Expand Up @@ -676,9 +668,14 @@ export class Relayer {
}

private constructRelayFilledMrkdwn(deposit: Deposit, repaymentChainId: number, fillAmount: BigNumber): string {
return (
this.constructBaseFillMarkdown(deposit, fillAmount) + `Relayer repayment: ${getNetworkName(repaymentChainId)}.`
);
let mrkdwn =
this.constructBaseFillMarkdown(deposit, fillAmount) + ` Relayer repayment: ${getNetworkName(repaymentChainId)}.`;

if (isDepositSpedUp(deposit)) {
mrkdwn += ` Modified relayer fee: ${formatFeePct(deposit.newRelayerFeePct)}%.`;
}

return mrkdwn;
}

private constructZeroSizeFilledMrkdwn(deposit: Deposit): string {
Expand Down

0 comments on commit d191cb1

Please sign in to comment.