-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into feat/rescue-tokens-levered-positions
- Loading branch information
Showing
195 changed files
with
48,683 additions
and
4,101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,13 @@ const config = { | |
chainId: parseInt(process.env.TARGET_CHAIN_ID ?? "34443", 10), | ||
rpcUrls: process.env.WEB3_HTTP_PROVIDER_URLS | ||
? process.env.WEB3_HTTP_PROVIDER_URLS.split(",") | ||
: ["https://mainnet.mode.network/"], // Updated to handle multiple RPC URLs | ||
: ["https://mode.drpc.org"], | ||
adminPrivateKey: process.env.ETHEREUM_ADMIN_PRIVATE_KEY ?? "", | ||
adminAccount: process.env.ETHEREUM_ADMIN_ACCOUNT ?? "", | ||
excludedComptrollers: process.env.EXCLUDED_COMPTROLLERS ? process.env.EXCLUDED_COMPTROLLERS.split(",") : [], | ||
discordWebhookUrl: process.env.DISCORD_WEBHOOK_URL ?? "", | ||
DISCORD_FAILURE_WEBHOOK_URL: process.env.DISCORD_FAILURE_WEBHOOK_URL || "", | ||
DISCORD_SUCCESS_WEBHOOK_URL: process.env.DISCORD_SUCCESS_WEBHOOK_URL || "", | ||
PER_discordWebhookUrl: process.env.PER_DISCORD_WEBHOOK_URL ?? "", | ||
sendgridApiKey: process.env.SENDGRID_API_KEY ?? "", | ||
sendgridEmailTo: process.env.SENDGRID_EMAIL_TO ?? "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { LiquidatablePool } from "@ionicprotocol/sdk"; | ||
import { createPublicClient, createWalletClient, fallback, Hex, http } from "viem"; | ||
import { privateKeyToAccount } from "viem/accounts"; | ||
import { base } from "viem/chains"; | ||
|
||
import config from "./config"; | ||
import { Liquidator } from "./services"; | ||
import { setUpSdk } from "./utils"; | ||
|
||
// Mock the liquidatePositions function | ||
const liquidatePositions = async (liquidator: Liquidator, options?: { blockNumber?: bigint }) => { | ||
const blockNumber = options?.blockNumber; | ||
console.log("Block number for liquidation:", blockNumber); | ||
const { logger } = liquidator.sdk; | ||
|
||
// Hardcoded liquidation data without unsupported properties | ||
const dummyLiquidation: LiquidatablePool = { | ||
comptroller: "0x8Fb3D4a94D0aA5D6EDaAC3Ed82B59a27f56d923a", | ||
liquidations: [ | ||
{ | ||
borrower: "0x88dFF072a2ad3880268Ad15BC647Aa4fb71cb7fB", | ||
repayAmount: BigInt(3014530), // Adjust based on expected type | ||
cErc20: "0x4341620757Bee7EB4553912FaFC963e59C949147", | ||
cTokenCollateral: "0x3120B4907851cc9D780eef9aF88ae4d5360175Fd", | ||
minProfitAmount: BigInt(0), | ||
flashSwapContract: "0x468cC91dF6F669CaE6cdCE766995Bd7874052FBc", | ||
redemptionStrategies: ["0x5cA3fd2c285C4138185Ef1BdA7573D415020F3C8"], | ||
strategyData: [ | ||
"0x000000000000000000000000d988097fb8612cc24eec14542bc03424c656005f000000000000000000000000ac48fcf1049668b285f3dc72483df5ae2162f7e8", | ||
], | ||
debtFundingStrategies: [], | ||
debtFundingStrategiesData: [], | ||
}, | ||
{ | ||
borrower: "0xF91bb6A963D870EF53Fdd77af3BB341366725dD8", | ||
repayAmount: BigInt(4688498), // Adjust based on expected type | ||
cErc20: "0xc53edEafb6D502DAEC5A7015D67936CEa0cD0F52", | ||
cTokenCollateral: "0xDb8eE6D1114021A94A045956BBeeCF35d13a30F2", | ||
minProfitAmount: BigInt(0), | ||
flashSwapContract: "0x468cC91dF6F669CaE6cdCE766995Bd7874052FBc", | ||
redemptionStrategies: ["0x5cA3fd2c285C4138185Ef1BdA7573D415020F3C8"], | ||
strategyData: [ | ||
"0x000000000000000000000000d988097fb8612cc24eec14542bc03424c656005f000000000000000000000000ac48fcf1049668b285f3dc72483df5ae2162f7e8", | ||
], | ||
debtFundingStrategies: [], | ||
debtFundingStrategiesData: [], | ||
}, | ||
], | ||
}; | ||
|
||
// Log the dummy liquidation details | ||
logger.info( | ||
`Liquidating pool: ${dummyLiquidation.comptroller} -- ${dummyLiquidation.liquidations.length} liquidations found` | ||
); | ||
|
||
// Simulate liquidating the pool using the hardcoded data | ||
await liquidator.liquidate(dummyLiquidation); | ||
|
||
logger.info("Dummy liquidation process completed."); | ||
}; | ||
|
||
const run = async () => { | ||
try { | ||
const account = privateKeyToAccount(config.adminPrivateKey as Hex); | ||
|
||
// Create public and wallet clients | ||
const client = createPublicClient({ | ||
chain: base, | ||
transport: fallback(config.rpcUrls.map((url) => http(url))), | ||
}); | ||
|
||
const walletClient = createWalletClient({ | ||
account, | ||
chain: base, | ||
transport: fallback(config.rpcUrls.map((url) => http(url))), | ||
}); | ||
|
||
// Set up the SDK with the correct chain and clients | ||
const sdk = setUpSdk(config.chainId, client as any, walletClient); | ||
|
||
// Initialize the Liquidator | ||
const liquidator = new Liquidator(sdk); | ||
|
||
sdk.logger.info(`Starting liquidation bot on chain: ${config.chainId}`); | ||
sdk.logger.info( | ||
`Config for bot: ${JSON.stringify({ ...sdk.chainLiquidationConfig, ...config, adminPrivateKey: "***********" })}` | ||
); | ||
|
||
// Run the dummy liquidation process | ||
await liquidatePositions(liquidator); | ||
|
||
sdk.logger.info("Dummy liquidation process completed."); | ||
} catch (error) { | ||
console.error("Error occurred during liquidation:", error); | ||
} | ||
}; | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { EncodedLiquidationTx, ErroredPool, LiquidatablePool } from "@ionicprotocol/sdk"; | ||
import { SupportedChains } from "@ionicprotocol/types"; | ||
import { MessageBuilder, Webhook } from "discord-webhook-node"; | ||
import { TransactionReceipt } from "viem"; | ||
|
||
import config from "../config"; | ||
export type SimplifiedTransactionReceipt = Pick< | ||
TransactionReceipt, | ||
"transactionHash" | "contractAddress" | "from" | "to" | "status" | ||
>; | ||
|
||
export class DiscordService { | ||
lastSentMessages: { | ||
erroredPools: { pools: Array<ErroredPool>; timestamp: number }; | ||
liquidations: { tx: EncodedLiquidationTx[] | null; timestamp: number }; | ||
}; | ||
chainId: SupportedChains; | ||
private errorColor = 0xff0000; | ||
private warningColor = 0xfcdb03; | ||
private infoColor = 0x00ff00; | ||
private failureHook = new Webhook(config.DISCORD_FAILURE_WEBHOOK_URL); | ||
private successHook = new Webhook(config.DISCORD_SUCCESS_WEBHOOK_URL); | ||
|
||
constructor(chainId: SupportedChains) { | ||
this.chainId = chainId; | ||
this.lastSentMessages = { | ||
erroredPools: { pools: [], timestamp: 0 }, | ||
liquidations: { tx: null, timestamp: 0 }, | ||
}; | ||
} | ||
|
||
private createBaseMessage(): MessageBuilder { | ||
return new MessageBuilder().addField("Chain", `Chain ID: ${this.chainId} (${SupportedChains[this.chainId]})`, true); | ||
} | ||
|
||
// Helper to handle rate limits | ||
private async sendWithRetry(hook: Webhook, message: MessageBuilder, retries: number = 3): Promise<void> { | ||
let attempt = 0; | ||
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
while (attempt < retries) { | ||
try { | ||
await hook.send(message); | ||
return; // exit if successful | ||
} catch (error: any) { | ||
if (error.response?.statusCode === 429) { | ||
// Rate limit error | ||
const retryAfter = parseInt(error.response?.headers["retry-after"]) * 1000 || 5000; | ||
console.warn(`Rate limited. Retrying after ${retryAfter / 1000}s...`); | ||
await delay(retryAfter); // Wait before retrying | ||
} else { | ||
console.error("Failed to send message to Discord:", error); | ||
throw error; // Rethrow error if not rate-limiting | ||
} | ||
} | ||
attempt++; | ||
} | ||
console.error("Failed to send message after maximum retries."); | ||
} | ||
|
||
async sendLiquidationSuccess(_successfulTxs: SimplifiedTransactionReceipt[], msg: string): Promise<void> { | ||
const baseMessage = this.createBaseMessage(); | ||
baseMessage.setColor(this.infoColor); | ||
baseMessage.setDescription(`**__Successful Liquidations:__**\n${msg}`); | ||
await this.sendWithRetry(this.successHook, baseMessage); | ||
} | ||
|
||
async sendLiquidationFailure(pool: LiquidatablePool, errorMessage: string): Promise<void> { | ||
const baseMessage = this.createBaseMessage(); | ||
baseMessage.setColor(this.errorColor); | ||
baseMessage.setDescription( | ||
`Failed Liquidation:\nBorrower: ${pool.liquidations[0].borrower}\nError: ${errorMessage}` | ||
); | ||
await this.sendWithRetry(this.failureHook, baseMessage); | ||
} | ||
|
||
async sendLiquidationFetchingFailure(_erroredPools: any[], msg: string): Promise<void> { | ||
const baseMessage = this.createBaseMessage(); | ||
baseMessage.setColor(this.warningColor); | ||
baseMessage.setDescription(`Failed to fetch liquidations:\n${msg}`); | ||
await this.sendWithRetry(this.failureHook, baseMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.