Skip to content

Commit

Permalink
refactor: Consolidate relayer fillRelay method (#967)
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. The generation of logging and
markdown included in logging was also tweaked here, but the output is 
basically the same.

This is a precursor to supporting messaging.
  • Loading branch information
pxrl committed Oct 5, 2023
1 parent 9da0a15 commit 07cb73e
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 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)
? `Filled deposit ${messageModifier}🚀`
: `Partially filled deposit ${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 All @@ -690,14 +687,17 @@ export class Relayer {

private constructBaseFillMarkdown(deposit: Deposit, fillAmount: BigNumber): string {
const { symbol, decimals } = this.clients.hubPoolClient.getTokenInfoForDeposit(deposit);
const srcChain = getNetworkName(deposit.originChainId);
const dstChain = getNetworkName(deposit.destinationChainId);
const amount = createFormatFunction(2, 4, false, decimals)(deposit.amount.toString());
const depositor = blockExplorerLink(deposit.depositor, deposit.originChainId);
const _fillAmount = createFormatFunction(2, 4, false, decimals)(fillAmount.toString());
const relayerFeePct = formatFeePct(deposit.relayerFeePct);
const realizedLpFeePct = formatFeePct(deposit.realizedLpFeePct);
return (
`Relayed depositId ${deposit.depositId} from ${getNetworkName(deposit.originChainId)} ` +
`to ${getNetworkName(deposit.destinationChainId)} of ` +
`${createFormatFunction(2, 4, false, decimals)(deposit.amount.toString())} ${symbol}. ` +
`with depositor ${blockExplorerLink(deposit.depositor, deposit.originChainId)}. ` +
`Fill amount of ${createFormatFunction(2, 4, false, decimals)(fillAmount.toString())} ${symbol} with ` +
`relayerFee ${formatFeePct(deposit.relayerFeePct)}% & ` +
`realizedLpFee ${formatFeePct(deposit.realizedLpFeePct)}%. `
`Relayed depositId ${deposit.depositId} from ${srcChain} to ${dstChain} of ${amount} ${symbol},` +
` with depositor ${depositor}. Fill amount of ${_fillAmount} ${symbol} with` +
` relayerFee ${relayerFeePct}% & realizedLpFee ${realizedLpFeePct}%.`
);
}

Expand Down

0 comments on commit 07cb73e

Please sign in to comment.