Skip to content

Commit

Permalink
Update bulk CSV import to support min/max whitelist validation
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandomg committed Apr 9, 2018
1 parent 5437728 commit bab0e89
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/components/Common/WhitelistInputBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
19 changes: 11 additions & 8 deletions src/utils/processWhitelist.js
Original file line number Diff line number Diff line change
@@ -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++
})

Expand Down
12 changes: 6 additions & 6 deletions src/utils/processWhitelist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'],
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit bab0e89

Please sign in to comment.