import React, { useState, useEffect } from 'react';
interface WalletState {
isInstalled: boolean;
isConnected: boolean;
account: string | null;
publicKey: string | null;
balance: number | null;
}
const WalletIntegration: React.FC = () => {
const [wallet, setWallet] = useState<WalletState>({
isInstalled: false,
isConnected: false,
account: null,
publicKey: null,
balance: null
});
// Check if eckoWALLET is installed
useEffect(() => {
const checkWallet = async () => {
const isInstalled = window.kadena?.isKadena || false;
setWallet(prev => ({ ...prev, isInstalled }));
if (isInstalled) {
// Check if already connected
await checkConnection();
}
};
// Wait for page load
if (document.readyState === 'complete') {
checkWallet();
} else {
window.addEventListener('load', checkWallet);
return () => window.removeEventListener('load', checkWallet);
}
}, []);
// Listen for account changes
useEffect(() => {
if (wallet.isInstalled) {
window.kadena.on('res_accountChange', handleAccountChange);
return () => {
// Cleanup if needed
};
}
}, [wallet.isInstalled]);
const checkConnection = async () => {
try {
const result = await window.kadena.request({
method: 'kda_checkStatus',
networkId: 'mainnet01'
});
if (result.result.status === 'success') {
setWallet(prev => ({
...prev,
isConnected: true,
account: result.result.account.account,
publicKey: result.result.account.publicKey
}));
await fetchBalance(result.result.account.account);
}
} catch (error) {
console.error('Connection check failed:', error);
}
};
const handleConnect = async () => {
try {
const result = await window.kadena.request({
method: 'kda_connect',
networkId: 'mainnet01'
});
if (result.status === 'success') {
setWallet(prev => ({
...prev,
isConnected: true,
account: result.account.account,
publicKey: result.account.publicKey
}));
await fetchBalance(result.account.account);
}
} catch (error) {
console.error('Connection failed:', error);
alert('Failed to connect wallet');
}
};
const handleDisconnect = async () => {
try {
await window.kadena.request({
method: 'kda_disconnect',
networkId: 'mainnet01',
domain: window.location.hostname
});
setWallet(prev => ({
...prev,
isConnected: false,
account: null,
publicKey: null,
balance: null
}));
} catch (error) {
console.error('Disconnect failed:', error);
}
};
const handleAccountChange = async () => {
console.log('Account changed, refreshing...');
await checkConnection();
};
const fetchBalance = async (account: string) => {
try {
const accountInfo = await window.kadena.request({
method: 'kda_requestAccount',
networkId: 'mainnet01'
});
if (accountInfo.status === 'success') {
setWallet(prev => ({
...prev,
balance: accountInfo.wallet.balance
}));
}
} catch (error) {
console.error('Failed to fetch balance:', error);
}
};
const handleSendTransaction = async () => {
if (!wallet.account) return;
try {
const result = await window.kadena.request({
method: 'kda_sendKadena',
data: {
networkId: 'mainnet01',
account: 'k:recipient-public-key',
amount: 1.0,
chainId: '1',
sourceChainId: '1',
domain: window.location.hostname
}
});
if (result.status === 'success') {
alert('Transaction sent successfully!');
await fetchBalance(wallet.account);
}
} catch (error) {
console.error('Transaction failed:', error);
alert('Transaction failed');
}
};
// Render UI
if (!wallet.isInstalled) {
return (
<div>
<h2>eckoWALLET Not Found</h2>
<p>Please install eckoWALLET to use this dApp.</p>
<a href="https://eckowallet.com" target="_blank" rel="noopener noreferrer">
Install eckoWALLET
</a>
</div>
);
}
if (!wallet.isConnected) {
return (
<div>
<h2>Connect Your Wallet</h2>
<button onClick={handleConnect}>
Connect eckoWALLET
</button>
</div>
);
}
return (
<div>
<h2>Wallet Connected</h2>
<div>
<p><strong>Account:</strong> {wallet.account}</p>
<p><strong>Public Key:</strong> {wallet.publicKey?.substring(0, 16)}...</p>
<p><strong>Balance:</strong> {wallet.balance?.toFixed(4)} KDA</p>
</div>
<div>
<button onClick={handleSendTransaction}>Send 1 KDA</button>
<button onClick={handleDisconnect}>Disconnect</button>
</div>
</div>
);
};
export default WalletIntegration;