Skip to content

Commit 2bdeaca

Browse files
authored
Merge pull request #336 from kodadot/main
🔖 : Speck with updated cache
2 parents 217fc91 + 4ea59aa commit 2bdeaca

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/mappings/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as a from './assets'
88
import * as n from './nfts'
99
import * as u from './uniques'
1010
import { BatchContext, Context, SelectedEvent } from './utils/types'
11-
import { updateOfferCache } from './utils/cache'
11+
import { updateSwapsCache } from './utils/cache'
1212

1313
type HandlerFunction = <T extends SelectedEvent>(item: T, ctx: Context) => Promise<void>
1414

@@ -237,7 +237,7 @@ export async function mainFrame(ctx: BatchContext<Store>): Promise<void> {
237237
const lastBlock = ctx.blocks[ctx.blocks.length - 1].header
238238
const lastDate = new Date(lastBlock.timestamp || Date.now())
239239
logger.info(`Found head block, updating cache`)
240-
await updateOfferCache(lastDate, lastBlock.height, ctx.store)
240+
await updateSwapsCache(lastDate, lastBlock.height, ctx.store)
241241
}
242242
}
243243

src/mappings/nfts/createSwap.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function handleCreateSwap(context: Context): Promise<void> {
3535
// the nft that is being swapped
3636
const nft = await get(context.store, NE, id)
3737
const considered = await get(context.store, CE, event.consideration.collectionId)
38-
const desired = isNFT(event.consideration) ? await get(context.store, NE, tokenIdOf(event.consideration as any)) : undefined
38+
const desired = isNFT(event.consideration) ? await get(context.store, NE, tokenIdOf(event.consideration as any)) : null
3939

4040
final.blockNumber = BigInt(event.blockNumber)
4141
final.createdAt = event.timestamp
@@ -44,9 +44,9 @@ export async function handleCreateSwap(context: Context): Promise<void> {
4444
final.considered = considered
4545
final.desired = desired
4646
final.expiration = deadline
47-
final.price = event.price
47+
final.price = event.price || null
4848
if (!offer) {
49-
(final as Swap).surcharge = event.surcharge
49+
(final as Swap).surcharge = event.surcharge || null
5050
}
5151
final.status = final.blockNumber >= deadline ? TradeStatus.EXPIRED : TradeStatus.ACTIVE
5252
final.updatedAt = event.timestamp

src/mappings/utils/cache.ts

+24-16
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const STATUS_ID = '0'
1010
const METADATA_STATUS_ID = '1'
1111
const METADATA_DELAY_MIN = 15 // every 24 hours
1212
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
1515

1616
enum MetadataQuery {
1717
missing = `SELECT
@@ -64,15 +64,20 @@ enum MetadataQuery {
6464
`,
6565
}
6666

67-
enum OfferQuery {
68-
expired = `UPDATE
69-
offer oe
67+
const SwapQuery = {
68+
expired: (table: string) => `UPDATE
69+
${table} oe
7070
SET status = 'EXPIRED'
7171
WHERE status = 'ACTIVE'
7272
AND expiration <= $1
7373
RETURNING oe.id;`
7474
}
7575

76+
const SWAP_INTERACTION_TO_TABLE_MAP = {
77+
[Interaction.SWAP]: 'swap',
78+
[Interaction.OFFER]: 'offer',
79+
}
80+
7681
const OPERATION = 'METADATA_CACHE' as any
7782

7883
/**
@@ -119,18 +124,21 @@ export async function updateMetadataCache(timestamp: Date, store: Store): Promis
119124
* @param timestamp - the timestamp of the block
120125
* @param store - subsquid store to handle the cache
121126
**/
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) })
124129
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) {
127133
try {
128-
await updateOfferAsExpired(store, blockNumber)
134+
for (const type of [Interaction.OFFER, Interaction.SWAP] as const) {
135+
await updateSwapAsExpired(type, blockNumber, store)
136+
}
129137
lastUpdate.lastBlockTimestamp = timestamp
130138
await store.save(lastUpdate)
131139
// success('[METADATA CACHE UPDATE]');
132140
} catch (e) {
133-
logError(e, (err) => logger.error(`[OFFER CACHE UPDATE] ${err.message}`))
141+
logError(e, (err) => logger.error(`[SWAPS CACHE UPDATE] ${err.message}`))
134142
}
135143
}
136144
}
@@ -179,11 +187,11 @@ async function updateMissingMetadata(store: Store) {
179187
// logger.info(`[CACHE UPDATE] MISSING METADATA - ${missing.length} NFTs, ${nft.length} NFTs, ${collection.length} Collections`);
180188
}
181189

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> {
183191
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`)
186194
} catch (e) {
187-
logError(e, (err) => logger.error(`[OFFERS EXPIRATION POOLER] ${err.message}`))
195+
logError(e, (err) => logger.error(`[SWAPS EXPIRATION POOLER] ${err.message}`))
188196
}
189-
}
197+
}

0 commit comments

Comments
 (0)