2023-10-16 09:14:02 +00:00
|
|
|
const { advanceTimeToForNextBlock, currentTime } = require("./evm")
|
2022-11-11 02:33:28 +00:00
|
|
|
const { slotId, requestId } = require("./ids")
|
2024-10-08 07:38:19 +00:00
|
|
|
const { maxPrice, payoutForDuration } = require("./price")
|
2022-09-07 05:25:01 +00:00
|
|
|
|
2024-05-06 13:13:32 +00:00
|
|
|
/**
|
|
|
|
* @dev This will not advance the time right on the "expiry threshold" but will most probably "overshoot it"
|
|
|
|
* because "currentTime" most probably is not the time at which the request is created, but it is used
|
|
|
|
* in the next timestamp calculation with `now + expiry`.
|
|
|
|
* @param request
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2022-09-23 02:33:39 +00:00
|
|
|
async function waitUntilCancelled(request) {
|
2024-05-06 13:13:32 +00:00
|
|
|
// We do +1, because the expiry check in contract is done as `>` and not `>=`.
|
|
|
|
await advanceTimeToForNextBlock((await currentTime()) + request.expiry + 1)
|
2022-08-04 02:14:36 +00:00
|
|
|
}
|
|
|
|
|
2024-10-08 07:38:19 +00:00
|
|
|
async function waitUntilSlotsFilled(contract, request, proof, token, slots) {
|
|
|
|
await token.approve(contract.address, request.ask.collateral * slots.length)
|
|
|
|
|
|
|
|
let requestEnd = (await contract.requestEnd(requestId(request))).toNumber()
|
|
|
|
const payouts = []
|
|
|
|
for (let slotIndex of slots) {
|
|
|
|
await contract.reserveSlot(requestId(request), slotIndex)
|
|
|
|
await contract.fillSlot(requestId(request), slotIndex, proof)
|
2023-03-08 11:02:34 +00:00
|
|
|
|
2024-10-08 07:38:19 +00:00
|
|
|
payouts[slotIndex] = payoutForDuration(
|
|
|
|
request,
|
|
|
|
await currentTime(),
|
|
|
|
requestEnd
|
|
|
|
)
|
2022-09-13 07:20:07 +00:00
|
|
|
}
|
2024-10-08 07:38:19 +00:00
|
|
|
|
|
|
|
return payouts
|
|
|
|
}
|
|
|
|
|
|
|
|
async function waitUntilStarted(contract, request, proof, token) {
|
|
|
|
return waitUntilSlotsFilled(
|
|
|
|
contract,
|
|
|
|
request,
|
|
|
|
proof,
|
|
|
|
token,
|
|
|
|
Array.from({ length: request.ask.slots }, (_, i) => i)
|
|
|
|
)
|
2022-09-13 07:20:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:49:37 +00:00
|
|
|
async function waitUntilFinished(contract, requestId) {
|
|
|
|
const end = (await contract.requestEnd(requestId)).toNumber()
|
2024-05-06 13:13:32 +00:00
|
|
|
// We do +1, because the end check in contract is done as `>` and not `>=`.
|
2023-10-16 09:14:02 +00:00
|
|
|
await advanceTimeToForNextBlock(end + 1)
|
2022-09-21 09:57:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 10:50:37 +00:00
|
|
|
async function waitUntilFailed(contract, request) {
|
|
|
|
slot = { request: requestId(request), slot: 0 }
|
2022-09-23 02:33:39 +00:00
|
|
|
for (let i = 0; i <= request.ask.maxSlotLoss; i++) {
|
2022-09-21 09:57:26 +00:00
|
|
|
slot.index = i
|
|
|
|
let id = slotId(slot)
|
2022-11-23 14:10:58 +00:00
|
|
|
await contract.forciblyFreeSlot(id)
|
2022-09-21 09:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:50:37 +00:00
|
|
|
async function waitUntilSlotFailed(contract, request, slot) {
|
|
|
|
let index = 0
|
|
|
|
let freed = 0
|
|
|
|
while (freed <= request.ask.maxSlotLoss) {
|
|
|
|
if (index !== slot.index) {
|
|
|
|
await contract.forciblyFreeSlot(slotId({ ...slot, index }))
|
|
|
|
freed++
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 07:09:48 +00:00
|
|
|
function patchOverloads(contract) {
|
|
|
|
contract.freeSlot = async (slotId, rewardRecipient, collateralRecipient) => {
|
|
|
|
const logicalXor = (a, b) => (a || b) && !(a && b)
|
|
|
|
if (logicalXor(rewardRecipient, collateralRecipient)) {
|
|
|
|
// XOR, if exactly one is truthy
|
|
|
|
throw new Error(
|
|
|
|
"Invalid freeSlot overload, you must specify both `rewardRecipient` and `collateralRecipient` or neither."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rewardRecipient && !collateralRecipient) {
|
|
|
|
// calls `freeSlot` overload without `rewardRecipient` and `collateralRecipient`
|
|
|
|
const fn = contract["freeSlot(bytes32)"]
|
|
|
|
return await fn(slotId)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn = contract["freeSlot(bytes32,address,address)"]
|
|
|
|
return await fn(slotId, rewardRecipient, collateralRecipient)
|
|
|
|
}
|
|
|
|
contract.withdrawFunds = async (requestId, withdrawRecipient) => {
|
|
|
|
if (!withdrawRecipient) {
|
|
|
|
// calls `withdrawFunds` overload without `withdrawRecipient`
|
|
|
|
const fn = contract["withdrawFunds(bytes32)"]
|
|
|
|
return await fn(requestId)
|
|
|
|
}
|
|
|
|
const fn = contract["withdrawFunds(bytes32,address)"]
|
|
|
|
return await fn(requestId, withdrawRecipient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 09:57:26 +00:00
|
|
|
module.exports = {
|
|
|
|
waitUntilCancelled,
|
|
|
|
waitUntilStarted,
|
2024-10-08 07:38:19 +00:00
|
|
|
waitUntilSlotsFilled,
|
2022-09-21 09:57:26 +00:00
|
|
|
waitUntilFinished,
|
|
|
|
waitUntilFailed,
|
2022-12-19 10:50:37 +00:00
|
|
|
waitUntilSlotFailed,
|
2024-08-19 07:09:48 +00:00
|
|
|
patchOverloads,
|
2022-09-21 09:57:26 +00:00
|
|
|
}
|