Skip to content

Commit

Permalink
feat: use attestor chain id instead of network names for api fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Mar 5, 2025
1 parent 217e635 commit 33b465b
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 79 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"concurrently": "^8.2.2",
"d3": "^7.9.0",
"decimal.js": "^10.4.3",
"dlc-btc-lib": "2.5.17",
"dlc-btc-lib": "2.6.5",
"dotenv": "^16.3.1",
"ethers": "5.7.2",
"formik": "^2.4.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { CustomSkeleton } from '@components/custom-skeleton/custom-skeleton';
import { DetailedEvent } from '@models/ethereum-models';
import { truncateAddress, unshiftValue } from 'dlc-btc-lib/utilities';

import { findEthereumNetworkByName, formatEvent, formatToFourDecimals } from '@shared/utils';
import {
formatEvent,
formatToFourDecimals,
getEthereumNetworkIDByAttestorChainID,
} from '@shared/utils';

export function MerchantDetailsTableItem(merchantFocusTableItem: DetailedEvent): React.JSX.Element {
if (!merchantFocusTableItem) return <CustomSkeleton height={'35px'} />;
Expand All @@ -16,7 +20,7 @@ export function MerchantDetailsTableItem(merchantFocusTableItem: DetailedEvent):
isMint,
chain: eventChain,
} = formatEvent(merchantFocusTableItem);
const ethereumNetwork = findEthereumNetworkByName(eventChain);
const ethereumNetwork = getEthereumNetworkIDByAttestorChainID(eventChain);

const isMobile = useBreakpointValue({ base: true, md: false });

