Skip to content

Commit f3a3d52

Browse files
authored
Merge pull request #55 from hyperweb-io/eth-browser
Eth browser
2 parents 172bb3c + aa12451 commit f3a3d52

File tree

4 files changed

+281
-136
lines changed

4 files changed

+281
-136
lines changed

examples/ethereum/app/page.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Box, Button, TextField, NumberField, FieldLabel, Callout } from "@inter
44
import React, { useState, useEffect } from "react"
55
import { Wallet, ArrowRight, RefreshCw, AlertCircle } from "lucide-react"
66
import { SignerFromBrowser } from "@interchainjs/ethereum/signers/SignerFromBrowser"
7-
import { parseEther, formatEther } from "@interchainjs/ethereum/utils/denominations"
87
import { MetaMaskInpageProvider } from "@metamask/providers";
98
import { useChain } from '@interchain-kit/react'
109
import { WalletState } from "@interchain-kit/core"
@@ -64,14 +63,15 @@ export default function WalletPage() {
6463
if (!ethereum) return
6564
try {
6665
console.log('ethereum in getBalance:', ethereum)
67-
const wallet = new SignerFromBrowser(
68-
ethereum!
69-
// window.ethereum as EthereumProvider
70-
)
71-
console.log('wallet in getBalance:', wallet)
72-
const balance = await wallet.getBalance()
73-
console.log('balance in getBalance:', balance)
74-
setBalance(formatEther(balance))
66+
// Use EIP-1193 provider directly to fetch balance
67+
const addr = account
68+
if (!addr) throw new Error('No connected account')
69+
const hexBalance = await (ethereum as any).request({
70+
method: 'eth_getBalance',
71+
params: [addr, 'latest']
72+
}) as string
73+
const wei = BigInt(hexBalance)
74+
setBalance(formatEther(wei))
7575
} catch (err: any) {
7676
console.error("Failed to get balance:", err)
7777
setError(err.message || "Failed to get balance")
@@ -107,7 +107,7 @@ export default function WalletPage() {
107107

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

112112
// Update balance
113113
await getBalance()
@@ -243,3 +243,22 @@ export default function WalletPage() {
243243
</main>
244244
)
245245
}
246+
247+
// Minimal helpers for ETH denominations (18 decimals)
248+
const WEI_PER_ETHER = 10n ** 18n
249+
function parseEther(value: number | string): bigint {
250+
const str = typeof value === 'number' ? value.toString() : value
251+
if (!str.includes('.')) return BigInt(str) * WEI_PER_ETHER
252+
const [whole, fracRaw] = str.split('.')
253+
const frac = (fracRaw || '').slice(0, 18).padEnd(18, '0')
254+
return BigInt(whole || '0') * WEI_PER_ETHER + BigInt(frac || '0')
255+
}
256+
257+
function formatEther(wei: bigint): string {
258+
const negative = wei < 0n
259+
const n = negative ? -wei : wei
260+
const whole = n / WEI_PER_ETHER
261+
const frac = n % WEI_PER_ETHER
262+
const fracStr = frac.toString().padStart(18, '0').replace(/0+$/, '')
263+
return `${negative ? '-' : ''}${whole.toString()}${fracStr ? '.' + fracStr : ''}`
264+
}

examples/ethereum/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@interchain-kit/metamask-extension": "0.3.39",
1818
"@interchain-kit/react": "0.3.39",
1919
"@interchain-ui/react": "^1.26.3",
20-
"@interchainjs/ethereum": "1.11.9",
20+
"@interchainjs/ethereum": "^1.17.4",
2121
"@keplr-wallet/types": "^0.12.221",
2222
"@metamask/providers": "^22.0.0",
2323
"autoprefixer": "^10.4.20",

examples/ethereum/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"lib": ["dom", "dom.iterable", "esnext"],
44
"allowJs": true,
5-
"target": "ES6",
5+
"target": "es2016",
66
"skipLibCheck": true,
77
"strict": true,
88
"noEmit": true,

0 commit comments

Comments
 (0)