Skip to content

Commit

Permalink
Add Akash cmd (#53)
Browse files Browse the repository at this point in the history
* Update registry.json

* Create akash.yml

* Create akash.ts
  • Loading branch information
baldator authored Nov 5, 2021
1 parent 8f13b1b commit e9633b2
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 1 deletion.
38 changes: 38 additions & 0 deletions .github/workflows/akash.yml
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 }}
150 changes: 150 additions & 0 deletions cmd/akash.ts
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)
});

1 change: 0 additions & 1 deletion registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"stack": "Middleware",
"subcategory": "Compute",
"symbol": "AKT",
"usage": "https://akashlytics.com/web3-index/revenue",
"coingeckoID": "akash-network",
"genesisDate": "2020-09-24"
}
Expand Down

1 comment on commit e9633b2

@vercel
Copy link

@vercel vercel bot commented on e9633b2 Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.