-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- ERC20Token was used in instances that were not EVM-specific, which is bad organization. - We decided that creating a token generic trait was overkill - especially since the main issue was just the Bytes. Instead, we/I decided that a good and simple solution was to: 1. Rename ERC20Token -> Token 2. Make the address Bytes instead of Address 3. Create a helper method to transform the Bytes to Address when needed
- Loading branch information
TAMARA LIPOWSKI
committed
Dec 9, 2024
1 parent
21d208e
commit b023b7b
Showing
12 changed files
with
156 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::protocol::errors::SimulationError; | ||
use alloy_primitives::{bytes::Bytes, Address}; | ||
|
||
/// Safely converts a `Bytes` object to an `Address` object. | ||
/// | ||
/// Checks the length of the `Bytes` before attempting to convert, and returns a `SimulationError` | ||
/// if not 20 bytes long. | ||
pub fn bytes_to_erc20_address(address: &Bytes) -> Result<Address, SimulationError> { | ||
if address.len() == 20 { | ||
Ok(Address::from_slice(address)) | ||
} else { | ||
Err(SimulationError::InvalidInput( | ||
format!("Invalid ERC20 token address: {:?}", address), | ||
None, | ||
)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::evm::protocol::vm::utils::hexstring_to_vec; | ||
#[test] | ||
fn test_bytes_to_erc20_address_0x() { | ||
let address = | ||
Bytes::from(hexstring_to_vec("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap()); | ||
assert_eq!( | ||
bytes_to_erc20_address(&address).unwrap(), | ||
Address::from_slice(&hex::decode("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_bytes_to_erc20_address_invalid() { | ||
let address = Bytes::from(hex::decode("C02aaA").unwrap()); | ||
assert!(bytes_to_erc20_address(&address).is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod erc20; | ||
pub mod safe_math; | ||
pub mod u256_num; | ||
pub mod uniswap_v2; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.