Skip to content

Commit

Permalink
fix(uniswapx-sdk): add priority order parsing and tests (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongeric authored Aug 7, 2024
1 parent eab3f3a commit 5b429b2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
77 changes: 77 additions & 0 deletions sdks/uniswapx-sdk/src/utils/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { BigNumber, constants } from "ethers";

import {
DutchOrderBuilder,
PriorityOrderBuilder,
RelayOrderBuilder,
V2DutchOrderBuilder,
} from "../builder";
import { OrderType } from "../constants";
import {
CosignedPriorityOrder,
CosignedV2DutchOrder,
DutchOrder,
RelayOrder,
UnsignedPriorityOrder,
UnsignedV2DutchOrder,
} from "../order";

Expand All @@ -20,15 +23,19 @@ describe("order utils", () => {
let dutchOrderExactOut: DutchOrder;
let cosignedV2DutchOrder: CosignedV2DutchOrder;
let unsignedV2DutchOrder: UnsignedV2DutchOrder;
let unsignedPriorityOrder: UnsignedPriorityOrder;
let cosignedPriorityOrder: CosignedPriorityOrder;
let limitOrder: DutchOrder;
let relayOrder: RelayOrder;
let chainId: number;
let priorityChainId: number;

const uniswapXOrderParser = new UniswapXOrderParser();
const relayOrderParser = new RelayOrderParser();

beforeAll(() => {
chainId = 1;
priorityChainId = 8453;
const dutchBuilder = new DutchOrderBuilder(chainId);
const deadline = Math.floor(Date.now() / 1000) + 1000;
const input = {
Expand Down Expand Up @@ -131,6 +138,36 @@ describe("order utils", () => {
"0x65c6470fea0e1ca7d204b6904d0c1b0b640d7e6dcd4be3065497756e163c0399288c3eea0fba9b31ed00f34ccffe389ec3027bcd764df9fa853eeae8f68c9beb1b"
)
.build();

const priorityBuilder = new PriorityOrderBuilder(priorityChainId)
.cosigner("0xe463635f6e73C1E595554C3ae216472D0fb929a9")
.deadline(deadline)
.swapper(constants.AddressZero)
.nonce(BigNumber.from(100))
.auctionStartBlock(BigNumber.from(123))
.baselinePriorityFeeWei(BigNumber.from(0))
.input({
token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
amount: BigNumber.from("1000000"),
mpsPerPriorityFeeWei: BigNumber.from(0),
})
.output({
token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
amount: BigNumber.from("1000000000000000000"),
mpsPerPriorityFeeWei: BigNumber.from(1),
recipient: "0x0000000000000000000000000000000000000000",
});

unsignedPriorityOrder = priorityBuilder.buildPartial();

cosignedPriorityOrder = priorityBuilder
.cosignerData({
auctionTargetBlock: BigNumber.from(123),
})
.cosignature(
"0x65c6470fea0e1ca7d204b6904d0c1b0b640d7e6dcd4be3065497756e163c0399288c3eea0fba9b31ed00f34ccffe389ec3027bcd764df9fa853eeae8f68c9beb1b"
)
.build();
});

describe("parseOrder", () => {
Expand Down Expand Up @@ -243,6 +280,20 @@ describe("order utils", () => {
uniswapXOrderParser.parseOrder(encodedOrder, chainId)
).toMatchObject(unsignedV2DutchOrder);
});

it("parses CosignedPriorityOrder", () => {
const encodedOrder = cosignedPriorityOrder.serialize();
expect(uniswapXOrderParser.parseOrder(encodedOrder, priorityChainId)).toEqual(
cosignedPriorityOrder
);
});

it("parses UnsignedPriorityOrder", () => {
const encodedOrder = unsignedPriorityOrder.serialize();
expect(
uniswapXOrderParser.parseOrder(encodedOrder, priorityChainId)
).toMatchObject(unsignedPriorityOrder);
});
});

describe("getOrderType", () => {
Expand Down Expand Up @@ -276,6 +327,16 @@ describe("order utils", () => {
OrderType.Dutch_V2
);
});
it("parses CosignedPriorityOrder type", () => {
expect(uniswapXOrderParser.getOrderType(cosignedPriorityOrder)).toEqual(
OrderType.Priority
);
});
it("parses UnsignedPriorityOrder type", () => {
expect(uniswapXOrderParser.getOrderType(unsignedPriorityOrder)).toEqual(
OrderType.Priority
);
});
});

describe("getOrderTypeFromEncoded", () => {
Expand Down Expand Up @@ -327,5 +388,21 @@ describe("order utils", () => {
)
).toEqual(OrderType.Dutch_V2);
});
it("parses UnsignedPriorityOrder type", () => {
expect(
uniswapXOrderParser.getOrderTypeFromEncoded(
unsignedPriorityOrder.serialize(),
priorityChainId
)
).toEqual(OrderType.Priority);
});
it("parses CosignedPriorityOrder type", () => {
expect(
uniswapXOrderParser.getOrderTypeFromEncoded(
cosignedPriorityOrder.serialize(),
priorityChainId
)
).toEqual(OrderType.Priority);
});
});
});
12 changes: 12 additions & 0 deletions sdks/uniswapx-sdk/src/utils/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { ethers } from "ethers";
import { OrderType, REVERSE_REACTOR_MAPPING } from "../constants";
import { MissingConfiguration } from "../errors";
import {
CosignedPriorityOrder,
CosignedV2DutchOrder,
DutchOrder,
Order,
RelayOrder,
UniswapXOrder,
UnsignedPriorityOrder,
UnsignedV2DutchOrder,
} from "../order";

Expand Down Expand Up @@ -98,6 +100,16 @@ export class UniswapXOrderParser extends OrderParser {
// if cosignature exists then returned cosigned version
return cosignedOrder;
}
case OrderType.Priority: {
// cosigned and unsigned serialized versions are the same format
const cosignedOrder = CosignedPriorityOrder.parse(order, chainId);
// if no cosignature, returned unsigned variant
if (cosignedOrder.info.cosignature === "0x") {
return UnsignedPriorityOrder.parse(order, chainId);
}
// if cosignature exists then returned cosigned version
return cosignedOrder;
}
default:
throw new MissingConfiguration("orderType", orderType);
}
Expand Down

0 comments on commit 5b429b2

Please sign in to comment.