class TrustEventMonitor {
constructor(contract) {
this.contract = contract;
this.attestations = new Map();
this.schemas = new Map();
this.trustScores = new Map();
this.setupListeners();
}
setupListeners() {
// Monitor attestations
this.contract.on('Attested', (recipient, attester, uid, schema, event) => {
this.handleAttested(recipient, attester, uid, schema, event);
});
// Monitor revocations
this.contract.on('Revoked', (recipient, attester, uid, schema, event) => {
this.handleRevoked(recipient, attester, uid, schema, event);
});
// Monitor schemas
this.contract.on('SchemaRegistered', (uid, registerer, event) => {
this.handleSchemaRegistered(uid, registerer, event);
});
// Monitor trust scores
this.contract.on('TrustScoreUpdated', (subject, oldScore, newScore, event) => {
this.handleTrustScoreUpdated(subject, oldScore, newScore, event);
});
}
handleAttested(recipient, attester, uid, schema, event) {
const attestation = {
recipient,
attester,
uid,
schema,
blockNumber: event.blockNumber,
timestamp: event.args.timestamp
};
// Store attestation
this.attestations.set(uid, attestation);
// Update recipient's attestation list
if (!this.recipientAttestations.has(recipient)) {
this.recipientAttestations.set(recipient, []);
}
this.recipientAttestations.get(recipient).push(uid);
console.log(`✅ New attestation: ${uid.slice(0, 10)}... for ${recipient.slice(0, 10)}...`);
// Trigger analytics update
this.updateAnalytics(recipient);
}
handleRevoked(recipient, attester, uid, schema, event) {
const attestation = this.attestations.get(uid);
if (attestation) {
attestation.revoked = true;
attestation.revokedAt = event.blockNumber;
}
console.log(`❌ Attestation revoked: ${uid.slice(0, 10)}...`);
// Update analytics
this.updateAnalytics(recipient);
}
handleSchemaRegistered(uid, registerer, event) {
this.schemas.set(uid, {
uid,
registerer,
blockNumber: event.blockNumber
});
console.log(`📋 New schema registered: ${uid.slice(0, 10)}...`);
}
handleTrustScoreUpdated(subject, oldScore, newScore, event) {
this.trustScores.set(subject, {
score: newScore.toString(),
previousScore: oldScore.toString(),
updatedAt: event.blockNumber
});
const change = newScore.sub(oldScore);
const direction = change.isNegative() ? '📉' : '📈';
console.log(`${direction} Trust score updated for ${subject.slice(0, 10)}...`);
console.log(` Old: ${oldScore.toString()}, New: ${newScore.toString()}`);
}
updateAnalytics(address) {
// Calculate analytics
const attestations = this.recipientAttestations.get(address) || [];
const activeAttestations = attestations.filter(uid => {
const att = this.attestations.get(uid);
return att && !att.revoked;
});
console.log(`📊 ${address.slice(0, 10)}... has ${activeAttestations.length} active attestations`);
}
getStats() {
return {
totalAttestations: this.attestations.size,
totalSchemas: this.schemas.size,
trackedUsers: this.trustScores.size
};
}
getRecipientStats(address) {
const attestations = this.recipientAttestations.get(address) || [];
const active = attestations.filter(uid => {
const att = this.attestations.get(uid);
return att && !att.revoked;
});
return {
total: attestations.length,
active: active.length,
revoked: attestations.length - active.length,
trustScore: this.trustScores.get(address)?.score || '0'
};
}
}
// Usage
const monitor = new TrustEventMonitor(contract);
// Get overall stats
console.log('Trust Registry Stats:', monitor.getStats());
// Get stats for specific user
const userStats = monitor.getRecipientStats('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb');
console.log('User Stats:', userStats);