11import type { AlertRule , RendererRecord , TemplateFieldFormat , TemplateRendererConfig } from '../alert-config'
22import { transactionLink } from '../utils/links'
3- import { explorerUrl , formatAddress , formatValue } from './format '
3+ import { severityColors } from './const '
44import { notifyDiscord } from './discord'
55import type { EventRendererParams } from './event/event'
6+ import { formatAddress , formatValue } from './format'
67import type { NotifyForTraceInput } from './trace'
7- import { severityColors } from './const'
88
99type TemplateContext = {
1010 decoded : Record < string , unknown >
@@ -43,11 +43,14 @@ export const createEventTemplateRenderer =
4343 interpolateTemplate ( config . titleTemplate , { decoded, meta, rule : toRuleTemplateContext ( rule ) } ) ??
4444 `${ params . name ?? rule . displayName ?? rule . topic } - ${ params . eventName } ` ,
4545 url : transactionLink ( params . log . transactionHash , params . ctx . chain ) ,
46- description : interpolateTemplate ( config . descriptionTemplate , {
47- decoded,
48- meta,
49- rule : toRuleTemplateContext ( rule ) ,
50- } ) ,
46+ description :
47+ interpolateTemplate ( config . descriptionTemplate , {
48+ decoded,
49+ meta,
50+ rule : toRuleTemplateContext ( rule ) ,
51+ } ) ??
52+ rule . description ??
53+ undefined ,
5154 fields : buildFields ( config , { decoded, meta, rule : toRuleTemplateContext ( rule ) } , params . ctx . chain ) ,
5255 } ,
5356 ] ,
@@ -88,12 +91,17 @@ export const createTraceTemplateRenderer =
8891 title :
8992 interpolateTemplate ( config . titleTemplate , { decoded, meta, rule : toRuleTemplateContext ( rule ) } ) ??
9093 `${ input . name ?? rule . displayName ?? rule . topic } - ${ input . functionName ?? input . trace . type } ` ,
91- url : input . trace . transaction ?. hash ? transactionLink ( input . trace . transaction . hash , input . ctx . chain ) : undefined ,
92- description : interpolateTemplate ( config . descriptionTemplate , {
93- decoded,
94- meta,
95- rule : toRuleTemplateContext ( rule ) ,
96- } ) ,
94+ url : input . trace . transaction ?. hash
95+ ? transactionLink ( input . trace . transaction . hash , input . ctx . chain )
96+ : undefined ,
97+ description :
98+ interpolateTemplate ( config . descriptionTemplate , {
99+ decoded,
100+ meta,
101+ rule : toRuleTemplateContext ( rule ) ,
102+ } ) ??
103+ rule . description ??
104+ undefined ,
97105 fields : buildFields ( config , { decoded, meta, rule : toRuleTemplateContext ( rule ) } , input . ctx . chain ) ,
98106 } ,
99107 ] ,
@@ -108,7 +116,11 @@ function normalizeTemplateConfig(configJson: RendererRecord['configJson']): Temp
108116 return { }
109117}
110118
111- function buildFields ( config : TemplateRendererConfig , context : TemplateContext , chain : EventRendererParams [ 'ctx' ] [ 'chain' ] ) {
119+ function buildFields (
120+ config : TemplateRendererConfig ,
121+ context : TemplateContext ,
122+ chain : EventRendererParams [ 'ctx' ] [ 'chain' ] ,
123+ ) {
112124 return ( config . fields ?? [ ] )
113125 . map ( ( field ) => {
114126 const rawValue = getPathValue ( context , field . path )
@@ -123,7 +135,11 @@ function buildFields(config: TemplateRendererConfig, context: TemplateContext, c
123135 . slice ( 0 , 25 )
124136}
125137
126- function formatTemplateField ( value : unknown , format : TemplateFieldFormat , chain : EventRendererParams [ 'ctx' ] [ 'chain' ] ) : string {
138+ function formatTemplateField (
139+ value : unknown ,
140+ format : TemplateFieldFormat ,
141+ chain : EventRendererParams [ 'ctx' ] [ 'chain' ] ,
142+ ) : string {
127143 if ( value == null ) return '_null_'
128144 if ( format === 'timestamp' ) {
129145 const timestamp = typeof value === 'number' ? value : Number ( value )
@@ -145,7 +161,9 @@ function formatTemplateField(value: unknown, format: TemplateFieldFormat, chain:
145161
146162function interpolateTemplate ( template : string | undefined , context : TemplateContext ) : string | undefined {
147163 if ( ! template ?. trim ( ) ) return undefined
148- return template . replace ( / \{ \{ \s * ( [ ^ } ] + ?) \s * \} \} / g, ( _match , path ) => stringifyTemplateValue ( getPathValue ( context , path ) ) )
164+ return template . replace ( / \{ \{ \s * ( [ ^ } ] + ?) \s * \} \} / g, ( _match , path ) =>
165+ stringifyTemplateValue ( getPathValue ( context , path ) ) ,
166+ )
149167}
150168
151169function stringifyTemplateValue ( value : unknown ) : string {
@@ -171,10 +189,7 @@ function isEmpty(value: unknown): boolean {
171189 return value == null || value === '' || ( Array . isArray ( value ) && value . length === 0 )
172190}
173191
174- function safeDecode (
175- event : EventRendererParams [ 'event' ] ,
176- log : EventRendererParams [ 'log' ] ,
177- ) : Record < string , unknown > {
192+ function safeDecode ( event : EventRendererParams [ 'event' ] , log : EventRendererParams [ 'log' ] ) : Record < string , unknown > {
178193 try {
179194 const decoded = event . decode ( log )
180195 if ( decoded && typeof decoded === 'object' && ! Array . isArray ( decoded ) ) {
0 commit comments