Skip to content

Commit

Permalink
fix(whitelist): new format (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrvk authored Mar 14, 2024
1 parent 7781c04 commit 9173868
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"changelog:generate": "changelog generate -a"
},
"dependencies": {
"@1inch/byte-utils": "2.0.0",
"@1inch/byte-utils": "2.1.0",
"@1inch/limit-order-sdk": "4.2.0",
"@metamask/eth-sig-util": "^5.0.2",
"bn.js": "^5.2.1",
Expand Down
9 changes: 7 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/api/quoter/quote/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Preset} from '../preset'
import {AuctionWhitelistItem, FusionOrder} from '../../../fusion-order'
import {QuoterRequest} from '../quoter.request'
import {bpsToRatioFormat} from '../../../sdk'
import {now} from '../../../utils/time'

export class Quote {
/**
Expand Down Expand Up @@ -129,14 +130,14 @@ export class Quote {

return {
address: resolver,
delay: isExclusive ? 0n : auctionStartTime
allowFrom: isExclusive ? now() : auctionStartTime
}
})
}

return this.whitelist.map((resolver) => ({
address: resolver,
delay: 0n
allowFrom: 0n
}))
}
}
6 changes: 4 additions & 2 deletions src/auction-calculator/auction-calculator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ describe('Auction Calculator', () => {
receiver: Address.fromBigInt(1n)
},
bankFee: 0n,
auctionStartTime,
whitelist: []
resolvingStartTime: auctionStartTime,
whitelist: [
{address: Address.ZERO_ADDRESS, allowFrom: auctionStartTime}
]
})

const auctionDetails = new AuctionDetails({
Expand Down
2 changes: 1 addition & 1 deletion src/auction-calculator/auction-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class AuctionCalculator {
details: AuctionDetails
): AuctionCalculator {
return new AuctionCalculator(
data.auctionStartTime,
data.resolvingStartTime,
details.duration,
details.initialRateBump,
details.points,
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ONE_INCH_LIMIT_ORDER_V4 =
'0x111111125421ca6dc452d289314280a0f8842a65'

export const UINT_160_MAX = (1n << 160n) - 1n
export const UINT_16_MAX = (1n << 16n) - 1n
export const UINT_80_MAX = (1n << 80n) - 1n
export const UINT_40_MAX = (1n << 40n) - 1n
export const UINT_32_MAX = (1n << 32n) - 1n
Expand Down
9 changes: 5 additions & 4 deletions src/fusion-order/fusion-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ describe('Fusion Order', () => {
address: new Address(
'0x00000000219ab540356cbb839cbe05303d7705fa'
),
delay: 0n
allowFrom: 0n
}
]
],
resolvingStartTime: 1673548139n
}
)

Expand All @@ -57,7 +58,7 @@ describe('Fusion Order', () => {
takingAmount: '1420000000',
makerTraits:
'29852648006495581632639394572552351243421169944806257724550573036760110989312',
salt: '14832508939800728556409473652845244531014097925085'
salt: '14969955465678758833706505435513058355190519874774'
})

const makerTraits = new MakerTraits(BigInt(builtOrder.makerTraits))
Expand Down Expand Up @@ -103,7 +104,7 @@ describe('Fusion Order', () => {
address: new Address(
'0x00000000219ab540356cbb839cbe05303d7705fa'
),
delay: 0n
allowFrom: 0n
}
]
}
Expand Down
27 changes: 24 additions & 3 deletions src/fusion-order/fusion-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {AuctionCalculator} from '../auction-calculator'
import {add0x} from '../utils'
import {ZX} from '../constants'
import {calcTakingAmount} from '../utils/amounts'
import {now} from '../utils/time'

export class FusionOrder {
private static defaultExtra = {
Expand Down Expand Up @@ -49,6 +50,9 @@ export class FusionOrder {
* Required if `allowPartialFills` is false
*/
nonce?: bigint
/**
* 0x prefixed without the token address
*/
permit?: string
/**
* Default is true
Expand Down Expand Up @@ -182,8 +186,12 @@ export class FusionOrder {
bankFee?: bigint
}
whitelist: AuctionWhitelistItem[]
/**
* Time from which order can be executed
*/
resolvingStartTime?: bigint
},
extra: {
extra?: {
unwrapWETH?: boolean
/**
* Required if `allowPartialFills` is false
Expand All @@ -205,7 +213,7 @@ export class FusionOrder {
*/
orderExpirationDelay?: bigint
enablePermit2?: boolean
} = FusionOrder.defaultExtra
}
): FusionOrder {
return new FusionOrder(
settlementExtension,
Expand All @@ -215,7 +223,7 @@ export class FusionOrder {
bankFee: details.fees?.bankFee || 0n,
integratorFee: details.fees?.integratorFee,
whitelist: details.whitelist,
auctionStartTime: details.auction.startTime
resolvingStartTime: details.resolvingStartTime ?? now()
}),
extra
)
Expand Down Expand Up @@ -332,4 +340,17 @@ export class FusionOrder {

return calculator.calcAuctionTakingAmount(takingAmount, bump)
}

