###### tags: `strunfun` # abicachemodule ## Description Convenience Module to cache Contract ABIs in Local Storage. This serves the purpose that we could load most any ABI on-the-fly and have recently used available without loading. So the main usecase is calling the`getABI` function. Where we provide the `address"` of the contract. Which should at best be a verified contract on etherscan from the mainnet. It would return `undefined` if we neither have the contract cached nor it is retrievable over the ABI Cache Service. ## Expectation to the Environment - `allModules.networkmodule` - dependency [networkmodule](https://hackmd.io/mJLlooBoT-u0AxreoISIkQ?view) - service access [ABI Cache Service](https://hackmd.io/taH_79GQSSiMeZaTlhGkLQ?view) - `allModules.statemodule` - dependency [statemodule](https://hackmd.io/nn708ujuRLeo0crmfr2X_Q?view) - `allModules.configmodule` - dependency configmodule - `allModules.configmodule.ABICacheSize` - config `ABICacheSize` ## Structure - `network°` - `state°` - `addressABIMap°` - `cacheEntries[` - `maxCacheSize#` - `.initialize§` - `touchCache§` `address"` - `.getABI§` `address"` ## Specification - `network°` = the reference to the external `networkmodule` - `state°` = the reference to the external `statemodule` - `addressABIMap°` = map which maps contract `address"` to the corresponding `abi°` - is reflected as `persistentState` - `cacheEntries[` = - list keeping track of the currently cached contracts by their `address"` - is in order of recency - `maxCacheSize#` = defines how many entries wie keep in `cacheEntries` - `.initialize§` = - wires up the external references - recovers `maxCacheSize#` from the configuration - recovers `addressABIMap°` from the `state°` - recovers `cacheEntries[` from the `addressABIMap°` - ensures `addressABIMap°` and `cacheEntries[` comply with `maxCacheSize#` - `touchCache§` `address"` = - unshifts the most recently used cache entry to the front of the list - forgets the latest entries which are too much - `.getABI§` `address"` = if contract is available in the cache then return `addessABIMap[address]` otherwise retrieve the abi from the ABI Cache Service `touchCache(address)` `return addressABIMap[address]`