Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirun committed Dec 1, 2022
0 parents commit afd3723
Show file tree
Hide file tree
Showing 6 changed files with 3,249 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/dist
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@opiumteam/opium-auction-v2-utils",
"version": "1.0.0",
"description": "Utils library for Opium Auction V2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"repository": "https://github.com/OpiumProtocol/opium-auction-v2-utils.git",
"author": "Ali Nuraldin <ali@opium.team>",
"license": "MIT",
"private": false,
"scripts": {
"build": "tsc",
"publish-lib": "npm run build && npm publish",
"publish-beta": "npm run build && npm publish --tag beta",
"publish-dryrun": "npm run build && npm publish --dry-run"
},
"devDependencies": {
"typescript": "^4.9.3"
},
"dependencies": {
"@1inch/limit-order-protocol": "^2.0.4",
"ethers": "^5.7.2"
}
}
206 changes: 206 additions & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import { ethers, BigNumberish } from "ethers";

import { LimitOrderBuilder, LimitOrder } from "@1inch/limit-order-protocol";

const abiCoder = new ethers.utils.AbiCoder();

const ABI = [
'function getLinearAuctionMakerAmount(uint256,uint256,uint256,uint256,uint256,bool,uint256)',
'function getLinearAuctionTakerAmount(uint256,uint256,uint256,uint256,uint256,bool,uint256)',
'function getExponentialAuctionMakerAmount(uint256,uint256,uint256,uint256,uint256,bool,uint256,uint256)',
'function getExponentialAuctionTakerAmount(uint256,uint256,uint256,uint256,uint256,bool,uint256,uint256)',
'function arbitraryStaticCall(address,bytes)',
'function and(address[],bytes[])',
'function nonceEquals(address,uint256)',
'function timestampBelow(uint256)',
];
const contractInterface = new ethers.utils.Interface(ABI);

/** Linear Auction */
const generateGetAmountLinear = (
method: "getLinearAuctionTakerAmount" | "getLinearAuctionMakerAmount",
helperAddress: string,
orderMakerAmount: BigNumberish,
orderTakerAmount: BigNumberish,
thresholdOrderTakerAmount: BigNumberish,
startedAt: BigNumberish,
endedAt: BigNumberish,
increasing: boolean
) => {
// Generate calldata for the function
const calldataOne = contractInterface.encodeFunctionData(
method as never,
[
orderMakerAmount,
orderTakerAmount,
thresholdOrderTakerAmount,
startedAt,
endedAt,
increasing,
0 // Will be removed from calldata
]
);

const calldataTwo = contractInterface.encodeFunctionData(
"arbitraryStaticCall",
[helperAddress, calldataOne]
)

return calldataTwo.substring(0, calldataTwo.length - 120)
}

const generateGetAmountExponential = (
method: "getExponentialAuctionTakerAmount" | "getExponentialAuctionMakerAmount",
helperAddress: string,
orderMakerAmount: BigNumberish,
orderTakerAmount: BigNumberish,
thresholdOrderTakerAmount: BigNumberish,
startedAt: BigNumberish,
endedAt: BigNumberish,
increasing: boolean,
amplifier: number
) => {
// Generate calldata for the function
const calldataOne = contractInterface.encodeFunctionData(
method as never,
[
orderMakerAmount,
orderTakerAmount,
thresholdOrderTakerAmount,
startedAt,
endedAt,
increasing,
amplifier,
0 // Will be removed from calldata
]
);

const calldataTwo = contractInterface.encodeFunctionData(
"arbitraryStaticCall",
[helperAddress, calldataOne]
)

return calldataTwo.substring(0, calldataTwo.length - 120)
}

/** General */
const generatePredicate = (helperAddress: string, makerAddress: string, nonce: number, timestamp: number) => {
return contractInterface.encodeFunctionData("and", [
[ helperAddress, helperAddress ],
[
contractInterface.encodeFunctionData("nonceEquals", [makerAddress, nonce]),
contractInterface.encodeFunctionData("timestampBelow", [timestamp])
]
])
}

