Skip to content

Commit 87602c3

Browse files
committed
feat: update ethers to master and use cfg files instead of cli params
1 parent c36cb5a commit 87602c3

File tree

4 files changed

+63
-68
lines changed

4 files changed

+63
-68
lines changed

Cargo.lock

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ edition = "2018"
66

77
[dependencies]
88
anyhow = "1.0.32"
9-
ethers = { git = "https://github.com/gakonst/ethers-rs", branch = "nonce-manager" }
10-
# ethers = { git = "https://github.com/gakonst/ethers-rs" }
9+
ethers = { git = "https://github.com/gakonst/ethers-rs" }
1110
serde_json = "1.0.57"
1211
tokio = { version = "0.2.22", features = ["macros"] }
1312

@@ -19,4 +18,4 @@ tracing-subscriber = "0.2.10"
1918
serde = "1.0.114"
2019

2120
[build-dependencies]
22-
ethers = { git = "https://github.com/gakonst/ethers-rs", branch = "nonce-manager", features = ["abigen"] }
21+
ethers = { git = "https://github.com/gakonst/ethers-rs", features = ["abigen"] }

src/keeper.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,34 @@ impl<P: JsonRpcClient> Keeper<P> {
7272
let watcher = self.client.clone();
7373
let mut on_block = watcher.watch_blocks().await?.stream();
7474

75+
let mut file: Option<std::fs::File> = None;
7576
while on_block.next().await.is_some() {
76-
let file = std::fs::OpenOptions::new()
77-
.read(true)
78-
.write(true)
79-
.create(true)
80-
.open(&fname)
81-
.unwrap();
82-
8377
let block_number = self.client.get_block_number().await?;
78+
79+
if block_number % 10 == 0.into() {
80+
// on each new block we open a new file handler to dump our state.
81+
// we should just have a database connection instead here...
82+
file = Some(
83+
std::fs::OpenOptions::new()
84+
.read(true)
85+
.write(true)
86+
.create(true)
87+
.open(&fname)
88+
.unwrap(),
89+
);
90+
}
91+
8492
let span = debug_span!("eloop", block = %block_number);
8593
let _enter = span.enter();
94+
95+
// run the logic for this block
8696
self.on_block(block_number).await?;
8797

8898
// update our last block
8999
self.last_block = block_number;
90100

91-
// Log once every 20 blocks (~300s)
92-
if block_number % 10 == 0.into() {
101+
// Log once every 10 blocks
102+
if let Some(file) = file.take() {
93103
self.log(file);
94104
}
95105
}

src/main.rs

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ethers::prelude::*;
22
use yield_liquidator::keeper::Keeper;
33

44
use gumdrop::Options;
5+
use serde::Deserialize;
56
use std::{convert::TryFrom, path::PathBuf, sync::Arc, time::Duration};
67
use tracing::info;
78
use tracing_subscriber::{filter::EnvFilter, fmt::Subscriber};
@@ -11,46 +12,17 @@ use tracing_subscriber::{filter::EnvFilter, fmt::Subscriber};
1112
struct Opts {
1213
help: bool,
1314

14-
#[options(
15-
help = "the Yield controller's address",
16-
default = "Cf699af73C25aC785E8Da72F4fA7872c86D43C15"
17-
)]
18-
controller: Address,
19-
20-
#[options(
21-
help = "the Yield liquidation's address",
22-
default = "b85F3d294edD2B76128cf918800BB21081f59223"
23-
)]
24-
liquidations: Address,
25-
26-
#[options(
27-
help = "the DAI/WETH Uniswap V2 pair",
28-
default = "6025b901C88e5739Cb4112fcf3F27E0264c5BdDe"
29-
)]
30-
uniswap: Address,
31-
32-
#[options(
33-
help = "the address of your flashloan contract",
34-
default = "FE35d86cb5b6c0273fAC070Cc40aFeA77574cEF0"
35-
)]
36-
flashloan: Address,
37-
38-
#[options(
39-
help = "the address of the Multicall contract (optional for any of the deployed testnets)"
40-
)]
41-
multicall: Option<Address>,
15+
#[options(help = "path to json file with the contract addresses")]
16+
config: PathBuf,
4217

