Skip to content

Commit

Permalink
chore: Handle MKR symbol, improve getIcon.
Browse files Browse the repository at this point in the history
  • Loading branch information
paouvrard committed Dec 21, 2021
1 parent 983e538 commit 95461a6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
67 changes: 49 additions & 18 deletions packages/aurora-erc20/src/natural-erc20/getMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ethers } from 'ethers'
import { getEthProvider, getBridgeParams } from '@near-eth/client/dist/utils'
import { erc20 } from '@near-eth/utils'
import { Account } from 'near-api-js'
import { getEthProvider, getBridgeParams, getNearAccount } from '@near-eth/client/dist/utils'
import { erc20, nep141 } from '@near-eth/utils'

const erc20Decimals: {[key: string]: number} = {}
export async function getDecimals (
Expand Down Expand Up @@ -32,22 +33,45 @@ export async function getDecimals (
}

const erc20Icons: {[key: string]: any} = {}
async function getIcon (address: string): Promise<any> {
if (erc20Icons[address] !== undefined) return erc20Icons[address]

// Checksum address needed to fetch token icons.
const url = `https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/${
ethers.utils.getAddress(address)
}/logo.png`
async function getIcon (
{ erc20Address, options }: {
erc20Address: string
options?: {
nearAccount?: Account
nep141Address?: string
nep141Factory?: string
}
}
): Promise<any> {
if (erc20Icons[erc20Address] !== undefined) return erc20Icons[erc20Address]
options = options ?? {}
const bridgeParams = getBridgeParams()

erc20Icons[address] = await new Promise(resolve => {
const img = new Image()
img.onload = () => resolve(url)
img.onerror = () => resolve(null)
img.src = url
})
let icon
try {
const nearAccount = options.nearAccount ?? await getNearAccount()
const nep141Factory: string = options.nep141Factory ?? bridgeParams.nep141Factory
const nep141Address = erc20Address.replace('0x', '').toLowerCase() + '.' + nep141Factory
const metadata = await nep141.getMetadata({ nep141Address, nearAccount })
icon = metadata.icon
} catch (error) {
console.warn(error)
}
if (!icon) {
// Checksum address needed to fetch token icons.
const url = `https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/${
ethers.utils.getAddress(erc20Address)
}/logo.png`

return erc20Icons[address]
icon = await new Promise(resolve => {
const img = new Image()
img.onload = () => resolve(url)
img.onerror = () => resolve(null)
img.src = url
})
}
erc20Icons[erc20Address] = icon
return icon
}

const erc20Symbols: {[key: string]: string} = {}
Expand All @@ -73,7 +97,11 @@ export async function getSymbol (
erc20Symbols[erc20Address] = symbol
} catch {
console.log(`Failed to read token symbol for: ${erc20Address}`)
symbol = erc20Address.slice(0, 5) + '…'
if (erc20Address.toLowerCase() === '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2') {
symbol = 'MKR'
} else {
symbol = erc20Address.slice(0, 5) + '…'
}
}
return symbol
}
Expand All @@ -96,12 +124,15 @@ export default async function getMetadata (
options?: {
provider?: ethers.providers.Provider
erc20Abi?: string
nearAccount?: Account
nep141Address?: string
nep141Factory?: string
}
}
): Promise<{erc20Address: string, decimals: number, icon: any, symbol: string}> {
const [decimals, icon, symbol] = await Promise.all([
getDecimals({ erc20Address, options }),
getIcon(erc20Address),
getIcon({ erc20Address, options }),
getSymbol({ erc20Address, options })
])
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/aurora-nep141/src/bridged-erc20/getAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function getAuroraErc20Address (
const address = await aurora.getErc20FromNep141({ nep141Address, nearAccount, auroraEvmAccount })
auroraErc20Addresses[nep141Address] = address
} catch (error) {
console.error(error, nep141Address)
console.warn(error, nep141Address)
return null
}
return auroraErc20Addresses[nep141Address]!
Expand Down
6 changes: 5 additions & 1 deletion packages/nep141-erc20/src/natural-erc20/getMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export async function getSymbol (
erc20Symbols[erc20Address] = symbol
} catch {
console.log(`Failed to read token symbol for: ${erc20Address}`)
symbol = erc20Address.slice(0, 5) + '…'
if (erc20Address.toLowerCase() === '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2') {
symbol = 'MKR'
} else {
symbol = erc20Address.slice(0, 5) + '…'
}
}
return symbol
}
Expand Down

0 comments on commit 95461a6

Please sign in to comment.