Commit 5a1a469f authored by Moody Salem's avatar Moody Salem

perf(multicall): remove the validation that caused app to feel sluggish

parent 4c28f348
...@@ -2,21 +2,27 @@ import { parseCallKey, toCallKey } from './actions' ...@@ -2,21 +2,27 @@ import { parseCallKey, toCallKey } from './actions'
describe('actions', () => { describe('actions', () => {
describe('#parseCallKey', () => { describe('#parseCallKey', () => {
it('throws for invalid address', () => { it('does not throw for invalid address', () => {
expect(() => parseCallKey('0x-0x')).toThrow('Invalid address: 0x') expect(parseCallKey('0x-0x')).toEqual({ address: '0x', callData: '0x' })
}) })
it('throws for invalid calldata', () => { it('does not throw for invalid calldata', () => {
expect(() => parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-abc')).toThrow('Invalid hex: abc') expect(parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-abc')).toEqual({
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
callData: 'abc'
})
}) })
it('throws for invalid format', () => { it('throws for invalid format', () => {
expect(() => parseCallKey('abc')).toThrow('Invalid call key: abc') expect(() => parseCallKey('abc')).toThrow('Invalid call key: abc')
}) })
it('throws for uppercase hex', () => { it('throws for uppercase calldata', () => {
expect(() => parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-0xabcD')).toThrow('Invalid hex: 0xabcD') expect(parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-0xabcD')).toEqual({
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
callData: '0xabcD'
})
}) })
it('parses pieces into address', () => { it('parses pieces into address', () => {
expect(parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-0xabcd')).toEqual({ expect(parseCallKey('0x6b175474e89094c44da98b954eedeac495271d0f-0xabcd')).toEqual({
address: '0x6B175474E89094C44Da98b954EedeAC495271d0F', address: '0x6b175474e89094c44da98b954eedeac495271d0f',
callData: '0xabcd' callData: '0xabcd'
}) })
}) })
...@@ -44,7 +50,7 @@ describe('actions', () => { ...@@ -44,7 +50,7 @@ describe('actions', () => {
}) })
it('concatenates address to data', () => { it('concatenates address to data', () => {
expect(toCallKey({ address: '0x6b175474e89094c44da98b954eedeac495271d0f', callData: '0xabcd' })).toEqual( expect(toCallKey({ address: '0x6b175474e89094c44da98b954eedeac495271d0f', callData: '0xabcd' })).toEqual(
'0x6B175474E89094C44Da98b954EedeAC495271d0F-0xabcd' '0x6b175474e89094c44da98b954eedeac495271d0f-0xabcd'
) )
}) })
}) })
......
import { createAction } from '@reduxjs/toolkit' import { createAction } from '@reduxjs/toolkit'
import { isAddress } from '../../utils'
export interface Call { export interface Call {
address: string address: string
callData: string callData: string
} }
const ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/
const LOWER_HEX_REGEX = /^0x[a-f0-9]*$/ const LOWER_HEX_REGEX = /^0x[a-f0-9]*$/
export function toCallKey(call: Call): string { export function toCallKey(call: Call): string {
const addr = isAddress(call.address) if (!ADDRESS_REGEX.test(call.address)) {
if (!addr) {
throw new Error(`Invalid address: ${call.address}`) throw new Error(`Invalid address: ${call.address}`)
} }
if (!LOWER_HEX_REGEX.test(call.callData)) { if (!LOWER_HEX_REGEX.test(call.callData)) {
throw new Error(`Invalid hex: ${call.callData}`) throw new Error(`Invalid hex: ${call.callData}`)
} }
return `${addr}-${call.callData}` return `${call.address}-${call.callData}`
} }
export function parseCallKey(callKey: string): Call { export function parseCallKey(callKey: string): Call {
...@@ -23,17 +22,8 @@ export function parseCallKey(callKey: string): Call { ...@@ -23,17 +22,8 @@ export function parseCallKey(callKey: string): Call {
if (pcs.length !== 2) { if (pcs.length !== 2) {
throw new Error(`Invalid call key: ${callKey}`) 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 { return {
address: addr, address: pcs[0],
callData: pcs[1] callData: pcs[1]
} }
} }
......
...@@ -3,7 +3,6 @@ import reducer, { MulticallState } from './reducer' ...@@ -3,7 +3,6 @@ import reducer, { MulticallState } from './reducer'
import { Store, createStore } from '@reduxjs/toolkit' import { Store, createStore } from '@reduxjs/toolkit'
const DAI_ADDRESS = '0x6b175474e89094c44da98b954eedeac495271d0f' const DAI_ADDRESS = '0x6b175474e89094c44da98b954eedeac495271d0f'
const CHECKSUMMED_DAI_ADDRESS = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
describe('multicall reducer', () => { describe('multicall reducer', () => {
let store: Store<MulticallState> let store: Store<MulticallState>
...@@ -32,7 +31,7 @@ describe('multicall reducer', () => { ...@@ -32,7 +31,7 @@ describe('multicall reducer', () => {
expect(store.getState()).toEqual({ expect(store.getState()).toEqual({
callListeners: { callListeners: {
[1]: { [1]: {
[`${CHECKSUMMED_DAI_ADDRESS}-0x`]: { [`${DAI_ADDRESS}-0x`]: {
[1]: 1 [1]: 1
} }
} }
...@@ -82,7 +81,7 @@ describe('multicall reducer', () => { ...@@ -82,7 +81,7 @@ describe('multicall reducer', () => {
) )
expect(store.getState()).toEqual({ expect(store.getState()).toEqual({
callResults: {}, callResults: {},
callListeners: { [1]: { [`${CHECKSUMMED_DAI_ADDRESS}-0x`]: {} } } callListeners: { [1]: { [`${DAI_ADDRESS}-0x`]: {} } }
}) })
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment