-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from map3xyz/db-utils
Adding Db utils to release database and update algolia index
- Loading branch information
Showing
10 changed files
with
1,629 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@map3xyz/assets-helper", | ||
"version": "1.0.180", | ||
"version": "1.0.188", | ||
"description": "A library for maintaining the assets repo.", | ||
"author": "pellicceama", | ||
"keywords": [ | ||
|
@@ -18,11 +18,12 @@ | |
"scripts": { | ||
"index": "ts-node ./src/index.ts", | ||
"clean": "rimraf ./dist/ ./exec/", | ||
"build": "yarn clean && tsc", | ||
"build": "yarn clean && tsc && cp -r ./src/releaser/schema ./dist/releaser/schema", | ||
"preparePublish": "yarn build && npm version patch", | ||
"prepareTest": "yarn ts-node ./src/cache.ts", | ||
"test": "yarn prepareTest && yarn build && ava", | ||
"bundle": "yarn build && pkg . --out-dir ./exec/" | ||
"bundle": "yarn build && pkg . --out-dir ./exec/", | ||
"release-db": "ts-node ./src/releaser/sync/sync-database.ts" | ||
}, | ||
"devDependencies": { | ||
"@types/ethereum-checksum-address": "^0.0.0", | ||
|
@@ -34,13 +35,24 @@ | |
"typescript": "^4.7.4" | ||
}, | ||
"dependencies": { | ||
"@octokit/auth-token": "^3.0.0", | ||
"@octokit/rest": "^19.0.3", | ||
"@types/moment": "^2.13.0", | ||
"@types/sqlite3": "^3.1.8", | ||
"@uniswap/token-lists": "^1.0.0-beta.30", | ||
"algoliasearch": "^4.15.0", | ||
"axios": "^0.27.2", | ||
"csv-parse": "^5.3.0", | ||
"dotenv": "^16.0.1", | ||
"ethereum-checksum-address": "^0.0.8", | ||
"ethers": "^5.7.2", | ||
"jsonschema": "^1.4.1", | ||
"ksuid": "^3.0.0", | ||
"moment": "^2.29.4", | ||
"octokit": "^2.0.5", | ||
"promised-sqlite3": "^1.2.0", | ||
"shelljs": "^0.8.5", | ||
"sqlite3": "^5.0.11", | ||
"uuid": "^8.3.2" | ||
}, | ||
"packageManager": "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import algoliasearch, { SearchIndex } from "algoliasearch"; | ||
import { networksForEach, assetsForEach } from "../../db"; | ||
import { ObjectType, Description, ChainObject, Network, Asset } from "../../model"; | ||
|
||
const updateOptions = { | ||
createIfNotExists: true, | ||
}; | ||
|
||
declare interface IndexedAsset { | ||
objectID: string; | ||
address?: string; | ||
networkCode: string; | ||
type: ObjectType; | ||
name: string; | ||
symbol: string; | ||
description: Description | {}; | ||
logoUrl: string | null; | ||
} | ||
|
||
function transform({ | ||
id: objectID, | ||
networkCode, | ||
type, | ||
name, | ||
symbol, | ||
description, | ||
logo, | ||
}: ChainObject): IndexedAsset { | ||
const logoParsed = logo; | ||
const logoUrl = logoParsed?.svg || logoParsed?.png || null; | ||
description = description || {}; | ||
|
||
return { | ||
objectID, | ||
networkCode, | ||
type, | ||
name, | ||
symbol, | ||
description, | ||
logoUrl, | ||
}; | ||
} | ||
|
||
export async function updateAlgoliaSearch( | ||
directory: string, | ||
batchSize: Number = 250 | ||
): Promise<void> { | ||
if(!process.env.ALGOLIA_APP_ID) { | ||
throw new Error('Missing Algolia App ID'); | ||
} | ||
|
||
if(!process.env.ALGOLIA_API_KEY) { | ||
throw new Error('Missing Algolia API Key'); | ||
} | ||
|
||
const client = algoliasearch(process.env.ALGOLIA_APP_ID, | ||
process.env.ALGOLIA_API_KEY); | ||
const assets = client.initIndex("assets"); | ||
|
||
try { | ||
await assets.setSettings({ | ||
searchableAttributes: [ | ||
"objectID", | ||
"unordered(name)", | ||
"symbol", | ||
"unordered(description)", | ||
"address", | ||
], | ||
attributesForFaceting: ["type", "networkCode"], | ||
}); | ||
|
||
await updateNetworks(assets, batchSize, directory); | ||
await updateAssets(assets, batchSize, directory); | ||
|
||
console.log('Updated Algolia Search') | ||
Promise.resolve(); | ||
} catch (err: any) { | ||
console.error('Error with Algolia search', err); | ||
throw err; | ||
} | ||
} | ||
|
||
async function updateNetworks(index: SearchIndex, batchSize: Number, dir: string) { | ||
let buffer: IndexedAsset[] = []; | ||
async function updateNetwork(networkInfo: Network) { | ||
if (buffer.length < batchSize) { | ||
buffer.push(transform(networkInfo)); | ||
} else { | ||
await index.partialUpdateObjects(buffer, updateOptions); | ||
buffer = []; | ||
} | ||
} | ||
async function completeUpdate() { | ||
if (buffer.length > 0) { | ||
const response = await index.partialUpdateObjects(buffer, updateOptions); | ||
} | ||
} | ||
|
||
await networksForEach(updateNetwork, completeUpdate, dir); | ||
return Promise.resolve(); | ||
} | ||
|
||
async function updateAssets(index: SearchIndex, batchSize: Number, dir: string) { | ||
let buffer: IndexedAsset[] = []; | ||
async function updateAsset(assetInfo: Asset) { | ||
if (buffer.length < batchSize) { | ||
const asset = transform(assetInfo); | ||
buffer.push({ ...asset, address: assetInfo.address }); | ||
} else { | ||
await index.partialUpdateObjects(buffer, updateOptions); | ||
buffer = []; | ||
} | ||
|
||
return Promise.resolve(); | ||
} | ||
async function completeUpdate() { | ||
if (buffer.length > 0) { | ||
await index.partialUpdateObjects(buffer, updateOptions); | ||
} | ||
return Promise.resolve(); | ||
} | ||
|
||
await assetsForEach(updateAsset, completeUpdate, dir); | ||
return Promise.resolve(); | ||
} |
Oops, something went wrong.