Skip to content

Commit

Permalink
Handle expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Aug 29, 2023
1 parent cec24ac commit 882b633
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/composable/orderTypes/Twap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PollResultCode,
PollResultErrors,
} from '../types'
import { encodeParams, getBlockInfo, isValidAbi } from '../utils'
import { encodeParams, formatEpoc, getBlockInfo, isValidAbi } from '../utils'
import { SupportedChainId } from '../../common'

// The type of Conditional Order
Expand Down Expand Up @@ -287,13 +287,26 @@ export class Twap extends ConditionalOrder<TwapData, TwapStruct> {
protected async pollValidate(params: PollParams): Promise<PollResultErrors | undefined> {
const { blockInfo = await getBlockInfo(params.provider), owner, chain, provider } = params
const { blockTimestamp } = blockInfo
const {} = this.data
const { numberOfParts, timeBetweenParts } = this.data

const startTimestamp = await this.startTimestamp(owner, chain, provider)

if (startTimestamp > blockTimestamp) {
// The start time hasn't started
return { result: PollResultCode.TRY_AT_EPOCH, epoch: startTimestamp, reason: "TWAP hasn't started yet" }
return {
result: PollResultCode.TRY_AT_EPOCH,
epoch: startTimestamp,
reason: `TWAP hasn't started yet. Starts at ${startTimestamp} (${formatEpoc(startTimestamp)})`,
}
}

const expirationTimestamp = startTimestamp + numberOfParts.mul(timeBetweenParts).toNumber()
if (blockTimestamp >= expirationTimestamp) {
// The order has expired
return {
result: PollResultCode.DONT_TRY_AGAIN,
reason: `TWAP has expired. Expired at ${expirationTimestamp} (${formatEpoc(expirationTimestamp)})`,
}
}

// TODO: Do not check again expired order
Expand Down
4 changes: 4 additions & 0 deletions src/composable/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ export async function getBlockInfo(provider: providers.Provider): Promise<BlockI
blockTimestamp: block.timestamp,
}
}

export function formatEpoc(epoch: number): string {
return new Date(epoch * 1000).toISOString()
}

0 comments on commit 882b633

Please sign in to comment.