Expand Down
6 changes: 2 additions & 4 deletions src/app/components/proof-of-reserve/proof-of-reserve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export function ProofOfReserve(): React.JSX.Element {
const isMobile = useBreakpointValue({ base: true, md: false }) ?? false;

function getChainImagePath(chainName: string): string {
const lowerCaseChainName = chainName.toLowerCase();

const chainImagePaths: { [key: string]: string } = {
'evm-mainnet': '/images/logos/eth-token.svg',
'evm-arbitrum': '/images/logos/arbitrum-token.svg',
Expand All @@ -50,9 +48,9 @@ export function ProofOfReserve(): React.JSX.Element {
'evm-localhost': '',
'evm-hardhat-arb': '',
'evm-hardhat-eth': '',
'xrpl-mainnet': '/images/logos/xrpl-token.svg',
'ripple-xrpl-mainnet': '/images/logos/xrpl-token.svg',
};
return chainImagePaths[lowerCaseChainName];
return chainImagePaths[chainName];
}

const chainData: ChainData[] = proofOfReserveByChain.map(chain => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CustomSkeleton } from '@components/custom-skeleton/custom-skeleton';
import { DetailedEvent } from '@models/ethereum-models';
import { truncateAddress } from 'dlc-btc-lib/utilities';

import { findEthereumNetworkByName, formatEvent } from '@shared/utils';
import { formatEvent, getEthereumNetworkIDByAttestorChainID } from '@shared/utils';

export function ProtocolHistoryTableItem(
protocolHistoryTableItem: DetailedEvent
Expand All @@ -20,7 +20,7 @@ export function ProtocolHistoryTableItem(
chain: eventChain,
} = formatEvent(protocolHistoryTableItem);

const ethereumNetwork = findEthereumNetworkByName(eventChain);
const ethereumNetwork = getEthereumNetworkIDByAttestorChainID(eventChain);

const isMobile = useBreakpointValue({ base: true, lg: false });
return (
Expand Down
32 changes: 14 additions & 18 deletions src/app/hooks/use-proof-of-reserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { Merchant, MerchantProofOfReserve } from '@models/merchant';
import { EthereumNetworkConfigurationContext } from '@providers/ethereum-network-configuration.provider';
import { RippleNetworkConfigurationContext } from '@providers/ripple-network-configuration.provider';
import { useQuery } from '@tanstack/react-query';
import { AttestorChainID } from 'dlc-btc-lib/models';
import { unshiftValue } from 'dlc-btc-lib/utilities';
import { pluck } from 'ramda';

import { API_HELPERS } from '@shared/constants/api.constants';
import { NetworkType } from '@shared/constants/network.constants';
import { EVMAttestorChainIDMap } from '@shared/constants/ethereum.constants';
import { XRPLAttestorChainIDMap } from '@shared/constants/ripple.constants';

export interface UseProofOfReserveReturnType {
proofOfReserveSum?: number;
Expand Down Expand Up @@ -41,19 +42,10 @@ export function useProofOfReserve(): UseProofOfReserveReturnType {
}

async function fetchProofOfReserveByChain(
chainName: string,
networkType: NetworkType
chainName: AttestorChainID
): Promise<ProofOfReserveByChainReturnType> {
const formatChainParam = (chain: string, network: NetworkType): string => {
if (network === NetworkType.XRPL) {
return `${network.toLowerCase()}-${chain.toLowerCase()}`;
}
return chain ? chain.toLowerCase() : '';
};

try {
const chainParam = formatChainParam(chainName, networkType);
const apiUrl = API_HELPERS.getProofOfReserveURL({ chain: chainParam });
const apiUrl = API_HELPERS.getProofOfReserveURL({ chain: chainName });

const response = await fetch(apiUrl);
if (!response.ok) {
Expand All @@ -62,7 +54,7 @@ export function useProofOfReserve(): UseProofOfReserveReturnType {

const data = await response.json();
return {
chain: `${networkType}-${chainName}`,
chain: chainName,
value: data,
};
} catch (error) {
Expand All @@ -83,14 +75,18 @@ export function useProofOfReserve(): UseProofOfReserveReturnType {
async function fetchAllProofOfReserve(): Promise<UseProofOfReserveReturnType> {
const proofOfReserve = await fetchProofOfReserve();

const ethereumChainNames = pluck('name', enabledEthereumNetworks);
const rippleChainNames = pluck('name', enabledRippleNetworks);
const evmAttestorChainIDs = enabledEthereumNetworks.map(
network => EVMAttestorChainIDMap[network.id]
);
const xrpAttestorChainIDs = enabledRippleNetworks.map(
network => XRPLAttestorChainIDMap[network.id]
);

const evmPorByChains = await Promise.allSettled(
ethereumChainNames.map(async chain => fetchProofOfReserveByChain(chain, NetworkType.EVM))
evmAttestorChainIDs.map(async chain => fetchProofOfReserveByChain(chain))
);
const xrplPorByChains = await Promise.allSettled(
rippleChainNames.map(async chain => fetchProofOfReserveByChain(chain, NetworkType.XRPL))
xrpAttestorChainIDs.map(async chain => fetchProofOfReserveByChain(chain))
);

const fulfilledPorByChains = [...evmPorByChains, ...xrplPorByChains]
Expand Down
2 changes: 1 addition & 1 deletion src/app/hooks/use-total-supply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function useTotalSupply(): UseTotalSupplyReturnType {

const responseData = await response.json();

return responseData;
return responseData.totalSupply;
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error fetching Total Supply', error);
Expand Down
20 changes: 10 additions & 10 deletions src/app/providers/ethereum-network-configuration.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@functions/configuration.functions';
import { EthereumNetworkConfiguration } from '@models/ethereum-models';
import { HasChildren } from '@models/has-children';
import { EthereumNetworkID } from 'dlc-btc-lib/models';
import { EVMAttestorChainID, EthereumNetworkID } from 'dlc-btc-lib/models';
import { equals, find } from 'ramda';
import {
arbitrum,
Expand Down Expand Up @@ -48,7 +48,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: mainnet.blockExplorers.default.apiUrl,
websocketURL: appConfiguration.l1Websocket,
httpURL: appConfiguration.l1HTTP,
ethereumAttestorChainID: 'evm-mainnet',
ethereumAttestorChainID: EVMAttestorChainID['evm-mainnet'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(mainnet),
Expand All @@ -69,7 +69,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: sepolia.blockExplorers.default.apiUrl,
websocketURL: appConfiguration.l1Websocket,
httpURL: appConfiguration.l1HTTP,
ethereumAttestorChainID: 'evm-sepolia',
ethereumAttestorChainID: EVMAttestorChainID['evm-sepolia'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(sepolia),
Expand All @@ -90,7 +90,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: base.blockExplorers.default.apiUrl,
websocketURL: appConfiguration.baseWebsocket,
httpURL: appConfiguration.baseHTTP,
ethereumAttestorChainID: 'evm-base',
ethereumAttestorChainID: EVMAttestorChainID['evm-base'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(base),
Expand All @@ -111,7 +111,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: baseSepolia.blockExplorers.default.apiUrl,
websocketURL: appConfiguration.baseWebsocket,
httpURL: appConfiguration.baseHTTP,
ethereumAttestorChainID: 'evm-basesepolia',
ethereumAttestorChainID: EVMAttestorChainID['evm-basesepolia'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(baseSepolia),
Expand All @@ -132,7 +132,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: arbitrum.blockExplorers.default.apiUrl,
websocketURL: appConfiguration.arbitrumWebsocket,
httpURL: appConfiguration.arbitrumHTTP,
ethereumAttestorChainID: 'evm-arbitrum',
ethereumAttestorChainID: EVMAttestorChainID['evm-arbitrum'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(arbitrum),
Expand All @@ -153,7 +153,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: arbitrumSepolia.blockExplorers.default.apiUrl,
websocketURL: appConfiguration.arbitrumWebsocket,
httpURL: appConfiguration.arbitrumHTTP,
ethereumAttestorChainID: 'evm-arbsepolia',
ethereumAttestorChainID: EVMAttestorChainID['evm-arbsepolia'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(arbitrumSepolia),
Expand All @@ -174,7 +174,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: avalanche.blockExplorers.default.apiUrl,
websocketURL: appConfiguration.avalancheWebsocket,
httpURL: appConfiguration.avalancheHTTP,
ethereumAttestorChainID: 'evm-avax',
ethereumAttestorChainID: EVMAttestorChainID['evm-avax'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(avalanche),
Expand All @@ -195,7 +195,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: bsc.blockExplorers.default.apiUrl,
websocketURL: appConfiguration.bscWebsocket,
httpURL: appConfiguration.bscHTTP,
ethereumAttestorChainID: 'evm-bsc',
ethereumAttestorChainID: EVMAttestorChainID['evm-bsc'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(bsc),
Expand All @@ -216,7 +216,7 @@ function getEthereumNetworkConfiguration(
ethereumExplorerAPIURL: '',
websocketURL: hardhat.rpcUrls.default.http[0],
httpURL: hardhat.rpcUrls.default.http[0],
ethereumAttestorChainID: 'evm-hardhat-arb',
ethereumAttestorChainID: EVMAttestorChainID['evm-hardhat-eth'],
enabledEthereumNetworks,
dlcManagerContract: getEthereumContractWithProvider(
getEthereumNetworkDeploymentPlans(hardhat),
Expand Down
6 changes: 3 additions & 3 deletions src/app/providers/ripple-network-configuration.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { createContext, useEffect, useState } from 'react';
import { getRippleNetworkByID } from '@functions/configuration.functions';
import { HasChildren } from '@models/has-children';
import { RippleNetwork, RippleNetworkConfiguration, RippleNetworkID } from '@models/ripple.models';
import { Client } from 'dlc-btc-lib/models';
import { Client, XRPLAttestorChainID } from 'dlc-btc-lib/models';
import { getRippleClient } from 'dlc-btc-lib/ripple-functions';
import { equals, find } from 'ramda';

Expand Down Expand Up @@ -32,13 +32,13 @@ function getRippleNetworkConfiguration(
return {
rippleExplorerAPIURL: 'https://livenet.xrpl.org/',
websocketURL: 'wss://s1.ripple.com/',
rippleAttestorChainID: 'ripple-xrpl-mainnet',
rippleAttestorChainID: XRPLAttestorChainID['ripple-xrpl-mainnet'],
};
case RippleNetworkID.Testnet:
return {
rippleExplorerAPIURL: 'https://testnet.xrpl.org/',
websocketURL: 'wss://s.altnet.rippletest.net:51233',
rippleAttestorChainID: 'ripple-xrpl-testnet',
rippleAttestorChainID: XRPLAttestorChainID['ripple-xrpl-testnet'],
};

default:
Expand Down
14 changes: 14 additions & 0 deletions src/shared/constants/ethereum.constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EVMAttestorChainID, EthereumNetworkID } from 'dlc-btc-lib/models';
import { Chain } from 'viem';
import {
arbitrum,
Expand All @@ -22,3 +23,16 @@ export const SUPPORTED_VIEM_CHAINS: Chain[] = [
baseSepolia,
hardhat,
];

export const EVMAttestorChainIDMap: Record<EthereumNetworkID, EVMAttestorChainID> = {
[EthereumNetworkID.Mainnet]: EVMAttestorChainID['evm-mainnet'],
[EthereumNetworkID.Sepolia]: EVMAttestorChainID['evm-sepolia'],
[EthereumNetworkID.Arbitrum]: EVMAttestorChainID['evm-arbitrum'],
[EthereumNetworkID.ArbitrumSepolia]: EVMAttestorChainID['evm-arbsepolia'],
[EthereumNetworkID.Avalanche]: EVMAttestorChainID['evm-avax'],
[EthereumNetworkID.Base]: EVMAttestorChainID['evm-base'],
[EthereumNetworkID.BaseSepolia]: EVMAttestorChainID['evm-basesepolia'],
[EthereumNetworkID.BSC]: EVMAttestorChainID['evm-bsc'],
[EthereumNetworkID.Hardhat]: EVMAttestorChainID['evm-hardhat-eth'],
[EthereumNetworkID.Holesky]: EVMAttestorChainID['evm-holesky'],
};
6 changes: 6 additions & 0 deletions src/shared/constants/ripple.constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RippleNetwork, RippleNetworkID } from '@models/ripple.models';
import { XRPLAttestorChainID } from 'dlc-btc-lib/models';

const RippleMainnet: RippleNetwork = {
id: RippleNetworkID.Mainnet,
Expand All @@ -12,3 +13,8 @@ const RippleTestnet: RippleNetwork = {
};

export const supportedRippleNetworks: RippleNetwork[] = [RippleMainnet, RippleTestnet];

export const XRPLAttestorChainIDMap: Record<RippleNetworkID, XRPLAttestorChainID> = {
[RippleNetworkID.Mainnet]: XRPLAttestorChainID['ripple-xrpl-mainnet'],
[RippleNetworkID.Testnet]: XRPLAttestorChainID['ripple-xrpl-testnet'],
};
18 changes: 4 additions & 14 deletions src/shared/models/ethereum-models.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { EthereumNetwork } from 'dlc-btc-lib/models';
import { EVMAttestorChainID, EthereumNetwork } from 'dlc-btc-lib/models';
import { Contract } from 'ethers';
import { Chain } from 'viem';

export interface EthereumNetworkConfiguration {
ethereumExplorerAPIURL: string;
websocketURL: string;
httpURL: string;
ethereumAttestorChainID:
| 'evm-mainnet'
| 'evm-sepolia'
| 'evm-arbitrum'
| 'evm-arbsepolia'
| 'evm-base'
| 'evm-basesepolia'
| 'evm-avax'
| 'evm-bsc'
| 'evm-hardhat-arb'
| 'evm-hardhat-eth';
ethereumAttestorChainID: EVMAttestorChainID;
enabledEthereumNetworks: EthereumNetwork[];
dlcManagerContract: Contract;
iBTCContract: Contract;
Expand All @@ -30,15 +20,15 @@ export interface DetailedEvent {
timestamp: number;
txHash: string;
isCCIP: boolean;
chain: string;
chain: EVMAttestorChainID;
eventType: 'mint' | 'burn' | 'transfer';
}
export interface FormattedEvent {
merchant: string;
iBTCAmount: number;
txHash: string;
date: string;
chain: string;
chain: EVMAttestorChainID;
isMint: boolean;
isCCIP: boolean;
displayAmount: number | null;
Expand Down
4 changes: 2 additions & 2 deletions src/shared/models/ripple.models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AttestorChainID } from 'dlc-btc-lib/models';
import { XRPLAttestorChainID } from 'dlc-btc-lib/models';

export interface RippleNetwork {
id: RippleNetworkID;
Expand All @@ -13,5 +13,5 @@ export enum RippleNetworkID {
export interface RippleNetworkConfiguration {
rippleExplorerAPIURL: string;
websocketURL: string;
rippleAttestorChainID: AttestorChainID;
rippleAttestorChainID: XRPLAttestorChainID;
}
Loading

0 comments on commit 33b465b

Please sign in to comment.