Replace revert strings with custom errors (#4261)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
const { forward } = require('../helpers/time');
|
||||
const { ProposalState } = require('./enums');
|
||||
|
||||
function zip(...args) {
|
||||
return Array(Math.max(...args.map(array => array.length)))
|
||||
@ -196,6 +197,44 @@ class GovernorHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes a list ProposalStates into a bytes32 representation where each bit enabled corresponds to
|
||||
* the underlying position in the `ProposalState` enum. For example:
|
||||
*
|
||||
* 0x000...10000
|
||||
* ^^^^^^------ ...
|
||||
* ^----- Succeeded
|
||||
* ^---- Defeated
|
||||
* ^--- Canceled
|
||||
* ^-- Active
|
||||
* ^- Pending
|
||||
*/
|
||||
function proposalStatesToBitMap(proposalStates, options = {}) {
|
||||
if (!Array.isArray(proposalStates)) {
|
||||
proposalStates = [proposalStates];
|
||||
}
|
||||
const statesCount = Object.keys(ProposalState).length;
|
||||
let result = 0;
|
||||
|
||||
const uniqueProposalStates = new Set(proposalStates.map(bn => bn.toNumber())); // Remove duplicates
|
||||
for (const state of uniqueProposalStates) {
|
||||
if (state < 0 || state >= statesCount) {
|
||||
expect.fail(`ProposalState ${state} out of possible states (0...${statesCount}-1)`);
|
||||
} else {
|
||||
result |= 1 << state;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.inverted) {
|
||||
const mask = 2 ** statesCount - 1;
|
||||
result = result ^ mask;
|
||||
}
|
||||
|
||||
const hex = web3.utils.numberToHex(result);
|
||||
return web3.utils.padLeft(hex, 64);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
GovernorHelper,
|
||||
proposalStatesToBitMap,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user