Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Jup v6 api for liquidations #229

Merged
merged 30 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
63280fa
feat: use jup v6 api for liquidating
jkbpvsc Aug 19, 2023
f33bcde
fix: correctly use api
jkbpvsc Aug 21, 2023
7e8279f
fix: use 1.4M cus
jkbpvsc Sep 10, 2023
6218b02
Merge remote-tracking branch 'origin/main' into jjjj/jup-api-liquidator
jkbpvsc Sep 10, 2023
3a68a88
fix: rm deprecated package
jkbpvsc Sep 10, 2023
a472136
fix: use MAX cus for liquidations
jkbpvsc Sep 10, 2023
19ce5e7
Merge branch 'main' into jjjj/jup-api-liquidator
jkbpvsc Oct 4, 2023
3f29954
Merge remote-tracking branch 'origin/main' into jjjj/jup-api-liquidator
jkbpvsc Oct 4, 2023
fdf873c
fix: remove runLiquidator file
jkbpvsc Oct 4, 2023
22daa0d
feat: configurable slippage
jkbpvsc Oct 21, 2023
b9f5820
Merge remote-tracking branch 'origin/jjjj/jup-api-liquidator' into jj…
jkbpvsc Oct 21, 2023
cfc58c8
various fixes
jkbpvsc Nov 8, 2023
94026e5
fix: liquidation amount
jkbpvsc Nov 8, 2023
a8b3e9e
fix: many fixes
jkbpvsc Nov 9, 2023
f6fa0f1
fix: more fixes
jkbpvsc Nov 9, 2023
55987b2
fix: liq math
jkbpvsc Nov 9, 2023
266cc20
fix: log account value
jkbpvsc Nov 9, 2023
45c42f6
fix: don't die on error
jkbpvsc Nov 9, 2023
4b492c4
fix: pre flight check
jkbpvsc Nov 9, 2023
3aef80f
fix: native balance amount
jkbpvsc Nov 10, 2023
7cf39db
fix: sol handling
jkbpvsc Nov 10, 2023
f90ec31
fix: wait for tx confirmation
jkbpvsc Nov 11, 2023
e22518b
fix: liq increase fee
jkbpvsc Nov 11, 2023
c198e9a
fix: higher swap fee
jkbpvsc Nov 11, 2023
8d8259d
fix: add sorted account mode, withdraw amount conversion
jkbpvsc Nov 12, 2023
26439ea
Merge remote-tracking branch 'origin/main' into jjjj/jup-api-liquidator
jkbpvsc Nov 21, 2023
5120b79
fix: multi bank fix
jkbpvsc Nov 21, 2023
d6388a2
fix: start and keep alive script
jkbpvsc Dec 4, 2023
babe4c7
fix: readme
jkbpvsc Dec 6, 2023
60d080a
Merge remote-tracking branch 'origin/main' into jjjj/jup-api-liquidator
jkbpvsc Dec 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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