Skip to content
Merged
Show file tree
Hide file tree
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
85 changes: 85 additions & 0 deletions src/modules/treasury/policies/alerts/alert-generator.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { AlertSeverity } from '../enums';
import { PolicyViolationEntity } from '../entities/policy-violation.entity';

export interface AlertData {
policyName: string;
walletAddress: string;
chainId: number;
transactionHash: string;
violatedRule: string;
thresholdValue: string;
observedValue: string;
severity: AlertSeverity;
recommendedAction?: string;
contextData?: Record<string, unknown>;
recipientAddress?: string;
transactionAmount?: string;
}

export function generateAlertForViolation(violation: PolicyViolationEntity): AlertData {
return {
policyName: violation.policyName,
walletAddress: violation.walletAddress,
chainId: violation.chainId,
transactionHash: violation.transactionHash,
violatedRule: violation.violatedRule,
thresholdValue: violation.thresholdValue,
observedValue: violation.observedValue,
severity: violation.severity as AlertSeverity,
recommendedAction: violation.recommendedAction || undefined,
contextData: violation.contextData || undefined,
recipientAddress: violation.recipientAddress || undefined,
transactionAmount: violation.transactionAmount || undefined,
};
}

export function formatAlertMessage(alert: AlertData): string {
const severityEmoji = getSeverityEmoji(alert.severity);
const title = `${severityEmoji} Treasury Policy Violation: ${alert.policyName}`;

const details = [
`Wallet: ${alert.walletAddress}`,
`Chain: ${alert.chainId}`,
`Transaction: ${alert.transactionHash}`,
`Violated Rule: ${alert.violatedRule}`,
`Threshold: ${alert.thresholdValue}`,
`Observed: ${alert.observedValue}`,
];

if (alert.recipientAddress) {
details.push(`Recipient: ${alert.recipientAddress}`);
}

if (alert.transactionAmount) {
details.push(`Amount: ${alert.transactionAmount}`);
}

if (alert.recommendedAction) {
details.push(`Recommended Action: ${alert.recommendedAction}`);
}

return `${title}\n${details.join('\n')}`;
}

export function getSeverityEmoji(severity: AlertSeverity): string {
switch (severity) {
case AlertSeverity.Critical:
return '🚨';
case AlertSeverity.High:
return '⚠️';
case AlertSeverity.Medium:
return '🔶';
case AlertSeverity.Low:
return '🔵';
case AlertSeverity.Info:
return 'ℹ️';
default:
return '⚠️';
}
}

export function getAlertSubject(alert: AlertData): string {
const severity = alert.severity;
const emoji = getSeverityEmoji(severity);
return `${emoji} Treasury Alert: ${alert.violatedRule} on ${alert.walletAddress}`;
}
1 change: 1 addition & 0 deletions src/modules/treasury/policies/alerts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './alert-generator.util';
19 changes: 19 additions & 0 deletions src/modules/treasury/policies/dto/create-treasury-policy.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PolicyType, PolicyStatus, AlertSeverity, ThresholdType } from '../enums';

export class CreateTreasuryPolicyDto {
policyName!: string;
description?: string;
policyType!: PolicyType;
status?: PolicyStatus;
walletAddress!: string;
chainId!: number;
thresholdType!: ThresholdType;
thresholdValue!: string;
thresholdConfig?: Record<string, unknown>;
severity?: AlertSeverity;
notificationPreferences?: Record<string, unknown>;
approvedAddresses?: string[];
businessHoursStart?: string;
businessHoursEnd?: string;
timeZone?: string;
}
21 changes: 21 additions & 0 deletions src/modules/treasury/policies/dto/create-violation.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AlertSeverity, ViolationStatus } from '../enums';

export class CreateViolationDto {
policyId!: string;
policyName!: string;
walletAddress!: string;
chainId!: number;
transactionHash!: string;
transactionBlock?: string;
violatedRule!: string;
thresholdValue!: string;
observedValue!: string;
transactionAmount?: string;
recipientAddress?: string;
balanceBefore?: string;
balanceAfter?: string;
severity!: AlertSeverity;
status?: ViolationStatus;
recommendedAction?: string;
contextData?: Record<string, unknown>;
}
5 changes: 5 additions & 0 deletions src/modules/treasury/policies/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './create-treasury-policy.dto';
export * from './update-treasury-policy.dto';
export * from './treasury-policy-query.dto';
export * from './create-violation.dto';
export * from './violation-query.dto';
10 changes: 10 additions & 0 deletions src/modules/treasury/policies/dto/treasury-policy-query.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PolicyType, PolicyStatus } from '../enums';

export class TreasuryPolicyQueryDto {
walletAddress?: string;
chainId?: number;
policyType?: PolicyType;
status?: PolicyStatus;
limit?: number;
offset?: number;
}
17 changes: 17 additions & 0 deletions src/modules/treasury/policies/dto/update-treasury-policy.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PolicyType, PolicyStatus, AlertSeverity, ThresholdType } from '../enums';

