Skip to content

Commit 309480e

Browse files
committed
build tvl artifact
1 parent a0ddcc0 commit 309480e

File tree

5 files changed

+128
-5
lines changed

5 files changed

+128
-5
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Build tvlModules and upload artifact
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
build-and-upload:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout repository
12+
uses: actions/checkout@v4
13+
with:
14+
fetch-depth: 0
15+
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: "20"
20+
21+
- name: Install dependencies
22+
run: npm ci
23+
24+
- name: Run buildImports script
25+
run: node scripts/buildImports.js
26+
27+
- name: Publish tvlModules.json as "latest" release
28+
uses: ncipollo/release-action@v1
29+
with:
30+
tag: latest
31+
name: Latest tvlModules
32+
artifacts: scripts/tvlModules.json
33+
allowUpdates: true

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ yarn.lock
1010
.vscode
1111
projects/binance/data.csv
1212

13-
*.log
13+
*.log
14+
15+
scripts/tvlModules.json

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"weeklyChanges": "git pull && git diff $(git log -1 --before=@{7.days.ago} --format=%H) --stat | grep -E \"projects/\" | cut -d / -f 2 | cut -d \" \" -f 1 | uniq | wc -l",
99
"dev": "babel-watch curve.js",
10+
"build": "node scripts/buildImports.js",
1011
"lint": "eslint -c .eslintrc.js .",
1112
"eslint:github-action": "eslint -c .eslintrc.js .",
1213
"test-interactive": "node utils/testInteractive",

projects/helper/cache.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ function getLink(project, chain) {
1515
return `https://${Bucket}.s3.eu-central-1.amazonaws.com/${getKey(project, chain)}`
1616
}
1717

18-
async function getCache(project, chain, { _ } = {}) {
18+
async function getCache(project, chain, { skipCompression } = {}) {
1919
const Key = getKey(project, chain)
2020
const fileKey = getFileKey(project, chain)
2121

2222
try {
23-
const json = await sdk.cache.readCache(fileKey)
23+
const json = await sdk.cache.readCache(fileKey, { skipCompression})
2424
if (!json || Object.keys(json).length === 0) throw new Error('Invalid data')
2525
return json
2626
} catch (e) {
@@ -36,11 +36,13 @@ async function getCache(project, chain, { _ } = {}) {
3636
}
3737
}
3838

39-
async function setCache(project, chain, cache, { _ } = {}) {
39+
async function setCache(project, chain, cache, { skipCompression } = {}) {
4040
const Key = getKey(project, chain)
4141

4242
try {
43-
await sdk.cache.writeCache(getFileKey(project, chain), cache)
43+
await sdk.cache.writeCache(getFileKey(project, chain), cache, {
44+
skipCompression,
45+
})
4446
} catch (e) {
4547
sdk.log('failed to write data to s3 bucket: ', Key)
4648
sdk.log(e)

scripts/buildImports.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const fs = require("fs")
2+
const { get } = require("../projects/helper/http")
3+
// const { setCache, getCache } = require("../projects/helper/cache")
4+
5+
async function run() {
6+
// await getCache('defi-configs', 'tvlModules')
7+
const configs = await get('https://api.llama.fi/_fe/static/configs')
8+
9+
const moduleMap = {}
10+
const protocols = configs.protocols.concat(configs.treasuries).concat(configs.entities)
11+
12+
console.log('# of protocols/treasuries/entities:', protocols.length)
13+
14+
for (const protocol of protocols) {
15+
try {
16+
17+
if (moduleMap[protocol.module]) continue; // already imported
18+
19+
const modulePath = `../projects/${protocol.module}`
20+
const importedModule = mockFunctions(require(modulePath))
21+
22+
if (importedModule.hallmarks)
23+
importedModule.hallmarks = convertHallmarkStrings(importedModule.hallmarks)
24+
25+
moduleMap[protocol.module] = importedModule
26+
} catch (e) {
27+
console.error(`Error importing ${protocol.module}:`, e)
28+
}
29+
}
30+
31+
fs.writeFileSync('scripts/tvlModules.json', JSON.stringify(moduleMap))
32+
// await setCache('defi-configs', 'tvlModules', moduleMap, {
33+
// skipCompression: true,
34+
// })
35+
36+
process.exit(0)
37+
}
38+
39+
run().catch((e) => {
40+
console.error(e)
41+
process.exit(1)
42+
})
43+
44+
45+
function convertHallmarkStrings(hallmarks) {
46+
if (!Array.isArray(hallmarks)) return hallmarks
47+
return hallmarks.map((item) => {
48+
if (typeof item?.[0] === 'string') {
49+
item[0] = dateStringToTimestamp(item[0])
50+
}
51+
if (Array.isArray(item?.[0])) {
52+
item[0].forEach((subItem, index) => {
53+
if (typeof subItem === 'string') {
54+
item[0][index] = dateStringToTimestamp(subItem)
55+
}
56+
})
57+
}
58+
return item
59+
}).filter((item) => {
60+
if (typeof item?.[0] === 'number') return true
61+
// if it is a range hallmark
62+
if (Array.isArray(item?.[0] && typeof item[0][0] === 'number' && typeof item[0][1] === 'number')) {
63+
return true
64+
}
65+
return false
66+
})
67+
}
68+
69+
//Replace all fuctions with mock functions in an object all the way down
70+
function mockFunctions(obj) {
71+
if (typeof obj === "function") {
72+
return '_f' // llamaMockedTVLFunction
73+
} else if (typeof obj === "object") {
74+
Object.keys(obj).forEach((key) => obj[key] = mockFunctions(obj[key]))
75+
}
76+
return obj
77+
}
78+
79+
function dateStringToTimestamp(dateString) {
80+
81+
let timestamp = Math.floor(+new Date(dateString) / 1e3)
82+
if (!isNaN(timestamp))
83+
return timestamp
84+
return dateString
85+
}

0 commit comments

Comments
 (0)