Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaituVR committed Dec 20, 2023
1 parent 15c27fc commit cb1da89
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { getSafeVersion } from './utils';
import db from './mysql';
// TODO: remove when all environments are updated
import constants from './constants.json';
import { name as packageName, version as packageVersion } from '../package.json';
import {
name as packageName,
version as packageVersion
} from '../package.json';

const router = express.Router();

Expand Down Expand Up @@ -45,7 +48,9 @@ async function calculateSafeMessageHash(safe, message, network = '1') {

router.get('/api', async (req, res) => {
const commit = process.env.COMMIT_HASH || '';
const version = commit ? `${packageVersion}#${commit.substring(0, 7)}` : packageVersion;
const version = commit
? `${packageVersion}#${commit.substring(0, 7)}`
: packageVersion;
return res.json({
name: packageName,
version
Expand All @@ -55,7 +60,10 @@ router.get('/api', async (req, res) => {
router.get('/api/messages/:hash', async (req, res) => {
try {
const { hash } = req.params;
const results = await db.queryAsync('SELECT * FROM messages WHERE msg_hash = ?', [hash]);
const results = await db.queryAsync(
'SELECT * FROM messages WHERE msg_hash = ?',
[hash]
);
return res.json(results);
} catch (e) {
capture(e);
Expand Down
50 changes: 42 additions & 8 deletions src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ const delay = 60 * 60 * 24 * 3;
const interval = 15e3;
const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org';

const SUPPORTED_NETWORKS = ['1', '5', '10', '56', '100', '137', '42161', '1088'];
const SUPPORTED_NETWORKS = [
'1',
'5',
'10',
'56',
'100',
'137',
'42161',
'1088'
];

const errorMessagesWhitelist = [
'signature validation failed',
Expand Down Expand Up @@ -38,24 +47,41 @@ async function send(body, env = 'livenet') {
}

async function processSig(address, safeHash, network) {
const query = 'SELECT * FROM messages WHERE address = ? AND hash = ? AND network = ? LIMIT 1';
const query =
'SELECT * FROM messages WHERE address = ? AND hash = ? AND network = ? LIMIT 1';
const [message] = await db.queryAsync(query, [address, safeHash, network]);
console.log('Process sig', network, address, safeHash);
try {
const result: any = await send(message.payload);
if (result.error_description && errorMessagesWhitelist.includes(result.error_description)) {
if (
result.error_description &&
errorMessagesWhitelist.includes(result.error_description)
) {
console.log('[processSig] Error', network, address, safeHash, result);
return;
}
await db.queryAsync(
'DELETE FROM messages WHERE address = ? AND hash = ? AND network = ? LIMIT 1',
[address, safeHash, network]
);
console.log('[processSig] Sent message for', network, address, safeHash, result);
console.log(
'[processSig] Sent message for',
network,
address,
safeHash,
result
);
} catch (e) {
capture(e, { address, safeHash, network });
// @ts-ignore
console.log('[processSig] Failed', network, address, safeHash, e, (e as any)?.message);
console.log(
'[processSig] Failed',
network,
address,
safeHash,
e,
(e as any)?.message
);
}
}

Expand All @@ -69,7 +95,11 @@ async function checkSignedMessages(messages, network) {
network,
provider,
abi,
messages.map(message => [message.address, 'signedMessages', [message.hash]]),
messages.map(message => [
message.address,
'signedMessages',
[message.hash]
]),
{
blockTag: 'latest'
}
Expand Down Expand Up @@ -98,8 +128,12 @@ async function processSigs() {

// Get all messages from last 3 days and filter by supported networks
const ts = parseInt((Date.now() / 1e3).toFixed()) - delay;
let messages = await db.queryAsync('SELECT * FROM messages WHERE ts > ?', [ts]);
messages = messages.filter(message => SUPPORTED_NETWORKS.includes(message.network));
let messages = await db.queryAsync('SELECT * FROM messages WHERE ts > ?', [
ts
]);
messages = messages.filter(message =>
SUPPORTED_NETWORKS.includes(message.network)
);
console.log('Total messages waiting: ', messages.length);

// Divide messages by network
Expand Down
7 changes: 6 additions & 1 deletion src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import db from './mysql';
export default function initMetrics(app: Express) {
init(app, {
normalizedPath: [['^/api/messages/.+', '/api/messages/#hash']],
whitelistedPath: [/^\/$/, /^\/api$/, /^\/api\/msg$/, /^\/api\/messages\/.+$/]
whitelistedPath: [
/^\/$/,
/^\/api$/,
/^\/api\/msg$/,
/^\/api\/messages\/.+$/
]
});
}

Expand Down

0 comments on commit cb1da89

Please sign in to comment.