Skip to content

Commit

Permalink
moo
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbeefalot committed Oct 4, 2020
0 parents commit 3e490a8
Show file tree
Hide file tree
Showing 14 changed files with 3,091 additions and 0 deletions.
Empty file added log/.keep
Empty file.
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "beefy-api",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "nodemon src/app.js",
"test": "nyc ava"
},
"dependencies": {
"@koa/cors": "^3.1.0",
"@koa/router": "^9.4.0",
"koa": "^2.13.0",
"koa-bodyparser": "^4.3.0",
"koa-helmet": "^5.2.0",
"koa-router": "^9.4.0"
},
"devDependencies": {
"ava": "^3.12.1",
"axios": "^0.20.0",
"dotenv": "^8.2.0",
"nodemon": "^2.0.4",
"nyc": "^15.1.0",
"prettier": "2.1.1"
}
}
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Beefy API

## Overview

Simple API for BeefyFinance.
8 changes: 8 additions & 0 deletions src/api/noop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

async function noop(ctx, next) {
console.log(`NOOP: ${ctx.url} NOT IMPLEMENTED YET!`);
ctx.status = { status: 200, msg: 'noop' };
}

module.exports = noop;
41 changes: 41 additions & 0 deletions src/api/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const axios = require('axios');
const compound = require('../utils/compound');

async function apy(ctx) {
try {
const resSimple = await axios.get('https://bsc.for.tube/api/v2/bank_tokens');
const resExtended = await axios.get('https://bsc.for.tube/api/v1/bank/markets?mode=extended', {
headers: {
authorization: process.env.FORTUBE_API_TOKEN,
},
});

const dataSimple = resSimple.data;
const dataExtended = resExtended.data.data;

let apys = {};

Object.values(dataSimple).map(item => {
const symbol = item.symbol.toLowerCase();
const apy = compound(parseFloat(item.estimated_ar));
apys[symbol] = apy;
});

dataExtended.map(item => {
apys[item.token_symbol.toLowerCase()] += parseFloat(item.deposit_interest_rate);
});

for (const key in apys) {
apys[key] = `${(apys[key] * 100).toFixed(2)}%`;
}

ctx.status = 200;
ctx.body = apys;
} catch (err) {
ctx.status = 500;
}
}

module.exports = { apy };
27 changes: 27 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const Koa = require('koa');
const helmet = require('koa-helmet');
const body = require('koa-bodyparser');
const cors = require('@koa/cors');

const logger = require('./middleware/logger');
const rt = require('./middleware/rt');
const powered = require('./middleware/powered');

const cfg = require(`./config/${process.env.NODE_ENV}`);
const router = require('./router');

const app = new Koa();
app.use(helmet());
app.use(cors());
app.use(logger);
app.use(rt);
app.use(powered);
app.use(body());

app.use(router.routes());
app.use(router.allowedMethods());

console.log('> beefy-api running!');
app.listen(cfg.port);
4 changes: 4 additions & 0 deletions src/config/development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"host": "localhost",
"port": 3001
}
4 changes: 4 additions & 0 deletions src/config/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"host": "localhost",
"port": 3001
}
10 changes: 10 additions & 0 deletions src/middleware/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

async function logger(ctx, next) {
console.log(`--> ${ctx.method} ${ctx.url}`);
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`<-- ${ctx.method} ${ctx.url} | status: ${ctx.status} | len: ${ctx.length} | time: ${rt} \n`);
}

module.exports = logger;
8 changes: 8 additions & 0 deletions src/middleware/powered.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

async function rt(ctx, next) {
await next();
ctx.set('X-Powered-By', 'moo!');
}

module.exports = rt;
10 changes: 10 additions & 0 deletions src/middleware/rt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

async function rt(ctx, next) {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
}

module.exports = rt;
11 changes: 11 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const Router = require('koa-router');
const router = new Router();

const noop = require('./api/noop');
const stats = require('./api/stats');

router.get('/apy', stats.apy);

module.exports = router;
5 changes: 5 additions & 0 deletions src/utils/compound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const compound = (r, n = 365, t = 1) => {
return (1 + r / n) ** (n * t) - 1;
};

module.exports = compound;
Loading

0 comments on commit 3e490a8

Please sign in to comment.