Skip to content

Commit

Permalink
fix: importing erc 20 tokens (#1659)
Browse files Browse the repository at this point in the history
* Revert "fix: use official notation for standards"

This reverts commit b314194.

* chore: align naming

* chore: use enums instead of magic strings
  • Loading branch information
Tuditi authored Jan 3, 2024
1 parent a19acaa commit f6a5f38
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { addNewTrackedTokenToActiveProfile } from '@core/wallet/actions'
import { TokenStandard, TokenTrackingStatus } from '@core/token/enums'
import { IConnectedDapp } from '../interface'
import { localize } from '@core/i18n'
import { NftStandard } from '@core/nfts/enums'

interface WatchAssetParams {
type: EvmAssetStandard
Expand All @@ -24,7 +25,7 @@ export async function handleWatchAsset(
chain: IChain,
responseCallback: (params: CallbackParameters) => void
): Promise<void> {
if (params.type !== 'ERC20' && params.type !== 'ERC721') {
if (params.type !== TokenStandard.Erc20 && params.type !== NftStandard.Erc721) {
responseCallback({ error: getSdkError('UNSUPPORTED_METHODS') })
return
}
Expand Down Expand Up @@ -63,7 +64,7 @@ async function getAssetInfo(
chain: IChain
): Promise<{ name: string; decimals: number; symbol: string } | undefined> {
switch (type) {
case 'ERC20': {
case TokenStandard.Erc20: {
const { address } = options
const contract = chain.getContract(ContractType.Erc20, address)
const name = options.name ? options.name : await contract?.methods.name().call()
Expand All @@ -72,7 +73,7 @@ async function getAssetInfo(

return { name: name, decimals, symbol }
}
case 'ERC721':
case NftStandard.Erc721:
// TODO
break
default:
Expand All @@ -91,15 +92,15 @@ function trackAsset(
networkId: NetworkId
): void {
switch (type) {
case 'ERC20':
case TokenStandard.Erc20:
addNewTrackedTokenToActiveProfile(
networkId,
address,
{ ...assetInfo, standard: TokenStandard.Erc20 },
TokenTrackingStatus.ManuallyTracked
)
break
case 'ERC721':
case NftStandard.Erc721:
// TODO
break
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ export class EvmExplorerApi extends BaseApi implements IExplorerApi {

async getAssetMetadata(assetAddress: string): Promise<IExplorerAssetMetadata | undefined> {
const response = await this.get<IExplorerAssetMetadata>(`tokens/${assetAddress}`)
return response
if (response) {
response.type = response.type.replace('-', '') as TokenStandard.Erc20 | NftStandard.Erc721
return response
}
}

async getAssetsForAddress(
address: string,
standard: TokenStandard.Erc20 | NftStandard.Erc721 = TokenStandard.Erc20
): Promise<IExplorerAsset[]> {
const tokenType = standard.replace('ERC', 'ERC-')
const response = await this.get<{ items: IExplorerAsset[]; next_page_params: unknown }>(
`addresses/${address}/tokens?type=${standard}`
`addresses/${address}/tokens?type=${tokenType}`
)

if (response) {
return response?.items ?? []
return (response?.items ?? []).map((asset) => ({
...asset,
token: {
...asset.token,
type: asset.token.type.replace('-', ''),
},
}))
} else {
return []
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum NftStandard {
Irc27 = 'IRC27',
Erc721 = 'ERC-721',
Erc721 = 'ERC721',
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum TokenStandard {
BaseToken = 'BASE_TOKEN',
Irc30 = 'IRC30',
Erc20 = 'ERC-20',
Erc20 = 'ERC20',
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jest.mock('@core/token/stores/persisted-tokens.store', () => ({
}))

jest.mock('@core/token/actions/getAccountTokensForAccount', () => ({
getAccountTokensForSelectedAccount: jest.fn((_) => {
getAccountTokensForAccount: jest.fn((_) => {
return {
[SupportedNetworkId.Testnet]: {
baseCoin: PERSISTED_ASSET_SHIMMER,
Expand Down

0 comments on commit f6a5f38

Please sign in to comment.