-
Notifications
You must be signed in to change notification settings - Fork 1.1k
read write functions #10774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
read write functions #10774
Changes from 1 commit
6e78819
541131f
88c0bdb
1678bb1
5a1026c
399bf0a
de9933b
86c8e76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,91 @@ | ||
| import { storeR2JSONString, getR2JSONString } from "../../utils/r2"; | ||
| import { getUnixTimeNow } from "../../api2/utils/time"; | ||
| import { chainsThatShouldNotBeLowerCased } from "../../utils/shared/constants"; | ||
| import { elastic, cache } from "@defillama/sdk"; | ||
|
|
||
| const r2Key = "distressedAssetsList.json"; | ||
|
|
||
| export async function isDistressed(key: string) { | ||
| function sanitizeKey(key: string) { | ||
| const chain = key.split(":")[0]; | ||
| const address = key.substring(chain.length + 1); | ||
| const normalizedAddress = chainsThatShouldNotBeLowerCased.includes(chain) | ||
| ? address | ||
| : address.toLowerCase(); | ||
| const data = await getR2JSONString(r2Key); | ||
| const normalizedAddress = chainsThatShouldNotBeLowerCased.includes(chain) ? address : address.toLowerCase(); | ||
| return `${chain}:${normalizedAddress}`; | ||
| } | ||
|
|
||
| export async function isDistressed(key: string, client?: any) { | ||
| const isLocalClient: boolean = client == undefined | ||
| if (isLocalClient) client = elastic.getClient(); | ||
|
|
||
| if (!data[chain]) return false; | ||
| if (data[chain][normalizedAddress]) return true; | ||
| const _id = sanitizeKey(key) | ||
| const { hits } = await client.search({ | ||
| index: "distressed-assets-store*", | ||
| body: { | ||
| query: { | ||
| match: { _id }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return false; | ||
| if (isLocalClient) await client?.close(); | ||
|
||
|
|
||
| return hits?.hits?.length > 0; | ||
| } | ||
|
|
||
| export async function addToDistressedList(key: string) { | ||
| const chain = key.split(":")[0]; | ||
| const address = key.substring(chain.length + 1); | ||
| const normalizedAddress = chainsThatShouldNotBeLowerCased.includes(chain) | ||
| ? address | ||
| : address.toLowerCase(); | ||
| const data = await getR2JSONString(r2Key); | ||
| export async function addToDistressed(keys: string[], client?: any) { | ||
| const isLocalClient: boolean = client == undefined | ||
| if (isLocalClient) client = elastic.getClient(); | ||
|
|
||
| const body: any[] = []; | ||
| keys.map((key: string) => { | ||
| const _id = sanitizeKey(key) | ||
| body.push({ index: { _index: "distressed-assets-store", _id } }); | ||
| }); | ||
|
|
||
| await client.bulk({ body }); | ||
|
|
||
| if (isLocalClient) await client?.close(); | ||
| } | ||
|
|
||
| export async function logDistressedCoins(keys: string[], protocol: string) { | ||
| await elastic.writeLog("distressed-assets", { keys, protocol, reportTime: getUnixTimeNow() }); | ||
| } | ||
|
|
||
| export async function readDistressedLogs() { | ||
| const esClient = elastic.getClient(); | ||
| const hourAgo = Math.floor(Date.now() / 1000) - 3600; | ||
| let { lastCheckTS } = (await cache.readExpiringJsonCache("distressed-assets-last-check")) || { lastCheckTS: 0 }; | ||
|
||
| if (!lastCheckTS || lastCheckTS < hourAgo) lastCheckTS = hourAgo - 1; | ||
|
|
||
| let { | ||
| hits: { hits }, | ||
| }: any = await esClient?.search({ | ||
| index: "distressed-assets*", | ||
| size: 9999, | ||
| body: { | ||
| query: { | ||
| range: { | ||
| // find records with reportTime > lastCheckTS | ||
| reportTime: { | ||
| gt: lastCheckTS, // reportTime is in ms | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!hits?.length) return; | ||
|
|
||
| const newDistressedCoins: string[] = []; | ||
| hits.map(({ _source: { keys } }: any) => { | ||
| newDistressedCoins.push(...keys); | ||
| }); | ||
|
|
||
| await addToDistressed(newDistressedCoins, esClient); | ||
|
|
||
| const timeNow = Math.floor(Date.now() / 1000); | ||
|
|
||
| if (!data[chain]) data[chain] = {}; | ||
| data[chain].push(normalizedAddress); | ||
| await cache.writeExpiringJsonCache( | ||
| "distressed-assets-last-check", | ||
| { lastCheckTS: timeNow }, | ||
| { expireAfter: 7 * 24 * 3600 } | ||
| ); | ||
|
|
||
| await storeR2JSONString(r2Key, JSON.stringify(data)); | ||
| await esClient?.close(); | ||
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, dont like the idea of using r2 strings as a db, can you replace it with a dedicated index (table in ES) in our coins ES db? are we storing the token mcaps in coins-current index now?