Skip to content

Commit

Permalink
Merge pull request #229 from mrgnlabs/jjjj/jup-api-liquidator
Browse files Browse the repository at this point in the history
Use Jup v6 api for liquidations
  • Loading branch information
jkbpvsc authored Dec 6, 2023
2 parents 7478f2a + 60d080a commit 288c631
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 461 deletions.
77 changes: 73 additions & 4 deletions apps/alpha-liquidator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,80 @@

> This bot structure is based on the Jupiter quote API, check it out [here](https://github.com/jup-ag/jupiter-quote-api)
## Prerequisite
## Setup

- [redis](https://redis.io/docs/getting-started/installation/install-redis-on-mac-os/)
Before running the alpha liquidator bot, ensure that you have the following prerequisites installed:

- Node.js (v14 or higher)
- Yarn package manager

Then, in the root of the mrgn-ts repo, install the dependencies and build the project by running:

```sh
yarn
yarn build
```

Make sure to configure the environment variables as described in the [Alpha Liquidator Configuration](#alpha-liquidator-configuration) section.

## How to run

1. `yarn`
2. `RPC_URL=xxxxxxx yarn start`
1. `./scripts/start.sh`

## Alpha Liquidator Configuration

The Alpha Liquidator application uses an environment configuration schema to manage its settings. This configuration is defined in `apps/alpha-liquidator/src/config.ts` and uses the `zod` library for schema definition and validation.

### Configuration Schema

Below are the environment variables used by the application, along with their expected types and default values:

- `RPC_ENDPOINT`: The RPC endpoint URL as a string.

- `LIQUIDATOR_PK`: The public key of the liquidator. It is a string that gets converted into a `PublicKey` object.

- `WALLET_KEYPAIR`: The wallet keypair for the liquidator. It is a string that either gets loaded from a file if it exists or gets converted from a JSON string into a `Keypair` object.

- `MIN_LIQUIDATION_AMOUNT_USD_UI`: The minimum liquidation amount in USD. It is a string that gets converted into a `BigNumber` object. Default is `"0.1"`.

- `MAX_SLIPPAGE_BPS`: The maximum slippage in basis points. It is a string that gets parsed into an integer. Default is `"250"`.

- `MIN_SOL_BALANCE`: The minimum balance of SOL required. It is a number with a default value of `0.5`.

- `ACCOUNT_COOL_DOWN_SECONDS`: The cool down period in seconds before reattempting a liquidation on an account that previously failed when using the SORT_ACCOUNTS_MODE. It is a string that gets parsed into an integer. Default is `"120"`.

- `SLEEP_INTERVAL`: The interval in milliseconds between checks. It is a string that gets parsed into an integer. Default is `"10000"`.

- `SENTRY`: A flag indicating whether Sentry is enabled for error logging. It accepts a string and converts it to a boolean. Default is `"false"`.

- `SENTRY_DSN`: The Sentry DSN string for error reporting. This field is optional.

- `SORT_ACCOUNTS_MODE`: An experimental feature flag indicating whether accounts should be sorted by the liquidation amount, with accounts having more to liquidate being prioritized. It accepts a string and converts it to a boolean. Default is `"false"`.

- `MARGINFI_ACCOUNT_BLACKLIST`: A comma-separated string of MarginFi account public keys to be blacklisted. It gets transformed into an array of `PublicKey` objects. This field is optional.

- `MARGINFI_ACCOUNT_WHITELIST`: A comma-separated string of MarginFi account public keys to be whitelisted. It gets transformed into an array of `PublicKey` objects. This field is optional.

### Required Configuration Fields

The following environment variables are mandatory for the application to run:

- `RPC_ENDPOINT`: The RPC endpoint URL as a string.
- `LIQUIDATOR_PK`: The public key of the liquidator. It is a string that gets converted into a `PublicKey` object.
- `WALLET_KEYPAIR`: The wallet keypair for the liquidator. It is a string that either gets loaded from a file if it exists or gets converted from a JSON string into a `Keypair` object.

## Validation and Parsing

The `env_config` object is created by parsing the `process.env` object through the `envSchema`. If any of the required environment variables are missing or invalid, the application will throw an error during the parsing process.

## Mutually Exclusive Fields

The application ensures that `MARGINFI_ACCOUNT_BLACKLIST` and `MARGINFI_ACCOUNT_WHITELIST` are mutually exclusive. If both are provided, an error is thrown.

## Sentry Integration

If Sentry is enabled (`SENTRY` is `true`), and a `SENTRY_DSN` is provided, the application initializes Sentry for error tracking. It also captures a startup message indicating that the Alpha Liquidator has started.

## Usage

To use the configuration, import `env_config` from the `config.ts` file. Ensure that the required environment variables are set before starting the application.
5 changes: 2 additions & 3 deletions apps/alpha-liquidator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"watch": "tsc-watch -p tsconfig.json --onCompilationComplete 'yarn build' --onSuccess 'yarn serve'",
"setup": "ts-node src/setup.ts",
"inspect": "ts-node src/inspect.ts",
"serve": "pm2-runtime scripts/pm2.config.js",
"start": "ts-node src/runLiquidatorOnlyJup.ts"
"start": "ts-node src/runLiquidatorJupApi.ts"
},
"license": "MIT",
"dependencies": {
Expand All @@ -33,7 +32,7 @@
"bn.js": "5.2.1",
"cookie": "^0.4.1",
"cron": "~2.0.0",
"cross-fetch": "3.1.5",
"cross-fetch": "^4.0.0",
"decimal.js": "10.4.2",
"dotenv": "^16.0.3",
"fetch-retry": "~5.0.3",
Expand Down
4 changes: 4 additions & 0 deletions apps/alpha-liquidator/scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
while true; do
yarn start
sleep 1 # Sleep for 1 second before restarting, adjust as needed
done
8 changes: 7 additions & 1 deletion apps/alpha-liquidator/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if (!process.env.RPC_ENDPOINT) {

/*eslint sort-keys: "error"*/
let envSchema = z.object({
ACCOUNT_COOL_DOWN_SECONDS: z.string().default("120").transform((s) => parseInt(s, 10)),
IS_DEV: z
.string()
.optional()
Expand All @@ -48,6 +49,7 @@ let envSchema = z.object({
return pkArrayStr.split(",").map((pkStr) => new PublicKey(pkStr));
})
.optional(),
MAX_SLIPPAGE_BPS: z.string().optional().default("250").transform((s) => Number.parseInt(s, 10)),
MIN_LIQUIDATION_AMOUNT_USD_UI: z
.string()
.default("0.1")
Expand All @@ -68,11 +70,15 @@ let envSchema = z.object({
.string()
.default("10000")
.transform((s) => parseInt(s, 10)),
SORT_ACCOUNTS_MODE: z
.string()
.optional()
.default("false")
.transform((s) => s === "true" || s === "1"),
WALLET_KEYPAIR: z.string().transform((keypairStr) => {
if (fs.existsSync(resolveHome(keypairStr))) {
return loadKeypair(keypairStr);
} else {
console.log(keypairStr);
return Keypair.fromSecretKey(new Uint8Array(JSON.parse(keypairStr)));
}
}),
Expand Down
Loading

0 comments on commit 288c631

Please sign in to comment.