-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update registry.json * Create akash.yml * Create akash.ts
- Loading branch information
Showing
3 changed files
with
188 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: akash update | ||
|
||
# Controls when the action will run. | ||
on: | ||
schedule: | ||
- cron: "0 * * * *" | ||
|
||
# Allows you to run this workflow manually from the Actions tab | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: run-akash-import | ||
cancel-in-progress: false | ||
|
||
jobs: | ||
run-akash-import: | ||
name: run-akash-import | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- name: install node v12 | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
- name: yarn install | ||
run: yarn install | ||
- name: install typescript | ||
run: npm install -g typescript | ||
- name: change directory | ||
run: cd ./cmd | ||
- name: compile | ||
run: tsc ./cmd/akash.ts --esModuleInterop | ||
- name: list files | ||
run: ls ./cmd | ||
- name: run | ||
run: node ./cmd/akash.js | ||
env: | ||
DATABASE_URL: ${{ secrets.DATABASE_URL }} |
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,150 @@ | ||
import prisma from "../lib/prisma"; | ||
|
||
const endpoint = "https://akashlytics.com/web3-index/revenue"; | ||
const conversionFactor = 1; | ||
const batchSize = 30; | ||
const axios = require("axios"); | ||
|
||
const coin = { | ||
name: "akash", | ||
symbol: "AKT", | ||
}; | ||
|
||
const today = new Date(); | ||
today.setUTCHours(0, 0, 0, 0); | ||
|
||
// Update akash daily revenue data | ||
// a cron job should hit this endpoint every half hour or so (can use github actions for cron) | ||
const akashImport = async () => { | ||
// Use the updatedAt field in the Day model and compare it with the | ||
// timestamp associated with the fee, if it's less than the timestamp | ||
// then update the day's revenue | ||
|
||
// Get last imported id: we will start importing from there | ||
console.log("Getting project id for ", coin.name); | ||
const project = await getProject(coin.name); | ||
console.log("Project id: ", project); | ||
const lastId = project.lastImportedId; | ||
const parsedId = parseInt(lastId, 10); | ||
if (isNaN(parsedId)) { | ||
throw new Error("unable to parse int."); | ||
} | ||
|
||
let toDate = new Date(); | ||
toDate.setUTCHours(0, 0, 0, 0); | ||
|
||
console.log( | ||
"Project: " + | ||
project.name + | ||
" - to date: " + | ||
toDate | ||
); | ||
|
||
const response = await axios | ||
.get(endpoint) | ||
.catch(function (error) { | ||
console.log("Error getting data from endpoint ", endpoint, error); | ||
}); | ||
|
||
console.log("response: ", response.data) | ||
console.log(response.data.days.length); | ||
console.log(response.data.days[0]); | ||
|
||
for (let index = 0; index < response.data.days.length - 1; index++) { | ||
const element = response.data.days[index]; | ||
console.log( | ||
"Store day " + | ||
element.date + | ||
" to DB - value: " + | ||
element.revenue * conversionFactor | ||
); | ||
const fee = { | ||
date: element.date, | ||
fees: element.revenue * conversionFactor, | ||
blockHeight: (element.date).toString(), | ||
}; | ||
await storeDBData(fee, project.id); | ||
} | ||
console.log("exit scrape function."); | ||
|
||
return; | ||
}; | ||
|
||
const getProject = async (name: string) => { | ||
let project = await prisma.project.findFirst({ | ||
where: { | ||
name: name, | ||
}, | ||
}); | ||
|
||
if (project == null) { | ||
console.log("Project " + name + " doesn't exist. Create it"); | ||
await prisma.project.create({ | ||
data: { | ||
name: name, | ||
lastImportedId: "1593561600", | ||
}, | ||
}); | ||
|
||
project = await prisma.project.findUnique({ | ||
where: { | ||
name: name, | ||
}, | ||
}); | ||
} | ||
|
||
return project; | ||
}; | ||
|
||
const storeDBData = async ( | ||
dayData: { date: any; fees: any; blockHeight?: string }, | ||
projectId: number | ||
) => { | ||
const day = await prisma.day.findFirst({ | ||
where: { | ||
date: dayData.date, | ||
projectId: projectId, | ||
}, | ||
}); | ||
|
||
if (day != null) { | ||
await prisma.day.update({ | ||
where: { | ||
id: day.id, | ||
}, | ||
data: { | ||
revenue: dayData.fees, | ||
}, | ||
}); | ||
} else { | ||
await prisma.day.create({ | ||
data: { | ||
date: dayData.date, | ||
revenue: dayData.fees, | ||
projectId: projectId, | ||
}, | ||
}); | ||
} | ||
|
||
// update lastBlockID | ||
await prisma.project.updateMany({ | ||
where: { | ||
name: coin.name, | ||
}, | ||
data: { | ||
lastImportedId: dayData.date.toString(), | ||
}, | ||
}); | ||
|
||
return; | ||
}; | ||
|
||
console.log("import akash"); | ||
|
||
akashImport().then(() => { | ||
process.exit(0); | ||
}) | ||
.catch(() => { | ||
process.exit(1) | ||
}); | ||
|
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
e9633b2
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.
Successfully deployed to the following URLs: