Skip to content

Commit

Permalink
PriorityOrderBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ConjunctiveNormalForm committed Jun 27, 2024
1 parent e3e5ecc commit 12d15d0
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 1 deletion.
154 changes: 154 additions & 0 deletions sdks/uniswapx-sdk/src/builder/PriorityOrderBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { BigNumber } from "ethers";
import invariant from "tiny-invariant";

import { OrderType, REACTOR_ADDRESS_MAPPING } from "../constants";
import { MissingConfiguration } from "../errors";
import {
PriorityInput,
PriorityOrder,
PriorityOrderInfo,
PriorityOutput,
} from "../order";
import { ValidationInfo } from "../order/validation";

import { OrderBuilder } from "./OrderBuilder";

/**
* Helper builder for generating priority gas auction orders
*/
export class PriorityOrderBuilder extends OrderBuilder {
private info: Partial<PriorityOrderInfo>;

static fromOrder(order: PriorityOrder): PriorityOrderBuilder {
// note chainId not used if passing in true reactor address
const builder = new PriorityOrderBuilder(order.chainId, order.info.reactor)
.deadline(order.info.deadline)
.swapper(order.info.swapper)
.nonce(order.info.nonce)
.input(order.info.input)
.validation({
additionalValidationContract: order.info.additionalValidationContract,
additionalValidationData: order.info.additionalValidationData,
});

for (const output of order.info.outputs) {
builder.output(output);
}
return builder;
}

constructor(
private chainId: number,
reactorAddress?: string,
private permit2Address?: string
) {
super();

const mappedReactorAddress = REACTOR_ADDRESS_MAPPING[chainId]
? REACTOR_ADDRESS_MAPPING[chainId][OrderType.Dutch]
: undefined;

if (reactorAddress) {
this.reactor(reactorAddress);
} else if (mappedReactorAddress) {
this.reactor(mappedReactorAddress);
} else {
throw new MissingConfiguration("reactor", chainId.toString());
}

this.info = {
outputs: [],
};
}

startBlock(startBlock: BigNumber): PriorityOrderBuilder {
this.info.startBlock = startBlock;
return this;
}

input(input: PriorityInput): PriorityOrderBuilder {
this.info.input = input;
return this;
}

output(output: PriorityOutput): PriorityOrderBuilder {
if (!this.info.outputs) {
this.info.outputs = [];
}
this.info.outputs.push(output);
return this;
}

deadline(deadline: number): PriorityOrderBuilder {
super.deadline(deadline);
return this;
}

swapper(swapper: string): PriorityOrderBuilder {
super.swapper(swapper);
return this;
}

nonce(nonce: BigNumber): PriorityOrderBuilder {
super.nonce(nonce);
return this;
}

validation(info: ValidationInfo): PriorityOrderBuilder {
super.validation(info);
return this;
}

// ensures that we only change non fee outputs
nonFeeRecipient(
newRecipient: string,
feeRecipient?: string
): PriorityOrderBuilder {
invariant(
newRecipient !== feeRecipient,
`newRecipient must be different from feeRecipient: ${newRecipient}`
);
if (!this.info.outputs) {
return this;
}
this.info.outputs = this.info.outputs.map((output) => {
// if fee output then pass through
if (
feeRecipient &&
output.recipient.toLowerCase() === feeRecipient.toLowerCase()
) {
return output;
}

return {
...output,
recipient: newRecipient,
};
});
return this;
}

build(): PriorityOrder {
invariant(this.info.input !== undefined, "input not set");
invariant(
this.info.outputs !== undefined && this.info.outputs.length !== 0,
"outputs not set"
);
invariant(this.info.startBlock !== undefined, "startBlock not set");
invariant(
this.info.input.mpsPerPriorityFeeWei.eq(0) &&
this.info.outputs.every((output) => output.mpsPerPriorityFeeWei.eq(0)),
"Priority auction not configured"
);

return new PriorityOrder(
Object.assign(this.getOrderInfo(), {
startBlock: this.info.startBlock,
input: this.info.input,
outputs: this.info.outputs,
}),
this.chainId,
this.permit2Address
);
}
}
5 changes: 4 additions & 1 deletion sdks/uniswapx-sdk/src/order/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { DutchOrder } from "./DutchOrder";
import { PriorityOrder } from "./PriorityOrder";
import { RelayOrder } from "./RelayOrder";
import { CosignedV2DutchOrder, UnsignedV2DutchOrder } from "./V2DutchOrder";

export * from "./DutchOrder";
export * from "./PriorityOrder";
export * from "./RelayOrder";
export * from "./types";
export * from "./validation";
Expand All @@ -11,6 +13,7 @@ export * from "./V2DutchOrder";
export type UniswapXOrder =
| DutchOrder
| UnsignedV2DutchOrder
| CosignedV2DutchOrder;
| CosignedV2DutchOrder
| PriorityOrder;

export type Order = UniswapXOrder | RelayOrder;

0 comments on commit 12d15d0

Please sign in to comment.