Skip to content
Closed
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
44 changes: 42 additions & 2 deletions src/services/chain-adapter/evm/asset-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ interface JsonRpcResponse<T> {
error?: { code: number; message: string }
}

const LOG_PREFIX = '[EvmAssetService]'

export class EvmAssetService implements IAssetService {
private readonly config: ChainConfig
private readonly rpcUrl: string

constructor(config: ChainConfig) {
this.config = config
this.rpcUrl = DEFAULT_RPC_URLS[config.id] ?? config.api?.url ?? DEFAULT_RPC_URLS['ethereum']!

if (import.meta.env.DEV) {
console.debug(`${LOG_PREFIX} Initialized for chain=${config.id}, rpcUrl=${this.rpcUrl}`)
}
}

private async rpc<T>(method: string, params: unknown[]): Promise<T> {
Expand Down Expand Up @@ -60,13 +66,30 @@ export class EvmAssetService implements IAssetService {

async getNativeBalance(address: Address): Promise<Balance> {
try {
if (import.meta.env.DEV) {
console.debug(`${LOG_PREFIX} getNativeBalance: chain=${this.config.id}, address=${address}, rpc=${this.rpcUrl}`)
}

const hexBalance = await this.rpc<string>('eth_getBalance', [address, 'latest'])
const balance = BigInt(hexBalance).toString()

if (import.meta.env.DEV) {
console.debug(`${LOG_PREFIX} getNativeBalance success: chain=${this.config.id}, balance=${balance}`)
}

return {
amount: Amount.fromRaw(balance, this.config.decimals, this.config.symbol),
symbol: this.config.symbol,
}
} catch {
} catch (error) {
// Log the error for debugging - this was previously silently swallowed
console.error(`${LOG_PREFIX} getNativeBalance failed:`, {
chain: this.config.id,
address,
rpcUrl: this.rpcUrl,
error: error instanceof Error ? error.message : String(error),
})

return {
amount: Amount.fromRaw('0', this.config.decimals, this.config.symbol),
symbol: this.config.symbol,
Expand All @@ -76,15 +99,32 @@ export class EvmAssetService implements IAssetService {

async getTokenBalance(address: Address, tokenAddress: Address): Promise<Balance> {
try {
if (import.meta.env.DEV) {
console.debug(`${LOG_PREFIX} getTokenBalance: chain=${this.config.id}, address=${address}, token=${tokenAddress}`)
}

// ERC20 balanceOf(address) selector: 0x70a08231
const data = `0x70a08231000000000000000000000000${address.slice(2).toLowerCase()}`
const hexBalance = await this.rpc<string>('eth_call', [{ to: tokenAddress, data }, 'latest'])
const balance = BigInt(hexBalance).toString()

if (import.meta.env.DEV) {
console.debug(`${LOG_PREFIX} getTokenBalance success: chain=${this.config.id}, token=${tokenAddress}, balance=${balance}`)
}

return {
amount: Amount.fromRaw(balance, 18, 'TOKEN'),
symbol: 'TOKEN',
}
} catch {
} catch (error) {
console.error(`${LOG_PREFIX} getTokenBalance failed:`, {
chain: this.config.id,
address,
tokenAddress,
rpcUrl: this.rpcUrl,
error: error instanceof Error ? error.message : String(error),
})

return {
amount: Amount.fromRaw('0', 18, 'UNKNOWN'),
symbol: 'UNKNOWN',
Expand Down