Files
smom-dbis-138/examples/metamask-react/src/useChain138.ts
defiQUG 1fb7266469 Add Oracle Aggregator and CCIP Integration
- Introduced Aggregator.sol for Chainlink-compatible oracle functionality, including round-based updates and access control.
- Added OracleWithCCIP.sol to extend Aggregator with CCIP cross-chain messaging capabilities.
- Created .gitmodules to include OpenZeppelin contracts as a submodule.
- Developed a comprehensive deployment guide in NEXT_STEPS_COMPLETE_GUIDE.md for Phase 2 and smart contract deployment.
- Implemented Vite configuration for the orchestration portal, supporting both Vue and React frameworks.
- Added server-side logic for the Multi-Cloud Orchestration Portal, including API endpoints for environment management and monitoring.
- Created scripts for resource import and usage validation across non-US regions.
- Added tests for CCIP error handling and integration to ensure robust functionality.
- Included various new files and directories for the orchestration portal and deployment scripts.
2025-12-12 14:57:48 -08:00

96 lines
2.3 KiB
TypeScript

import { useState, useEffect } from 'react';
const CHAIN_ID = '0x8a';
interface UseChain138Return {
isConnected: boolean;
isLoading: boolean;
connect: () => Promise<void>;
chainId: string | null;
}
export function useChain138(): UseChain138Return {
const [isConnected, setIsConnected] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [chainId, setChainId] = useState<string | null>(null);
const checkConnection = async () => {
if (typeof window === 'undefined' || !window.ethereum) {
setIsConnected(false);
setIsLoading(false);
return;
}
try {
const currentChainId = await window.ethereum.request({
method: 'eth_chainId',
}) as string;
setChainId(currentChainId);
setIsConnected(currentChainId === CHAIN_ID);
} catch (error) {
setIsConnected(false);
} finally {
setIsLoading(false);
}
};
const connect = async () => {
if (typeof window === 'undefined' || !window.ethereum) {
throw new Error('MetaMask is not installed');
}
const networkMetadata = {
chainId: CHAIN_ID,
chainName: 'DeFi Oracle Meta Mainnet',
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18,
},
rpcUrls: ['https://rpc.d-bis.org'],
blockExplorerUrls: ['https://explorer.d-bis.org'],
};
try {
// Try to switch first
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: CHAIN_ID }],
});
} catch (error: any) {
// If switch fails, network might not be added
if (error.code === 4902) {
// Add the network
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [networkMetadata],
});
} else {
throw error;
}
}
await checkConnection();
};
useEffect(() => {
checkConnection();
if (window.ethereum) {
const handleChainChanged = () => {
checkConnection();
};
window.ethereum.on('chainChanged', handleChainChanged);
return () => {
window.ethereum?.removeListener('chainChanged', handleChainChanged);
};
}
}, []);
return { isConnected, isLoading, connect, chainId };
}