@@ -10,8 +10,8 @@ const STATUS_ID = '0'
10
10
const METADATA_STATUS_ID = '1'
11
11
const METADATA_DELAY_MIN = 15 // every 24 hours
12
12
const TO_MINUTES = 60_000
13
- const OFFER_STATUS_ID = '2'
14
- // const OFFER_DELAY_MIN = 30 // every 15 minutes
13
+ const SWAP_STATUS_ID = '2'
14
+ const SWAP_DELAY_MIN = 15 // every 15 minutes
15
15
16
16
enum MetadataQuery {
17
17
missing = `SELECT
@@ -64,15 +64,20 @@ enum MetadataQuery {
64
64
` ,
65
65
}
66
66
67
- enum OfferQuery {
68
- expired = `UPDATE
69
- offer oe
67
+ const SwapQuery = {
68
+ expired : ( table : string ) => `UPDATE
69
+ ${ table } oe
70
70
SET status = 'EXPIRED'
71
71
WHERE status = 'ACTIVE'
72
72
AND expiration <= $1
73
73
RETURNING oe.id;`
74
74
}
75
75
76
+ const SWAP_INTERACTION_TO_TABLE_MAP = {
77
+ [ Interaction . SWAP ] : 'swap' ,
78
+ [ Interaction . OFFER ] : 'offer' ,
79
+ }
80
+
76
81
const OPERATION = 'METADATA_CACHE' as any
77
82
78
83
/**
@@ -119,18 +124,21 @@ export async function updateMetadataCache(timestamp: Date, store: Store): Promis
119
124
* @param timestamp - the timestamp of the block
120
125
* @param store - subsquid store to handle the cache
121
126
**/
122
- export async function updateOfferCache ( timestamp : Date , blockNumber : number , store : Store ) : Promise < void > {
123
- const lastUpdate = await getOrCreate ( store , CacheStatus , OFFER_STATUS_ID , { id : OFFER_STATUS_ID , lastBlockTimestamp : new Date ( 0 ) } )
127
+ export async function updateSwapsCache ( timestamp : Date , blockNumber : number , store : Store ) : Promise < void > {
128
+ const lastUpdate = await getOrCreate ( store , CacheStatus , SWAP_STATUS_ID , { id : SWAP_STATUS_ID , lastBlockTimestamp : new Date ( 0 ) } )
124
129
const passedMins = getPassedMinutes ( timestamp , lastUpdate . lastBlockTimestamp )
125
- pending ( Interaction . OFFER , `${ passedMins } MINS SINCE LAST UPDATE` )
126
- if ( passedMins >= DELAY_MIN ) {
130
+ pending ( Interaction . SWAP , `${ passedMins } MINS SINCE LAST UPDATE` )
131
+
132
+ if ( passedMins >= SWAP_DELAY_MIN ) {
127
133
try {
128
- await updateOfferAsExpired ( store , blockNumber )
134
+ for ( const type of [ Interaction . OFFER , Interaction . SWAP ] as const ) {
135
+ await updateSwapAsExpired ( type , blockNumber , store )
136
+ }
129
137
lastUpdate . lastBlockTimestamp = timestamp
130
138
await store . save ( lastUpdate )
131
139
// success('[METADATA CACHE UPDATE]');
132
140
} catch ( e ) {
133
- logError ( e , ( err ) => logger . error ( `[OFFER CACHE UPDATE] ${ err . message } ` ) )
141
+ logError ( e , ( err ) => logger . error ( `[SWAPS CACHE UPDATE] ${ err . message } ` ) )
134
142
}
135
143
}
136
144
}
@@ -179,11 +187,11 @@ async function updateMissingMetadata(store: Store) {
179
187
// logger.info(`[CACHE UPDATE] MISSING METADATA - ${missing.length} NFTs, ${nft.length} NFTs, ${collection.length} Collections`);
180
188
}
181
189
182
- export async function updateOfferAsExpired ( store : Store , blockNumber : string | bigint | number ) : Promise < void > {
190
+ export async function updateSwapAsExpired ( type : Interaction . SWAP | Interaction . OFFER , blockNumber : string | bigint | number , store : Store ) : Promise < void > {
183
191
try {
184
- const rows = await emOf ( store ) . query ( OfferQuery . expired , [ blockNumber ] )
185
- logger . info ( `[OFFERS EXPIRATION POOLER] ${ rows . length } Offers updated` )
192
+ const [ rows ] = await emOf ( store ) . query ( SwapQuery . expired ( SWAP_INTERACTION_TO_TABLE_MAP [ type ] ) , [ blockNumber ] )
193
+ logger . info ( `[SWAPS EXPIRATION POOLER] ${ rows . length } ${ type . toLowerCase ( ) } s updated` )
186
194
} catch ( e ) {
187
- logError ( e , ( err ) => logger . error ( `[OFFERS EXPIRATION POOLER] ${ err . message } ` ) )
195
+ logError ( e , ( err ) => logger . error ( `[SWAPS EXPIRATION POOLER] ${ err . message } ` ) )
188
196
}
189
- }
197
+ }
0 commit comments