forked from beefyfinance/beefy-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3e490a8
Showing
14 changed files
with
3,091 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,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" | ||
} | ||
} |
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 @@ | ||
# Beefy API | ||
|
||
## Overview | ||
|
||
Simple API for BeefyFinance. |
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,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; |
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,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 }; |
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,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); |
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,4 @@ | ||
{ | ||
"host": "localhost", | ||
"port": 3001 | ||
} |
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,4 @@ | ||
{ | ||
"host": "localhost", | ||
"port": 3001 | ||
} |
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,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; |
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,8 @@ | ||
'use strict'; | ||
|
||
async function rt(ctx, next) { | ||
await next(); | ||
ctx.set('X-Powered-By', 'moo!'); | ||
} | ||
|
||
module.exports = rt; |
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,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; |
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,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; |
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 @@ | ||
const compound = (r, n = 365, t = 1) => { | ||
return (1 + r / n) ** (n * t) - 1; | ||
}; | ||
|
||
module.exports = compound; |
Oops, something went wrong.