-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into metamask-not-being-called-#632
- Loading branch information
Showing
12 changed files
with
200 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Web3 from 'web3' | ||
|
||
const isNumber = (number) => !isNaN(parseFloat(number)) | ||
|
||
/** | ||
* Execute a callback with each valid whitelist item in the given list | ||
* | ||
* @param {Array} rows Array of whitelist items. Each element in the array has the structure `[address, min, max]`, for | ||
* example: `['0x1234567890123456789012345678901234567890', '1', '10']` | ||
* @param {Function} cb The function to be called with each valid item | ||
* @returns {Object} Object with a `called` property, indicating the number of times the callback was called | ||
*/ | ||
export default function (rows, cb) { | ||
let called = 0 | ||
rows.forEach((row) => { | ||
if (row.length !== 3) return | ||
|
||
const [addr, min, max] = row | ||
|
||
if (!Web3.utils.isAddress(addr) || !isNumber(min) || !isNumber(max)) return | ||
|
||
cb({ addr, min, max }) | ||
|
||
called++ | ||
}) | ||
|
||
return { called } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import processWhitelist from './processWhitelist' | ||
|
||
describe('processWhitelist function', () => { | ||
it('should call the callback for each whitelist item', () => { | ||
// Given | ||
const rows = [ | ||
['0x1111111111111111111111111111111111111111', '1', '10'], | ||
['0x2222222222222222222222222222222222222222', '1', '10'], | ||
['0x3333333333333333333333333333333333333333', '1', '10'] | ||
] | ||
const cb = jest.fn() | ||
|
||
// When | ||
processWhitelist(rows, cb) | ||
|
||
// Then | ||
expect(cb).toHaveBeenCalledTimes(3) | ||
expect(cb.mock.calls[0]).toEqual([{ addr: rows[0][0], min: rows[0][1], max: rows[0][2] }]) | ||
expect(cb.mock.calls[1]).toEqual([{ addr: rows[1][0], min: rows[1][1], max: rows[1][2] }]) | ||
expect(cb.mock.calls[2]).toEqual([{ addr: rows[2][0], min: rows[2][1], max: rows[2][2] }]) | ||
}) | ||
|
||
it('should ignore items that don\t have 3 elements', () => { | ||
// Given | ||
const rows = [ | ||
['1', '10'], | ||
['0x2222222222222222222222222222222222222222', '10'], | ||
['0x3333333333333333333333333333333333333333', '1'], | ||
['0x4444444444444444444444444444444444444444'], | ||
[], | ||
['0x4444444444444444444444444444444444444444', '1', '10', '100'], | ||
] | ||
const cb = jest.fn() | ||
|
||
// When | ||
processWhitelist(rows, cb) | ||
|
||
// Then | ||
expect(cb).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it('should return the number of times the callback was called', () => { | ||
// Given | ||
const rows = [ | ||
['0x1111111111111111111111111111111111111111', '1', '10'], | ||
['0x2222222222222222222222222222222222222222', '1', '10'], | ||
['0x3333333333333333333333333333333333333333', '1', '10'] | ||
] | ||
const cb = jest.fn() | ||
|
||
// When | ||
const { called } = processWhitelist(rows, cb) | ||
|
||
// Then | ||
expect(called).toBe(3) | ||
}) | ||
|
||
it('should ignore invalid numbers', () => { | ||
// Given | ||
const rows = [ | ||
['0x1111111111111111111111111111111111111111', 'foo', '10'], | ||
['0x2222222222222222222222222222222222222222', '1', 'bar'], | ||
['0x3333333333333333333333333333333333333333', '', '10'], | ||
['0x4444444444444444444444444444444444444444', '1', ''] | ||
] | ||
const cb = jest.fn() | ||
|
||
// When | ||
const { called } = processWhitelist(rows, cb) | ||
|
||
// Then | ||
expect(called).toBe(0) | ||
}) | ||
|
||
it('should ignore invalid addresses', () => { | ||
// Given | ||
const rows = [ | ||
['0x123456789012345678901234567890123456789', '1', '10'], // 41 characters | ||
['0x12345678901234567890123456789012345678901', '1', '10'], // 43 characters | ||
['0x90F8bf6A479f320ead074411a4B0e7944Ea8c9CG', '1', '10'], // invalid character | ||
['0x90F8bf6A479f320ead074411a4B0e7944Ea8c9c1', '1', '10'] // invalid checksum | ||
] | ||
const cb = jest.fn() | ||
|
||
// When | ||
const { called } = processWhitelist(rows, cb) | ||
|
||
// Then | ||
expect(called).toBe(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule token-wizard-test-automation
added at
03c628