Skip to content

Commit

Permalink
refactor: don't reschedule next if price is already locked
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Mar 5, 2023
1 parent 39f9cf9 commit c29f2c2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/inter-protocol/src/auction/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,17 @@ export const computeRoundTiming = (params, baseTime) => {
startDelay,
);
const endTime = TimeMath.addAbsRel(startTime, actualDuration);
const lockTime = TimeMath.subtractAbsRel(startTime, lockPeriod);

const next = { startTime, endTime, steps, endRate, startDelay, clockStep };
const next = {
startTime,
endTime,
steps,
endRate,
startDelay,
clockStep,
lockTime,
};
return harden(next);
};

Expand Down Expand Up @@ -144,8 +153,17 @@ export const makeScheduler = async (
auctionState = AuctionState.WAITING;

auctionDriver.finalize();
const afterNow = TimeMath.addAbsRel(time, TimeMath.toRel(1n, timerBrand));
nextSchedule = computeRoundTiming(params, afterNow);

// only recalculate the next schedule at this point if the lock time has
// not been reached.
const nextLock = nextSchedule.lockTime;
if (TimeMath.compareAbs(time, nextLock) < 0) {
const afterNow = TimeMath.addAbsRel(
time,
TimeMath.toRel(1n, timerBrand),
);
nextSchedule = computeRoundTiming(params, afterNow);
}
liveSchedule = undefined;

E(timer).cancel(stepCancelToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const checkSchedule = (t, params, baseTime, rawExpect) => {
endRate: rawExpect.endRate,
startDelay: TimeMath.toRel(rawExpect.startDelay, brand),
clockStep: TimeMath.toRel(rawExpect.clockStep, brand),
lockTime: TimeMath.toAbs(rawExpect.lockTime, brand),
};
t.deepEqual(schedule, expect);
};
Expand Down Expand Up @@ -76,6 +77,7 @@ test('simple schedule', checkSchedule, makeDefaultParams(), 100, {
endRate: 6_500n,
startDelay: 300,
clockStep: 600,
lockTime: 3000,
});

test(
Expand All @@ -90,6 +92,7 @@ test(
endRate: 6_500n,
startDelay: 300,
clockStep: 600,
lockTime: 6600,
},
);

Expand All @@ -107,6 +110,7 @@ test(
endRate: 6_500n,
startDelay: 300,
clockStep: 300,
lockTime: 3000,
},
);

Expand Down Expand Up @@ -155,6 +159,7 @@ test(
endRate: 10_500n - 2_001n,
startDelay: 300,
clockStep: 600,
lockTime: 3000,
},
);

Expand All @@ -180,6 +185,7 @@ test(
endRate: 10_500n - 5n * 10n,
startDelay: 300,
clockStep: 600,
lockTime: 3000,
},
);

Expand All @@ -197,5 +203,6 @@ test(
endRate: 10_500n - 5n * 350n,
startDelay: 300,
clockStep: 600,
lockTime: 3000,
},
);
6 changes: 6 additions & 0 deletions packages/inter-protocol/test/auction/test-scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ test('schedule start to finish', async t => {
endRate: 6500n,
startDelay: TimeMath.toRel(1n, timerBrand),
clockStep: TimeMath.toRel(2n, timerBrand),
lockTime: TimeMath.toAbs(126n, timerBrand),
};
t.deepEqual(schedule.nextAuctionSchedule, firstSchedule);

Expand All @@ -93,6 +94,7 @@ test('schedule start to finish', async t => {
endRate: 6500n,
startDelay: TimeMath.toRel(1n, timerBrand),
clockStep: TimeMath.toRel(2n, timerBrand),
lockTime: TimeMath.toAbs(136, timerBrand),
});

t.is(fakeAuctioneer.getState().step, 1);
Expand Down Expand Up @@ -130,6 +132,7 @@ test('schedule start to finish', async t => {
endRate: 6500n,
startDelay: TimeMath.toRel(1n, timerBrand),
clockStep: TimeMath.toRel(2n, timerBrand),
lockTime: TimeMath.toAbs(136n, timerBrand),
};
t.deepEqual(finalSchedule.nextAuctionSchedule, secondSchedule);

Expand All @@ -150,6 +153,7 @@ test('schedule start to finish', async t => {
endRate: 6500n,
startDelay: TimeMath.toRel(1n, timerBrand),
clockStep: TimeMath.toRel(2n, timerBrand),
lockTime: TimeMath.toAbs(146n, timerBrand),
});

t.is(fakeAuctioneer.getState().step, 4);
Expand Down Expand Up @@ -521,6 +525,7 @@ test('duration = freq', async t => {
endRate: 50n,
startDelay: TimeMath.toRel(5n, timerBrand),
clockStep: TimeMath.toRel(60n, timerBrand),
lockTime: TimeMath.toAbs(345n, timerBrand),
};
t.deepEqual(schedule.nextAuctionSchedule, firstSchedule);

Expand All @@ -535,6 +540,7 @@ test('duration = freq', async t => {
endRate: 50n,
startDelay: TimeMath.toRel(5n, timerBrand),
clockStep: TimeMath.toRel(60n, timerBrand),
lockTime: TimeMath.toAbs(705n, timerBrand),
};
t.deepEqual(schedule.nextAuctionSchedule, secondSchedule);
});

0 comments on commit c29f2c2

Please sign in to comment.