Skip to content

Commit

Permalink
perf(multicall): remove the validation that caused app to feel sluggish
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Jun 4, 2020
1 parent 4c28f34 commit 5a1a469
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
22 changes: 14 additions & 8 deletions src/state/multicall/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ import { parseCallKey, toCallKey } from './actions'

describe('actions', () => {
describe('#parseCallKey', () => {
it('throws for invalid address', () => {
expect(() => parseCallKey('0x-0x')).toThrow('Invalid address: 0x')
it('does not throw for invalid address', () => {
expect(parseCallKey('0x-0x')).toEqual({ address: '0x', callData: '0x' })
})
it('throws for invalid calldata', () => {
expect(() => parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-abc')).toThrow('Invalid hex: abc')
it('does not throw for invalid calldata', () => {
expect(parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-abc')).toEqual({
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
callData: 'abc'
})
})
it('throws for invalid format', () => {
expect(() => parseCallKey('abc')).toThrow('Invalid call key: abc')
})
it('throws for uppercase hex', () => {
expect(() => parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-0xabcD')).toThrow('Invalid hex: 0xabcD')
it('throws for uppercase calldata', () => {
expect(parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-0xabcD')).toEqual({
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
callData: '0xabcD'
})
})
it('parses pieces into address', () => {
expect(parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-0xabcd')).toEqual({
address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
callData: '0xabcd'
})
})
Expand Down Expand Up @@ -44,7 +50,7 @@ describe('actions', () => {
})
it('concatenates address to data', () => {
expect(toCallKey({ address: '0x6b175474e89094c44da98b954eedeac495271d0f', callData: '0xabcd' })).toEqual(
'0x6B175474E89094C44Da98b954EedeAC495271d0F-0xabcd'
'0x6b175474e89094c44da98b954eedeac495271d0f-0xabcd'
)
})
})
Expand Down
18 changes: 4 additions & 14 deletions src/state/multicall/actions.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
import { createAction } from '@reduxjs/toolkit'
import { isAddress } from '../../utils'

export interface Call {
address: string
callData: string
}

const ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/
const LOWER_HEX_REGEX = /^0x[a-f0-9]*$/
export function toCallKey(call: Call): string {
const addr = isAddress(call.address)
if (!addr) {
if (!ADDRESS_REGEX.test(call.address)) {
throw new Error(`Invalid address: ${call.address}`)
}
if (!LOWER_HEX_REGEX.test(call.callData)) {
throw new Error(`Invalid hex: ${call.callData}`)
}
return `${addr}-${call.callData}`
return `${call.address}-${call.callData}`
}

export function parseCallKey(callKey: string): Call {
const pcs = callKey.split('-')
if (pcs.length !== 2) {
throw new Error(`Invalid call key: ${callKey}`)
}
const addr = isAddress(pcs[0])
if (!addr) {
throw new Error(`Invalid address: ${pcs[0]}`)
}

if (!LOWER_HEX_REGEX.test(pcs[1])) {
throw new Error(`Invalid hex: ${pcs[1]}`)
}

return {
address: addr,
address: pcs[0],
callData: pcs[1]
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/state/multicall/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import reducer, { MulticallState } from './reducer'
import { Store, createStore } from '@reduxjs/toolkit'

const DAI_ADDRESS = '0x6b175474e89094c44da98b954eedeac495271d0f'
const CHECKSUMMED_DAI_ADDRESS = '0x6B175474E89094C44Da98b954EedeAC495271d0F'

describe('multicall reducer', () => {
let store: Store<MulticallState>
Expand Down Expand Up @@ -32,7 +31,7 @@ describe('multicall reducer', () => {
expect(store.getState()).toEqual({
callListeners: {
[1]: {
[`${CHECKSUMMED_DAI_ADDRESS}-0x`]: {
[`${DAI_ADDRESS}-0x`]: {
[1]: 1
}
}
Expand Down Expand Up @@ -82,7 +81,7 @@ describe('multicall reducer', () => {
)
expect(store.getState()).toEqual({
callResults: {},
callListeners: { [1]: { [`${CHECKSUMMED_DAI_ADDRESS}-0x`]: {} } }
callListeners: { [1]: { [`${DAI_ADDRESS}-0x`]: {} } }
})
})
})
Expand Down

1 comment on commit 5a1a469

@vercel
Copy link

@vercel vercel bot commented on 5a1a469 Jun 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.