Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{backend, test}: change backend files and backend tests to typescript #204

Merged
merged 20 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions backend/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("babel-register")({
presets: ["es2015"],
});
// need to manually import regeneratorRuntime for babel w/ async
// https://github.com/babel/babel/issues/9849#issuecomment-487040428
// require("regenerator-runtime/runtime");
import "regenerator-runtime/runtime";

// Run backend with interval cache updates.
const { updateLumensCache } = require("./routes");
Expand Down
53 changes: 33 additions & 20 deletions backend/ledgers.js → backend/ledgers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import stellarSdk from "stellar-sdk";
import moment from "moment";
import _ from "lodash";
import * as postgres from "./postgres.js";
import { map, pick } from "lodash";
import { QueryTypes } from "sequelize";
import { Response } from "express";
import * as postgres from "./postgres";

let cachedData;
interface Ledger {
date: string;
transaction_count: number;
operation_count: number;
}

interface LedgerSql {
date: string;
transaction_count: string;
operation_count: string;
}

export const handler = function(req, res) {
let cachedData: Array<Ledger>;

export const handler = function(_: any, res: Response) {
res.send(cachedData);
};

Expand All @@ -20,20 +33,19 @@ function updateResults() {
order by 1 desc`;

postgres.sequelize
.query(query, { type: postgres.sequelize.QueryTypes.SELECT })
.then((results) => (cachedData = _.each(results, convertFields)));
.query(query, { type: QueryTypes.SELECT })
.then((results: Array<LedgerSql>) => {
cachedData = map(results, convertFields);
});
}

function convertFields(ledger) {
// String to int fields
const fields = ["transaction_count", "operation_count"];
for (let field of fields) {
ledger[field] = parseInt(ledger[field]);
}

// Remove year from date
ledger["date"] = ledger["date"].substring(5);
return ledger;
function convertFields(ledger: LedgerSql): Ledger {
return {
// Remove year from date
date: ledger["date"].substring(5),
transaction_count: parseInt(ledger.transaction_count),
operation_count: parseInt(ledger.operation_count),
};
}

// Wait for schema sync
Expand All @@ -44,7 +56,7 @@ postgres.sequelize.addHook("afterBulkSync", () => {
if (process.env.UPDATE_DATA == "true") {
// Stream ledgers - get last paging_token/
postgres.LedgerStats.findOne({ order: [["sequence", "DESC"]] }).then(
(lastLedger) => {
(lastLedger: postgres.LedgerStats) => {
let pagingToken;
if (!lastLedger) {
pagingToken = "now";
Expand All @@ -58,8 +70,9 @@ postgres.sequelize.addHook("afterBulkSync", () => {
.cursor(pagingToken)
.limit(200)
.stream({
onmessage: (ledger) => {
let newLedger = _.pick(ledger, [
// TODO - use type any until https://github.com/stellar/js-stellar-sdk/issues/731 resolved
onmessage: (ledger: any) => {
let newLedger: any = pick(ledger, [
"sequence",
"closed_at",
"paging_token",
Expand Down
18 changes: 15 additions & 3 deletions backend/lumens.js → backend/lumens.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import * as commonLumens from "../common/lumens.js";
import BigNumber from "bignumber.js";
import { Response } from "express";

export let cachedData;
interface CachedData {
updatedAt: Date;
totalCoins: string;
availableCoins: string;
programs: {
directDevelopment: string;
ecosystemSupport: string;
useCaseInvestment: string;
userAcquisition: string;
};
}

export let cachedData: CachedData;

export const v1Handler = function(req, res) {
export const v1Handler = function(_: any, res: Response) {
res.send(cachedData);
};

Expand Down
71 changes: 0 additions & 71 deletions backend/postgres.js

This file was deleted.

59 changes: 59 additions & 0 deletions backend/postgres.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Sequelize, Model, DataTypes } from "sequelize";

export const sequelize = new Sequelize(
process.env.DEV
? "postgres://localhost/dashboard?sslmode=disable"
: process.env.POSTGRES_URL || "",
process.env.DEV
? {}
: {
dialect: "postgres",
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
},
);

export class LedgerStats extends Model {
public sequence!: number;
public closed_at!: Date;
public paging_token!: string;
public transaction_count!: number;
public operation_count!: number;
}

LedgerStats.init(
{
sequence: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
},
closed_at: {
type: DataTypes.DATE,
allowNull: false,
},
paging_token: {
type: DataTypes.STRING,
allowNull: false,
},
transaction_count: {
type: DataTypes.INTEGER,
allowNull: false,
},
operation_count: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
tableName: "ledger_stats",
sequelize,
},
);

// Create schema if doesn't exist
sequelize.sync({ hooks: true });
8 changes: 4 additions & 4 deletions backend/routes.js → backend/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import express from "express";
import proxy from "express-http-proxy";
import logger from "morgan";

import * as lumens from "./lumens.js";
import * as lumensV2V3 from "./v2v3/lumens.js";
import * as ledgers from "./ledgers.js";
import * as lumens from "./lumens";
import * as lumensV2V3 from "./v2v3/lumens";
import * as ledgers from "./ledgers";

export var app = express();
app.set("port", process.env.PORT || 5000);
Expand All @@ -16,7 +16,7 @@ if (process.env.DEV) {
app.use(
"/",
proxy("localhost:3000", {
filter: function(req, res) {
filter: function(req, _) {
return (
req.path == "/" ||
req.path.indexOf(".js") >= 0 ||
Expand Down
66 changes: 52 additions & 14 deletions backend/v2v3/lumens.js → backend/v2v3/lumens.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,80 @@
import * as commonLumens from "../../common/lumens.js";
import BigNumber from "bignumber.js";
import { Response } from "express";

const LUMEN_SUPPLY_METRICS_URL =
"https://www.stellar.org/developers/guides/lumen-supply-metrics.html";

// v2:
export let lumensDataV2;
interface LumensDataV2 {
updatedAt: Date;
originalSupply: string;
inflationLumens: string;
burnedLumens: string;
totalSupply: string;
upgradeReserve: string;
feePool: string;
sdfMandate: string;
circulatingSupply: string;
_details: string;
}

export let lumensDataV2: LumensDataV2;

/* For CoinMarketCap */
let totalSupplyData;
let circulatingSupplyData;
let totalSupplyData: number;
let circulatingSupplyData: number;

export const v2Handler = function(req, res) {
export const v2Handler = function(_: any, res: Response) {
res.send(lumensDataV2);
};
export const v2TotalSupplyHandler = function(req, res) {
export const v2TotalSupplyHandler = function(_: any, res: Response) {
res.json(totalSupplyData);
};
export const v2CirculatingSupplyHandler = function(req, res) {
export const v2CirculatingSupplyHandler = function(_: any, res: Response) {
res.json(circulatingSupplyData);
};

// v3:
export let lumensDataV3;
export let totalSupplyCheckResponse;
interface LumensDataV3 {
updatedAt: Date;
originalSupply: string;
inflationLumens: string;
burnedLumens: string;
totalSupply: BigNumber;
upgradeReserve: string;
feePool: string;
sdfMandate: string;
circulatingSupply: BigNumber;
_details: string;
}
export let lumensDataV3: LumensDataV3;

interface TotalSupplyCheckResponse {
updatedAt: Date;
totalSupply: BigNumber;
inflationLumens: string;
burnedLumens: string;
totalSupplySum: BigNumber;
upgradeReserve: string;
feePool: string;
sdfMandate: string;
circulatingSupply: BigNumber;
}
export let totalSupplyCheckResponse: TotalSupplyCheckResponse;

export const v3Handler = function(req, res) {
export const v3Handler = function(_: any, res: Response) {
res.send(lumensDataV3);
};
export const totalSupplyCheckHandler = function(req, res) {
export const totalSupplyCheckHandler = function(_: any, res: Response) {
res.json(totalSupplyCheckResponse);
};

/* For CoinMarketCap */
export const v3TotalSupplyHandler = function(req, res) {
export const v3TotalSupplyHandler = function(_: any, res: Response) {
res.json(totalSupplyCheckResponse.totalSupplySum);
};
export const v3CirculatingSupplyHandler = function(req, res) {
export const v3CirculatingSupplyHandler = function(_: any, res: Response) {
res.json(totalSupplyCheckResponse.circulatingSupply);
};

Expand Down Expand Up @@ -75,8 +113,8 @@ export function updateApiLumens() {
};

/* For CoinMarketCap */
totalSupplyData = totalSupply * 1;
circulatingSupplyData = circulatingSupply * 1;
totalSupplyData = Number(totalSupply);
circulatingSupplyData = Number(circulatingSupply);

console.log("/api/v2/lumens data saved!");

Expand Down
15 changes: 15 additions & 0 deletions common/lumens.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export var ORIGINAL_SUPPLY_AMOUNT: string;
export function getLumenBalance(horizonURL: string, accountId: string): string;
export function totalLumens(horizonURL: string): string;
export function inflationLumens(): Promise<BigNumber>;
export function feePool(): string;
export function burnedLumens(): string;
export function directDevelopmentAll(): Promise<string>;
export function distributionEcosystemSupport(): Promise<string>;
export function distributionUseCaseInvestment(): Promise<string>;
export function distributionUserAcquisition(): Promise<string>;
export function getUpgradeReserve(): string;
export function sdfAccounts(): Promise<BigNumber>;
export function totalSupply(): Promise<BigNumber>;
export function noncirculatingSupply(): Promise<BigNumber>;
export function circulatingSupply(): Promise<BigNumber>;