Skip to content

Commit

Permalink
Add googleCloudLog, improve logging of info; alert on failed liquidat…
Browse files Browse the repository at this point in the history
…ion (compound-finance#619)

* Add googleCloudLog, improve logging of info; alert on failed liquidation

* lint

* log useFlashbots
  • Loading branch information
scott-silver authored Nov 14, 2022
1 parent c80e115 commit 0076d71
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
20 changes: 20 additions & 0 deletions scripts/liquidation_bot/googleCloudLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Utility to log data in a structured way that will be reflected in Google Cloud
Platform logs.
Basically, lets you specify a severity level so we can do things like filtering
and alerting in GCP.
https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
*/

export enum LogSeverity {
INFO = 'INFO', // Routine information, such as ongoing status or performance
WARNING = 'WARNING', // Warning events, things that might cause problems
ERROR = 'ERROR', // Error events, things that are likely to cause problems
ALERT = 'ALERT' // A person must take an action immediately; may cause someone to be alerted
}

export default function googleCloudLog(severity: LogSeverity, message: string) {
console.log(JSON.stringify({ severity, message }));
}
12 changes: 9 additions & 3 deletions scripts/liquidation_bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './liquidateUnderwaterBorrowers';
import { FlashbotsBundleProvider } from '@flashbots/ethers-provider-bundle';
import { providers, Wallet } from 'ethers';
import googleCloudLog, { LogSeverity } from './googleCloudLog';

const loopDelay = 5000;
const loopsUntilUpdateAssets = 1000;
Expand All @@ -28,6 +29,11 @@ async function main() {

const network = hre.network.name;

googleCloudLog(
LogSeverity.INFO,
`Liquidation Bot started ${JSON.stringify({network, deployment, liquidatorAddress, useFlashbots})}`
);

const dm = new DeploymentManager(
network,
deployment,
Expand Down Expand Up @@ -85,14 +91,14 @@ async function main() {
let loops = 0;
while (true) {
if (loops >= loopsUntilUpdateAssets) {
console.log('Updating assets');
googleCloudLog(LogSeverity.INFO, 'Updating assets');
assets = await getAssets(comet);
loops = 0;
}

const currentBlockNumber = await hre.ethers.provider.getBlockNumber();

console.log(`currentBlockNumber: ${currentBlockNumber}`);
googleCloudLog(LogSeverity.INFO, `currentBlockNumber: ${currentBlockNumber}`);

if (currentBlockNumber !== lastBlockNumber) {
lastBlockNumber = currentBlockNumber;
Expand All @@ -110,7 +116,7 @@ async function main() {
);
}
} else {
console.log(`block already checked; waiting ${loopDelay}ms`);
googleCloudLog(LogSeverity.INFO, `block already checked; waiting ${loopDelay}ms`);
await new Promise(resolve => setTimeout(resolve, loopDelay));
}

Expand Down
22 changes: 13 additions & 9 deletions scripts/liquidation_bot/liquidateUnderwaterBorrowers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { exp } from '../../test/helpers';
import { FlashbotsBundleProvider } from '@flashbots/ethers-provider-bundle';
import { PopulatedTransaction } from 'ethers';
import googleCloudLog, { LogSeverity } from './googleCloudLog';

export interface SignerWithFlashbots {
signer: SignerWithAddress;
Expand Down Expand Up @@ -39,10 +40,10 @@ async function sendTxn(
signerWithFlashbots: SignerWithFlashbots
) {
if (signerWithFlashbots.flashbotsProvider) {
console.log('Sending a private txn');
googleCloudLog(LogSeverity.INFO, 'Sending a private txn');
await sendFlashbotsPrivateTransaction(txn, signerWithFlashbots.flashbotsProvider);
} else {
console.log('Sending a public txn');
googleCloudLog(LogSeverity.INFO, 'Sending a public txn');
await signerWithFlashbots.signer.sendTransaction(txn);
}
}
Expand All @@ -53,16 +54,19 @@ async function attemptLiquidation(
signerWithFlashbots: SignerWithFlashbots
) {
try {
googleCloudLog(LogSeverity.INFO, `Attempting to liquidate ${targetAddresses} via ${liquidator.address}`);
const txn = await liquidator.populateTransaction.initFlash({
accounts: targetAddresses,
pairToken: daiPool.tokenAddress,
poolFee: daiPool.poolFee
});
await sendTxn(txn, signerWithFlashbots);
console.log(`Successfully liquidated ${targetAddresses}`);
googleCloudLog(LogSeverity.INFO, `Successfully liquidated ${targetAddresses} via ${liquidator.address}`);
} catch (e) {
console.log(`Failed to liquidate ${targetAddresses}`);
console.log(e.message);
googleCloudLog(
LogSeverity.ALERT,
`Failed to liquidate ${targetAddresses} via ${liquidator.address}: ${e.message}`
);
}
}

Expand Down Expand Up @@ -92,13 +96,13 @@ export async function liquidateUnderwaterBorrowers(
): Promise<boolean> {
const uniqueAddresses = await getUniqueAddresses(comet);

console.log(`${uniqueAddresses.size} unique addresses found`);
googleCloudLog(LogSeverity.INFO, `${uniqueAddresses.size} unique addresses found`);

let liquidationAttempted = false;
for (const address of uniqueAddresses) {
const isLiquidatable = await comet.isLiquidatable(address);

console.log(`${address} isLiquidatable=${isLiquidatable}`);
googleCloudLog(LogSeverity.INFO, `${address} isLiquidatable=${isLiquidatable}`);

if (isLiquidatable) {
await attemptLiquidation(
Expand All @@ -118,10 +122,10 @@ export async function arbitragePurchaseableCollateral(
assets: Asset[],
signerWithFlashbots: SignerWithFlashbots
) {
console.log(`Checking for purchaseable collateral`);
googleCloudLog(LogSeverity.INFO, `Checking for purchaseable collateral`);

if (await hasPurchaseableCollateral(comet, assets)) {
console.log(`There is purchaseable collateral`);
googleCloudLog(LogSeverity.INFO, `There is purchaseable collateral`);
await attemptLiquidation(
liquidator,
[], // empty list means we will only buy collateral and not absorb
Expand Down

0 comments on commit 0076d71

Please sign in to comment.