Skip to content

Commit ff573e9

Browse files
committed
feat: working fetch-tokenlist + logo asset download
1 parent 73a1d1a commit ff573e9

File tree

1 file changed

+69
-23
lines changed

1 file changed

+69
-23
lines changed

src/lib/scripts/fetch-tokenlist.js

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const CONFIG = {
1212
'https://raw.githubusercontent.com/burrbear-dev/default-lists/main/src/tokens/mainnet/defaultTokenList.json',
1313
TARGET_TOKEN_FILE: 'src/tokenlists/balancer/tokens/berachain.ts',
1414
ASSETS_DIR: 'src/assets/images/tokens',
15-
LOG_FILE: 'fetch-tokenlist.log',
15+
LOG_FILE: `fetch-tokenlist-${new Date()
16+
.toISOString()
17+
.replace(/[:.]/g, '-')
18+
.slice(0, 19)}.log`,
1619
}
1720

18-
// GitHub authentication
19-
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
20-
2121
// Statistics tracking
2222
const stats = {
2323
totalTokens: 0,
@@ -43,14 +43,6 @@ function curlGet(url, options = {}) {
4343
'2', // wait 2 seconds between retries
4444
]
4545

46-
// Add GitHub authentication if token is provided
47-
if (
48-
GITHUB_TOKEN &&
49-
(url.includes('github.com') || url.includes('raw.githubusercontent.com'))
50-
) {
51-
curlOptions.push('-H', `Authorization: token ${GITHUB_TOKEN}`)
52-
}
53-
5446
if (options.output) {
5547
curlOptions.push('-o', options.output)
5648
}
@@ -213,9 +205,15 @@ async function updateTokenList(tokenAddresses) {
213205
.map((addr) => ` '${addr}'`)
214206
.join(',\n')}\n]`
215207

216-
// Backup original file
208+
// Backup original file BEFORE any changes
217209
if (fs.existsSync(targetFile)) {
218-
fs.copyFileSync(targetFile, `${targetFile}.backup`)
210+
const timestamp = new Date()
211+
.toISOString()
212+
.replace(/[:.]/g, '-')
213+
.slice(0, 19)
214+
const backupFile = `${targetFile}.backup-${timestamp}`
215+
fs.copyFileSync(targetFile, backupFile)
216+
log(`Backup created: ${path.basename(backupFile)}`)
219217
}
220218

221219
// Write new content
@@ -230,6 +228,56 @@ async function updateTokenList(tokenAddresses) {
230228
}
231229
}
232230

231+
/**
232+
* Download logo assets using curl with better path handling
233+
*/
234+
async function downloadAssets(tokens) {
235+
log('Starting asset downloads using curl...')
236+
237+
// Ensure assets directory exists
238+
fs.ensureDirSync(path.resolve(CONFIG.ASSETS_DIR))
239+
240+
for (const token of tokens) {
241+
if (!token.logoURI) {
242+
log(`Token ${token.symbol || token.address} missing logoURI`, 'warning')
243+
continue
244+
}
245+
246+
// Extract file extension from original URL
247+
const originalFilename = extractFilenameFromUrl(token.logoURI)
248+
if (!originalFilename) {
249+
log(`Could not extract filename from URL: ${token.logoURI}`, 'warning')
250+
continue
251+
}
252+
253+
// Get file extension from original filename
254+
const fileExtension = path.extname(originalFilename)
255+
256+
// Use token address as filename with original extension
257+
const filename = `${token.address}${fileExtension}`
258+
const targetPath = path.resolve(CONFIG.ASSETS_DIR, filename)
259+
260+
// Skip if file already exists
261+
if (fs.existsSync(targetPath)) {
262+
log(`Skipping existing file: ${filename}`)
263+
stats.skippedDownloads++
264+
continue
265+
}
266+
267+
try {
268+
// Use curl with proper path escaping
269+
const curlCommand = `curl -s -L --max-time 30 --retry 3 --retry-delay 2 -o "${targetPath}" "${token.logoURI}"`
270+
execSync(curlCommand, { shell: true })
271+
272+
log(`Downloaded: ${originalFilename} -> ${filename}`)
273+
stats.successfulDownloads++
274+
} catch (error) {
275+
log(`Failed to download ${filename}: ${error.message}`, 'error')
276+
stats.failedDownloads++
277+
}
278+
}
279+
}
280+
233281
/**
234282
* Print summary report
235283
*/
@@ -238,6 +286,9 @@ function printSummary() {
238286
console.log(chalk.cyan('FETCH TOKENLIST SUMMARY'))
239287
console.log('='.repeat(50))
240288
console.log(`Total tokens processed: ${stats.totalTokens}`)
289+
console.log(`Successful downloads: ${chalk.green(stats.successfulDownloads)}`)
290+
console.log(`Failed downloads: ${chalk.red(stats.failedDownloads)}`)
291+
console.log(`Skipped downloads: ${chalk.yellow(stats.skippedDownloads)}`)
241292
console.log(`Token list updated successfully!`)
242293

243294
if (stats.errors.length > 0) {
@@ -255,14 +306,6 @@ function printSummary() {
255306
async function integrateTokenList() {
256307
log('Starting token list integration workflow...')
257308

258-
// Check for GitHub token
259-
if (!GITHUB_TOKEN) {
260-
log(
261-
'Warning: No GITHUB_TOKEN provided. This may fail if the repository is private.',
262-
'warning'
263-
)
264-
}
265-
266309
try {
267310
// Clear log file
268311
fs.writeFileSync(CONFIG.LOG_FILE, '')
@@ -276,7 +319,10 @@ async function integrateTokenList() {
276319
// Step 3: Update token list file
277320
await updateTokenList(tokenAddresses)
278321

279-
// Step 4: Print summary
322+
// Step 4: Download logo assets
323+
await downloadAssets(validTokens)
324+
325+
// Step 5: Print summary
280326
printSummary()
281327

282328
log('Token list integration completed successfully!', 'success')

0 commit comments

Comments
 (0)