Skip to content
This repository was archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
add description function to asset service (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
elmutt authored Sep 8, 2021
1 parent 6cef314 commit e826d51
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions packages/asset-service/src/service/AssetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import axios from 'axios'
import localAssetData from './generatedAssetData.json'

export class AssetService {
private assetFileUrl: string
private assetFileUrl: string | undefined

private assetData: BaseAsset[]
private flatAssetData: Asset[]

constructor(assetFileUrl: string) {
constructor(assetFileUrl?: string) {
this.assetFileUrl = assetFileUrl
}

Expand All @@ -25,6 +25,7 @@ export class AssetService {
*/
async initialize() {
try {
if (!this.assetFileUrl) throw new Error()
const { data } = await axios.get<BaseAsset[]>(this.assetFileUrl)
this.assetData = data
} catch (err) {
Expand Down Expand Up @@ -64,4 +65,18 @@ export class AssetService {
? this.flatAssetData.filter((asset) => asset.network == network)
: this.flatAssetData
}

async description(name: string, tokenId?: string): Promise<string | null> {
if (typeof name !== 'string') throw new Error('Invalid asset name')
const contractUrl = typeof tokenId === 'string' ? `/contract/${tokenId?.toLowerCase()}` : ''
try {
const { data } = await axios.get(
`https://api.coingecko.com/api/v3/coins/${name.toLowerCase()}${contractUrl}`
)
return data?.description?.en || null
} catch (e) {
console.error('AssetService:description:error', e)
return null
}
}
}
2 changes: 1 addition & 1 deletion packages/asset-service/src/service/generatedAssetData.json

Large diffs are not rendered by default.

0 comments on commit e826d51

Please sign in to comment.