export class UpdateTreasuryPolicyDto {
policyName?: string;
description?: string;
policyType?: PolicyType;
status?: PolicyStatus;
thresholdType?: ThresholdType;
thresholdValue?: string;
thresholdConfig?: Record<string, unknown>;
severity?: AlertSeverity;
notificationPreferences?: Record<string, unknown>;
approvedAddresses?: string[];
businessHoursStart?: string;
businessHoursEnd?: string;
timeZone?: string;
}
14 changes: 14 additions & 0 deletions src/modules/treasury/policies/dto/violation-query.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ViolationStatus, AlertSeverity } from '../enums';

export class ViolationQueryDto {
policyId?: string;
walletAddress?: string;
chainId?: number;
transactionHash?: string;
status?: ViolationStatus;
severity?: AlertSeverity;
fromDate?: Date;
toDate?: Date;
limit?: number;
offset?: number;
}
3 changes: 3 additions & 0 deletions src/modules/treasury/policies/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './treasury-policy.entity';
export * from './policy-violation.entity';
export * from './policy-audit-log.entity';
41 changes: 41 additions & 0 deletions src/modules/treasury/policies/entities/policy-audit-log.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Entity, PrimaryGeneratedColumn, Column, Index, CreateDateColumn } from 'typeorm';

@Entity('policy_audit_logs')
@Index(['policyId'])
@Index(['action'])
@Index(['performedBy'])
@Index(['createdAt'])
export class PolicyAuditLogEntity {
@PrimaryGeneratedColumn('uuid')
id!: string;

@Column({ name: 'policy_id' })
policyId!: string;

@Column({ name: 'action' })
action!: string;

@Column({ name: 'performed_by' })
performedBy!: string;

@Column({ name: 'changes', type: 'json', nullable: true })
changes?: Record<string, unknown>;

@Column({ name: 'previous_state', type: 'json', nullable: true })
previousState?: Record<string, unknown>;

@Column({ name: 'new_state', type: 'json', nullable: true })
newState?: Record<string, unknown>;

@Column({ name: 'ip_address', type: 'varchar', length: 45, nullable: true })
ipAddress?: string;

@Column({ name: 'user_agent', type: 'text', nullable: true })
userAgent?: string;

@Column({ name: 'reason', type: 'text', nullable: true })
reason?: string;

@CreateDateColumn({ name: 'created_at' })
createdAt!: Date;
}
106 changes: 106 additions & 0 deletions src/modules/treasury/policies/entities/policy-violation.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
Index,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';
import { ViolationStatus, AlertSeverity } from '../enums';

@Entity('policy_violations')
@Index(['policyId'])
@Index(['walletAddress', 'chainId'])
@Index(['transactionHash'])
@Index(['status'])
@Index(['severity'])
@Index(['createdAt'])
export class PolicyViolationEntity {
@PrimaryGeneratedColumn('uuid')
id!: string;

@Column({ name: 'policy_id' })
policyId!: string;

@Column({ name: 'policy_name' })
policyName!: string;

@Column({ name: 'wallet_address' })
walletAddress!: string;

@Column({ name: 'chain_id' })
chainId!: number;

@Column({ name: 'transaction_hash' })
transactionHash!: string;

@Column({ name: 'transaction_block', type: 'bigint', nullable: true })
transactionBlock?: string;

@Column({ name: 'violated_rule' })
violatedRule!: string;

@Column({ name: 'threshold_value', type: 'decimal', precision: 36, scale: 18 })
thresholdValue!: string;

@Column({ name: 'observed_value', type: 'decimal', precision: 36, scale: 18 })
observedValue!: string;

@Column({ name: 'transaction_amount', type: 'decimal', precision: 36, scale: 18, nullable: true })
transactionAmount?: string;

@Column({ name: 'recipient_address', nullable: true })
recipientAddress?: string;

@Column({ name: 'balance_before', type: 'decimal', precision: 36, scale: 18, nullable: true })
balanceBefore?: string;

@Column({ name: 'balance_after', type: 'decimal', precision: 36, scale: 18, nullable: true })
balanceAfter?: string;

@Column({
type: 'varchar',
enum: AlertSeverity,
})
severity!: AlertSeverity;

@Column({
type: 'varchar',
enum: ViolationStatus,
default: ViolationStatus.Active,
})
status!: ViolationStatus;

@Column({ name: 'recommended_action', type: 'text', nullable: true })
recommendedAction?: string;

@Column({ name: 'context_data', type: 'json', nullable: true })
contextData?: Record<string, unknown>;

@Column({ name: 'acknowledged_by', type: 'varchar', length: 255, nullable: true })
acknowledgedBy?: string;

@Column({ name: 'acknowledged_at', type: 'timestamp', nullable: true })
acknowledgedAt?: Date;

@Column({ name: 'resolved_by', type: 'varchar', length: 255, nullable: true })
resolvedBy?: string;

@Column({ name: 'resolved_at', type: 'timestamp', nullable: true })
resolvedAt?: Date;

@Column({ name: 'resolution_notes', type: 'text', nullable: true })
resolutionNotes?: string;

@Column({ name: 'alert_sent', type: 'boolean', default: false })
alertSent!: boolean;

@Column({ name: 'alert_sent_at', type: 'timestamp', nullable: true })
alertSentAt?: Date;

@CreateDateColumn({ name: 'created_at' })
createdAt!: Date;

@UpdateDateColumn({ name: 'updated_at' })
updatedAt!: Date;
}
Loading
Loading