UNPKG

synkrokonn-dev

Version:

Plugin-based cross-chain orchestration middleware for Web3 enterprise automation.

34 lines (33 loc) 1.48 kB
import { ethers } from 'ethers'; export class EthereumAdapter { constructor(rpcUrl, privateKey) { if (!privateKey || !privateKey.match(/^([0-9a-fA-F]{64})$/)) { throw new Error("Invalid or missing PRIVATE_KEY"); } console.log("PRIVATE_KEY loaded (first 8 chars):", privateKey.slice(0, 8)); this.provider = new ethers.JsonRpcProvider(rpcUrl); this.wallet = new ethers.Wallet(privateKey, this.provider); } async getAccountAddress() { return await this.wallet.getAddress(); } async sendTransaction(contractAddress, functionName, args) { const abi = [`function ${functionName}(${args.types.join(',')})`]; const contract = new ethers.Contract(contractAddress, abi, this.wallet); const isReadOnly = functionName.toLowerCase().startsWith("get") || functionName.toLowerCase().startsWith("read"); if (isReadOnly) { const result = await contract.callStatic[functionName](...args.values); console.log("Read-only result:", result); return result; } const tx = await contract[functionName](...args.values); await tx.wait(); return tx.hash; } async listenToEvents(contractAddress, abi, callback) { const contract = new ethers.Contract(contractAddress, abi, this.wallet); contract.on("*", callback); console.log(`Listening to events from ${contractAddress}`); } }