Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
change all console logs to bunyan
Browse files Browse the repository at this point in the history
add response time header to requests
  • Loading branch information
CelestialCrafter committed Dec 11, 2023
1 parent c65475c commit 27af9ca
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@uniswap/smart-order-router": "^3.16.29",
"@uniswap/v3-periphery": "^1.4.3",
"autoprefixer": "^10.4.16",
"bunyan": "^1.8.15",
"ethers": "6.8.1",
"jsonwebtoken": "^9.0.1",
"mongoose": "^8.0.0",
Expand Down
25 changes: 15 additions & 10 deletions src/blockchain/approval.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
import { providerUrl, repopulateAndSend } from '$lib/blockchain.server';
import { User } from '$lib/models.server';
import { setVapidDetails } from '$lib/push.server';
import { log } from '$lib/logging.server';

import { getWorth } from './worth';

const provider = new JsonRpcProvider(providerUrl);
Expand Down Expand Up @@ -53,7 +55,9 @@ const executeApproval = async (privateKey, { id, strengthToUSD, baseToken = defa
const ethBalance = (await provider.getBalance(address)).toBigInt();

if (ethBalance < minimumEth) {
await executeOOMNotification(id, address).catch(console.log);
await executeOOMNotification(id, address).catch(err =>
log.error({ error: err }, 'oom notification error')
);
return [];
}

Expand All @@ -73,15 +77,16 @@ const executeApproval = async (privateKey, { id, strengthToUSD, baseToken = defa
.filter(p => p)
);

renew.forEach(async data =>
data.renew
? console.log(
`Renewing ${await data.contract.getAddress()} for ${toReadableAmount(
data.value,
await data.contract.decimals()
)}`
)
: null
renew.forEach(
async data =>
data.renew &&
log.debug(
{
address: await data.contract.getAddress(),
value: toReadableAmount(data.value, await data.contract.decimals())
},
'renewing'
)
);

return Promise.allSettled(
Expand Down
14 changes: 6 additions & 8 deletions src/blockchain/check.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import jwt from 'jsonwebtoken';
import mongoose from 'mongoose';
import { inspect } from 'util';

import { ALGORITHM_SERVER_URI, JWT_SECRET } from '$env/static/private';
import { toReadableAmount, defaultBaseToken } from '$lib/blockchain';
import { Bot } from '$lib/models.server';
import { log } from '$lib/logging.server';

import executeApprovals from './approval';
import executeTransactions from './trading';
import addWorths from './worth';

export default async redis => {
console.log('Running algorithm check');
log.info('algorithm check');
try {
const token = jwt.sign({ server: true }, JWT_SECRET, { algorithm: 'HS256' });
const response = await (
Expand All @@ -22,9 +22,9 @@ export default async redis => {
})
).json();

if (!response.new_datapoint) return console.log('No New Datapoints');
if (!response.new_datapoint) return log.debug('no new datapoints');
} catch (err) {
return console.log('Couldnt connect to algorithm server');
return log.error({ error: err }, 'algorithm check error');
}

await redis.connect();
Expand Down Expand Up @@ -69,10 +69,8 @@ export default async redis => {
);
const worthsResults = await addWorths(tradeData);

const inspectOptions = { depth: 10, colors: true };
console.log('approvals', inspect(approvalResults, inspectOptions));
console.log('transactions', inspect(transactionResults, inspectOptions));
console.log('worths', inspect(worthsResults, inspectOptions));
log.info(transactionResults, 'transactions');
log.info(worthsResults, 'worths');

// Update worths
Bot.bulkWrite(
Expand Down
5 changes: 4 additions & 1 deletion src/blockchain/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { AlphaRouter, SwapType } from '@uniswap/smart-order-router';

import { tokens, fromReadableAmount, addresses, defaultBaseToken } from '$lib/blockchain';
import { providerUrl, repopulateAndSend } from '$lib/blockchain.server';
import { log } from '$lib/logging.server';
import { PUBLIC_CHAINID } from '$env/static/public';

import { executeApproval } from './approval';

const provider = new JsonRpcProvider(providerUrl);

const router = new AlphaRouter({
Expand All @@ -31,7 +34,7 @@ const executeTransaction = async (
{ baseAmount, baseToken = defaultBaseToken, modToken, action }
) => {
const wallet = new Wallet(privateKey, provider);
console.log(`Running trades for ${await wallet.getAddress()}`);
log.debug({ address: await wallet.getAddress() }, 'running trades');

const amount = CurrencyAmount.fromRawAmount(
baseToken,
Expand Down
22 changes: 20 additions & 2 deletions src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { createClient } from 'redis';
import { DB_URI, REDIS_URI } from '$env/static/private';
import { evaluateModelsWhenConnectionReady } from '$lib/models.server';

import executeAlgorithmCheck from './blockchain/check';
import executeAlgorithmCheck from './trading/check';
import { log } from '$lib/logging.server';

mongoose.connect(DB_URI);
const redis = createClient({ url: REDIS_URI });
Expand All @@ -17,13 +18,30 @@ export const job = schedule.scheduleJob(`*/5 * * * *`, () =>
.finally(() => redis.isOpen && redis.disconnect())
);

export const handle = async ({ event, resolve }) => {
const startTimestamp = Date.now();

const response = await resolve(event);
const responseTime = Date.now() - startTimestamp;

// @TODO include request id, and log body
log.info({ request: event, responseTime }, 'request');
response.headers.set('X-Response-Time', responseTime);

return response;
};

export const handleError = async ({ error, event }) => {
log.error({ error, request: event }, 'request error');
};

const shutdown = async signal => {
job.cancel();
await (await mongoose.connection).disconnect();
redis.isOpen && (await redis.disconnect());
throw Error(`Shutting down${signal ? `due to ${signal}` : ''}...`);
};

redis.on('error', err => console.log('Redis Client Error', err));
redis.on('error', err => log.error({ error: err }, 'redis error'));
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
1 change: 1 addition & 0 deletions src/lib/blockchain.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { chainNames } from '$lib/blockchain';
export const providerUrl = `https://${
chainNames[Number(PUBLIC_CHAINID)]
}.infura.io/v3/${INFURA_SECRET}`;

const deBigNumberify = object => {
const newObject = Object.assign(object, {});

Expand Down
53 changes: 53 additions & 0 deletions src/lib/logging.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { WARN, createLogger, stdSerializers } from 'bunyan';

const transactionSerializer = transaction => {
if (!transaction?.status) return transaction;

return {
successful: transaction.status,
transactionHash: transaction.transactionHash,
from: transaction.from,
to: transaction.to,
gas: transaction.gasUsed
};
};

const svelteRequestEventSerializer = event => {
if (!event?.request) return event;

const headers = Object.fromEntries(event.request.headers);
delete headers.cookie;

return {
method: event.request.method,
url: event.url,
headers,
remoteAddress: event.getClientAddress(),
platform: event.platform,
id: crypto.randomUUID()
};
};

export const log = createLogger({
name: 'main-server',
streams: [
{
type: 'rotating-file',
path: '/var/log/auto-trading/main-server.log',
period: '1d',
count: 7
},
{
stream: process.stdout
},
{
stream: process.stderr,
level: WARN
}
],
serializers: {
transaction: transactionSerializer,
request: svelteRequestEventSerializer,
error: stdSerializers.err
}
});
5 changes: 2 additions & 3 deletions src/routes/dashboard/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
const selectedAccount = getContext('selectedAccount');
const user = getContext('user');
</script>

<main class="flex items-center justify-center p-6">
<section class="border-heavy flex h-full w-5/6">
<section class="flex h-full w-5/6 border-heavy">
<Sidebar />
<div class="flex flex-grow flex-col gap-3">
{#if $selectedAccount}
{@const { address, id, balance } = $selectedAccount}
<section class="flex items-center">
<section class="flex items-center">
<div class="">
<h1 class="text-2xl">
{address}
Expand Down
1 change: 0 additions & 1 deletion src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
self.oninstall = () => self.skipWaiting();
self.onpush = event => {
const data = event.data.json();
console.log(data);
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
Expand Down

0 comments on commit 27af9ca

Please sign in to comment.