Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mbidenaio committed Mar 4, 2021
1 parent a9abacb commit c7d377d
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 94 deletions.
35 changes: 25 additions & 10 deletions bsc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ethers = require('ethers');
const abi = require('./abi.js');
const InputDataDecoder = require('ethereum-input-data-decoder');
require('dotenv').config();
const logger = require('../logger').child({component: "bsc"})


exports.mint = async function (address, amount) {
Expand All @@ -27,7 +28,7 @@ exports.mint = async function (address, amount) {
fees: parseFloat(fees / 10 ** 18)
}
} catch (error) {
console.log(error);
logger.error(`Failed to mint: ${error}`);
return null
}

Expand All @@ -46,6 +47,7 @@ exports.isValidBurnTx = async function (txHash, address, amount, date) {
}
return result.inputs[1]
} catch (error) {
logger.error(`Failed to extract dest address: ${error}`);
return false
}
}
Expand All @@ -60,39 +62,52 @@ exports.isValidBurnTx = async function (txHash, address, amount, date) {
let txReceipt = await provider.getTransactionReceipt(txHash);

if (txReceipt.status !== 1) {
logger.info(`Wrong status, actual: ${txReceipt.status}, expected: 1`);
return false
}
if (txReceipt.logs.length === 0) {
logger.info(`No logs`);
return false
}
if (txReceipt.to.toLowerCase() !== process.env.BSC_CONTRACT.toLowerCase()) {
logger.info(`Wrong recipient, actual: ${txReceipt.to}, expected: ${process.env.BSC_CONTRACT}`);
return false
}
let tx = await provider.getTransaction(txHash)
let destAddress = tx && extractDestAddress(tx.data)
if (destAddress.toLowerCase() !== address.toLowerCase().slice(2)) {
logger.info(`Wrong dest address, actual: ${destAddress}, expected: ${address}`);
return false
}
if (contract.interface.parseLog(txReceipt.logs[0]).name !== "Transfer") {
const method = contract.interface.parseLog(txReceipt.logs[0]).name
if (method !== "Transfer") {
logger.info(`Wrong method, actual: ${method}, expected: Transfer`);
return false
}
if (!(contract.interface.parseLog(txReceipt.logs[0]).args.value >= ethers.utils.parseEther(amount.toString()))) {
const value = contract.interface.parseLog(txReceipt.logs[0]).args.value
if (!(value >= ethers.utils.parseEther(amount.toString()))) {
logger.info(`Wrong value, actual: ${value}, expected: at least ${amount}`);
return false
}
if (contract.interface.parseLog(txReceipt.logs[0]).args.from.toLowerCase() !== tx.from.toLowerCase()) {
const from = contract.interface.parseLog(txReceipt.logs[0]).args.from
if (from.toLowerCase() !== tx.from.toLowerCase()) {
logger.info(`Wrong sender, actual: ${from}, expected: ${tx.from}`);
return false
}
if (contract.interface.parseLog(txReceipt.logs[0]).args.to.toLowerCase() !== "0x0000000000000000000000000000000000000000") {
const to = contract.interface.parseLog(txReceipt.logs[0]).args.to
if (to.toLowerCase() !== "0x0000000000000000000000000000000000000000") {
logger.info(`Wrong recipient, actual: ${to}, expected: 0x0000000000000000000000000000000000000000`);
return false
}
const block = await provider.getBlock(tx.blockHash)
const blockDate = new Date(block.timestamp * 1000);
if (blockDate.getTime() < date.getTime()) {
logger.info("Tx is not actual");
return false
}
return true
} catch (error) {
console.log("Failed to check if burn tx is valid", error);
logger.error(`Failed to check if burn tx is valid: ${error}`);
return false
}
}
Expand All @@ -107,7 +122,7 @@ exports.isTxExist = async function (txHash) {
return false
}
} catch (error) {
console.log(error);
logger.error(`Failed to check if tx exists: ${error}`);
return false
}
}
Expand All @@ -121,7 +136,7 @@ exports.isTxConfirmed = async function (txHash) {
return false
}
} catch (error) {
console.log(error);
logger.error(`Failed to check if tx is confirmed: ${error}`);
return false
}

