-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: DefiLlama validator for oss-directory (#2791)
* This will check if the URLs are well formed * It will also check whether the slugs are valid against the DefiLlama API * Then I added the validator to the GitHub app so that it can generate error messages in PR comments
- Loading branch information
Showing
8 changed files
with
869 additions
and
316 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
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,5 @@ | ||
interface GenericValidator { | ||
isValid(addr: string): Promise<boolean>; | ||
} | ||
|
||
export { GenericValidator }; |
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 +1,2 @@ | ||
export * from "./onchain/evm.js"; | ||
export * from "./web/defillama.js"; |
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,51 @@ | ||
import { DefiLlamaValidator } from "./defillama.js"; | ||
|
||
const DEFILLAMA_API_TIMEOUT = 10000; // 10s | ||
|
||
describe("sum module", () => { | ||
let v: DefiLlamaValidator; | ||
beforeEach(() => { | ||
v = new DefiLlamaValidator(); | ||
}); | ||
|
||
test("isValidUrl", () => { | ||
expect(v.isValidUrl("abc")).toBe(false); | ||
expect(v.isValidUrl("https://www.opensource.observer")).toBe(false); | ||
expect(v.isValidUrl("https://defillama.com/protocol")).toBe(false); | ||
expect(v.isValidUrl("https://defillama.com/protocol/")).toBe(false); | ||
expect(v.isValidUrl("https://defillama.com/protocol/slug")).toBe(true); | ||
expect(v.isValidUrl("https://defillama.com/protocol/slug/")).toBe(true); | ||
expect(v.isValidUrl("https://defillama.com/protocol/slug/extra")).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
test( | ||
"isValidSlug", | ||
async () => { | ||
expect(await v.isValidSlug("INVALID-SLUG")).toBe(false); | ||
expect(await v.isValidSlug("uniswap-v3")).toBe(true); | ||
}, | ||
DEFILLAMA_API_TIMEOUT, | ||
); | ||
|
||
test( | ||
"isValid", | ||
async () => { | ||
expect(await v.isValid("https://www.opensource.observer")).toBe(false); | ||
expect(await v.isValid("https://defillama.com/protocol/slug")).toBe( | ||
false, | ||
); | ||
expect(await v.isValid("https://defillama.com/protocol/uniswap-v3")).toBe( | ||
true, | ||
); | ||
}, | ||
DEFILLAMA_API_TIMEOUT, | ||
); | ||
|
||
test("getSlug", () => { | ||
expect(v.getSlug("https://defillama.com/protocol/uniswap-v3")).toBe( | ||
"uniswap-v3", | ||
); | ||
}); | ||
}); |
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,88 @@ | ||
import { GenericValidator } from "../common/interfaces.js"; | ||
|
||
const DEFILLAMA_URL_PREFIX = "https://defillama.com/protocol/"; | ||
const DEFILLAMA_API_PROTOCOLS = "https://api.llama.fi/protocols"; | ||
|
||
class DefiLlamaValidator implements GenericValidator { | ||
private _validSlugs: string[]; | ||
|
||
constructor() { | ||
this._validSlugs = []; | ||
} | ||
|
||
private async _refreshValidSlugs() { | ||
if (this._validSlugs.length > 0) { | ||
return; | ||
} | ||
const protocolsResult = await fetch(DEFILLAMA_API_PROTOCOLS); | ||
const protocols = await protocolsResult.json(); | ||
this._validSlugs = protocols | ||
.map((p: any) => p.slug) | ||
.filter((s: string) => !!s); | ||
} | ||
|
||
/** | ||
* Naive implementation of getting the slug in a DefiLlama URL | ||
* This can definitely be improved | ||
* @param url | ||
* @returns | ||
*/ | ||
getPath(u: string): string { | ||
const noTrailingSlash = u.endsWith("/") ? u.slice(0, -1) : u; | ||
const noPrefix = noTrailingSlash.startsWith(DEFILLAMA_URL_PREFIX) | ||
? noTrailingSlash.replace(DEFILLAMA_URL_PREFIX, "") | ||
: noTrailingSlash; | ||
return noPrefix; | ||
} | ||
|
||
/** | ||
* Just check whether the URL is properly formed locally | ||
*/ | ||
isValidUrl(addr: string): boolean { | ||
if (!addr.startsWith(DEFILLAMA_URL_PREFIX)) { | ||
//Invalid DefiLlama URL: URLs must begin with ${DEFILLAMA_URL_PREFIX} | ||
return false; | ||
} | ||
const path = this.getPath(addr); | ||
if (path.includes("/")) { | ||
//Invalid DefiLlama URL: URL must point to root protocol address | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check whether the slug is valid by querying the DefiLlama API | ||
*/ | ||
async isValidSlug(slug: string): Promise<boolean> { | ||
await this._refreshValidSlugs(); | ||
const found = this._validSlugs.find((s: string) => s === slug); | ||
return !!found; | ||
} | ||
|
||
/** | ||
* Checks any arbitrary string to see if it is a valid DefiLlama URL | ||
* @param addr | ||
* @returns | ||
*/ | ||
async isValid(addr: string): Promise<boolean> { | ||
const validUrl = this.isValidUrl(addr); | ||
const slug = this.getPath(addr); | ||
const validSlug = await this.isValidSlug(slug); | ||
return validUrl && validSlug; | ||
} | ||
|
||
/** | ||
* Get the slug from a DefiLlama URL | ||
*/ | ||
getSlug(addr: string): string { | ||
const validUrl = this.isValidUrl(addr); | ||
if (!validUrl) { | ||
throw new Error("Invalid DefiLlama URL"); | ||
} | ||
const slug = this.getPath(addr); | ||
return slug; | ||
} | ||
} | ||
|
||
export { DefiLlamaValidator }; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.