/**
* Check whether address allowed to execute order at the given time
*
* @param executor address of executor
* @param executionTime timestamp in sec at which order planning to execute
*/
public canExecuteAt(executor: Address, executionTime: bigint): boolean {
return this.fusionExtension.postInteractionData.canExecuteAt(
executor,
executionTime
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ describe('PostInteractionData', () => {
it('Should encode/decode with no fees and whitelist', () => {
const data = SettlementPostInteractionData.new({
bankFee: 0n,
auctionStartTime: 1708117482n,
whitelist: []
resolvingStartTime: 1708117482n,
whitelist: [
{
address: Address.ZERO_ADDRESS,
allowFrom: 0n
}
]
})

expect(
Expand All @@ -18,11 +23,11 @@ describe('PostInteractionData', () => {
it('Should encode/decode with fees and whitelist', () => {
const data = SettlementPostInteractionData.new({
bankFee: 0n,
auctionStartTime: 1708117482n,
resolvingStartTime: 1708117482n,
whitelist: [
{
address: Address.ZERO_ADDRESS,
delay: 0n
allowFrom: 0n
}
],
integratorFee: {
Expand All @@ -35,4 +40,65 @@ describe('PostInteractionData', () => {
SettlementPostInteractionData.decode(data.encode())
).toStrictEqual(data)
})

it('Should generate correct whitelist', () => {
const start = 1708117482n

const data = SettlementPostInteractionData.new({
resolvingStartTime: start,
whitelist: [
{
address: Address.fromBigInt(2n),
allowFrom: start + 1000n
},
{
address: Address.ZERO_ADDRESS,
allowFrom: start - 10n // should be set to start
},
{
address: Address.fromBigInt(1n),
allowFrom: start + 10n
},
{
address: Address.fromBigInt(3n),
allowFrom: start + 10n
}
]
})

expect(data.whitelist).toStrictEqual([
{
addressHalf: Address.ZERO_ADDRESS.toString().slice(-20),
delay: 0n
},
{
addressHalf: Address.fromBigInt(1n).toString().slice(-20),
delay: 10n
},
{
addressHalf: Address.fromBigInt(3n).toString().slice(-20),
delay: 0n
},
{
addressHalf: Address.fromBigInt(2n).toString().slice(-20),
delay: 990n
}
])

expect(data.canExecuteAt(Address.fromBigInt(1n), start + 10n)).toEqual(
true
)
expect(data.canExecuteAt(Address.fromBigInt(1n), start + 9n)).toEqual(
false
)
expect(data.canExecuteAt(Address.fromBigInt(3n), start + 10n)).toEqual(
true
)
expect(data.canExecuteAt(Address.fromBigInt(3n), start + 9n)).toEqual(
false
)
expect(data.canExecuteAt(Address.fromBigInt(2n), start + 50n)).toEqual(
false
)
})
})
Loading

0 comments on commit 9173868

Please sign in to comment.