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

fix: end timestamp when span not 0 #165

Merged
merged 1 commit into from
Sep 5, 2023
Merged
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
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