Skip to content

Commit

Permalink
fix: end timestamp when span not 0
Browse files Browse the repository at this point in the history
  • Loading branch information
mfw78 committed Sep 5, 2023
1 parent 916ad37 commit 503b03a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/composable/orderTypes/Twap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ export class Twap extends ConditionalOrder<TwapData, TwapStruct> {
return parseInt(cabinet, 16)
}

/**
* Given the start timestamp of the TWAP, calculate the end timestamp.
* @dev As usually the `endTimestamp` is used when determining a TWAP's validity, we don't
* do any lookup to the blockchain to determine the start timestamp, as this has likely
* already been done during the verification flow.
* @dev Beware to handle the case of `span != 0` ie. `durationOfPart.durationType !== DurationType.AUTO`.
* @param startTimestamp The start timestamp of the TWAP.
* @returns The timestamp at which the TWAP will end.
*/
private endTimestamp(startTimestamp: number): number {
const { numberOfParts, timeBetweenParts, durationOfPart } = this.data

if (durationOfPart && durationOfPart.durationType === DurationType.LIMIT_DURATION) {
return startTimestamp + numberOfParts.sub(1).mul(timeBetweenParts).add(durationOfPart.duration).toNumber()
}

return startTimestamp + numberOfParts.mul(timeBetweenParts).toNumber()
}

/**
* Checks if the owner authorized the conditional order.
*
Expand All @@ -294,7 +313,6 @@ export class Twap extends ConditionalOrder<TwapData, TwapStruct> {
protected async pollValidate(params: PollParams): Promise<PollResultErrors | undefined> {
const { blockInfo = await getBlockInfo(params.provider) } = params
const { blockTimestamp } = blockInfo
const { numberOfParts, timeBetweenParts } = this.data

const startTimestamp = await this.startTimestamp(params)

Expand All @@ -307,7 +325,7 @@ export class Twap extends ConditionalOrder<TwapData, TwapStruct> {
}
}

const expirationTimestamp = startTimestamp + numberOfParts.mul(timeBetweenParts).toNumber()
const expirationTimestamp = this.endTimestamp(startTimestamp)
if (blockTimestamp >= expirationTimestamp) {
// The order has expired
return {
Expand Down

0 comments on commit 503b03a

Please sign in to comment.