Skip to content
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
7 changes: 2 additions & 5 deletions src/lib/libraries/intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function encodeOutputs(outputs: MandateOutput[]) {
const ONE_MINUTE = 60;
const ONE_HOUR = 60 * ONE_MINUTE;
const ONE_DAY = 24 * ONE_HOUR;
const EXCLUSIVITY_WINDOW = 5 * ONE_MINUTE; // gives solvers time to fill before exclusivity ends

/**
* @notice Class representing a Li.Fi Intent. Contains intent abstractions and helpers.
Expand Down Expand Up @@ -179,16 +180,12 @@ export class Intent {
}

encodeOutputs(currentTime: number) {
// Get the current epoch timestamp:
currentTime;
const ONE_MINUTE = 60;

let context: `0x${string}` = "0x";
if (this.exclusiveFor) {
const paddedExclusiveFor: `0x${string}` = `0x${this.exclusiveFor.replace("0x", "").padStart(64, "0")}`;
context = encodePacked(
["bytes1", "bytes32", "uint32"],
["0xe0", paddedExclusiveFor, currentTime + ONE_MINUTE]
["0xe0", paddedExclusiveFor, currentTime + EXCLUSIVITY_WINDOW]
);
}

Expand Down
23 changes: 21 additions & 2 deletions tests/unit/intent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { chain, Token } from "../../src/lib/config";

const FORTY_FOUR_HOURS = 44 * 60 * 60; // 158_400
const TWO_DAYS = 2 * 24 * 60 * 60; // 172_800
const FIVE_MINUTES = 5 * 60; // 300

function token(chain: chain): Token {
return {
Expand All @@ -14,9 +15,9 @@ function token(chain: chain): Token {
};
}

function makeIntent(inputChain: chain, outputChain: chain) {
function makeIntent(inputChain: chain, outputChain: chain, exclusiveFor = "") {
return new Intent({
exclusiveFor: "",
exclusiveFor,
inputTokens: [{ token: token(inputChain), amount: 1n }],
outputTokens: [{ token: token(outputChain), amount: 1n }],
verifier: "polymer",
Expand Down Expand Up @@ -78,4 +79,22 @@ describe("Intent deadlines", () => {

expect(order.fillDeadline).toBeLessThanOrEqual(order.expires);
});

it("encodes exclusiveFor deadline as now + 5 minutes in output context", () => {
const exclusiveFor = "0x2222222222222222222222222222222222222222";
const before = Math.floor(Date.now() / 1000);
const { order } = makeIntent("base", "arbitrum", exclusiveFor).singlechain();
const after = Math.floor(Date.now() / 1000);

const context = order.outputs[0].context;
expect(context.length).toBeGreaterThan(2); // not just "0x"

// Layout: bytes1 0xe0 || bytes32 exclusiveFor || uint32 deadline
// = 37 bytes = 74 hex chars after the "0x" prefix.
expect(context.length).toBe(2 + 74);

const deadline = parseInt(context.slice(-8), 16);
expect(deadline).toBeGreaterThanOrEqual(before + FIVE_MINUTES);
expect(deadline).toBeLessThanOrEqual(after + FIVE_MINUTES);
});
});
Loading