-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
69 lines (63 loc) · 2.28 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const Web3 = require('web3')
const assert = require('assert')
const BN = Web3.utils.BN
BN.fromEth = ethValue => new BN(ethValue).mul(new BN('1000000000000000000'))
BN.prototype.over = function (divisor) {
return this.div(new BN(divisor))
}
const extendedAssert = new Proxy(assert, {
get(target, property) {
const handler = {
equalByComparator: function (actual, expected, message, comparator) {
let err = message
if (!(message instanceof Error)) {
err = new target.AssertionError({
message: message,
actual: actual.toString(),
expected: expected.toString(),
})
}
return target.ok(comparator(actual, expected), err)
},
equal: function (actual, expected, message) {
if (typeof actual.eq === 'function') {
return this.equalByComparator(actual, expected, message, (a, b) => a.eq(b))
}
return target.equal(actual, expected, message)
},
strictEqual: function (actual, expected, message) {
if (typeof actual.eq === 'function') {
return this.equalByComparator(actual, expected, message, (a, b) => a.eq(b))
}
return target.strictEqual(actual, expected, message)
},
eventArgsEqual: function (actual, expected, message) {
const actualArguments = actual.args
Object.entries(expected).forEach(([arg, value]) => {
this.strictEqual(actualArguments[arg], value, `${message}: ${arg}`)
}, this)
},
}
return Object.hasOwnProperty.call(handler, property) ? handler[property] : target[property]
},
})
const getEventsByName = (transaction, eventName) => {
if (eventName != null) {
if (typeof eventName === 'string') return transaction.logs.filter(log => log.event === eventName)
if (eventName instanceof Array) return transaction.logs.filter(log => new Set(eventName).has(log.event))
}
return transaction.logs
}
const toEthString = weiValue => {
let ethValue = weiValue
while (ethValue.length <= 18) ethValue = `0${ethValue}`
return `${ethValue.substr(0, ethValue.length - 18)}.${ethValue.substr(-18)}`
}
const asyncSleep = ms => new Promise(resolve => setTimeout(resolve, ms))
module.exports = {
BN,
getEventsByName,
assert: extendedAssert,
toEthString,
asyncSleep,
}