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

Add TypeScript and static types support to service #16

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f332373
Add `tsconfig.json` file
Coinslist Sep 7, 2022
485fb28
add `tsconfig.json` file
Coinslist Sep 8, 2022
baecbe2
Add TypeScript type definitions for dependencies
Coinslist Sep 8, 2022
31c331d
update service start script to `node dist/server.js``
Coinslist Sep 8, 2022
291d657
Add static typing to `controllers/BalanceController`
Coinslist Sep 8, 2022
de6116a
add static types to `controllers/SubmitController`
Coinslist Sep 8, 2022
bd22c83
Add static types to `controllers/IndexController`
Coinslist Sep 8, 2022
b881b10
Add static types to `src/server` and `src/app` implementation
Coinslist Sep 8, 2022
691b701
Add static types to `models/Conversion`
Coinslist Sep 8, 2022
027bbee
Add static types to `models/Data`
Coinslist Sep 8, 2022
bb865b2
Add static types to `models/Transaction`
Coinslist Sep 8, 2022
9b4228b
Add static types to `models/Transfer`
Coinslist Sep 8, 2022
0a09bc5
Add static types to `modules/bsc`
Coinslist Sep 8, 2022
248fc40
Add static types to `modules/chores`
Coinslist Sep 8, 2022
678786d
Add static types to `modules/data`
Coinslist Sep 8, 2022
cf8228b
Add static types to `modules/db`
Coinslist Sep 8, 2022
ea9b718
Add static types to `modules/rpc`
Coinslist Sep 8, 2022
8b63ccf
Add static types to `modules/web3`
Coinslist Sep 8, 2022
621f1c9
Add static types to `modules`
Coinslist Sep 8, 2022
ba5ba13
Add static types to `utils/config`
Coinslist Sep 8, 2022
074d6c8
Add static types to `utils/`
Coinslist Sep 8, 2022
58849f1
add globally shared types for `/service`
Coinslist Sep 8, 2022
81e4034
add appropriate type for `models/Conversion`
Coinslist Sep 8, 2022
8b4cbf5
Fix type on `models/Transaction` model
Coinslist Sep 8, 2022
b03a3ee
Fix `models/Conversion` type
Coinslist Sep 8, 2022
65313eb
Fix `modules/web3.ts` class `Web3Base`, make `confirmations` member p…
Coinslist Sep 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Backend service is configured using environment variables that need to be set be

The command for running the service is:
```shell
node src/server.js
node dist/server.js
```

### Frontend Application
Expand Down
13 changes: 10 additions & 3 deletions service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"name": "bgl-wbgl-bridge-service",
"version": "0.3.1",
"description": "BGL-WBGL(ETH) bridge",
"main": "src/server.js",
"main": "dist/server.js",
"license": "MIT",
"type": "module",
"scripts": {
"dev": "nodemon -L --inspect=0.0.0.0:9229 src/server.js",
"dev": "nodemon -L --inspect=0.0.0.0:9229 dist/server.js",
"build": "tsc -p tsconfig.buid.json",
"prepare": "npm run build",
"test": "mocha src/tests/**/*.js -r dotenv/config --timeout 10000",
"lint:check": "eslint .",
"lint:fix": "eslint --fix",
Expand All @@ -25,14 +27,19 @@
"web3": "^1.3.6"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/node": "^18.7.15",
"eslint": "^8.12.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^9.2.2",
"nodemon": "^2.0.7",
"pre-commit": "^1.2.2",
"prettier": "^2.6.2",
"should": "^13.2.3"
"should": "^13.2.3",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
},
"options": {
"mocha": "--timeout 20000 --recursive --require should"
Expand Down
8 changes: 4 additions & 4 deletions service/src/app.js → service/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import express from "express";
import express, {Request, Response} from "express";
import cors from "cors";
import { port } from "./utils/config.js";
import { port } from "./utils/config";
import {
BalanceController,
IndexController,
SubmitController,
} from "./controllers/index.js";
} from "./controllers";

const app = express();
app.set("port", port);
app.use(cors());
app.use(express.json());

app.use(function(req, res, next) {
app.use(function(req: Request, res: Response, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
Expand Down
8 changes: 0 additions & 8 deletions service/src/controllers/BalanceController.js

This file was deleted.

9 changes: 9 additions & 0 deletions service/src/controllers/BalanceController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Request, Response} from 'express'
import { Bsc, Eth, RPC } from "../modules";

export const bgl = async (_req: Request, res: Response) =>
res.json(Math.floor(await RPC.getBalance()));
export const eth = async (_req: Request, res: Response) =>
res.json(parseInt(await Eth.getWBGLBalance()));
export const bsc = async (_req: Request, res: Response) =>
res.json(parseInt(await Bsc.getWBGLBalance()));
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Db, RPC, Eth, Bsc } from "../modules/index.js";
import {Request, Response} from "express"
import { Db, RPC, Eth, Bsc } from "../modules/index";

export const healthCheck = async (_req, res) => {
export const healthCheck = async (_req: Request, res: Response) => {
try {
await RPC.getBalance();
if (!Db.isConnected()) {
res.json(500, {
res.status(500).json({
status: "error",
message: "Database connection not available",
});
})
}
res.json({
status: "ok",
Expand All @@ -18,11 +19,11 @@ export const healthCheck = async (_req, res) => {
}
};

export const state = async (_req, res) => {
export const state = async (_req: Request, res: Response) => {
try {
const blockchainInfo = await RPC.getBlockchainInfo();
if (!Db.isConnected()) {
res.json(500, {
res.status(500).json({
status: "error",
message: "Database connection not available",
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Transfer from "../models/Transfer.js";
import { RPC, Eth, Bsc } from "../modules/index.js";
import { bsc, eth, feePercentage } from "../utils/config.js";
import { isValidBglAddress, isValidEthAddress, sha3 } from "../utils/index.js";
import {Request, Response} from "express";
import Transfer from "../models/Transfer";
import { RPC, Eth, Bsc } from "../modules";
import { bsc, eth, feePercentage } from "../utils/config";
import { isValidBglAddress, isValidEthAddress, sha3 } from "../utils/index";

export const bglToWbgl = async (req, res) => {
export const bglToWbgl = async (req: Request, res: Response) => {
const data = req.body;
if (!data.hasOwnProperty("address") || !isValidEthAddress(data.address)) {
res.status(400).json({
Expand Down Expand Up @@ -52,7 +53,7 @@ export const bglToWbgl = async (req, res) => {
}
};

export const wbglToBgl = async (req, res) => {
export const wbglToBgl = async (req: Request, res: Response) => {
const data = req.body;
if (
!data.hasOwnProperty("ethAddress") ||
Expand Down
5 changes: 0 additions & 5 deletions service/src/controllers/index.js

This file was deleted.

5 changes: 5 additions & 0 deletions service/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as IndexController from "./IndexController";
import * as BalanceController from "./BalanceController";
import * as SubmitController from "./SubmitController";

export { IndexController, BalanceController, SubmitController };
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import mongoose from "mongoose";
import mongoose, { Document } from "mongoose";
import {Chain} from "../types"

interface IConversion {
type: string,
chain: Chain,
transfer: string,
transaction: string,
address: string,
amount: string,
sendAmount: number,
txid: string,
nonce: number,
receipt: Object,
returnTxid: string,
status: string,
txChecks: number,
}

export type ConversionModelType = IConversion & Document<any, any, IConversion>

const schema = new mongoose.Schema(
{
Expand Down Expand Up @@ -27,4 +46,4 @@ const schema = new mongoose.Schema(
{ timestamps: true },
);

export default mongoose.model("Conversion", schema);
export default mongoose.model<IConversion>("Conversion", schema);
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import mongoose from "mongoose";
import { Chain } from "../types";

export interface ITransaction {
type: string,
chain: Chain,
id: string,
transfer: string,
address: string,
amount: number,
blockHash: string,
time: number
}

export interface Tx extends ITransaction {
confirmations: number
txid: string,
category: string
}

const schema = new mongoose.Schema(
{
Expand All @@ -18,4 +36,4 @@ const schema = new mongoose.Schema(
{ timestamps: true },
);

export default mongoose.model("Transaction", schema);
export default mongoose.model<ITransaction>("Transaction", schema);
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import mongoose from "mongoose";
import { Chain } from "../types";

enum ChainTypes {
bgl = "bgl",
wbgl = "wbgl",
}

type ChainType = string | ChainTypes

interface ITransfer {
id: string,
type: ChainType,
chain: Chain,
from: string,
to: string
}

const schema = new mongoose.Schema(
{
Expand All @@ -11,4 +27,4 @@ const schema = new mongoose.Schema(
{ timestamps: true },
);

export default mongoose.model("Transfer", schema);
export default mongoose.model<ITransfer>("Transfer", schema);
24 changes: 20 additions & 4 deletions service/src/modules/bsc.js → service/src/modules/bsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ import { bsc, confirmations } from "../utils/config.js";
import Web3Base from "./web3.js";

class Bsc extends Web3Base {
private networkId!: number
private chainId!: number

constructor(
endpoint: string,
id: string,
contractAddress: string,
custodialAccountAddress: string,
custodialAccountKey: Buffer,
nonceDataName: string,
confirmations: number,
) {
super(endpoint, id, contractAddress, custodialAccountAddress,custodialAccountKey, nonceDataName, confirmations)
}

async getNetworkId() {
if (!this.networkId) {
this.networkId = await this.web3.eth.net.getId();
Expand All @@ -17,13 +32,14 @@ class Bsc extends Web3Base {
return this.chainId;
}

//@ts-ignore
async transactionOpts() {
const params = {
name: "bnb",
networkId: await this.getNetworkId(),
chainId: await this.getChainId(),
};
const common = Common.default.forCustomChain(
const common = Common.forCustomChain(
"mainnet",
params,
"petersburg",
Expand All @@ -33,10 +49,10 @@ class Bsc extends Web3Base {
}

export default new Bsc(
bsc.endpoint,
bsc.endpoint as string,
"bsc",
bsc.contract,
bsc.account,
bsc.contract as string,
bsc.account as string,
bsc.key,
"bscNonce",
confirmations.bsc,
Expand Down
Loading