Skip to content

Commit

Permalink
fix: deadline check added
Browse files Browse the repository at this point in the history
  • Loading branch information
CoviloMilos committed Jan 30, 2023
1 parent ec28946 commit 8ac857b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/modules/fungibleTrading/fundgibleTrading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class FungibleTrading {
private extractSwapOptions(recipient: string, options?: SwapOptions): Required<SwapOptions> {
return {
slippageTolerance: options?.slippageTolerance ?? DEFAULT_SLIPPAGE_TOLERANCE,
deadline: options?.deadline ?? this.getDeadline(),
deadline: this.getDeadline(options?.deadline),
recipient: options?.recipient ?? recipient
};
}
Expand All @@ -130,7 +130,11 @@ export class FungibleTrading {
}

// Unix timestamp after which the transaction will revert.
private getDeadline(): number {
return Math.floor(Date.now() / 1000 + 1800); // 30min
private getDeadline(deadline: number | undefined): number {
const defaultDeadLine = Math.floor(Date.now() / 1000 + 1800); // 30min
if (deadline && deadline < defaultDeadLine) {
throw new TheaError({ type: "INVALID_DEADLINE", message: "Deadline can't be in past" });
}
return deadline ? deadline : defaultDeadLine;
}
}
3 changes: 2 additions & 1 deletion src/utils/theaError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export type ErrorType =
| "INVALID_APP_ID"
| "INVALID_SIGNATURE_SIZE"
| "INVALID_SIGNATURE_LAYOUT"
| "INVALID_NONCE";
| "INVALID_NONCE"
| "INVALID_DEADLINE";

export type ErrorProps = {
type: ErrorType;
Expand Down
9 changes: 8 additions & 1 deletion test/modules/fungibleTrading/fungibleTrading.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe("Fungible Trading", () => {
});

it("should extract swap options", async () => {
const swapOptions = { slippageTolerance: 1, deadline: 1000, recipient: "0x123" };
const swapOptions = { slippageTolerance: 1, deadline: Date.now() + 100000, recipient: "0x123" };
const swapSpy = jest.spyOn(fungibleTrading.swapRouter, "swap");
await fungibleTrading.swapTokens({ amountIn: amount, tokenIn: "SDG" }, swapOptions);

Expand All @@ -150,5 +150,12 @@ describe("Fungible Trading", () => {
"SDG"
);
});

it("should throw error if deadline is in past", async () => {
const swapOptions = { slippageTolerance: 1, deadline: 1000, recipient: "0x123" };
await expect(fungibleTrading.swapTokens({ amountIn: amount, tokenIn: "SDG" }, swapOptions)).rejects.toThrow(
new TheaError({ type: "INVALID_DEADLINE", message: "Deadline can't be in past" })
);
});
});
});

0 comments on commit 8ac857b

Please sign in to comment.