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

chore: add log levels #158

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ PROVIDER_URL=
PORT=3000
PRIORITY_POOLS_ORDER_186=18,22,1
PRIORITY_POOLS_ORDER_195=1,23,22,2,5
PRIORITY_POOLS_ORDER_196=1,23,22,2,5
PRIORITY_POOLS_ORDER_196=1,23,22,2,5
LOG_LEVEL=INFO
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"homepage": "https://github.com/NexusMutual/cover-router#readme",
"dependencies": {
"@nexusmutual/deployments": "^2.10.0",
"@nexusmutual/utils": "^0.0.1",
"convict": "^6.2.4",
"dotenv": "^16.0.3",
"ethers": "^5.7.2",
Expand Down
5 changes: 5 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const convict = require('convict');

const config = convict({
logLevel: {
doc: 'The logging level to set',
default: 'WARN',
env: 'LOG_LEVEL',
},
port: {
doc: 'The port to bind.',
format: 'port',
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config();

const { addresses } = require('@nexusmutual/deployments');
const { setLogLevel } = require('@nexusmutual/utils');
const { ethers } = require('ethers');
const express = require('express');
const swaggerUI = require('swagger-ui-express');
Expand All @@ -15,6 +16,8 @@ const { capacityRouter, pricingRouter, quoteRouter, reindexRouter } = require('.
const { createStore, initialState, load, save } = require('./store');

const main = async () => {
setLogLevel(config.get('logLevel'));

// provider
const providerURL = config.get('provider');
const provider = new ethers.providers.JsonRpcProvider(providerURL);
Expand Down Expand Up @@ -61,7 +64,7 @@ const main = async () => {
app.set('synchronizer', synchronizer);

if (!isFromCache) {
console.log('Missing initial state, delaying startup until the state is fully loaded');
console.warn('Missing initial state, delaying startup until the state is fully loaded');
await synchronizer.updateAssetRates();
await synchronizer.updateAll();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/chainApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const createChainApi = async contracts => {

const fetchPoolProduct = async (productId, poolId, globalCapacityRatio, capacityReductionRatio) => {
const stakingPool = contracts('StakingPool', poolId);
console.log('Fetching allocations for product', productId, 'in pool', poolId, 'at address', stakingPool.address);
console.info(`Fetching allocations for product ${productId} in pool ${poolId} at address ${stakingPool.address}`);

// pool allocations and capacities
const allocations = await stakingPool.getActiveAllocations(productId);
Expand Down
16 changes: 8 additions & 8 deletions src/lib/eventsApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = async (provider, contracts) => {
const blockBucketId = calculateBucketId(blockTimestamp);

if (blockBucketId === activeBucketId) {
console.log(`Event: Bucket ${currentBucketId} expired`);
console.info(`Event: Bucket ${currentBucketId} expired`);

currentBucketId = activeBucketId;
emitter.emit('bucket:change');
Expand All @@ -44,7 +44,7 @@ module.exports = async (provider, contracts) => {
const blockTrancheId = calculateTrancheId(blockTimestamp);

if (blockTrancheId === activeTrancheId) {
console.log(`Event: Tranche ${currentTrancheId} expired`);
console.info(`Event: Tranche ${currentTrancheId} expired`);

currentTrancheId = activeTrancheId;
emitter.emit('tranche:change');
Expand All @@ -62,7 +62,7 @@ module.exports = async (provider, contracts) => {
const stakingPool = contracts('StakingPool', poolId);
for (const eventName of events) {
stakingPool.on(eventName, () => {
console.log(`Event: ${eventName} triggered for Pool ${poolId}`);
console.info(`Event: ${eventName} triggered for Pool ${poolId}`);
emitter.emit('pool:change', poolId);
});
}
Expand All @@ -71,27 +71,27 @@ module.exports = async (provider, contracts) => {
// subscribe to events on new staking pool
stakingPoolFactory.on('StakingPoolCreated', async poolId => {
const poolIdParsed = BigNumber.isBigNumber(poolId) ? poolId.toNumber() : poolId;
console.log(`Event: Pool ${poolIdParsed} created`);
console.info(`Event: Pool ${poolIdParsed} created`);
emitter.emit('pool:change', poolIdParsed);
const stakingPool = contracts('StakingPool', poolIdParsed);
for (const eventName of events) {
stakingPool.on(eventName, () => {
console.log(`Event: ${eventName} triggered for Pool ${poolIdParsed}`);
console.info(`Event: ${eventName} triggered for Pool ${poolIdParsed}`);
emitter.emit('pool:change', poolIdParsed);
});
}
});

stakingProducts.on('ProductUpdated', productId => {
console.log(`Event: Product ${productId} update`);
console.info(`Event: Product ${productId} update`);
emitter.emit('product:change', productId);
});
coverProducts.on('ProductSet', productId => {
console.log(`Event: Product ${productId} set`);
console.info(`Event: Product ${productId} set`);
emitter.emit('product:change', productId);
});
cover.on('CoverEdited', (coverId, productId) => {
console.log(`Event: Cover ${coverId} for product ${productId} edited`);
console.info(`Event: Cover ${coverId} for product ${productId} edited`);
emitter.emit('product:change', productId);
});

Expand Down
17 changes: 10 additions & 7 deletions src/lib/quoteEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ const customAllocationPriorityFixedPrice = (amountToAllocate, poolsData, customP
const poolId = customPoolIdPriorityCopy.shift();
const pool = poolsData.find(poolData => poolData.poolId === poolId);
if (!pool) {
console.info(`Unable to find pool ${poolId} in poolsData array. Skipping\n`, inspect(poolsData, { depth: null }));
console.warn(`Unable to find pool ${poolId} in poolsData array. Skipping\n`);
console.warn(`Available poolIds in poolsData: ${poolsData.map(p => p.poolId).join(', ')}`);
console.debug('poolsData: ', inspect(poolsData, { depth: null }));
continue;
}

Expand Down Expand Up @@ -239,7 +241,7 @@ const quoteEngine = (store, productId, amount, period, coverAsset) => {

// rounding up to nearest allocation unit
const amountToAllocate = divCeil(coverAmountInNxm, NXM_PER_ALLOCATION_UNIT).mul(NXM_PER_ALLOCATION_UNIT);
console.log('Amount to allocate:', formatEther(amountToAllocate), 'nxm');
console.info(`Amount to allocate: ${formatEther(amountToAllocate)} nxm`);

const poolsData = productPools.map(pool => {
const { poolId, targetPrice, bumpedPrice, bumpedPriceUpdateTime, allocations, trancheCapacities } = pool;
Expand Down Expand Up @@ -293,7 +295,8 @@ const quoteEngine = (store, productId, amount, period, coverAsset) => {

const pool = poolsData.find(data => poolId.toString() === data.poolId.toString());
if (!pool) {
console.error('poolsData: ', inspect(poolsData, { depth: null }));
console.info(`Available poolIds in poolsData: ${poolsData.map(p => p.poolId).join(', ')}`);
console.debug('poolsData: ', inspect(poolsData, { depth: null }));
throw new Error(`Unable to find pool ${poolId} in poolsData`);
}

Expand All @@ -311,10 +314,10 @@ const quoteEngine = (store, productId, amount, period, coverAsset) => {
asset: selectAsset(store, assetId),
}));

console.log('Pool:', poolId);
console.log('Initially used capacity:', formatEther(pool.initialCapacityUsed), 'nxm');
console.log('Total pool capacity :', formatEther(pool.totalCapacity), 'nxm');
console.log('Pool capacity :', formatEther(capacityInNxm), 'nxm');
console.info('Pool:', poolId);
console.info('Initially used capacity:', formatEther(pool.initialCapacityUsed), 'nxm');
console.info('Total pool capacity :', formatEther(pool.totalCapacity), 'nxm');
console.info('Pool capacity :', formatEther(capacityInNxm), 'nxm');

const coverAmountInAsset = amountToAllocate.mul(assetRate).div(WeiPerEther);

Expand Down
6 changes: 3 additions & 3 deletions src/lib/synchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = async (store, chainApi, eventsApi) => {
payload: { productId, poolId, poolProduct },
});
}
console.log(`Update: product data for product with id ${productId}`);
console.info(`Update: product data for product with id ${productId}`);
};

async function updatePool(poolId) {
Expand All @@ -54,7 +54,7 @@ module.exports = async (store, chainApi, eventsApi) => {
payload: { productId, poolId, poolProduct },
});
}
console.log(`Update: Pool data for pool with id ${poolId}`);
console.info(`Update: Pool data for pool with id ${poolId}`);
}

const updateAll = async () => {
Expand All @@ -79,7 +79,7 @@ module.exports = async (store, chainApi, eventsApi) => {
const rate = await chainApi.fetchTokenPriceInAsset(assetId);
store.dispatch({ type: SET_ASSET_RATE, payload: { assetId, rate } });
}
console.log('Update: Asset rates');
console.info('Update: Asset rates');
};

eventsApi.on('pool:change', updatePool);
Expand Down
18 changes: 14 additions & 4 deletions src/routes/capacity.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { inspect } = require('node:util');

const { ethers, BigNumber } = require('ethers');
const express = require('express');

Expand Down Expand Up @@ -71,6 +73,8 @@ router.get(
asyncRoute(async (req, res) => {
const periodQuery = Number(req.query.period) || 30;

console.info(`Request: periodQuery=${periodQuery}`);

if (!Number.isInteger(periodQuery) || periodQuery < 28 || periodQuery > 365) {
return res.status(400).send({ error: 'Invalid period: must be an integer between 28 and 365', response: null });
}
Expand All @@ -81,7 +85,7 @@ router.get(
const capacities = getAllProductCapacities(store, period);

const response = capacities.map(capacity => formatCapacityResult(capacity));
console.log(JSON.stringify(capacities, null, 2));
console.debug('Response: ', inspect(response, { depth: null }));

res.json(response);
} catch (error) {
Expand Down Expand Up @@ -167,6 +171,8 @@ router.get(
const productId = Number(req.params.productId);
const periodQuery = Number(req.query.period) || 30;

console.info(`Request: productId=${productId}, periodQuery=${periodQuery}`);

if (!Number.isInteger(periodQuery) || periodQuery < 28 || periodQuery > 365) {
return res.status(400).send({ error: 'Invalid period: must be an integer between 28 and 365', response: null });
}
Expand All @@ -184,7 +190,7 @@ router.get(
}

const response = formatCapacityResult(capacity);
console.log(JSON.stringify(response, null, 2));
console.info('Response: ', inspect(response, { depth: null }));

res.json(response);
} catch (error) {
Expand Down Expand Up @@ -267,6 +273,8 @@ router.get(
const poolId = Number(req.params.poolId);
const periodQuery = Number(req.query.period) || 30;

console.info(`Request: poolId=${poolId}, periodQuery=${periodQuery}`);

if (!Number.isInteger(periodQuery) || periodQuery < 28 || periodQuery > 365) {
return res.status(400).send({ error: 'Invalid period: must be an integer between 28 and 365', response: null });
}
Expand All @@ -284,7 +292,7 @@ router.get(
utilizationRate: poolCapacity.utilizationRate.toNumber(),
productsCapacity: poolCapacity.productsCapacity.map(productCapacity => formatCapacityResult(productCapacity)),
};
console.log(JSON.stringify(response, null, 2));
console.info('Response: ', inspect(response, { depth: null }));

res.json(response);
} catch (error) {
Expand Down Expand Up @@ -343,6 +351,8 @@ router.get(
const productId = Number(req.params.productId);
const periodQuery = Number(req.query.period) || 30;

console.info(`Request: poolId=${poolId}, productId=${productId}, periodQuery=${periodQuery}`);

if (!Number.isInteger(periodQuery) || periodQuery < 28 || periodQuery > 365) {
return res.status(400).send({ error: 'Invalid period: must be an integer between 28 and 365', response: null });
}
Expand All @@ -362,7 +372,7 @@ router.get(
}

const response = formatCapacityResult(capacity);
console.log(JSON.stringify(response, null, 2));
console.info('Response: ', inspect(response, { depth: null }));

res.json(response);
} catch (error) {
Expand Down
6 changes: 5 additions & 1 deletion src/routes/pricing.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { inspect } = require('node:util');

const express = require('express');

const { asyncRoute } = require('../lib/helpers');
Expand Down Expand Up @@ -60,6 +62,8 @@ router.get(
asyncRoute(async (req, res) => {
const productId = Number(req.params.productId);

console.info(`Request: productId=${productId}`);

if (!Number.isInteger(productId) || productId < 0) {
return res.status(400).send({ error: 'Invalid productId: must be an integer', response: null });
}
Expand All @@ -73,7 +77,7 @@ router.get(
}

const response = formatPricingResult(pricingResult);
console.log(JSON.stringify(response, null, 2));
console.info('Response: ', inspect(response, { depth: null }));

res.json(response);
} catch (error) {
Expand Down
7 changes: 6 additions & 1 deletion src/routes/quote.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { inspect } = require('node:util');

const { BigNumber, ethers } = require('ethers');
const express = require('express');

Expand Down Expand Up @@ -148,6 +150,9 @@ router.get(
const period = BigNumber.from(req.query.period).mul(24 * 3600);
const coverAsset = Number(req.query.coverAsset);

const normalizedRequestQuery = { ...req.query, amount: BigNumber.from(req.query.amount).toString() };
console.info('Request: ', inspect(normalizedRequestQuery, { depth: null }));

const store = req.app.get('store');
const route = await quoteEngine(store, productId, amount, period, coverAsset);

Expand Down Expand Up @@ -208,7 +213,7 @@ router.get(
})),
};

console.log(JSON.stringify(response, null, 2));
console.info('Response: ', inspect(response, { depth: null }));

res.json(response);
}),
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eventApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function contractFactoryMock(addresses, provider) {

const mockedFactory = (name, id = 0, forceNew = false) => {
if (name !== 'StakingPoolFactory') {
console.log(name, id, forceNew);
console.log({ name, id, forceNew });
return factory(name, id, forceNew);
}

Expand Down
2 changes: 0 additions & 2 deletions test/unit/quoteEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ describe('Quote Engine tests', () => {
{
const quote = quotes[1];

console.log(quote.premiumInNxm.toString());

expect(quote.poolId).to.be.equal(2);
expect(quote.premiumInNxm.toString()).to.be.equal('468378082191780821');
expect(quote.premiumInAsset.toString()).to.be.equal('13453897657327743831');
Expand Down
Loading