1
- import { AlmanacRecord , AlmanacResolution , Block , Event , Transaction } from "../../types" ;
1
+ import {
2
+ AlmanacRecord ,
3
+ AlmanacResolution ,
4
+ Block ,
5
+ Contract ,
6
+ Event , EventAttribute ,
7
+ InstantiateContractMessage ,
8
+ Interface ,
9
+ } from "../../types" ;
2
10
import { parseAttributes , WasmdEventAttributesI } from "../utils" ;
3
11
4
12
export async function cacheAlmanacResolution ( contractId : string , agentId : string , record : AlmanacRecord ) : Promise < void > {
5
13
try {
6
- logger . info ( `[cacheAlmanacResolution] (recordId ${ record . id } ): caching record` ) ;
14
+ logger . info ( `[cacheAlmanacResolution] (recordId: ${ record . id } ): caching record` ) ;
7
15
const resolutionEntity = AlmanacResolution . create ( {
8
16
id : record . id ,
9
17
agentId,
@@ -17,39 +25,59 @@ export async function cacheAlmanacResolution(contractId: string, agentId: string
17
25
}
18
26
19
27
export async function expireAlmanacResolutionsRelativeToHeight ( height : bigint ) : Promise < void > {
20
- /* for contract in almanac contracts {
21
- * expiryHeight = contract.expiryHeight
22
- * }
23
- */
24
-
25
- const blocks = await Block . getByHeight ( BigInt ( height ) ) ;
26
- // const blocks = await Block.getByHeight(BigInt(expiryHeight));
27
- if ( ! blocks || blocks . length !== 1 ) {
28
- logger . error ( `[expireAlmanacResolutionAtHeight] (height: ${ height } ): found ${ blocks ?. length ?? 0 } blocks at that height` ) ;
29
- }
28
+ // TODO: optimize with custom `Store` methods exposing more sequelize functionality.
29
+ const almanacContracts = await store . getByField ( "Contract" , "interface" , Interface . MicroAgentAlmanac ) as Contract [ ] ;
30
30
31
- const events = await Event . getByBlockId ( blocks [ 0 ] . id ) ;
32
- if ( ! events || events . length !== 1 ) {
33
- logger . error ( `[expireAlmanacResolutionAtHeight] (height: ${ height } ): found ${ events ?. length ?? 0 } events at that height` ) ;
34
- }
31
+ for ( const contract of almanacContracts ) {
32
+ const instantiateMsg = await InstantiateContractMessage . get ( contract . instantiateMessageId ) ;
33
+ try {
34
+ const { expiry_height} = JSON . parse ( instantiateMsg . payload ) ;
35
+ if ( ! expiry_height ) {
36
+ logger . warn ( `[expireAlmanacResolutionsRelativeToHeight] (height: ${ height } ): falsey expiry_height: ${ expiry_height } ` ) ;
37
+ continue ;
38
+ }
35
39
36
- for ( const event of events ) {
37
- const attributes = parseAttributes < WasmdEventAttributesI > ( event . attributes ) ;
38
- if ( ! attributes . action || attributes . action !== "register" ) {
39
- continue ;
40
- }
40
+ const blocks = await Block . getByHeight ( BigInt ( expiry_height ) ) ;
41
+ if ( ! blocks || blocks . length !== 1 ) {
42
+ logger . error ( `[expireAlmanacResolutionAtHeight] (height: ${ height } ): found ${ blocks ?. length ?? 0 } blocks at that height` ) ;
43
+ continue ;
44
+ }
45
+
46
+ const events = await Event . getByBlockId ( blocks [ 0 ] . id ) ;
47
+ if ( ! events || events . length !== 1 ) {
48
+ logger . error ( `[expireAlmanacResolutionAtHeight] (height: ${ height } ): found ${ events ?. length ?? 0 } events at that height` ) ;
49
+ continue ;
50
+ }
41
51
42
- // NB: recordId is equivalent to ID of register event.
43
- await expireAlmanacResolution ( event . id ) ;
52
+ // TODO: consider relating EventAttributes to Blocks to reduce # of serial lookups.
53
+ for ( const event of events ) {
54
+ const eventAttributes = await EventAttribute . getByEventId ( event . id ) ;
55
+ if ( ! events || events . length !== 1 ) {
56
+ logger . error ( `[expireAlmanacResolutionAtHeight] (height: ${ height } ): found ${ eventAttributes ?. length ?? 0 } event attributes with event ID: ${ event . id } ` ) ;
57
+ continue ;
58
+ }
59
+
60
+ const attributes = parseAttributes < WasmdEventAttributesI > ( eventAttributes ) ;
61
+ if ( ! attributes . action || attributes . action !== "register" ) {
62
+ logger . warn ( `[expireAlmanacResolutionsRelativeToHeight] (height: ${ height } ): expected "register" action, got: ${ attributes . action } ` ) ;
63
+ continue ;
64
+ }
65
+
66
+ // NB: resolution ID is equivalent to register event ID.
67
+ await expireAlmanacResolution ( event . id ) ;
68
+ }
69
+ } catch ( error ) {
70
+ logger . warn ( `[expireAlmanacResolutionsRelativeToHeight] (height: ${ height } ): unable to parse instantiate message payload` ) ;
71
+ }
44
72
}
45
73
}
46
74
47
- export async function expireAlmanacResolution ( recordId : string ) : Promise < void > {
75
+ export async function expireAlmanacResolution ( id : string ) : Promise < void > {
48
76
try {
49
- logger . info ( `[expireAlmanacResolution] (recordId ${ recordId } ): expiring record` ) ;
50
- await AlmanacResolution . remove ( recordId ) ;
77
+ logger . info ( `[expireAlmanacResolution] (recordId: ${ id } ): expiring record` ) ;
78
+ await AlmanacResolution . remove ( id ) ;
51
79
} catch ( error ) {
52
- logger . warn ( `[expireAlmanacResolution] (recordId: ${ recordId } ): ${ error . stack } ` ) ;
80
+ logger . warn ( `[expireAlmanacResolution] (recordId: ${ id } ): ${ error . stack } ` ) ;
53
81
}
54
82
}
55
83
0 commit comments