Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 29 additions & 10 deletions examples/ethereum/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Box, Button, TextField, NumberField, FieldLabel, Callout } from "@inter
import React, { useState, useEffect } from "react"
import { Wallet, ArrowRight, RefreshCw, AlertCircle } from "lucide-react"
import { SignerFromBrowser } from "@interchainjs/ethereum/signers/SignerFromBrowser"
import { parseEther, formatEther } from "@interchainjs/ethereum/utils/denominations"
import { MetaMaskInpageProvider } from "@metamask/providers";
import { useChain } from '@interchain-kit/react'
import { WalletState } from "@interchain-kit/core"
Expand Down Expand Up @@ -64,14 +63,15 @@ export default function WalletPage() {
if (!ethereum) return
try {
console.log('ethereum in getBalance:', ethereum)
const wallet = new SignerFromBrowser(
ethereum!
// window.ethereum as EthereumProvider
)
console.log('wallet in getBalance:', wallet)
const balance = await wallet.getBalance()
console.log('balance in getBalance:', balance)
setBalance(formatEther(balance))
// Use EIP-1193 provider directly to fetch balance
const addr = account
if (!addr) throw new Error('No connected account')
const hexBalance = await (ethereum as any).request({
method: 'eth_getBalance',
params: [addr, 'latest']
}) as string
const wei = BigInt(hexBalance)
setBalance(formatEther(wei))
} catch (err: any) {
console.error("Failed to get balance:", err)
setError(err.message || "Failed to get balance")
Expand Down Expand Up @@ -107,7 +107,7 @@ export default function WalletPage() {

// Wait for confirmation
await transaction.wait()
setTxLink(`${CHAIN_INFO.blockExplorerUrls[0]}/tx/${transaction.txHash}`) // ← set explorer link
setTxLink(`${CHAIN_INFO.blockExplorerUrls[0]}/tx/${transaction.transactionHash}`) // ← set explorer link

// Update balance
await getBalance()
Expand Down Expand Up @@ -243,3 +243,22 @@ export default function WalletPage() {
</main>
)
}

// Minimal helpers for ETH denominations (18 decimals)
const WEI_PER_ETHER = 10n ** 18n
function parseEther(value: number | string): bigint {
const str = typeof value === 'number' ? value.toString() : value
if (!str.includes('.')) return BigInt(str) * WEI_PER_ETHER
const [whole, fracRaw] = str.split('.')
const frac = (fracRaw || '').slice(0, 18).padEnd(18, '0')
return BigInt(whole || '0') * WEI_PER_ETHER + BigInt(frac || '0')
}

function formatEther(wei: bigint): string {
const negative = wei < 0n
const n = negative ? -wei : wei
const whole = n / WEI_PER_ETHER
const frac = n % WEI_PER_ETHER
const fracStr = frac.toString().padStart(18, '0').replace(/0+$/, '')
return `${negative ? '-' : ''}${whole.toString()}${fracStr ? '.' + fracStr : ''}`
}
2 changes: 1 addition & 1 deletion examples/ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@interchain-kit/metamask-extension": "0.3.39",
"@interchain-kit/react": "0.3.39",
"@interchain-ui/react": "^1.26.3",
"@interchainjs/ethereum": "1.11.9",
"@interchainjs/ethereum": "^1.17.4",
"@keplr-wallet/types": "^0.12.221",
"@metamask/providers": "^22.0.0",
"autoprefixer": "^10.4.20",
Expand Down
2 changes: 1 addition & 1 deletion examples/ethereum/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"target": "ES6",
"target": "es2016",
"skipLibCheck": true,
"strict": true,
"noEmit": true,
Expand Down
Loading
Loading