Skip to content

Commit

Permalink
Backward compatible address prefix for ETH network (#176)
Browse files Browse the repository at this point in the history
* Backward compatible address prefix for ETH network

* Fix ETH address

* Fix fmt
  • Loading branch information
karim-en authored Jan 8, 2025
1 parent 796772c commit d5d365a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions near/omni-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ impl OmniAddress {
format!("sol-{hashed_address}")
}
}
OmniAddress::Eth(address) => {
if self.is_zero() {
"eth".to_string()
} else {
address.to_string()[2..].to_string()
}
}
_ => self.encode('-', true),
}
}
Expand Down
19 changes: 18 additions & 1 deletion near/omni-types/src/tests/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ fn test_stringify() {
}

#[test]
fn test_get_token_prefix() {
fn test_get_native_token_prefix() {
for chain_kind in [
ChainKind::Near,
ChainKind::Sol,
Expand All @@ -439,3 +439,20 @@ fn test_get_token_prefix() {
);
}
}

#[test]
fn test_get_evm_token_prefix() {
let address = "0x23ddd3e3692d1861ed57ede224608875809e127f";
let eth_address: OmniAddress = format!("eth:{address}").parse().unwrap();
let prefix = eth_address.get_token_prefix();
assert_eq!(prefix, "23ddd3e3692d1861ed57ede224608875809e127f");

for chain_kind in [ChainKind::Base, ChainKind::Arb] {
let chain_kind_prefix: String = chain_kind.as_ref().to_lowercase();
let chain_address: OmniAddress = format!("{chain_kind_prefix}:{address}").parse().unwrap();
assert_eq!(
chain_address.get_token_prefix(),
format!("{chain_kind_prefix}-{address}"),
);
}
}

0 comments on commit d5d365a

Please sign in to comment.