Skip to content

Commit

Permalink
api: rewrite python functions in javascript (#1630)
Browse files Browse the repository at this point in the history
This fixes an issue with oversized builds which include node_modules inside the python functions despite the folder being excluded by the build process
  • Loading branch information
ns212 authored Jan 18, 2024
1 parent fbb0213 commit 3e973c4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 302 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ dist
.vscode
/.env.development.local.example
/.env
.vercel
166 changes: 0 additions & 166 deletions api/health.py

This file was deleted.

4 changes: 0 additions & 4 deletions api/requirements.txt

This file was deleted.

81 changes: 81 additions & 0 deletions api/supply_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const KINT_SUBSCAN_URL = 'https://kintsugi.api.subscan.io/'
const INTR_SUBSCAN_URL = 'https://interlay.api.subscan.io/'

const KINT_API_KEY = process.env.SUBSCAN_API_KEY
const INTR_API_KEY = process.env.INTR_SUBSCAN_API_KEY

function fromDecimals (val, decimals) {
return val / Math.pow(10, decimals)
}

async function subscanRequest (url, method = 'GET', json = null) {
const headers = {
'Content-Type': 'application/json',
'X-API-KEY': url.startsWith(INTR_SUBSCAN_URL) ? INTR_API_KEY : KINT_API_KEY
}

const options = { method, headers }
if (json) {
options.body = JSON.stringify(json)
}

const response = await fetch(url, options)
return await response.json()
}

export default async function (request, response) {
if (request.method !== 'GET') {
return response.status(400).send('Bad Request')
}
const path = request.url.split('?')[0]
switch (path) {
case '/supply/intr-total-supply': {
const INTR_SUPPLY = 1_000_000_000
return response
.status(200)
.send(INTR_SUPPLY.toString())
}
case '/supply/kint-total-supply': {
const KINT_SUPPLY = 10_000_000
return response
.status(200)
.send(KINT_SUPPLY.toString())
}
case '/supply/intr-circ-supply': {
const unvestedSupply = fromDecimals(parseInt((await subscanRequest(INTR_SUBSCAN_URL + 'api/scan/token')).data.detail.INTR.available_balance), 10)
const systemAccountsSupply = (await subscanRequest(INTR_SUBSCAN_URL + 'api/scan/accounts', 'POST',
{ filter: 'system', row: 25, page: 0 })).data.list
.reduce((acc, curr) => acc + parseFloat(curr.balance), 0)
const circulatingSupply = unvestedSupply - systemAccountsSupply

return response
.status(200)
.send(circulatingSupply.toString())
}
case '/supply/kint-circ-supply': {
const kintUnvestedSupply = fromDecimals(parseInt((await subscanRequest(KINT_SUBSCAN_URL + 'api/scan/token')).data.detail.KINT.available_balance), 12)
const kintSystemAccountsSupply = (await subscanRequest(KINT_SUBSCAN_URL + 'api/scan/accounts', 'POST',
{ filter: 'system', row: 25, page: 0 })).data.list
.reduce((acc, curr) => acc + parseFloat(curr.balance), 0)
const kintCirculatingSupply = kintUnvestedSupply - kintSystemAccountsSupply

return response
.status(200)
.send(kintCirculatingSupply.toString())
}
case '/supply/ibtc-supply': {
const ibtcSupply = fromDecimals(parseInt((await subscanRequest(INTR_SUBSCAN_URL + 'api/scan/token')).data.detail.IBTC.available_balance), 10)
return response
.status(200)
.send(ibtcSupply.toString())
}
case '/supply/kbtc-supply': {
const kbtcSupply = fromDecimals(parseInt((await subscanRequest(KINT_SUBSCAN_URL + 'api/scan/token')).data.detail.KBTC.available_balance), 8)
return response
.status(200)
.send(kbtcSupply.toString())
}
default:
response.status(404).send('Not Found')
}
};
125 changes: 0 additions & 125 deletions api/supply_info.py

This file was deleted.

Loading

2 comments on commit 3e973c4

@vercel
Copy link

@vercel vercel bot commented on 3e973c4 Jan 18, 2024

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 3e973c4 Jan 18, 2024

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.