4318
#[options(
44-
help = "the Ethereum node HTTP endpoint",
19+
help = "the Ethereum node endpoint (HTTP or WS)",
4520
default = "http://localhost:8545"
4621
)]
4722
url: String,
4823

49-
#[options(
50-
help = "your private key",
51-
default = "8b6e036da61e5ac4874a770d7258583cd1b373798e740deaff4015fea80294b0"
52-
)]
53-
private_key: String,
24+
#[options(help = "path to your private key")]
25+
private_key: PathBuf,
5426

5527
#[options(help = "polling interval (ms)", default = "1000")]
5628
interval: u64,
@@ -62,6 +34,15 @@ struct Opts {
6234
min_profit: U256,
6335
}
6436

37+
#[derive(Deserialize)]
38+
struct Config {
39+
controller: Address,
40+
liquidations: Address,
41+
uniswap: Address,
42+
flashloan: Address,
43+
multicall: Option<Address>,
44+
}
45+
6546
#[tokio::main]
6647
async fn main() -> anyhow::Result<()> {
6748
Subscriber::builder()
@@ -83,19 +64,25 @@ async fn main() -> anyhow::Result<()> {
8364
}
8465

8566
async fn run<P: JsonRpcClient>(opts: Opts, provider: Provider<P>) -> anyhow::Result<()> {
86-
let wallet: Wallet = opts.private_key.parse()?;
67+
info!("Starting Yield Liquidator.");
68+
let wallet: Wallet = std::fs::read_to_string(opts.private_key)?.parse()?;
8769
let client = wallet
8870
.connect(provider)
89-
.interval(Duration::from_millis(opts.interval));
71+
.interval(Duration::from_millis(opts.interval))
72+
// enable the nonce-manager so that we can send multiple transactions in a row
73+
// without waiting for them to be included in the mempool
74+
.with_nonce_manager();
9075
let client = Arc::new(client);
91-
info!("Starting Yield Liquidator.");
92-
info!("Node: {}", opts.url);
9376
info!("Profits will be sent to {:?}", client.address());
94-
info!("Controller: {:?}", opts.controller);
95-
info!("Liquidations: {:?}", opts.liquidations);
96-
info!("Uniswap: {:?}", opts.uniswap);
97-
info!("Multicall: {:?}", opts.multicall);
98-
info!("FlashLiquidator {:?}", opts.flashloan);
77+
78+
info!("Node: {}", opts.url);
79+
80+
let cfg: Config = serde_json::from_reader(std::fs::File::open(opts.config)?)?;
81+
info!("Controller: {:?}", cfg.controller);
82+
info!("Liquidations: {:?}", cfg.liquidations);
83+
info!("Uniswap: {:?}", cfg.uniswap);
84+
info!("Multicall: {:?}", cfg.multicall);
85+
info!("FlashLiquidator {:?}", cfg.flashloan);
9986
info!("Persistent data will be stored at: {:?}", opts.file);
10087

10188
let file = std::fs::OpenOptions::new()
@@ -108,11 +95,11 @@ async fn run<P: JsonRpcClient>(opts: Opts, provider: Provider<P>) -> anyhow::Res
10895

10996
let mut keeper = Keeper::new(
11097
client,
111-
opts.controller,
112-
opts.liquidations,
113-
opts.uniswap,
114-
opts.flashloan,
115-
opts.multicall,
98+
cfg.controller,
99+
cfg.liquidations,
100+
cfg.uniswap,
101+
cfg.flashloan,
102+
cfg.multicall,
116103
opts.min_profit,
117104
state,
118105
)

0 commit comments

Comments
 (0)