Skip to content

Commit

Permalink
improve: Consolidate RetryProvider cast in RPCUtils (#895)
Browse files Browse the repository at this point in the history
Makes it easier to use the convertEthersRPCToZKSyncRPC() helper.
  • Loading branch information
pxrl committed Aug 21, 2023
1 parent 982dd58 commit e8ea4ad
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/clients/bridges/ZKSyncAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
spreadEventWithBlockNumber,
assign,
Event,
RetryProvider,
ZERO_ADDRESS,
getTokenAddress,
} from "../../utils";
Expand All @@ -19,7 +18,7 @@ import { CONTRACT_ADDRESSES } from "../../common";
import { TOKEN_SYMBOLS_MAP } from "@across-protocol/contracts-v2";
import { isDefined } from "../../utils/TypeGuards";
import { gasPriceOracle, utils } from "@across-protocol/sdk-v2";
import { convertEthersRPCToZKSyncRPC } from "../../utils/RPCUtils";
import { zkSync as zkSyncUtils } from "../../utils/chains";
import { BigNumberish } from "../../utils/FormattingUtils";

/**
Expand Down Expand Up @@ -146,9 +145,9 @@ export class ZKSyncAdapter extends BaseAdapter {
// Next, load estimated executed L1 gas price of the message transaction and the L2 gas limit.
const l1Provider = this.getProvider(this.hubChainId);
const l2Provider = this.spokePoolClients[this.chainId].spokePool.provider;
let zkProvider;
let zkProvider: zksync.Provider;
try {
zkProvider = convertEthersRPCToZKSyncRPC(l2Provider as RetryProvider);
zkProvider = zkSyncUtils.convertEthersRPCToZKSyncRPC(l2Provider);
} catch (error) {
this.logger.warn({
at: "ZkSyncClient#sendTokenToTargetChain",
Expand All @@ -158,7 +157,7 @@ export class ZKSyncAdapter extends BaseAdapter {
}
// If zkSync provider can't be created for some reason, default to a very conservative 2mil L2 gas limit
// which should be sufficient for this transaction.
const l2GasLimit = (await isDefined(zkProvider))
const l2GasLimit = isDefined(zkProvider)
? await zksync.utils.estimateDefaultBridgeDepositL2Gas(
l1Provider,
zkProvider,
Expand Down
1 change: 1 addition & 0 deletions src/utils/chains/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as zkSync from "./zkSync";
9 changes: 5 additions & 4 deletions src/utils/RPCUtils.ts → src/utils/chains/zkSync.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import assert from "assert";
import { providers as ethersProviders } from "ethers";
import { Provider as ZKSyncProvider } from "zksync-web3";
import { isDefined } from "./TypeGuards";
import { RetryProvider } from "./ProviderUtils";
import { RetryProvider } from "../ProviderUtils";
import { isDefined } from "../TypeGuards";

/**
* Converts a valid Ethers Provider into a ZKSync Provider
* @param ethersProvider The Ethers provider that we wish to convert
* @returns A ZKSync Provider
* @throws If the provider is not a valid JsonRpcProvider
*/
export function convertEthersRPCToZKSyncRPC(ethersProvider: RetryProvider): ZKSyncProvider {
export function convertEthersRPCToZKSyncRPC(ethersProvider: ethersProviders.Provider): ZKSyncProvider {
const url = (ethersProvider as RetryProvider).providers[0].connection.url;
assert(isDefined(url), "Provider must be of type JsonRpcProvider");
assert(isDefined(url), "Provider must be of type RetryProvider");
return new ZKSyncProvider(url);
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type { Block, TransactionResponse, TransactionReceipt, Provider } from "@
export { config } from "dotenv";

// Utils specifically for this bot.
export * from "./chains";
export * from "./ProviderUtils";
export * from "./SignerUtils";
export * from "./DepositUtils";
Expand Down

0 comments on commit e8ea4ad

Please sign in to comment.