Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove rpcs #220

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions app/(routes)/packets/packet-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CHAIN_CONFIGS, CHAIN, clientToDisplay } from 'utils/chains/configs';
import { Chain } from 'utils/types/chain';
import { classNames, numberWithCommas } from 'utils/functions';
import { LinkAndCopy } from 'components/link-and-copy';
import { ethers } from 'ethers';
import { useEffect, useState } from 'react';

export function PacketDetails(packet: Packet | null) {
Expand All @@ -28,15 +27,13 @@ export function PacketDetails(packet: Packet | null) {
useEffect(() => {
async function checkTxFunding() {
if (packet && sourceChain && destChain) {
const recv = await calcTxFunding(destChain.rpc, packet.totalRecvFeesDeposited, packet.recvGasLimit);
const ack = await calcTxFunding(sourceChain.rpc, packet.totalAckFeesDeposited, packet.ackGasLimit);
const recv = await calcTxFunding(destChain.id, packet.totalRecvFeesDeposited, packet.recvGasLimit);
const ack = await calcTxFunding(sourceChain.id, packet.totalAckFeesDeposited, packet.ackGasLimit);
setRecvFunding(recv);
setAckFunding(ack);
}
}
checkTxFunding();
let intervalId = setInterval(checkTxFunding, 2000);
return () => clearInterval(intervalId);
}, [packet, sourceChain, destChain]);

return !packet ? (
Expand Down Expand Up @@ -190,13 +187,13 @@ function formatDuration(duration: number) {
return `${(duration / 3600).toFixed(1)}h`;
}

async function calcTxFunding(chainRpc: string, feesDeposited?: number, gasLimit?: number) {
async function calcTxFunding(chainId: Number, feesDeposited?: number, gasLimit?: number) {
if (!feesDeposited || !gasLimit) {
return 0;
}

const provider = new ethers.JsonRpcProvider(chainRpc)
const feeData = await provider.getFeeData();
const feeResponse = await fetch(`/api/packets/fee-data?chainId=${chainId}`, { cache: 'no-store' });
const feeData = await feeResponse.json();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the type issue and approve the changes.

The function calcTxFunding has been updated to use a chain ID and fetch fee data from an API, aligning with the PR's objectives. However, the type 'Number' should be corrected to 'number' for consistency with TypeScript best practices.

Apply this diff to correct the type issue:

-async function calcTxFunding(chainId: Number, feesDeposited?: number, gasLimit?: number) {
+async function calcTxFunding(chainId: number, feesDeposited?: number, gasLimit?: number) {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async function calcTxFunding(chainId: Number, feesDeposited?: number, gasLimit?: number) {
if (!feesDeposited || !gasLimit) {
return 0;
}
const provider = new ethers.JsonRpcProvider(chainRpc)
const feeData = await provider.getFeeData();
const feeResponse = await fetch(`/api/packets/fee-data?chainId=${chainId}`, { cache: 'no-store' });
const feeData = await feeResponse.json();
async function calcTxFunding(chainId: number, feesDeposited?: number, gasLimit?: number) {
if (!feesDeposited || !gasLimit) {
return 0;
}
const feeResponse = await fetch(`/api/packets/fee-data?chainId=${chainId}`, { cache: 'no-store' });
const feeData = await feeResponse.json();
Tools
Biome

[error] 190-190: Don't use 'Number' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'number' instead

(lint/complexity/noBannedTypes)

const gasPrice = Number(feeData.gasPrice);

feesDeposited = Number(feesDeposited);
Expand Down
33 changes: 33 additions & 0 deletions app/api/packets/fee-data/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextRequest, NextResponse } from 'next/server';
import { CHAIN_CONFIGS, CHAIN } from 'utils/chains/configs';
import { ethers } from 'ethers';

export const dynamic = 'force-dynamic'; // defaults to auto

export async function GET(request: NextRequest) {
const { searchParams } = request.nextUrl;
let chainId = Number(searchParams.get('chainId') || '');

if (!chainId) {
return NextResponse.json({ error: 'Invalid chainId' }, { status: 400 });
}

let chainRpc = '';
for (const chain of Object.keys(CHAIN_CONFIGS)) {
const chainName = chain as CHAIN;
const chainVals = CHAIN_CONFIGS[chainName];
if (chainId === chainVals.id) {
chainRpc = chainVals.rpc;
break;
}
}

if (!chainRpc) {
return NextResponse.json({ error: 'Chain not found' }, { status: 404 });
}

const provider = new ethers.JsonRpcProvider(chainRpc)
const feeData = await provider.getFeeData();

return NextResponse.json(feeData, { status: 200 });
}
4 changes: 2 additions & 2 deletions app/utils/chains/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ let baseDispatcher = process.env.DISPATCHER_ADDRESS_BASE!;
let opClientName = process.env.OPTIMISM_CLIENT_NAME!;
let baseClientName = process.env.BASE_CLIENT_NAME!;

let optimismRPC = process.env.OPTIMISM_RPC || 'https://opt-sepolia.g.alchemy.com/v2/jKvLhhXvtnWdNeZrKst0demxnwJcYH1o';
let baseRPC = process.env.BASE_RPC || 'https://base-sepolia.g.alchemy.com/v2/776dC6qT-NTtupdnxlUJuXGbUIKWWhLe';
let optimismRPC = process.env.OPTIMISM_RPC!;
let baseRPC = process.env.BASE_RPC!;
Comment on lines +12 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential risk of runtime errors due to non-null assertions.

The use of the non-null assertion operator (!) for optimismRPC and baseRPC assumes that these environment variables are always set. This change removes the previous safety net of fallback URLs and could lead to runtime errors if the environment variables are not set.

Consider adding error handling or documentation to ensure that these variables are set before deployment.


Consider adding runtime checks or fallback mechanisms.

The direct use of optimismRPC and baseRPC in the CHAIN_CONFIGS object without any checks could lead to failures if these environment variables are not set. This could compromise the application's robustness and lead to service disruptions.

Consider implementing runtime checks or fallback mechanisms within the CHAIN_CONFIGS object to handle cases where these variables might not be set.


export const CHAIN_CONFIGS: {
[key in CHAIN]: Chain;
Expand Down
Loading