Mark Spanbroek b5ab3869b9 marketplace: better tests for repair
Co-Authored-By: Adam Uhlíř <adam@uhlir.dev>
2025-06-12 15:50:09 +02:00

113 lines
3.4 KiB
JavaScript

const { advanceTimeTo, currentTime } = require("./evm")
const { slotId, requestId } = require("./ids")
const { payoutForDuration } = require("./price")
const { collateralPerSlot } = require("./collateral")
async function waitUntilCancelled(contract, request) {
const expiry = await contract.requestExpiry(requestId(request))
// We do +1, because the expiry check in contract is done as `>` and not `>=`.
await advanceTimeTo(expiry + 1)
}
async function waitUntilSlotFilled(contract, request, proof, token, slotIndex) {
let collateral = collateralPerSlot(request)
await token.approve(contract.address, collateral)
await contract.reserveSlot(requestId(request), slotIndex)
await contract.fillSlot(requestId(request), slotIndex, proof)
const start = await currentTime()
const end = await contract.requestEnd(requestId(request))
return payoutForDuration(request, start, end)
}
async function waitUntilSlotsFilled(contract, request, proof, token, slots) {
const payouts = []
for (let slotIndex of slots) {
payouts[slotIndex] = await waitUntilSlotFilled(
contract,
request,
proof,
token,
slotIndex
)
}
return payouts
}
async function waitUntilStarted(contract, request, proof, token) {
return waitUntilSlotsFilled(
contract,
request,
proof,
token,
Array.from({ length: request.ask.slots }, (_, i) => i)
)
}
async function waitUntilFinished(contract, requestId) {
const end = await contract.requestEnd(requestId)
// We do +1, because the end check in contract is done as `>` and not `>=`.
await advanceTimeTo(end + 1)
}
async function waitUntilFailed(contract, request) {
slot = { request: requestId(request), slot: 0 }
for (let i = 0; i <= request.ask.maxSlotLoss; i++) {
slot.index = i
let id = slotId(slot)
await contract.freeSlot(id)
}
}
async function waitUntilSlotFailed(contract, request, slot) {
let index = 0
let freed = 0
while (freed <= request.ask.maxSlotLoss) {
if (index !== slot.index) {
await contract.freeSlot(slotId({ ...slot, index }))
freed++
}
index++
}
}
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)
}
}
module.exports = {
waitUntilCancelled,
waitUntilStarted,
waitUntilSlotFilled,
waitUntilSlotsFilled,
waitUntilFinished,
waitUntilFailed,
waitUntilSlotFailed,
patchOverloads,
}