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

Commit

Permalink
implement bunyan logging (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
CelestialCrafter authored Dec 11, 2023
2 parents c65475c + 9414c55 commit d05eca5
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 40 deletions.
File renamed without changes.
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
15 changes: 7 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,9 @@ 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');
log.info(approvalResults, 'approvals');

// Update worths
Bot.bulkWrite(
Expand Down
3 changes: 2 additions & 1 deletion src/blockchain/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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';

const provider = new JsonRpcProvider(providerUrl);
Expand All @@ -31,7 +32,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
20 changes: 19 additions & 1 deletion src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DB_URI, REDIS_URI } from '$env/static/private';
import { evaluateModelsWhenConnectionReady } from '$lib/models.server';

import executeAlgorithmCheck from './blockchain/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
}
});
47 changes: 31 additions & 16 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
<main class="p-0 text-2xl overflow-visible" >
<div class="header absolute flex gap-3 items-center w-screen h-25 p-6 pl-64 pr-64 bg-section-300">
<img src="/notfound.png" class="h-16 rounded-md" alt="logo">
<p class="">auto-trader</p>
<span class="subtext">/</span>
<a href="/identity" class="text-xl transition-all underline-offset-3 decoration-white/30 hover:decoration-white underline hover:underline-offset-[6px]">sign-in</a>
<a href="/dashboard" class="text-xl transition-all underline-offset-3 decoration-white/30 hover:decoration-white underline hover:underline-offset-[6px]">dashboard</a>
</div>
<main class="overflow-visible p-0 text-2xl">
<div class="header h-25 absolute flex w-screen items-center gap-3 bg-section-300 p-6 pl-64 pr-64">
<img src="/notfound.png" class="h-16 rounded-md" alt="logo" />
<p class="">auto-trader</p>
<span class="subtext">/</span>
<!-- eslint-disable max-len -->
<a
href="/identity"
class="underline-offset-3 text-xl underline decoration-white/30 transition-all hover:decoration-white hover:underline-offset-[6px]"
>sign-in</a
>
<a
href="/dashboard"
class="underline-offset-3 text-xl underline decoration-white/30 transition-all hover:decoration-white hover:underline-offset-[6px]"
>dashboard</a
>
<!-- eslint-enable max-len -->
</div>

<div class="flex w-screen h-screen justify-center items-center gap-3">
<div class="w-1/4">
<h1 class="text-6xl">lorem ipsum</h1>
<p class="subtext mt-4">dolor sit amet consectetur adipisicing elit. Explicabo corrupti, quibusdam laudantium sit, fugit nisi quae provident porro id laborum voluptatem perspiciatis praesentium, perferendis vero! Quo voluptas quod et ipsam.</p>
</div>
<img src="dashboard.webp" alt="dashboard" class="w-1/2 rounded-2xl">
</div>
</main>
<div class="flex h-screen w-screen items-center justify-center gap-3">
<div class="w-1/4">
<h1 class="text-6xl">lorem ipsum</h1>
<!-- eslint-disable-next-line max-len -->
<p class="subtext mt-4">
dolor sit amet consectetur adipisicing elit. Explicabo corrupti, quibusdam laudantium sit,
fugit nisi quae provident porro id laborum voluptatem perspiciatis praesentium, perferendis
vero! Quo voluptas quod et ipsam.
</p>
</div>
<img src="dashboard.webp" alt="dashboard" class="w-1/2 rounded-2xl" />
</div>
</main>
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 d05eca5

Please sign in to comment.