Expand All @@ -137,7 +152,7 @@ exports.getContractAddress = function () {

async function getIdenaPrice() {
let resp = await axios.get("https://api.coingecko.com/api/v3/simple/price?ids=idena&vs_currencies=bnb");
if (resp.status == 200 && resp.data.idena.bnb) {
if (resp.status === 200 && resp.data.idena.bnb) {
return ethers.utils.parseEther(resp.data.idena.bnb.toString());
} else {
return 0
Expand All @@ -153,7 +168,7 @@ exports.isNewTx = async function (tx) {
return true
}
} catch (error) {
console.log(error);
logger.error(`Failed to check if tx is new: ${error}`);
return false
}
}
30 changes: 22 additions & 8 deletions idena/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ const {
axios = require("axios"),
fs = require('fs');
require('dotenv').config();
const logger = require('../logger').child({component: "idena"})

exports.send = async function (address, amount) {
try {
let epoch = await getEpoch();
let nonce = await getNonce(epoch);
if (nonce !== null && epoch !== null) {
logger.info(`Sending idena tx, address: ${address}, amount: ${amount}, epoch: ${epoch}, nonce: ${nonce}`)
amount = parseFloat(amount) - parseFloat(process.env.IDENA_FIXED_FEES)
const tx = await new Transaction(
nonce,
Expand Down Expand Up @@ -38,7 +40,7 @@ exports.send = async function (address, amount) {
}

} catch (error) {
console.log(error);
logger.error(`Failed to send tx: ${error}`);
return null
}

Expand All @@ -54,7 +56,7 @@ async function getTransaction(tx) {
});
return transaction.data.result || null
} catch (error) {
console.error("Failed to get idena transaction:", error);
logger.error(`Failed to get tx: ${error}`);
return null
}
}
Expand All @@ -79,7 +81,7 @@ exports.isTxConfirmed = async function (tx) {
});
return bcn_syncing.data.result.highestBlock > bcn_block.data.result.height + parseInt(process.env.IDENA_CONFIRMATIONS_BLOCKS) || false
} catch (error) {
console.log(error);
logger.error(`Failed to check if tx is confirmed: ${error}`);
return false
}
}
Expand All @@ -89,6 +91,7 @@ exports.isTxActual = async function (txHash, date) {
const transaction = await getTransaction(txHash);
return await isTxActual(transaction, date)
} catch (error) {
logger.error(`Failed to check if tx is actual: ${error}`);
return false
}
}
Expand All @@ -97,6 +100,7 @@ async function isTxActual(tx, date) {
try {
return new Date(tx.timestamp * 1000).getTime() >= date.getTime()
} catch (error) {
logger.error(`Failed to check if tx is actual: ${error}`);
return false
}
}
Expand All @@ -111,6 +115,7 @@ async function getEpoch() {
})
return apiResp.data.result.epoch;
} catch (error) {
logger.error(`Failed to get epoch: ${error}`);
return null
}
}
Expand All @@ -134,7 +139,7 @@ async function getNonce(epoch) {
return null
}
} catch (error) {
console.log(error);
logger.error(`Failed to get nonce: ${error}`);
return null
}
}
Expand All @@ -149,34 +154,42 @@ exports.isValidSendTx = async function (txHash, address, amount, date) {
}
return comment.substring(prefix.length)
} catch (error) {
logger.error(`Failed to extract dest address: ${error}`);
return false
}
}

try {
let transaction = await getTransaction(txHash);
if (!transaction) {
logger.info("No tx");
return false
}
const destAddress = extractDestAddress(transaction.payload)
if (!destAddress || destAddress.toLowerCase() !== address.toLowerCase()) {
logger.info(`Wrong dest address, actual: ${destAddress}, expected: ${address}`);
return false
}
if (transaction.to !== privateKeyToAddress(process.env.IDENA_PRIVATE_KEY)) {
const recipient = privateKeyToAddress(process.env.IDENA_PRIVATE_KEY)
if (transaction.to !== recipient) {
logger.info(`Wrong tx recipient, actual: ${transaction.to}, expected: ${recipient}`);
return false
}
if (!(parseFloat(transaction.amount) >= parseFloat(amount))) {
logger.info(`Wrong tx amount, actual: ${transaction.amount}, expected: at least ${amount}`);
return false
}
if (transaction.type !== "send") {
logger.info(`Wrong tx type, actual: ${transaction.type}, expected: send`);
return false
}
if (transaction.timestamp && !await isTxActual(transaction, date)) {
logger.info("Tx is not actual");
return false
}
return true
} catch (error) {
console.log(error);
logger.error(`Failed to check if idena tx is valid: ${error}`);
return false
}
}
Expand All @@ -190,14 +203,15 @@ exports.isTxExist = async function (txHash) {
return false
}
} catch (error) {
console.log(error);
logger.error(`Failed to check if tx exists: ${error}`);
return false
}
}

exports.getWalletAddress = function () {
return privateKeyToAddress(process.env.IDENA_PRIVATE_KEY);
}

exports.isNewTx = async function (tx) {
try {
const [data] = await db.promise().execute("SELECT `id` FROM `used_txs` WHERE `tx_hash` = ? AND `blockchain` = 'idena';", [tx]);
Expand All @@ -207,7 +221,7 @@ exports.isNewTx = async function (tx) {
return true
}
} catch (error) {
logger.error(`Failed to check if tx is new: ${error}`);
return false
}

}
Loading

0 comments on commit c7d377d

Please sign in to comment.