dagger-contracts/test/ids.js

53 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

const { ethers } = require("hardhat")
const { keccak256, defaultAbiCoder } = ethers.utils
function requestId(request) {
2023-03-08 11:02:34 +00:00
const Ask = "tuple(int64, uint256, uint256, uint256, uint256, uint256, int64)"
2023-11-22 11:47:31 +00:00
const Content = "tuple(string, bytes32)"
const Request =
"tuple(address, " + Ask + ", " + Content + ", uint256, bytes32)"
return keccak256(defaultAbiCoder.encode([Request], requestToArray(request)))
}
function askToArray(ask) {
return [
ask.slots,
ask.slotSize,
ask.duration,
ask.proofProbability,
ask.reward,
2023-03-08 11:02:34 +00:00
ask.collateral,
ask.maxSlotLoss,
]
}
function contentToArray(content) {
return [content.cid, content.merkleRoot]
}
function requestToArray(request) {
return [
[
request.client,
askToArray(request.ask),
contentToArray(request.content),
request.expiry,
request.nonce,
],
]
}
2022-07-19 09:33:54 +00:00
function slotId(slot) {
const types = "tuple(bytes32, uint256)"
const values = [slot.request, slot.index]
const encoding = defaultAbiCoder.encode([types], [values])
return keccak256(encoding)
}
module.exports = {
requestId,
2022-07-19 09:33:54 +00:00
slotId,
requestToArray,
askToArray,
}