

import { callRpc } from '../../core/rpc';
import { Transaction } from '../../types';
import {
  isSystemContract as registryIsSystemContract,
  AddressType
} from '../../utils/registry';

  /**
   * 
   * @param value 
   * @returns 
   */
export function isTransactionObject(value: string | Transaction): value is Transaction {
  return typeof value !== 'string' && value !== null && typeof value === 'object' && 'hash' in value;
}

/**
 * 
 * @param address 
 * @returns 
 */
export function isSystemContract(address: string): boolean {
  return registryIsSystemContract(address.toLowerCase());
}

/**
 * 
 * @param {string} address 
 * @returns {Promise<boolean>} 
 */
export async function checkIsContract(address: string): Promise<boolean> {
  try {
    const code = await callRpc<string>('eth_getCode', [address, 'latest']);
    return code !== '0x' && code !== '0x0';
  } catch (error) {
    console.error(`Error checking if ${address} is a contract:`, error);
    return false;
  }
}

/**
 * 
 * @param {string} address 
 * @returns {Promise<string>} 
 */
export async function getContractBytecode(address: string): Promise<string> {
  try {
    const code = await callRpc<string>('eth_getCode', [address, 'latest']);
    return code;
  } catch (error) {
    console.error(`Error getting bytecode for ${address}:`, error);
    throw error;
  }
} 