Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrvk committed Feb 28, 2024
1 parent 1d01b3a commit 7b84e8d
Show file tree
Hide file tree
Showing 44 changed files with 1,829 additions and 4,077 deletions.
5 changes: 2 additions & 3 deletions src/api/orders/orders.request.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {isValidAddress} from '../../validations'
import {isHexString, isValidAddress} from '../../validations'
import {
ActiveOrdersRequestParams,
OrdersByMakerParams,
OrderStatusParams
} from './types'
import Web3 from 'web3'
import {PaginationParams, PaginationRequest} from '../pagination'

export class ActiveOrdersRequest {
Expand Down Expand Up @@ -52,7 +51,7 @@ export class OrderStatusRequest {
return `orderHash length should be equals 66`
}

if (!Web3.utils.isHex(this.orderHash)) {
if (!isHexString(this.orderHash)) {
return `orderHash have to be hex`
}

Expand Down
16 changes: 13 additions & 3 deletions src/api/quoter/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Preset {

public readonly startAuctionIn: number

public readonly bankFee: string
public readonly bankFee: bigint

public readonly initialRateBump: number

Expand All @@ -18,23 +18,33 @@ export class Preset {

public readonly points: AuctionPoint[]

public readonly gasCostInfo: {
gasBumpEstimate: bigint
gasPriceEstimate: bigint
}

constructor(preset: PresetData) {
this.auctionDuration = preset.auctionDuration
this.startAuctionIn = preset.startAuctionIn
this.bankFee = preset.bankFee
this.bankFee = BigInt(preset.bankFee)
this.initialRateBump = preset.initialRateBump
this.auctionStartAmount = BigInt(preset.auctionStartAmount)
this.auctionEndAmount = BigInt(preset.auctionEndAmount)
this.tokenFee = BigInt(preset.tokenFee)
this.points = preset.points
this.gasCostInfo = {
gasPriceEstimate: BigInt(preset.gasCost?.gasPriceEstimate || 0n),
gasBumpEstimate: BigInt(preset.gasCost?.gasBumpEstimate || 0n)
}
}

createAuctionDetails(additionalWaitPeriod = 0): AuctionDetails {
return new AuctionDetails({
duration: this.auctionDuration,
auctionStartTime: this.calcAuctionStartTime(additionalWaitPeriod),
initialRateBump: this.initialRateBump,
points: this.points
points: this.points,
gasCost: this.gasCostInfo
})
}

Expand Down
16 changes: 5 additions & 11 deletions src/api/quoter/quote/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export class Quote {

public readonly quoteId: string | null

public readonly bankFee: bigint

constructor(
private readonly network: NetworkEnum,
private readonly params: QuoterRequest,
Expand All @@ -63,7 +61,6 @@ export class Quote {
this.whitelist = response.whitelist
this.settlementAddress = response.settlementAddress
this.recommendedPreset = response.recommended_preset
this.bankFee = BigInt(response.bankFee || 0)
}

createFusionOrder(paramsData?: FusionOrderParamsData): FusionOrder {
Expand All @@ -78,7 +75,7 @@ export class Quote {

const auctionDetails = preset.createAuctionDetails()

const suffix = PostInteractionData.new({
const postInteractionData = PostInteractionData.new({
whitelist: this.whitelist.map((resolver) => ({
address: new Address(resolver),
allowance: 0
Expand All @@ -89,7 +86,7 @@ export class Quote {
? new Address(paramsData?.takingFeeReceiver)
: Address.ZERO_ADDRESS
},
bankFee: this.bankFee,
bankFee: preset.bankFee,
auctionStartTime: auctionDetails.auctionStartTime
})

Expand All @@ -112,12 +109,8 @@ export class Quote {
network: this.network
},
auctionDetails,
suffix,
postInteractionData,
{
deadline:
auctionDetails.auctionStartTime +
auctionDetails.duration +
32n,
nonce:
params.nonce === undefined
? undefined
Expand All @@ -127,7 +120,8 @@ export class Quote {
? this.params.fromTokenAddress + params.permit.substring(2)
: undefined,
allowPartialFills: paramsData?.allowPartialFills
}
},
auctionDetails.auctionStartTime + auctionDetails.duration + 32n
)
}

Expand Down
15 changes: 6 additions & 9 deletions src/api/quoter/quoter-custom-preset.request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {BigNumber} from '@ethersproject/bignumber'
import {isValidAmount} from '../../validations'
import {
CustomPreset,
Expand Down Expand Up @@ -71,23 +70,21 @@ export class QuoterCustomPresetRequest {
}

private validatePoints(
points?: CustomPresetPoint[],
auctionStartAmount?: string,
auctionEndAmount?: string
points: CustomPresetPoint[] = [],
auctionStartAmount: string,
auctionEndAmount: string
): string | null {
if (!points) {
return null
}

try {
const toTokenAmounts = points.map((p) =>
BigNumber.from(p.toTokenAmount)
)
const toTokenAmounts = points.map((p) => BigInt(p.toTokenAmount))

const isValid = toTokenAmounts.every(
(amount) =>
amount.lte(BigNumber.from(auctionStartAmount)) &&
amount.gte(BigNumber.from(auctionEndAmount))
amount <= BigInt(auctionStartAmount) &&
amount >= BigInt(auctionEndAmount)
)

if (!isValid) {
Expand Down
5 changes: 4 additions & 1 deletion src/api/quoter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export type QuoterResponse = {
settlementAddress: string
whitelist: string[]
quoteId: string | null
bankFee: string | number
}

export type QuoterPresets = {
Expand All @@ -51,6 +50,10 @@ export type PresetData = {
auctionEndAmount: string
tokenFee: string
points: AuctionPoint[]
gasCost?: {
gasBumpEstimate: string
gasPriceEstimate: string
}
}

export type AuctionPoint = {
Expand Down
38 changes: 20 additions & 18 deletions src/auction-calculator/auction-calculator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {PostInteractionData} from '../post-interaction-data'
import {AuctionDetails, AuctionPoint} from '../auction-details'
import {BigNumber} from '@ethersproject/bignumber'
import {linearInterpolation} from './calc'
import {RATE_BUMP_DENOMINATOR} from './constants'
import {addRatioToAmount} from '../sdk'
Expand All @@ -27,35 +26,38 @@ export class AuctionCalculator {
)
}

/**
* @see https://github.com/1inch/limit-order-settlement/blob/1b6757eecb2574953b543821db6f7bbff5afee48/contracts/extensions/BaseExtension.sol#L56
*/
calcAuctionTakingAmount(takingAmount: string, rate: number): string {
const auctionTakingAmount = BigNumber.from(takingAmount)
.mul(BigNumber.from(rate).add(RATE_BUMP_DENOMINATOR))
.div(RATE_BUMP_DENOMINATOR)
const auctionTakingAmount =
(BigInt(takingAmount) * (BigInt(rate) + RATE_BUMP_DENOMINATOR)) /
RATE_BUMP_DENOMINATOR

if (this.takerFeeRatio === 0n) {
return auctionTakingAmount.toString()
}

return addRatioToAmount(
auctionTakingAmount.toBigInt(),
auctionTakingAmount,
this.takerFeeRatio
).toString()
}

/**
* @see https://github.com/1inch/limit-order-settlement/blob/3c7cf9eacbaf7a60624d7a6f069c59d809f2204a/contracts/libraries/OrderSuffix.sol#L75
* @see https://github.com/1inch/limit-order-settlement/blob/1b6757eecb2574953b543821db6f7bbff5afee48/contracts/extensions/BaseExtension.sol#L121
* @param time auction timestamp in seconds
*/
calcRateBump(time: number): number {
let cumulativeTime = BigNumber.from(this.startTime)
const lastTime = BigNumber.from(this.duration).add(cumulativeTime)
const startBump = BigNumber.from(this.initialRateBump)
let cumulativeTime = BigInt(this.startTime)
const lastTime = BigInt(this.duration) + cumulativeTime
const startBump = BigInt(this.initialRateBump)

const currentTime = BigNumber.from(time)
const currentTime = BigInt(time)

if (currentTime.lte(cumulativeTime)) {
if (currentTime <= cumulativeTime) {
return Number(this.initialRateBump)
} else if (currentTime.gte(lastTime)) {
} else if (currentTime >= lastTime) {
return 0
}

Expand All @@ -65,10 +67,10 @@ export class AuctionCalculator {
for (let i = this.points.length - 1; i >= 0; i--) {
const {coefficient, delay} = this.points[i]

cumulativeTime = cumulativeTime.add(delay)
const coefficientBN = BigNumber.from(coefficient)
cumulativeTime = cumulativeTime + BigInt(delay)
const coefficientBN = BigInt(coefficient)

if (cumulativeTime.gt(currentTime)) {
if (cumulativeTime >= currentTime) {
const rate = linearInterpolation(
prevCumulativeTime,
cumulativeTime,
Expand All @@ -77,7 +79,7 @@ export class AuctionCalculator {
currentTime
)

return rate.toNumber()
return Number(rate)
}

prevCumulativeTime = cumulativeTime
Expand All @@ -88,10 +90,10 @@ export class AuctionCalculator {
prevCumulativeTime,
lastTime,
prevCoefficient,
BigNumber.from(0),
0n,
currentTime
)

return rate.toNumber()
return Number(rate)
}
}
28 changes: 13 additions & 15 deletions src/auction-calculator/calc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {BigNumber} from '@ethersproject/bignumber'

/**
*
* v2(t-t1) + v1(t2-t)
Expand All @@ -9,21 +7,21 @@ import {BigNumber} from '@ethersproject/bignumber'
* @see https://github.com/1inch/limit-order-settlement/blob/3c7cf9eacbaf7a60624d7a6f069c59d809f2204a/contracts/libraries/OrderSuffix.sol#L94
*/
export function linearInterpolation(
t1: BigNumber,
t2: BigNumber,
v1: BigNumber,
v2: BigNumber,
t: BigNumber
): BigNumber {
const timePassedFromNow = t.sub(t1)
const timeLeft = t2.sub(t)
t1: bigint,
t2: bigint,
v1: bigint,
v2: bigint,
t: bigint
): bigint {
const timePassedFromNow = t - t1
const timeLeft = t2 - t

const partialCoefficient = v2.mul(timePassedFromNow)
const partialPrevCoefficient = v1.mul(timeLeft)
const partialCoefficient = v2 * timePassedFromNow
const partialPrevCoefficient = v1 * timeLeft

const coefficient = partialCoefficient.add(partialPrevCoefficient)
const coefficient = partialCoefficient + partialPrevCoefficient

const pointsTimeDiff = t2.sub(t1)
const pointsTimeDiff = t2 - t1

return coefficient.div(pointsTimeDiff)
return coefficient / pointsTimeDiff
}
2 changes: 1 addition & 1 deletion src/auction-calculator/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const RATE_BUMP_DENOMINATOR = 10_000_000 // 100%
export const RATE_BUMP_DENOMINATOR = 10_000_000n // 100%
Loading

0 comments on commit 7b84e8d

Please sign in to comment.