dagger-contracts/test/marketplace.js
Eric 73a2ca0bd3
feat: adds an optional payoutAddress to allow payouts to be paid to separate address (#144)
* initial commit for splitting payouts

Collateral goes to slot's host address, while reward payouts go to the slot's host payoutAddress

* Add fillSlot overload to make payoutAddress "optional"

* add tests for payoutAddress

* add doc to patchFillSlotOverloads

* formatting

* remove optional payoutAddress parameter

* Move payoutAddress to freeSlot

- remove payoutAddress parameter from `fillSlot`
- remove `payoutAddress` from slot struct and storage
- add payoutAddress parameter to `freeSlot`, preventing the need for storage

* formatting

* update certora spec to match updated function signature

* Add withdrawAddress to withdrawFunds

- prevent erc20 msg.sender blacklisting

* Update tests for paying out to withdrawAddress

* formatting

* Add collateralRecipient

* refactor: change withdrawFunds and freeSlot overloads

- `withdrawFunds` now has an option withdrawRecipient parameter
- `freeSlot` now has two optional parameters: rewardRecipient, and collateralRecipient. Both or none must be specified.

* update certora spec for new sigs
2024-08-19 17:09:48 +10:00

90 lines
2.9 KiB
JavaScript

const { advanceTimeToForNextBlock, currentTime } = require("./evm")
const { slotId, requestId } = require("./ids")
const { price } = require("./price")
/**
* @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>}
*/
async function waitUntilCancelled(request) {
// We do +1, because the expiry check in contract is done as `>` and not `>=`.
await advanceTimeToForNextBlock((await currentTime()) + request.expiry + 1)
}
async function waitUntilStarted(contract, request, proof, token) {
await token.approve(contract.address, price(request) * request.ask.slots)
for (let i = 0; i < request.ask.slots; i++) {
await contract.fillSlot(requestId(request), i, proof)
}
}
async function waitUntilFinished(contract, requestId) {
const end = (await contract.requestEnd(requestId)).toNumber()
// We do +1, because the end check in contract is done as `>` and not `>=`.
await advanceTimeToForNextBlock(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.forciblyFreeSlot(id)
}
}
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++
}
}
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,
waitUntilFinished,
waitUntilFailed,
waitUntilSlotFailed,
patchOverloads,
}