Skip to content

Commit

Permalink
upgrade backend stellar-sdk to @stellar
Browse files Browse the repository at this point in the history
  • Loading branch information
acharb committed May 20, 2024
1 parent 32ec166 commit a93b01f
Show file tree
Hide file tree
Showing 6 changed files with 1,837 additions and 981 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/dist
*/**/dist
*/**/build
/lib/
*/**/lib

# misc
.DS_Store
Expand Down
3 changes: 3 additions & 0 deletions backend/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ["@babel/preset-typescript", "@babel/preset-env"],
};
3 changes: 3 additions & 0 deletions backend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
transform: {
"^.+\\.(js|jsx|ts|tsx|mjs)$": ["babel-jest"],
},
};
11 changes: 9 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@
},
"dependencies": {
"@google-cloud/bigquery": "^5.11.0",
"@stellar/stellar-sdk": "^11.1.0",
"@types/express": "^4.17.13",
"@types/express-http-proxy": "^1.6.3",
"@types/morgan": "^1.9.3",
"axios": "^0.27.2",
"express": "^4.17.3",
"express-http-proxy": "^1.6.3",
"jest": "^27.5.1",
"lodash": "^4.17.21",
"morgan": "^1.10.0",
"path": "^0.12.7",
"redis": "^4.0.4",
"stellar-sdk": "^10.1.1",
"supertest": "^6.2.2",
"ts-node": "^10.5.0"
},
"devDependencies": {
"jest": "^29.7.0",
"@babel/core": "^7.24.5",
"@babel/plugin-transform-runtime": "^7.24.3",
"@babel/preset-env": "^7.24.5",
"@babel/preset-typescript": "^7.24.1",
"@types/jest": "^27.5.0",
"@types/json-schema": "^7.0.15",
"@types/lodash": "^4.14.178",
"@types/node": "^17.0.21",
"@types/urijs": "^1.19.25",
"babel-jest": "^29.7.0",
"nodemon": "^2.0.15",
"ts-jest": "^28.0.1",
"typescript": "^4.5.5"
Expand Down
28 changes: 10 additions & 18 deletions backend/src/ledgers/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { QueryRowsResponse } from "@google-cloud/bigquery";
import stellarSdk from "stellar-sdk";
import { Horizon } from "@stellar/stellar-sdk";

import { redisClient } from "../redis/redisSetup";
import { bqClient, BQHistoryLedger, getBqQueryByDate } from "../bigQuery";
Expand Down Expand Up @@ -58,15 +58,6 @@ export interface LedgerAverages {
transaction_success_avg: sum;
operation_avg: sum;
}
// TODO - import Horizon type once https://github.com/stellar/js-stellar-sdk/issues/731 resolved
export type LedgerRecord = {
closed_at: string;
paging_token: string;
sequence: number;
successful_transaction_count: number;
failed_transaction_count: number;
operation_count: number;
};

export async function updateLedgers(isTestnet: boolean) {
const horizonServer = getHorizonServer(isTestnet);
Expand Down Expand Up @@ -121,13 +112,13 @@ export async function updateLedgers(isTestnet: boolean) {
);
}

const horizon = new stellarSdk.Server(horizonServer);
const horizon = new Horizon.Server(horizonServer);
horizon
.ledgers()
.cursor(CURSOR_NOW)
.limit(200)
.stream({
onmessage: async (ledger: LedgerRecord) => {
onmessage: async (ledger) => {
for (const interval of intervalTypes) {
const REDIS_LEDGER_KEY = getServerNamespace(
REDIS_LEDGER_KEYS[interval],
Expand All @@ -139,15 +130,16 @@ export async function updateLedgers(isTestnet: boolean) {
isTestnet,
);
await updateCache(
[ledger],
// TODO: remove any when this is resolved https://github.com/stellar/js-stellar-sdk/issues/972
[ledger as any as Horizon.ServerApi.LedgerRecord],
REDIS_LEDGER_KEY,
REDIS_PAGING_TOKEN_KEY_VALUE,
interval,
);
}
console.log(
`${isTestnet ? "[TESTNET]" : "[MAINNET]"}: updated to ledger ${
ledger.closed_at
(ledger as any as Horizon.ServerApi.LedgerRecord).closed_at
}`,
);
},
Expand All @@ -164,8 +156,8 @@ export async function catchup(
total: number = 0,
) {
const horizonServer = getHorizonServer(isTestnet);
const horizon = new stellarSdk.Server(horizonServer);
let ledgers: LedgerRecord[] = [];
const horizon = new Horizon.Server(horizonServer);
let ledgers: Horizon.ServerApi.LedgerRecord[] = [];

const resp = await horizon
.ledgers()
Expand Down Expand Up @@ -200,7 +192,7 @@ export async function catchup(
}

export async function updateCache(
ledgers: LedgerRecord[],
ledgers: Array<Horizon.ServerApi.LedgerRecord>,
ledgersKey: string,
pagingTokenKey: string,
interval,
Expand All @@ -213,7 +205,7 @@ export async function updateCache(
const cachedStats: LedgerStat[] = JSON.parse(json);
let pagingToken = "";

ledgers.forEach((ledger: LedgerRecord) => {
ledgers.forEach((ledger: Horizon.ServerApi.LedgerRecord) => {
const date: string = getLedgerKey[interval](new Date(ledger.closed_at));
const index: number = findIndex(cachedStats, { date });
if (index === -1) {
Expand Down
Loading

0 comments on commit a93b01f

Please sign in to comment.