/** External */
export enum AuctionPricingFunction {
LINEAR,
EXPONENTIAL
}

export enum AuctionPricingDirection {
INCREASING,
DECREASING
}

export const buildAuctionOrder = (
helperAddress: string,
limitOrderBuilder: LimitOrderBuilder,
order: {
makerAssetAddress: string,
takerAssetAddress: string,
makerAddress: string,
makerAmount: string,
permit?: string,
},
auction: {
pricingFunction: AuctionPricingFunction,
pricingDirection: AuctionPricingDirection,
partialFill: boolean,
minTakerAmount: string,
maxTakerAmount: string,
startedAt: number,
endedAt: number,
amplifier?: number
}
) => {
const makerLimitOrder = limitOrderBuilder.buildLimitOrder({
makerAssetAddress: order.makerAssetAddress,
takerAssetAddress: order.takerAssetAddress,
makerAddress: order.makerAddress,
makerAmount: order.makerAmount,
takerAmount: auction.maxTakerAmount,
permit: order.permit,
receiver: helperAddress,
predicate: generatePredicate(helperAddress, order.makerAddress, 0, auction.endedAt),
});

if (auction.pricingFunction === AuctionPricingFunction.LINEAR) {
makerLimitOrder.getTakerAmount = generateGetAmountLinear(
"getLinearAuctionTakerAmount",
helperAddress,
order.makerAmount,
auction.maxTakerAmount,
auction.minTakerAmount,
auction.startedAt,
auction.endedAt,
auction.pricingDirection === AuctionPricingDirection.INCREASING
)
makerLimitOrder.getMakerAmount = generateGetAmountLinear(
"getLinearAuctionMakerAmount",
helperAddress,
order.makerAmount,
auction.maxTakerAmount,
auction.minTakerAmount,
auction.startedAt,
auction.endedAt,
auction.pricingDirection === AuctionPricingDirection.INCREASING
)
} else if (auction.pricingFunction === AuctionPricingFunction.EXPONENTIAL) {
makerLimitOrder.getTakerAmount = generateGetAmountExponential(
"getExponentialAuctionTakerAmount",
helperAddress,
order.makerAmount,
auction.maxTakerAmount,
auction.minTakerAmount,
auction.startedAt,
auction.endedAt,
auction.pricingDirection === AuctionPricingDirection.INCREASING,
auction.amplifier ?? 1
)
makerLimitOrder.getMakerAmount = generateGetAmountExponential(
"getExponentialAuctionMakerAmount",
helperAddress,
order.makerAmount,
auction.maxTakerAmount,
auction.minTakerAmount,
auction.startedAt,
auction.endedAt,
auction.pricingDirection === AuctionPricingDirection.INCREASING,
auction.amplifier ?? 1
)
} else {
throw new Error('Unsupported pricing function')
}

makerLimitOrder.interaction =
helperAddress +
abiCoder.encode(
["address", "uint256", "uint256"],
[order.makerAddress, auction.partialFill ? '0' : order.makerAmount, auction.startedAt]
).substring(2)

return makerLimitOrder
}

export const encodeOrder = (order: LimitOrder) => {
const abiCoder = new ethers.utils.AbiCoder();
return abiCoder.encode(
[
"tuple(uint256 salt, address makerAsset, address takerAsset, address maker, address receiver, address allowedSender, uint256 makingAmount, uint256 takingAmount, bytes makerAssetData, bytes takerAssetData, bytes getMakerAmount, bytes getTakerAmount, bytes predicate, bytes permit, bytes interaction) order",
],
[order]
);
};
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AuctionPricingDirection, AuctionPricingFunction, buildAuctionOrder, encodeOrder } from './build'

export {
AuctionPricingDirection, AuctionPricingFunction, buildAuctionOrder, encodeOrder
}
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2015",
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true
},
"include": [
"src/**/*"
]
}
Loading

0 comments on commit afd3723

Please sign in to comment.