From bab0e896cc38c78dffae88e21ccb7d53639d128d Mon Sep 17 00:00:00 2001 From: fernandomg Date: Mon, 9 Apr 2018 15:37:12 -0300 Subject: [PATCH] Update bulk CSV import to support min/max whitelist validation --- src/components/Common/WhitelistInputBlock.js | 5 ++++- src/utils/processWhitelist.js | 19 +++++++++++-------- src/utils/processWhitelist.spec.js | 12 ++++++------ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/components/Common/WhitelistInputBlock.js b/src/components/Common/WhitelistInputBlock.js index 27141f5e7..cc6e02680 100644 --- a/src/components/Common/WhitelistInputBlock.js +++ b/src/components/Common/WhitelistInputBlock.js @@ -181,7 +181,10 @@ export class WhitelistInputBlock extends React.Component { Papa.parse(file, { skipEmptyLines: true, complete: results => { - const { called } = processWhitelist(results.data, item => { + const { called } = processWhitelist({ + rows: results.data, + decimals: this.props.decimals + }, item => { this.props.tierStore.addWhitelistItem(item, this.props.num) }) diff --git a/src/utils/processWhitelist.js b/src/utils/processWhitelist.js index 6d220e2e0..153c0494b 100644 --- a/src/utils/processWhitelist.js +++ b/src/utils/processWhitelist.js @@ -1,26 +1,29 @@ -import Web3 from 'web3' - -const isNumber = (number) => !isNaN(parseFloat(number)) +import { isAddress, validateWhitelistMin, validateWhitelistMax } from './validations' /** * 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 {Object} whitelistInformation + * @param {Array} whitelistInformation.rows Array of whitelist items. Each element in the array has the structure + * `[address, min, max]`, for example: `['0x1234567890123456789012345678901234567890', '1', '10']` + * @param {Number} whitelistInformation.decimals Amount of decimals allowed for the min and max values * @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) { +export default function ({ rows, decimals }, 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 + if ( + isAddress()(addr) || + validateWhitelistMin({ min, max, decimals }) || + validateWhitelistMax({ min, max, decimals }) + ) return cb({ addr, min, max }) - called++ }) diff --git a/src/utils/processWhitelist.spec.js b/src/utils/processWhitelist.spec.js index b03d9fa42..294be21ee 100644 --- a/src/utils/processWhitelist.spec.js +++ b/src/utils/processWhitelist.spec.js @@ -11,7 +11,7 @@ describe('processWhitelist function', () => { const cb = jest.fn() // When - processWhitelist(rows, cb) + processWhitelist({ rows, decimals: 3 }, cb) // Then expect(cb).toHaveBeenCalledTimes(3) @@ -20,7 +20,7 @@ describe('processWhitelist function', () => { 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', () => { + it('should ignore items that don\'t have 3 elements', () => { // Given const rows = [ ['1', '10'], @@ -33,7 +33,7 @@ describe('processWhitelist function', () => { const cb = jest.fn() // When - processWhitelist(rows, cb) + processWhitelist({ rows, decimals: 3 }, cb) // Then expect(cb).toHaveBeenCalledTimes(0) @@ -49,7 +49,7 @@ describe('processWhitelist function', () => { const cb = jest.fn() // When - const { called } = processWhitelist(rows, cb) + const { called } = processWhitelist({ rows, decimals: 3 }, cb) // Then expect(called).toBe(3) @@ -66,7 +66,7 @@ describe('processWhitelist function', () => { const cb = jest.fn() // When - const { called } = processWhitelist(rows, cb) + const { called } = processWhitelist({ rows, decimals: 3 }, cb) // Then expect(called).toBe(0) @@ -83,7 +83,7 @@ describe('processWhitelist function', () => { const cb = jest.fn() // When - const { called } = processWhitelist(rows, cb) + const { called } = processWhitelist({ rows, decimals: 3 }, cb) // Then expect(called).toBe(0)