web3agent-aof
Version:
AI Framework for Onchain Operations and DeFi Interactions
125 lines • 4.78 kB
JavaScript
import { EventEmitter } from 'events';
export class EVMAgent extends EventEmitter {
provider;
wallet;
priceMonitors;
healthFactorMonitors;
constructor(provider, wallet) {
super();
this.provider = provider;
this.wallet = wallet;
this.priceMonitors = new Map();
this.healthFactorMonitors = new Map();
}
monitorPriceChange(token, callback) {
if (this.priceMonitors.has(token)) {
clearInterval(this.priceMonitors.get(token));
}
this.getTokenPrice(token).then(lastPrice => {
const interval = setInterval(async () => {
try {
const currentPrice = await this.getTokenPrice(token);
const priceChange = ((currentPrice - lastPrice) / lastPrice) * 100;
callback({
token,
price: currentPrice,
previousPrice: lastPrice,
percentageChange: priceChange
});
lastPrice = currentPrice;
}
catch (error) {
console.error(`Error monitoring price for ${token}:`, error);
}
}, 60000);
this.priceMonitors.set(token, interval);
});
}
monitorHealthFactor(protocol, callback) {
if (this.healthFactorMonitors.has(protocol)) {
clearInterval(this.healthFactorMonitors.get(protocol));
}
this.getHealthFactor(protocol).then(lastHealthFactor => {
const interval = setInterval(async () => {
try {
const currentHealthFactor = await this.getHealthFactor(protocol);
callback({
protocol,
healthFactor: currentHealthFactor,
previousHealthFactor: lastHealthFactor
});
lastHealthFactor = currentHealthFactor;
}
catch (error) {
console.error(`Error monitoring health factor for ${protocol}:`, error);
}
}, 60000);
this.healthFactorMonitors.set(protocol, interval);
});
}
stopPriceMonitoring(token) {
const interval = this.priceMonitors.get(token);
if (interval) {
clearInterval(interval);
this.priceMonitors.delete(token);
}
}
stopHealthFactorMonitoring(protocol) {
const interval = this.healthFactorMonitors.get(protocol);
if (interval) {
clearInterval(interval);
this.healthFactorMonitors.delete(protocol);
}
}
stopAllMonitoring() {
this.priceMonitors.forEach(interval => clearInterval(interval));
this.healthFactorMonitors.forEach(interval => clearInterval(interval));
this.priceMonitors.clear();
this.healthFactorMonitors.clear();
}
async startPriceMonitoring(token, callback) {
let lastPrice = await this.getTokenPrice(token);
return setInterval(async () => {
try {
const currentPrice = await this.getTokenPrice(token);
const priceChange = ((currentPrice - lastPrice) / lastPrice) * 100;
callback({
token,
price: currentPrice,
previousPrice: lastPrice,
percentageChange: priceChange
});
lastPrice = currentPrice;
}
catch (error) {
console.error(`Error monitoring price for ${token}:`, error);
}
}, 60000); // Check every minute
}
async startHealthFactorMonitoring(protocol, callback) {
let lastHealthFactor = await this.getHealthFactor(protocol);
return setInterval(async () => {
try {
const currentHealthFactor = await this.getHealthFactor(protocol);
callback({
protocol,
healthFactor: currentHealthFactor,
previousHealthFactor: lastHealthFactor
});
lastHealthFactor = currentHealthFactor;
}
catch (error) {
console.error(`Error monitoring health factor for ${protocol}:`, error);
}
}, 60000); // Check every minute
}
async getTokenPrice(token) {
// Implementation using Chainlink price feeds
return 0; // Placeholder
}
async getHealthFactor(protocol) {
// Implementation using protocol-specific methods (e.g., Aave)
return 0; // Placeholder
}
}
//# sourceMappingURL=EVMAgent.js.map