Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 expired swaps remain active #333

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mappings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as a from './assets'
import * as n from './nfts'
import * as u from './uniques'
import { BatchContext, Context, SelectedEvent } from './utils/types'
import { updateOfferCache } from './utils/cache'
import { updateSwapsCache } from './utils/cache'

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

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

Expand Down
34 changes: 21 additions & 13 deletions src/mappings/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const STATUS_ID = '0'
const METADATA_STATUS_ID = '1'
const METADATA_DELAY_MIN = 15 // every 24 hours
const TO_MINUTES = 60_000
const OFFER_STATUS_ID = '2'
const SWAP_STATUS_ID = '2'
// const OFFER_DELAY_MIN = 30 // every 15 minutes

enum MetadataQuery {
Expand Down Expand Up @@ -64,15 +64,20 @@ enum MetadataQuery {
`,
}

enum OfferQuery {
expired = `UPDATE
offer oe
const SwapQuery = {
expired: (table: string) => `UPDATE
${table} oe
SET status = 'EXPIRED'
WHERE status = 'ACTIVE'
AND expiration <= $1
RETURNING oe.id;`
}

const SWAP_INTERACTION_TO_TABLE_MAP = {
[Interaction.SWAP]: 'swap',
[Interaction.OFFER]: 'offer',
}

const OPERATION = 'METADATA_CACHE' as any

/**
Expand Down Expand Up @@ -119,18 +124,21 @@ export async function updateMetadataCache(timestamp: Date, store: Store): Promis
* @param timestamp - the timestamp of the block
* @param store - subsquid store to handle the cache
**/
export async function updateOfferCache(timestamp: Date, blockNumber: number, store: Store): Promise<void> {
const lastUpdate = await getOrCreate(store, CacheStatus, OFFER_STATUS_ID, { id: OFFER_STATUS_ID, lastBlockTimestamp: new Date(0) })
export async function updateSwapsCache(timestamp: Date, blockNumber: number, store: Store): Promise<void> {
const lastUpdate = await getOrCreate(store, CacheStatus, SWAP_STATUS_ID, { id: SWAP_STATUS_ID, lastBlockTimestamp: new Date(0) })
const passedMins = getPassedMinutes(timestamp, lastUpdate.lastBlockTimestamp)
pending(Interaction.OFFER, `${passedMins} MINS SINCE LAST UPDATE`)
pending(Interaction.SWAP, `${passedMins} MINS SINCE LAST UPDATE`)

if (passedMins >= DELAY_MIN) {
try {
await updateOfferAsExpired(store, blockNumber)
for (const type of [Interaction.OFFER, Interaction.SWAP] as const) {
await updateSwapAsExpired(type, blockNumber, store)
}
lastUpdate.lastBlockTimestamp = timestamp
await store.save(lastUpdate)
// success('[METADATA CACHE UPDATE]');
} catch (e) {
logError(e, (err) => logger.error(`[OFFER CACHE UPDATE] ${err.message}`))
logError(e, (err) => logger.error(`[SWAPS CACHE UPDATE] ${err.message}`))
}
}
}
Expand Down Expand Up @@ -179,11 +187,11 @@ async function updateMissingMetadata(store: Store) {
// logger.info(`[CACHE UPDATE] MISSING METADATA - ${missing.length} NFTs, ${nft.length} NFTs, ${collection.length} Collections`);
}

export async function updateOfferAsExpired(store: Store, blockNumber: string | bigint | number): Promise<void> {
export async function updateSwapAsExpired(type: Interaction.SWAP | Interaction.OFFER, blockNumber: string | bigint | number, store: Store): Promise<void> {
try {
const rows = await emOf(store).query(OfferQuery.expired, [blockNumber])
logger.info(`[OFFERS EXPIRATION POOLER] ${rows.length} Offers updated`)
const [rows] = await emOf(store).query(SwapQuery.expired(SWAP_INTERACTION_TO_TABLE_MAP[type]), [blockNumber])
logger.info(`[SWAPS EXPIRATION POOLER] ${rows.length} ${type.toLowerCase()}s updated`)
} catch (e) {
logError(e, (err) => logger.error(`[OFFERS EXPIRATION POOLER] ${err.message}`))
logError(e, (err) => logger.error(`[SWAPS EXPIRATION POOLER] ${err.message}`))
}
}