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

Fix: Filter out nonexistent pair #188

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading