Skip to content

Commit

Permalink
fix: Use RL relayer address for basic gas cost estimation (#1036)
Browse files Browse the repository at this point in the history
It's invalid to use the current relayer's EOA as the simulated relayer
address because that address may not hold USDC on all chains. Instead,
default to the main RL address, which is known to have all relevant
tokens on all chains.

The relayer's own address will be used for dynamic message-based
simulation.
  • Loading branch information
pxrl authored Oct 24, 2023
1 parent 7fd6494 commit 0fb0c45
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/clients/ProfitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Deposit, DepositWithBlock, L1Token, SpokePoolClientsByChain } from "../
import { HubPoolClient } from ".";

const { formatEther } = ethersUtils;
const { EMPTY_MESSAGE } = sdkConsts;
const { EMPTY_MESSAGE, DEFAULT_SIMULATED_RELAYER_ADDRESS: TEST_RELAYER } = sdkConsts;
const { bnOne, bnUint32Max, fixedPointAdjustment: fixedPoint, isMessageEmpty, resolveDepositMessage } = sdkUtils;

// We use wrapped ERC-20 versions instead of the native tokens such as ETH, MATIC for ease of computing prices.
Expand Down Expand Up @@ -67,9 +67,7 @@ const GAS_TOKEN_DECIMALS = 18;
// @dev This address is known on each chain and has previously been used to simulate Deposit gas costs.
// Since _some_ known recipient address is needed for simulating a fill, default to this one. nb. Since
// the SpokePool implements custom behaviour when relayer === recipient, it's important not to use the
// relayer's own address.
// @todo: Consider using an address with confirmed 0 destinationToken balance so that the initial cost
// of populating storage is included in the estimate.
// relayer's own address. The specified address is deliberately setup by RL to have a 0 token balance.
const TEST_RECIPIENT = "0xBb23Cd0210F878Ea4CcA50e9dC307fb0Ed65Cf6B";

// These are used to simulate fills on L2s to return estimated gas costs.
Expand Down Expand Up @@ -425,7 +423,7 @@ export class ProfitClient {
}

private async updateGasCosts(): Promise<void> {
const { enabledChainIds, hubPoolClient, relayerAddress, relayerFeeQueries } = this;
const { enabledChainIds, hubPoolClient, relayerFeeQueries } = this;
const depositId = random(bnUint32Max.toNumber()); // random depositId + "" originToken => ~impossible to collide.
const fillAmount = bnOne;
const quoteTimestamp = getCurrentTime();
Expand All @@ -436,6 +434,10 @@ export class ProfitClient {
destinationChainId === hubPoolClient.chainId
? USDC
: hubPoolClient.getDestinationTokenForL1Token(USDC, destinationChainId);

// @dev The relayer _can not_ be the recipient, since the SpokePool short-circuits the ERC20 transfer
// and consumes less gas. Instead, just use the main RL address as the simulated relayer, since it has
// all supported tokens and approvals in place on all chains.
const deposit: Deposit = {
depositId,
depositor: TEST_RECIPIENT,
Expand All @@ -452,7 +454,7 @@ export class ProfitClient {
};

// An extra toBN cast is needed as the provider returns a different BigNumber type.
const gasCost = await relayerFeeQueries[destinationChainId].getGasCosts(deposit, fillAmount, relayerAddress);
const gasCost = await relayerFeeQueries[destinationChainId].getGasCosts(deposit, fillAmount, TEST_RELAYER);
this.totalGasCosts[this.enabledChainIds[idx]] = toBN(gasCost);
});

Expand Down

0 comments on commit 0fb0c45

Please sign in to comment.