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

Refactor/fix error paths #24

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
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
37 changes: 22 additions & 15 deletions app/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import {isTest, serviceUrl} from './config'
import { isTest, serviceUrl } from './config'

export const url = path => serviceUrl + path
export const url = (path) => serviceUrl + path

export const fetcher = url => fetch(url).then(res => res.json())
export const fetcher = (url) => fetch(url).then((res) => res.json())

export const post = async (url, data) => {
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
return response.json()
try {
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
return await response.json()
} catch (error) {
return error.message
}
}

export function chainLabel(chainId) {
@@ -32,10 +36,13 @@ export function chainLabel(chainId) {
}

export function isChainValid(chainId) {
return isTest ? [3, 97, '0x3', '0x61'].includes(chainId) : [1, 56, '0x1', '0x38'].includes(chainId)
return isTest
? [3, 97, '0x3', '0x61'].includes(chainId)
: [1, 56, '0x1', '0x38'].includes(chainId)
}

export const isChainBsc = chainId => (typeof chainId == 'string' ? ['0x38', '0x61'] : [56, 97]).includes(chainId)
export const isChainBsc = (chainId) =>
(typeof chainId == 'string' ? ['0x38', '0x61'] : [56, 97]).includes(chainId)

let contracts
export async function getTokenAddress(chainId) {
32 changes: 23 additions & 9 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
FROM debian:bullseye-slim
FROM ubuntu:20.04 as builder

RUN apt-get update -y \
&& apt-get install curl ca-certificates apt-transport-https bash perl -y \
&& apt-get clean
ARG VERSION=0.1.9

RUN curl -L "http://security.ubuntu.com/ubuntu/pool/main/p/perl/perl-modules-5.30_5.30.0-9ubuntu0.2_all.deb" -o "/var/tmp/perl-modules.deb" \
&& curl -L "https://github.com/BitgesellOfficial/bitgesell/releases/download/0.1.7/bitgesell_0.1.7_amd64.deb" -o "/var/tmp/bitgesell.deb" \
&& dpkg -i "/var/tmp/perl-modules.deb" \
&& dpkg -i "/var/tmp/bitgesell.deb" \
RUN apt update \
&& apt install -y --no-install-recommends \
libatomic1 \
wget \
ca-certificates \
apt-transport-https

RUN cd /tmp/ \
&& wget https://github.com/BitgesellOfficial/bitgesell/releases/download/${VERSION}/bitgesell_${VERSION}_amd64.deb \
&& wget http://ports.ubuntu.com/pool/main/p/perl/perl-modules-5.30_5.30.0-9build1_all.deb \
&& dpkg -i perl-modules-5.30_5.30.0-9build1_all.deb \
&& dpkg -i bitgesell_${VERSION}_amd64.deb \
&& apt-get install -y -f \
&& apt clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN cd /tmp/ \
&& wget https://github.com/BitgesellOfficial/bitgesell/releases/download/${VERSION}/bitgesell_${VERSION}_amd64.deb \
&& wget http://ports.ubuntu.com/pool/main/p/perl/perl-modules-5.30_5.30.0-9build1_all.deb \
&& dpkg -i perl-modules-5.30_5.30.0-9build1_all.deb \
&& dpkg -i bitgesell_${VERSION}_amd64.deb \
&& apt-get install -y -f \
&& rm -rf "/var/tmp/*"
&& apt clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR "/root/.BGL"

3 changes: 2 additions & 1 deletion service/package.json
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@
"ethereumjs-tx": "^2.1.2",
"express": "^4.17.1",
"mongoose": "^5.12.10",
"web3": "^1.3.6"
"web3": "^1.3.6",
"winston": "^3.8.2"
},
"devDependencies": {
"eslint": "^8.12.0",
13 changes: 13 additions & 0 deletions service/src/controllers/SubmitController.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ 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 { logger } from "../utils/logger.js";

export const bglToWbgl = async (req, res) => {
const data = req.body;
@@ -22,6 +23,7 @@ export const bglToWbgl = async (req, res) => {
chain,
to: data.address,
}).exec();

if (!transfer) {
const bglAddress = await RPC.createAddress();
transfer = new Transfer({
@@ -32,6 +34,7 @@ export const bglToWbgl = async (req, res) => {
to: data.address,
});
}

transfer.markModified("type");
await transfer.save();

@@ -42,8 +45,11 @@ export const bglToWbgl = async (req, res) => {
balance: await Chain.getWBGLBalance(),
feePercentage: feePercentage,
});

} catch (e) {
console.error(`Error: couldn't reach either RPC server or mongodb `, e);
logger.error(`Error: couldn't reach either RPC server or mongodb `, e);

res.status(400).json({
status: "error",
message: "Network is likely to be down.",
@@ -130,10 +136,17 @@ export const wbglToBgl = async (req, res) => {
});
} catch (e) {
console.error(`Error: network related error `, e);
logger.error(`Error: network related error `, e);
res.status(400).json({
status: "error",
message: "Network is likely to be down.",
});
return;
}
};


// Borrow from:
// - coinbase
// - Binance bridge
// - base off issues
20 changes: 14 additions & 6 deletions service/src/modules/bsc.js
Original file line number Diff line number Diff line change
@@ -4,17 +4,25 @@ import Web3Base from "./web3.js";

class Bsc extends Web3Base {
async getNetworkId() {
if (!this.networkId) {
this.networkId = await this.web3.eth.net.getId();
try {
if (!this.networkId) {
this.networkId = await this.web3.eth.net.getId();
}
return this.networkId;
} catch (error) {
return null;
}
return this.networkId;
}

async getChainId() {
if (!this.chainId) {
this.chainId = await this.web3.eth.getChainId();
try {
if (!this.chainId) {
this.chainId = await this.web3.eth.getChainId();
}
return this.chainId;
} catch (error) {
return null
}
return this.chainId;
}

async transactionOpts() {
21 changes: 21 additions & 0 deletions service/src/modules/chores.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { Data, RPC, Eth, Bsc } from "./index.js";
import Transaction from "../models/Transaction.js";
import Transfer from "../models/Transfer.js";
import Conversion from "../models/Conversion.js";
import {logger} from "../utils/logger.js"

let ethNonce = 0;
let bscNonce = 0;
@@ -36,6 +37,11 @@ async function returnBGL(conversion, address) {
conversion.returnTxid = await RPC.send(address, conversion.amount);
await conversion.save();
} catch (e) {
logger.error(
`Error returning BGL to ${address}, conversion ID: ${conversion._id}.`,
e,
);

console.error(
`Error returning BGL to ${address}, conversion ID: ${conversion._id}.`,
e,
@@ -59,6 +65,10 @@ async function returnWBGL(Chain, conversion, address) {
`Error returning WBGL (${Chain.id}) to ${address}, conversion ID: ${conversion._id}.`,
e,
);
logger.error(
`Error returning WBGL (${Chain.id}) to ${address}, conversion ID: ${conversion._id}.`,
e,
);
conversion.status = "error";
await conversion.save();
}
@@ -156,6 +166,8 @@ async function checkBglTransactions() {
await Data.set("lastBglBlockHash", result["lastblock"]);
} catch (e) {
console.error("Error: checkBglTransactions function failed. Check network");
logger.error("Error: checkBglTransactions function failed. Check network");

}

setTimeout(checkBglTransactions, 60000);
@@ -236,6 +248,11 @@ export async function checkWbglTransfers(Chain = Eth, prefix = "Eth") {
`Error sending ${sendAmount} BGL to ${transfer.to}`,
e,
);

logger.error(
`Error sending ${sendAmount} BGL to ${transfer.to}`,
e,
);
conversion.status = "error";
await conversion.save();

@@ -248,6 +265,7 @@ export async function checkWbglTransfers(Chain = Eth, prefix = "Eth") {
});
} catch (e) {
console.error("Error: checkWbglTransfers function failed. Check network");
logger.error("Error: checkWbglTransfers function failed. Check network");
}

setTimeout(() => checkWbglTransfers(Chain, prefix), 60000);
@@ -282,6 +300,9 @@ async function checkPendingConversions(Chain) {
console.error(
"Error: checkPendingConversions function failed. Check chain network or mongodb",
);
logger.error(
"Error: checkPendingConversions function failed. Check chain network or mongodb",
);
}
setTimeout(() => checkPendingConversions(Chain), 60000);
}
23 changes: 20 additions & 3 deletions service/src/modules/rpc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from "bitcoin-core";
import { rpc, confirmations } from "../utils/config.js";
import { logger } from "../utils/logger.js";

let client;

@@ -31,19 +32,35 @@ export const listSinceBlock = async (
blockHash,
confirmation = confirmations.bgl,
) => {
try {
return await getClient().command(
"listsinceblock",
blockHash ? blockHash : undefined,
confirmation,
);
} catch(error) {
console.error('Failed to list since block', e)
logger.error('Failed to list since block', e)
}
};

export const getTransactionFromAddress = async (txid) => {
try {
const rawTx = await getClient().command("getrawtransaction", txid, true);
const vin = rawTx["vin"][0];
const txIn = await getClient().command("getrawtransaction", vin.txid, true);
return txIn["vout"][vin["vout"]]["scriptPubKey"]["address"];
};
} catch (e) {
return null
}
}

export const send = async (address, amount) => {
try {
await getClient().command("sendtoaddress", address, amount);

export const send = async (address, amount) =>
await getClient().command("sendtoaddress", address, amount);
} catch (e) {
console.error('Failed to send', e)
logger.error(`Failed to send ${amount} to ${address}`, e)
}
}
29 changes: 29 additions & 0 deletions service/src/utils/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import winston from "winston"

const options = {
file: {
level: "info",
filename: "./logs/service.log",
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: "debug",
handleExceptions: true,
json: false,
colorize: true,
},
}

export const logger = winston.createLogger({
levels: winston.config.npm.levels,
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console),
],
exitOnError: false,
})