Skip to content

Commit

Permalink
Merge pull request #188 from fuzzland/0xAWM-patch-3
Browse files Browse the repository at this point in the history
Fix: Filter out nonexistent pair
  • Loading branch information
0xAWM authored Sep 5, 2023
2 parents a41c950 + 2d301a0 commit 7180ee7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/evm/onchain/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,21 @@ impl OnChainConfig {
}

impl OnChainConfig {
fn get_pair(&self, token: &str, network: &str, is_pegged: bool) -> Vec<PairData> {
fn get_pair(&self, token: &str, network: &str, block: &str, is_pegged: bool) -> Vec<PairData> {
let params = format!("[\"{}\", false]", block);
let resp = self
._request("eth_getBlockByNumber".to_string(), params)
.unwrap();
let timestamp = resp
.as_object()
.unwrap()
.get("timestamp")
.unwrap()
.as_str()
.unwrap();
let timestamp_ms =
u64::from_str_radix(timestamp.trim_start_matches("0x"), 16).unwrap() * 1000;

// API calls are limited to 300 requests per minute
let url = format!("https://api.dexscreener.com/latest/dex/tokens/{token}");
let resp: Value = reqwest::blocking::get(url).unwrap().json().unwrap();
Expand All @@ -817,11 +831,15 @@ impl OnChainConfig {
// only v2
if !pair["labels"]
.as_array()
.unwrap()
.unwrap_or(&vec![])
.contains(&Value::String("v2".to_string()))
{
continue;
}
// only existed in the target block
if pair["pairCreatedAt"].as_u64().unwrap_or_default() > timestamp_ms {
continue;
}
let base_token = pair["baseToken"].as_object().unwrap()["address"]
.as_str()
.unwrap()
Expand All @@ -842,7 +860,7 @@ impl OnChainConfig {
next: another_token,
decimals0: 18,
decimals1: 18,
src_exact: "uniswapv2".to_string(),
src_exact: pair["dexId"].as_str().unwrap().to_owned() + "v2",
rate: 0,
token: token.to_string(),
initial_reserves_0: "".to_string(),
Expand Down Expand Up @@ -931,6 +949,7 @@ impl OnChainConfig {
&self,
token: &str,
network: &str,
block: &str,
hop: u32,
known: &mut HashSet<String>,
) -> HashMap<String, Vec<PairData>> {
Expand All @@ -941,15 +960,18 @@ impl OnChainConfig {
}

let mut hops: HashMap<String, Vec<PairData>> = HashMap::new();
hops.insert(token.to_string(), self.get_pair(token, network, false));
hops.insert(
token.to_string(),
self.get_pair(token, network, block, false),
);

let pegged_tokens = self.get_pegged_token(network);

for i in hops.clone().get(token).unwrap() {
if pegged_tokens.values().any(|v| v == &i.next) || known.contains(&i.next) {
continue;
}
let next_hops = self.get_all_hops(&i.next, network, hop + 1, known);
let next_hops = self.get_all_hops(&i.next, network, block, hop + 1, known);
hops.extend(next_hops);
}

Expand All @@ -972,7 +994,11 @@ impl OnChainConfig {
src_exact: "".to_string(),
};
}
let mut peg_info = self.get_pair(token, network, true).get(0).unwrap().clone();
let mut peg_info = self
.get_pair(token, network, block, true)
.get(0)
.unwrap()
.clone();

self.add_reserve_info(&mut peg_info, block);
let p0 = i128::from_str_radix(&peg_info.initial_reserves_0, 16).unwrap();
Expand Down Expand Up @@ -1059,7 +1085,7 @@ impl OnChainConfig {
}

let mut known: HashSet<String> = HashSet::new();
let hops = self.get_all_hops(token, network, 0, &mut known);
let hops = self.get_all_hops(token, network, block, 0, &mut known);

let mut routes: Vec<Vec<PairData>> = vec![];

Expand Down
1 change: 1 addition & 0 deletions src/evm/uniswap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl UniswapProvider {
pub fn from_str(s: &str) -> Option<Self> {
match s {
"pancakeswap" => Some(Self::PancakeSwap),
"pancakeswapv2" => Some(Self::PancakeSwap),
"sushiswap" => Some(Self::SushiSwap),
"uniswapv2" => Some(Self::UniswapV2),
"uniswapv3" => Some(Self::UniswapV3),
Expand Down

0 comments on commit 7180ee7

Please sign in to comment.