Skip to content

Commit

Permalink
handshake-manager: Add Katana token mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Oct 6, 2023
1 parent f3dad82 commit 8953831
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
7 changes: 7 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![deny(clippy::missing_docs_in_private_items)]
#![feature(generic_const_exprs)]

use num_bigint::BigUint;
use std::sync::{Arc, RwLock};
use tokio::sync::RwLock as TokioRwLock;

Expand All @@ -23,3 +24,9 @@ pub type AsyncShared<T> = Arc<TokioRwLock<T>>;
pub fn new_async_shared<T>(wrapped: T) -> AsyncShared<T> {
Arc::new(TokioRwLock::new(wrapped))
}

/// From a biguint, get a hex string with a 0x prefix
pub fn biguint_to_str_addr(x: &BigUint) -> String {
let addr = x.to_str_radix(16 /* radix */);
format!("0x{addr}")
}
35 changes: 34 additions & 1 deletion common/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::{
fmt::{self, Display},
};

use crate::biguint_to_str_addr;

use super::exchange::{Exchange, ALL_EXCHANGES};

/// A helper enum to describe the state of each ticker on each Exchange. Same means that the ERC-20
Expand Down Expand Up @@ -626,6 +628,25 @@ lazy_static! {
),
].into_iter().collect()
};

/// Remaps Katana ERC20 addresses to their Ethereum mainnet counterparts
/// so that a price can be determined
///
/// TODO: This will be removed when we implement our custom bridging solution
static ref KATANA_TOKEN_REMAP: HashMap<String, String> = {
vec![
// USDC
(
"0x8e3feea13add88dce4439bc1d02a662ab4c4cb6dca4639dccba89b4e594680".to_string(),
Token::from_ticker("USDC").get_addr().to_string(),
),
// ETH
(
"0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7".to_string(),
Token::from_ticker("WETH").get_addr().to_string(),
),
].into_iter().collect()
};
}

/// The core Token abstraction, used for unambiguous definition of an ERC-20 asset.
Expand Down Expand Up @@ -656,6 +677,13 @@ impl Token {
}
}

/// Given an ERC-20 address on Katana, return a new Token
pub fn from_katana_addr(addr: &str) -> Self {
Self {
addr: KATANA_TOKEN_REMAP.get(addr).unwrap().to_string(),
}
}

/// Given an ERC-20 contract address represented as a `BigUint`, returns a Token
pub fn from_addr_biguint(addr: &BigUint) -> Self {
Self {
Expand All @@ -665,7 +693,12 @@ impl Token {

/// Given an ERC-20 contract address on Starknet-Goerli (represented as a `BigUint`) returns a Token
pub fn from_starknet_goerli_addr_biguint(addr: &BigUint) -> Self {
Self::from_starknet_goerli_addr(&format!("0x{}", addr.to_str_radix(16 /* radix */)))
Self::from_starknet_goerli_addr(&biguint_to_str_addr(addr))
}

/// Given an ERC-20 contract address on Katana (represented as a `BigUint`) returns a Token
pub fn from_katana_addr_biguint(addr: &BigUint) -> Self {
Self::from_katana_addr(&biguint_to_str_addr(addr))
}

/// Given an ERC-20 ticker, returns a new Token.
Expand Down
4 changes: 4 additions & 0 deletions workers/handshake-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ impl HandshakeExecutor {
Token::from_addr_biguint(&order.base_mint),
Token::from_addr_biguint(&order.quote_mint),
),
ChainId::Katana => (
Token::from_katana_addr_biguint(&order.base_mint),
Token::from_katana_addr_biguint(&order.quote_mint),
),
_ => todo!("Implement price remapping for {:?}", self.chain_id),
}
}
Expand Down

0 comments on commit 8953831

Please sign in to comment.