1
- import {
2
- AlmanacRecord ,
3
- AlmanacResolution ,
4
- Block ,
5
- Contract ,
6
- Event , EventAttribute ,
7
- InstantiateContractMessage ,
8
- Interface ,
9
- } from "../../types" ;
10
- import { parseAttributes , WasmdEventAttributesI } from "../utils" ;
1
+ import { AlmanacRecord , AlmanacResolution , Contract , Interface , } from "../../types" ;
11
2
12
3
export async function cacheAlmanacResolution ( contractId : string , agentId : string , record : AlmanacRecord ) : Promise < void > {
13
4
try {
@@ -25,51 +16,15 @@ export async function cacheAlmanacResolution(contractId: string, agentId: string
25
16
}
26
17
27
18
export async function expireAlmanacResolutionsRelativeToHeight ( height : bigint ) : Promise < void > {
28
- // TODO: optimize with custom `Store` methods exposing more sequelize functionality.
29
- const almanacContracts = await store . getByField ( "Contract" , "interface" , Interface . MicroAgentAlmanac ) as Contract [ ] ;
30
-
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
- }
39
-
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
- }
51
-
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
- }
72
- }
19
+ // NB: resolution, record, and registration ID are the same across related entities.
20
+ const expiringResolutionIdsSql = `SELECT r.id
21
+ FROM app.almanac_registrations r
22
+ WHERE r.expiry_height <= ${ height }
23
+ ` ;
24
+ const expiringResolutionIds = await store . selectRaw ( expiringResolutionIdsSql ) ;
25
+
26
+ // NB: will throw an error if any promise rejects.
27
+ await Promise . all ( expiringResolutionIds . map ( r => expireAlmanacResolution ( String ( r . id ) ) ) ) ;
73
28
}
74
29
75
30
export async function expireAlmanacResolution ( id : string ) : Promise < void > {
0 commit comments