Skip to content

Commit

Permalink
chore: added cache of resolved addresses and tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
naddison36 committed Jul 26, 2021
1 parent f7eb453 commit 6d197a3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
36 changes: 36 additions & 0 deletions tasks/utils/networkAddressFactory.ts
Expand Up @@ -201,6 +201,18 @@ export const getNetworkAddress = (contractName: ContractNames, hre: HardhatRunti
return getChainAddress(contractName, chain)
}

// Singleton instances of different contract names and token symbols
const resolvedAddressesInstances: { [contractNameSymbol: string]: { [tokenType: string]: string } } = {}

// Update the singleton instance so we don't need to resolve this next time
const updateResolvedAddresses = (addressContractNameSymbol: string, tokenType: AssetAddressTypes, address: string) => {
if (resolvedAddressesInstances[addressContractNameSymbol]) {
resolvedAddressesInstances[addressContractNameSymbol][tokenType] = address
} else {
resolvedAddressesInstances[addressContractNameSymbol] = { [tokenType]: address }
}
}

// Resolves a contract name or token symbol to an ethereum address
export const resolveAddress = (
addressContractNameSymbol: string,
Expand All @@ -210,6 +222,10 @@ export const resolveAddress = (
let address = addressContractNameSymbol
// If not an Ethereum address
if (!addressContractNameSymbol.match(ethereumAddress)) {
// If previously resolved then return from singleton instances
if (resolvedAddressesInstances[addressContractNameSymbol]?.[tokenType])
return resolvedAddressesInstances[addressContractNameSymbol][tokenType]

// If an mStable contract name
address = getChainAddress(addressContractNameSymbol as ContractNames, chain)

Expand All @@ -222,21 +238,41 @@ export const resolveAddress = (

address = token[tokenType]
console.log(`Resolved asset with symbol ${addressContractNameSymbol} and type "${tokenType}" to address ${address}`)

// Update the singleton instance so we don't need to resolve this next time
updateResolvedAddresses(addressContractNameSymbol, tokenType, address)
return address
}

console.log(`Resolved contract name "${addressContractNameSymbol}" to address ${address}`)

// Update the singleton instance so we don't need to resolve this next time
updateResolvedAddresses(addressContractNameSymbol, tokenType, address)

return address
}
return address
}

// Singleton instances of different contract names and token symbols
const resolvedTokenInstances: { [address: string]: { [tokenType: string]: Token } } = {}

export const resolveToken = (symbol: string, chain = Chain.mainnet, tokenType: AssetAddressTypes = "address"): Token => {
// If previously resolved then return from singleton instances
if (resolvedTokenInstances[symbol]?.[tokenType]) return resolvedTokenInstances[symbol][tokenType]

// If a token Symbol
const token = tokens.find((t) => t.symbol === symbol && t.chain === chain)
if (!token) throw Error(`Can not fine token symbol ${symbol} on chain ${chain}`)
if (!token[tokenType]) throw Error(`Can not find token type "${tokenType}" for ${symbol} on chain ${chain}`)

console.log(`Resolved token symbol ${symbol} and type "${tokenType}" to address ${token[tokenType]}`)

if (resolvedTokenInstances[symbol]) {
resolvedTokenInstances[symbol][tokenType] = token
} else {
resolvedTokenInstances[symbol] = { [tokenType]: token }
}

return token
}
2 changes: 1 addition & 1 deletion tasks/weekly.ts
Expand Up @@ -13,7 +13,7 @@ task("distribute-mta-mainnet", "Distributes MTA rewards on Mainnet")
const rewardSymbol = MTA.symbol
const ownerTokenType: AssetAddressTypes = "vault"
const vaultsOrPools: Array<Token | ContractNames> = [mUSD, mBTC, GUSD, BUSD, alUSD, HBTC, TBTC, MTA, "UniswapV2-MTA/WETH"]
const mtaAmounts = [23278.21, 9016.99, 43980.33, 23324.25, 30180.15, 16966.51, 14729.57, 40000, 5000]
const mtaAmounts = [22482.92, 9812.28, 49124.82, 22966.19, 28172.94, 14351.23, 14565.61, 40000, 5000]

// Create a comma separated list of token symbols and amounts
const symbolOrNames = vaultsOrPools.map((v) => {
Expand Down
4 changes: 2 additions & 2 deletions test-utils/fork.ts
Expand Up @@ -23,8 +23,8 @@ export const impersonate = async (addr: string, fund = true): Promise<Signer> =>
return ethers.provider.getSigner(addr)
}

export const impersonateAccount = async (address: string): Promise<Account> => {
const signer = await impersonate(address)
export const impersonateAccount = async (address: string, fund = true): Promise<Account> => {
const signer = await impersonate(address, fund)
return {
signer,
address,
Expand Down

0 comments on commit 6d197a3

Please sign in to comment.