-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: memoize fetchErc20Decimals, relocate to utils/token, and ap…
…ply to other instances (#27088)
- Loading branch information
Showing
8 changed files
with
119 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ERC20_DEFAULT_DECIMALS = 18; |
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,50 @@ | ||
import { getTokenStandardAndDetails } from '../../../store/actions'; | ||
import { ERC20_DEFAULT_DECIMALS } from '../constants/token'; | ||
import { fetchErc20Decimals } from './token'; | ||
|
||
const MOCK_ADDRESS = '0x514910771af9ca656af840dff83e8264ecf986ca'; | ||
const MOCK_DECIMALS = 36; | ||
|
||
jest.mock('../../../store/actions', () => ({ | ||
getTokenStandardAndDetails: jest.fn(), | ||
})); | ||
|
||
describe('fetchErc20Decimals', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
/** Reset memoized function using getTokenStandardAndDetails for each test */ | ||
fetchErc20Decimals?.cache?.clear?.(); | ||
}); | ||
|
||
it(`should return the default number, ${ERC20_DEFAULT_DECIMALS}, if no decimals were found from details`, async () => { | ||
(getTokenStandardAndDetails as jest.Mock).mockResolvedValue({}); | ||
const decimals = await fetchErc20Decimals(MOCK_ADDRESS); | ||
|
||
expect(decimals).toBe(ERC20_DEFAULT_DECIMALS); | ||
}); | ||
|
||
it('should return the decimals for a given token address', async () => { | ||
(getTokenStandardAndDetails as jest.Mock).mockResolvedValue({ | ||
decimals: MOCK_DECIMALS, | ||
}); | ||
const decimals = await fetchErc20Decimals(MOCK_ADDRESS); | ||
|
||
expect(decimals).toBe(MOCK_DECIMALS); | ||
}); | ||
|
||
it('should memoize the result for the same token addresses', async () => { | ||
(getTokenStandardAndDetails as jest.Mock).mockResolvedValue({ | ||
decimals: MOCK_DECIMALS, | ||
}); | ||
|
||
const firstCallResult = await fetchErc20Decimals(MOCK_ADDRESS); | ||
const secondCallResult = await fetchErc20Decimals(MOCK_ADDRESS); | ||
|
||
expect(firstCallResult).toBe(secondCallResult); | ||
expect(getTokenStandardAndDetails).toHaveBeenCalledTimes(1); | ||
|
||
await fetchErc20Decimals('0xDifferentAddress'); | ||
expect(getTokenStandardAndDetails).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
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,32 @@ | ||
import { memoize } from 'lodash'; | ||
import { Hex } from '@metamask/utils'; | ||
import { getTokenStandardAndDetails } from '../../../store/actions'; | ||
|
||
export const ERC20_DEFAULT_DECIMALS = 18; | ||
|
||
/** | ||
* Fetches the decimals for the given token address. | ||
* | ||
* @param {Hex | string} address - The ethereum token contract address. It is expected to be in hex format. | ||
* We currently accept strings since we have a patch that accepts a custom string | ||
* {@see .yarn/patches/@metamask-eth-json-rpc-middleware-npm-14.0.1-b6c2ccbe8c.patch} | ||
*/ | ||
export const fetchErc20Decimals = memoize( | ||
async (address: Hex | string): Promise<number> => { | ||
try { | ||
const { decimals: decStr } = await getTokenStandardAndDetails(address); | ||
if (!decStr) { | ||
return ERC20_DEFAULT_DECIMALS; | ||
} | ||
for (const radix of [10, 16]) { | ||
const parsedDec = parseInt(decStr, radix); | ||
if (isFinite(parsedDec)) { | ||
return parsedDec; | ||
} | ||
} | ||
return ERC20_DEFAULT_DECIMALS; | ||
} catch { | ||
return ERC20_DEFAULT_DECIMALS; | ||
} | ||
}, | ||
); |