discover-dapps/utils/Utils.js

36 lines
1.6 KiB
JavaScript
Raw Normal View History

/* global assert */
function isException(error) {
let strError = error.toString();
return strError.includes('VM Exception') || strError.includes('invalid opcode') || strError.includes('invalid JUMP');
}
function ensureException(error) {
assert(isException(error), error.toString());
}
const PREFIX = "VM Exception while processing transaction: ";
async function tryCatch(promise, message) {
try {
await promise;
throw null;
}
catch (error) {
assert(error, "Expected an error but did not get one");
assert(error.message.startsWith(PREFIX + message), "Expected an error starting with '" + PREFIX + message + "' but got '" + error.message + "' instead");
}
};
module.exports = {
zeroAddress : '0x0000000000000000000000000000000000000000',
isException : isException,
ensureException : ensureException,
catchRevert : async function(promise) {await tryCatch(promise, "revert" );},
catchOutOfGas : async function(promise) {await tryCatch(promise, "out of gas" );},
catchInvalidJump : async function(promise) {await tryCatch(promise, "invalid JUMP" );},
catchInvalidOpcode : async function(promise) {await tryCatch(promise, "invalid opcode" );},
catchStackOverflow : async function(promise) {await tryCatch(promise, "stack overflow" );},
catchStackUnderflow : async function(promise) {await tryCatch(promise, "stack underflow" );},
catchStaticStateChange : async function(promise) {await tryCatch(promise, "static state change");},
};