Skip to content

Commit

Permalink
Merge branch 'master' into bz/mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bmzig authored May 16, 2024
2 parents 53b26ec + 7855967 commit 638a367
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 52 deletions.
14 changes: 9 additions & 5 deletions src/dataworker/DataworkerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ export function _buildRelayerRefundRoot(
return;
}

const l2TokenCounterpart = clients.hubPoolClient.getL2TokenForL1TokenAtBlock(leaf.l1Tokens[index], leaf.chainId);
const l2TokenCounterpart = clients.hubPoolClient.getL2TokenForL1TokenAtBlock(
leaf.l1Tokens[index],
leaf.chainId,
endBlockForMainnet
);
// If we've already seen this leaf, then skip.
const existingLeaf = relayerRefundLeaves.find(
(relayerRefundLeaf) =>
Expand Down Expand Up @@ -420,7 +424,7 @@ export async function _buildPoolRebalanceRoot(
const l1TokenCounterpart = clients.hubPoolClient.getL1TokenForL2TokenAtBlock(
l2TokenAddress,
repaymentChainId,
latestMainnetBlock
mainnetBundleEndBlock
);

updateRunningBalance(runningBalances, repaymentChainId, l1TokenCounterpart, totalRefundAmount);
Expand All @@ -443,7 +447,7 @@ export async function _buildPoolRebalanceRoot(
const l1TokenCounterpart = clients.hubPoolClient.getL1TokenForL2TokenAtBlock(
outputToken,
destinationChainId,
latestMainnetBlock
mainnetBundleEndBlock
);
const lpFee = deposit.lpFeePct.mul(deposit.inputAmount).div(fixedPointAdjustment);
updateRunningBalance(runningBalances, destinationChainId, l1TokenCounterpart, deposit.inputAmount.sub(lpFee));
Expand All @@ -467,7 +471,7 @@ export async function _buildPoolRebalanceRoot(
const l1TokenCounterpart = clients.hubPoolClient.getL1TokenForL2TokenAtBlock(
outputToken,
destinationChainId,
latestMainnetBlock
mainnetBundleEndBlock
);
const lpFee = deposit.lpFeePct.mul(deposit.inputAmount).div(fixedPointAdjustment);
updateRunningBalance(runningBalances, destinationChainId, l1TokenCounterpart, lpFee.sub(deposit.inputAmount));
Expand Down Expand Up @@ -503,7 +507,7 @@ export async function _buildPoolRebalanceRoot(
const l1TokenCounterpart = clients.hubPoolClient.getL1TokenForL2TokenAtBlock(
inputToken,
originChainId,
latestMainnetBlock
mainnetBundleEndBlock
);
updateRunningBalance(runningBalances, originChainId, l1TokenCounterpart, deposit.inputAmount);
});
Expand Down
8 changes: 2 additions & 6 deletions src/finalizer/utils/arbitrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getCurrentTime,
getRedisCache,
getBlockForTimestamp,
getL1TokenInfo,
} from "../../utils";
import { TokensBridged } from "../../interfaces";
import { HubPoolClient, SpokePoolClient } from "../../clients";
Expand Down Expand Up @@ -55,12 +56,7 @@ async function multicallArbitrumFinalizations(
const finalizableMessages = await getFinalizableMessages(logger, tokensBridged, hubSigner);
const callData = await Promise.all(finalizableMessages.map((message) => finalizeArbitrum(message.message)));
const crossChainTransfers = finalizableMessages.map(({ info: { l2TokenAddress, amountToReturn } }) => {
const l1TokenCounterpart = hubPoolClient.getL1TokenForL2TokenAtBlock(
l2TokenAddress,
CHAIN_ID,
hubPoolClient.latestBlockSearched
);
const l1TokenInfo = hubPoolClient.getTokenInfo(1, l1TokenCounterpart);
const l1TokenInfo = getL1TokenInfo(l2TokenAddress, CHAIN_ID);
const amountFromWei = convertFromWei(amountToReturn.toString(), l1TokenInfo.decimals);
const withdrawal: CrossChainMessage = {
originationChainId: CHAIN_ID,
Expand Down
9 changes: 2 additions & 7 deletions src/finalizer/utils/linea/l2ToL1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Wallet } from "ethers";
import { groupBy } from "lodash";

import { HubPoolClient, SpokePoolClient } from "../../../clients";
import { Signer, winston, convertFromWei } from "../../../utils";
import { Signer, winston, convertFromWei, getL1TokenInfo } from "../../../utils";
import { FinalizerPromise, CrossChainMessage } from "../../types";
import { TokensBridged } from "../../../interfaces";
import {
Expand Down Expand Up @@ -103,12 +103,7 @@ export async function lineaL2ToL1Finalizer(
// Populate cross chain transfers for claimed messages
const transfers = claimable.map(({ tokensBridged }) => {
const { l2TokenAddress, amountToReturn } = tokensBridged;
const l1TokenCounterpart = hubPoolClient.getL1TokenForL2TokenAtBlock(
l2TokenAddress,
l2ChainId,
hubPoolClient.latestBlockSearched
);
const { decimals, symbol: l1TokenSymbol } = hubPoolClient.getTokenInfo(l1ChainId, l1TokenCounterpart);
const { decimals, symbol: l1TokenSymbol } = getL1TokenInfo(l2TokenAddress, l2ChainId);
const amountFromWei = convertFromWei(amountToReturn.toString(), decimals);
const transfer: CrossChainMessage = {
originationChainId: l2ChainId,
Expand Down
14 changes: 4 additions & 10 deletions src/finalizer/utils/opStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import assert from "assert";
import { groupBy } from "lodash";
import * as optimismSDK from "@eth-optimism/sdk";
import { HubPoolClient, SpokePoolClient } from "../../clients";
import { L1Token, TokensBridged } from "../../interfaces";
import { TokensBridged } from "../../interfaces";
import {
BigNumber,
chainIsOPStack,
convertFromWei,
getBlockForTimestamp,
getCachedProvider,
getCurrentTime,
getL1TokenInfo,
getNetworkName,
getRedisCache,
getUniqueLogIndex,
Expand Down Expand Up @@ -217,13 +218,6 @@ async function getOptimismFinalizableMessages(
);
}

function getL1TokenInfoForOptimismToken(chainId: OVM_CHAIN_ID, hubPoolClient: HubPoolClient, l2Token: string): L1Token {
return hubPoolClient.getL1TokenInfoForL2Token(
SpokePoolClient.getExecutedRefundLeafL2Token(chainId, l2Token),
chainId
);
}

async function finalizeOptimismMessage(
_chainId: OVM_CHAIN_ID,
crossChainMessenger: OVM_CROSS_CHAIN_MESSENGER,
Expand Down Expand Up @@ -274,7 +268,7 @@ async function multicallOptimismFinalizations(
)
);
const withdrawals = finalizableMessages.map((message) => {
const l1TokenInfo = getL1TokenInfoForOptimismToken(chainId, hubPoolClient, message.event.l2TokenAddress);
const l1TokenInfo = getL1TokenInfo(message.event.l2TokenAddress, chainId);
const amountFromWei = convertFromWei(message.event.amountToReturn.toString(), l1TokenInfo.decimals);
const withdrawal: CrossChainMessage = {
originationChainId: chainId,
Expand Down Expand Up @@ -306,7 +300,7 @@ async function multicallOptimismL1Proofs(
provableMessages.map((message) => proveOptimismMessage(chainId, crossChainMessenger, message, message.logIndex))
);
const withdrawals = provableMessages.map((message) => {
const l1TokenInfo = getL1TokenInfoForOptimismToken(chainId, hubPoolClient, message.event.l2TokenAddress);
const l1TokenInfo = getL1TokenInfo(message.event.l2TokenAddress, chainId);
const amountFromWei = convertFromWei(message.event.amountToReturn.toString(), l1TokenInfo.decimals);
const proof: CrossChainMessage = {
originationChainId: chainId,
Expand Down
20 changes: 5 additions & 15 deletions src/finalizer/utils/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getCurrentTime,
getRedisCache,
getBlockForTimestamp,
getL1TokenInfo,
} from "../../utils";
import { EthersError, TokensBridged } from "../../interfaces";
import { HubPoolClient, SpokePoolClient } from "../../clients";
Expand Down Expand Up @@ -204,9 +205,7 @@ async function resolvePolygonRetrievalFinalizations(
})
);
const callData = await Promise.all(
tokensInFinalizableMessages.map((l2Token) =>
retrieveTokenFromMainnetTokenBridger(l2Token, hubSigner, hubPoolClient)
)
tokensInFinalizableMessages.map((l2Token) => retrieveTokenFromMainnetTokenBridger(l2Token, hubSigner))
);
const crossChainMessages = finalizableMessages.map((finalizableMessage) =>
resolveCrossChainTransferStructure(finalizableMessage, "misc", hubPoolClient)
Expand All @@ -223,12 +222,7 @@ function resolveCrossChainTransferStructure(
hubPoolClient: HubPoolClient
): CrossChainMessage {
const { l2TokenAddress, amountToReturn } = finalizableMessage;
const l1TokenCounterpart = hubPoolClient.getL1TokenForL2TokenAtBlock(
l2TokenAddress,
CHAIN_ID,
hubPoolClient.latestBlockSearched
);
const l1TokenInfo = hubPoolClient.getTokenInfo(1, l1TokenCounterpart);
const l1TokenInfo = getL1TokenInfo(l2TokenAddress, CHAIN_ID);
const amountFromWei = convertFromWei(amountToReturn.toString(), l1TokenInfo.decimals);
const transferBase = {
originationChainId: CHAIN_ID,
Expand All @@ -246,12 +240,8 @@ function getMainnetTokenBridger(mainnetSigner: Signer): Contract {
return getDeployedContract("PolygonTokenBridger", 1, mainnetSigner);
}

async function retrieveTokenFromMainnetTokenBridger(
l2Token: string,
mainnetSigner: Signer,
hubPoolClient: HubPoolClient
): Promise<Multicall2Call> {
const l1Token = hubPoolClient.getL1TokenForL2TokenAtBlock(l2Token, CHAIN_ID, hubPoolClient.latestBlockSearched);
async function retrieveTokenFromMainnetTokenBridger(l2Token: string, mainnetSigner: Signer): Promise<Multicall2Call> {
const l1Token = getL1TokenInfo(l2Token, CHAIN_ID).address;
const mainnetTokenBridger = getMainnetTokenBridger(mainnetSigner);
const callData = await mainnetTokenBridger.populateTransaction.retrieve(l1Token);
return {
Expand Down
8 changes: 2 additions & 6 deletions src/finalizer/utils/zkSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getBlockForTimestamp,
getCurrentTime,
getEthAddressForChain,
getL1TokenInfo,
getRedisCache,
getUniqueLogIndex,
winston,
Expand Down Expand Up @@ -65,12 +66,7 @@ export async function zkSyncFinalizer(
const txns = await prepareFinalizations(l1ChainId, l2ChainId, withdrawalParams);

const withdrawals = candidates.map(({ l2TokenAddress, amountToReturn }) => {
const l1TokenCounterpart = hubPoolClient.getL1TokenForL2TokenAtBlock(
l2TokenAddress,
l2ChainId,
hubPoolClient.latestBlockSearched
);
const { decimals, symbol: l1TokenSymbol } = hubPoolClient.getTokenInfo(l1ChainId, l1TokenCounterpart);
const { decimals, symbol: l1TokenSymbol } = getL1TokenInfo(l2TokenAddress, l2ChainId);
const amountFromWei = convertFromWei(amountToReturn.toString(), decimals);
const withdrawal: CrossChainMessage = {
originationChainId: l2ChainId,
Expand Down
7 changes: 4 additions & 3 deletions src/libexec/RelayerSpokePoolIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ async function run(argv: string[]): Promise<void> {
const spokePool = await utils.getSpokePoolContract(chainId);

process.on("SIGHUP", () => {
logger.debug({ at: "Relayer#run", message: "Received SIGHUP, stopping..." });
logger.debug({ at: "Relayer#run", message: `Received SIGHUP in ${chain} listener, stopping...` });
stop = true;
});

process.on("disconnect", () => {
logger.debug({ at: "Relayer::run", message: "Parent disconnected, stopping..." });
logger.debug({ at: "Relayer::run", message: `${chain} parent disconnected, stopping...` });
stop = true;
});

Expand Down Expand Up @@ -310,11 +310,12 @@ if (require.main === module) {
process.exitCode = NODE_SUCCESS;
})
.catch((error) => {
logger.error({ at: "RelayerSpokePoolIndexer", message: "Process exited with error.", error });
logger.error({ at: "RelayerSpokePoolIndexer", message: `${chain} listener exited with error.`, error });
process.exitCode = NODE_APP_ERR;
})
.finally(async () => {
await disconnectRedisClients();
logger.debug({ at: "RelayerSpokePoolIndexer", message: `Exiting ${chain} listener.` });
exit(process.exitCode);
});
}

0 comments on commit 638a367

Please sign in to comment.