Remove Babel (#1074)
* Test helpers no longer rely on Babel. * Behaviours are no longer imported. * Removed Babel dependency. * Fixed linter errors.
This commit is contained in:
@ -1 +1,5 @@
|
||||
export default 'revert';
|
||||
const EVMRevert = 'revert';
|
||||
|
||||
module.exports = {
|
||||
EVMRevert,
|
||||
};
|
||||
|
||||
@ -1 +1,5 @@
|
||||
export default 'invalid opcode';
|
||||
const EVMThrow = 'invalid opcode';
|
||||
|
||||
module.exports = {
|
||||
EVMThrow,
|
||||
};
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export function advanceBlock () {
|
||||
function advanceBlock () {
|
||||
return new Promise((resolve, reject) => {
|
||||
web3.currentProvider.sendAsync({
|
||||
jsonrpc: '2.0',
|
||||
@ -11,7 +11,7 @@ export function advanceBlock () {
|
||||
}
|
||||
|
||||
// Advances the block number so that the last mined block is `number`.
|
||||
export default async function advanceToBlock (number) {
|
||||
async function advanceToBlock (number) {
|
||||
if (web3.eth.blockNumber > number) {
|
||||
throw Error(`block number ${number} is in the past (current is ${web3.eth.blockNumber})`);
|
||||
}
|
||||
@ -20,3 +20,8 @@ export default async function advanceToBlock (number) {
|
||||
await advanceBlock();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
advanceBlock,
|
||||
advanceToBlock,
|
||||
};
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export default async promise => {
|
||||
async function assertJump (promise) {
|
||||
try {
|
||||
await promise;
|
||||
assert.fail('Expected invalid opcode not received');
|
||||
@ -6,4 +6,8 @@ export default async promise => {
|
||||
const invalidOpcodeReceived = error.message.search('invalid opcode') >= 0;
|
||||
assert(invalidOpcodeReceived, `Expected "invalid opcode", got ${error} instead`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
assertJump,
|
||||
};
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export default async promise => {
|
||||
async function assertRevert (promise) {
|
||||
try {
|
||||
await promise;
|
||||
assert.fail('Expected revert not received');
|
||||
@ -6,4 +6,8 @@ export default async promise => {
|
||||
const revertFound = error.message.search('revert') >= 0;
|
||||
assert(revertFound, `Expected "revert", got ${error} instead`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
assertRevert,
|
||||
};
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
const SolidityEvent = require('web3/lib/web3/event.js');
|
||||
|
||||
export default function decodeLogs (logs, contract, address) {
|
||||
function decodeLogs (logs, contract, address) {
|
||||
return logs.map(log => {
|
||||
const event = new SolidityEvent(null, contract.events[log.topics[0]], address);
|
||||
return event.decode(log);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
decodeLogs,
|
||||
};
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
export default function ether (n) {
|
||||
function ether (n) {
|
||||
return new web3.BigNumber(web3.toWei(n, 'ether'));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ether,
|
||||
};
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
const should = require('chai').should();
|
||||
|
||||
const inLogs = async (logs, eventName, eventArgs = {}) => {
|
||||
async function inLogs (logs, eventName, eventArgs = {}) {
|
||||
const event = logs.find(e => e.event === eventName);
|
||||
should.exist(event);
|
||||
for (const [k, v] of Object.entries(eventArgs)) {
|
||||
@ -8,12 +8,12 @@ const inLogs = async (logs, eventName, eventArgs = {}) => {
|
||||
event.args[k].should.eq(v);
|
||||
}
|
||||
return event;
|
||||
};
|
||||
}
|
||||
|
||||
const inTransaction = async (tx, eventName, eventArgs = {}) => {
|
||||
async function inTransaction (tx, eventName, eventArgs = {}) {
|
||||
const { logs } = await tx;
|
||||
return inLogs(logs, eventName, eventArgs);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
inLogs,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export default async promise => {
|
||||
async function expectThrow (promise) {
|
||||
try {
|
||||
await promise;
|
||||
} catch (error) {
|
||||
@ -18,4 +18,8 @@ export default async promise => {
|
||||
return;
|
||||
}
|
||||
assert.fail('Expected throw not received');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
expectThrow,
|
||||
};
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import latestTime from './latestTime';
|
||||
const { latestTime } = require('./latestTime');
|
||||
|
||||
// Increases ganache time by the passed duration in seconds
|
||||
export default function increaseTime (duration) {
|
||||
function increaseTime (duration) {
|
||||
const id = Date.now();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@ -31,14 +31,15 @@ export default function increaseTime (duration) {
|
||||
*
|
||||
* @param target time in seconds
|
||||
*/
|
||||
export async function increaseTimeTo (target) {
|
||||
async function increaseTimeTo (target) {
|
||||
let now = (await latestTime());
|
||||
|
||||
if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`);
|
||||
let diff = target - now;
|
||||
return increaseTime(diff);
|
||||
}
|
||||
|
||||
export const duration = {
|
||||
const duration = {
|
||||
seconds: function (val) { return val; },
|
||||
minutes: function (val) { return val * this.seconds(60); },
|
||||
hours: function (val) { return val * this.minutes(60); },
|
||||
@ -46,3 +47,9 @@ export const duration = {
|
||||
weeks: function (val) { return val * this.days(7); },
|
||||
years: function (val) { return val * this.days(365); },
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
increaseTime,
|
||||
increaseTimeTo,
|
||||
duration,
|
||||
};
|
||||
|
||||
@ -1,7 +1,11 @@
|
||||
import { ethGetBlock } from './web3';
|
||||
const { ethGetBlock } = require('./web3');
|
||||
|
||||
// Returns the time of the last mined block in seconds
|
||||
export default async function latestTime () {
|
||||
async function latestTime () {
|
||||
const block = await ethGetBlock('latest');
|
||||
return block.timestamp;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
latestTime,
|
||||
};
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
import { soliditySha3 } from 'web3-utils';
|
||||
const { soliditySha3 } = require('web3-utils');
|
||||
|
||||
const INTERFACE_ID_LENGTH = 4;
|
||||
|
||||
export default (interfaces = []) => {
|
||||
function makeInterfaceId (interfaces = []) {
|
||||
const interfaceIdBuffer = interfaces
|
||||
.map(methodSignature => soliditySha3(methodSignature)) // keccak256
|
||||
.map(h =>
|
||||
@ -18,4 +18,8 @@ export default (interfaces = []) => {
|
||||
}, Buffer.alloc(INTERFACE_ID_LENGTH));
|
||||
|
||||
return `0x${interfaceIdBuffer.toString('hex')}`;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
makeInterfaceId,
|
||||
};
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { sha3, bufferToHex } from 'ethereumjs-util';
|
||||
const { sha3, bufferToHex } = require('ethereumjs-util');
|
||||
|
||||
export default class MerkleTree {
|
||||
class MerkleTree {
|
||||
constructor (elements) {
|
||||
// Filter empty strings and hash elements
|
||||
this.elements = elements.filter(el => el).map(el => sha3(el));
|
||||
@ -129,3 +129,7 @@ export default class MerkleTree {
|
||||
return Buffer.concat([...args].sort(Buffer.compare));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MerkleTree,
|
||||
};
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
const _ = require('lodash');
|
||||
const ethjsABI = require('ethjs-abi');
|
||||
|
||||
export function findMethod (abi, name, args) {
|
||||
function findMethod (abi, name, args) {
|
||||
for (var i = 0; i < abi.length; i++) {
|
||||
const methodArgs = _.map(abi[i].inputs, 'type').join(',');
|
||||
if ((abi[i].name === name) && (methodArgs === args)) {
|
||||
@ -10,8 +10,13 @@ export function findMethod (abi, name, args) {
|
||||
}
|
||||
}
|
||||
|
||||
export default function sendTransaction (target, name, argsTypes, argsValues, opts) {
|
||||
function sendTransaction (target, name, argsTypes, argsValues, opts) {
|
||||
const abiMethod = findMethod(target.abi, name, argsTypes);
|
||||
const encodedData = ethjsABI.encodeMethod(abiMethod, argsValues);
|
||||
return target.sendTransaction(Object.assign({ data: encodedData }, opts));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
findMethod,
|
||||
sendTransaction,
|
||||
};
|
||||
|
||||
@ -1,22 +1,28 @@
|
||||
import utils from 'ethereumjs-util';
|
||||
const utils = require('ethereumjs-util');
|
||||
|
||||
/**
|
||||
* Hash and add same prefix to the hash that ganache use.
|
||||
* @param {string} message the plaintext/ascii/original message
|
||||
* @return {string} the hash of the message, prefixed, and then hashed again
|
||||
*/
|
||||
export const hashMessage = (message) => {
|
||||
function hashMessage (message) {
|
||||
const messageHex = Buffer.from(utils.sha3(message).toString('hex'), 'hex');
|
||||
const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + messageHex.length.toString());
|
||||
return utils.bufferToHex(utils.sha3(Buffer.concat([prefix, messageHex])));
|
||||
};
|
||||
}
|
||||
|
||||
// signs message using web3 (auto-applies prefix)
|
||||
export const signMessage = (signer, message = '', options = {}) => {
|
||||
function signMessage (signer, message = '', options = {}) {
|
||||
return web3.eth.sign(signer, web3.sha3(message, options));
|
||||
};
|
||||
}
|
||||
|
||||
// signs hex string using web3 (auto-applies prefix)
|
||||
export const signHex = (signer, message = '') => {
|
||||
function signHex (signer, message = '') {
|
||||
return signMessage(signer, message, { encoding: 'hex' });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hashMessage,
|
||||
signMessage,
|
||||
signHex,
|
||||
};
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
|
||||
// from https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
|
||||
module.export = web3.eth.transactionMined = function (txnHash, interval) {
|
||||
// From https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
|
||||
function transactionMined (txnHash, interval) {
|
||||
var transactionReceiptAsync;
|
||||
interval = interval || 500;
|
||||
transactionReceiptAsync = function (txnHash, resolve, reject) {
|
||||
@ -30,4 +29,10 @@ module.export = web3.eth.transactionMined = function (txnHash, interval) {
|
||||
transactionReceiptAsync(txnHash, resolve, reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
web3.eth.transactionMined = transactionMined;
|
||||
|
||||
module.exports = {
|
||||
transactionMined,
|
||||
};
|
||||
|
||||
@ -2,6 +2,8 @@ const pify = require('pify');
|
||||
|
||||
const ethAsync = pify(web3.eth);
|
||||
|
||||
export const ethGetBalance = ethAsync.getBalance;
|
||||
export const ethSendTransaction = ethAsync.sendTransaction;
|
||||
export const ethGetBlock = ethAsync.getBlock;
|
||||
module.exports = {
|
||||
ethGetBalance: ethAsync.getBalance,
|
||||
ethSendTransaction: ethAsync.sendTransaction,
|
||||
ethGetBlock: ethAsync.getBlock,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user