Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Merge d32f8b9 into 04fafbb
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Dec 1, 2020
2 parents 04fafbb + d32f8b9 commit 8b612ab
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/services/factories/tokenList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,12 @@ export function getTokensFactory(factoryParams: {
}
}

async function updateTokenDetails(networkId: number, numTokens: number): Promise<void> {
const tokensConfig = tokenListApi.getTokens(networkId)
async function updateTokenDetails(
networkId: number,
numTokens: number,
tokensConfig: TokenDetails[] = [],
): Promise<void> {
if (tokensConfig.length === 0) tokensConfig = tokenListApi.getTokens(networkId)

// Get filtered ids and addresses
const filteredAddressesAndIds = await _getFilteredIdsMap(networkId, numTokens, tokensConfig)
Expand All @@ -284,6 +288,36 @@ export function getTokensFactory(factoryParams: {
tokenListApi.persistTokens({ networkId, tokenList: tokenDetails })
}

async function filterAndFillTokenList(networkId: Network, tokensConfig: TokenDetails[]): Promise<TokenDetails[]> {
const tokensWithNoDetails: TokenDetails[] = []
const tokensWithDetails = tokensConfig.reduce<TokenDetails[]>((acc, token) => {
// fill in addressMainnet if not present and on Mainnet
if (networkId === Network.Mainnet && !token.addressMainnet) {
token.addressMainnet = token.address
}

if (!token.name || !token.symbol) {
tokensWithNoDetails.push(token)
return acc
}

acc.push(token)
return acc
}, [])

// bail out if no non-detailed tokens
if (tokensWithNoDetails.length === 0) return tokensWithDetails

const updatedTokens = await Promise.all(
tokensWithNoDetails.map(async (token) => ({
...token,
...(await getErc20DetailsOrAddress(networkId, token.address)),
})),
)

return tokensWithDetails.concat(updatedTokens)
}

async function updateTokens(networkId: number): Promise<void> {
areTokensUpdated.add(networkId)

Expand All @@ -293,16 +327,19 @@ export function getTokensFactory(factoryParams: {
tokenListApi.setListReady(false)

const numTokens = await retry(() => exchangeApi.getNumTokens(networkId))
const tokens = tokenListApi.getTokens(networkId)
const preTokens = tokenListApi.getTokens(networkId)

logDebug(`[tokenListFactory][${networkId}] Contract has ${numTokens}; local list has ${preTokens.length}`)

logDebug(`[tokenListFactory][${networkId}] Contract has ${numTokens}; local list has ${tokens.length}`)
// Check token list for any tokens with missing details and fill if so
const tokens = await filterAndFillTokenList(networkId, preTokens)

// TODO: review this logic
// why update tokenDetails when token list lengths don't match?
// why would token IDs not also need to be updated?
if (numTokens > tokens.length) {
// When there are more tokens in the contract than locally, fetch the new tokens
await updateTokenDetails(networkId, numTokens)
await updateTokenDetails(networkId, numTokens, tokens)
} else {
// Otherwise, only update the ids
await updateTokenIds(networkId, tokens)
Expand Down

0 comments on commit 8b612ab

Please sign in to comment.