Skip to content

Commit

Permalink
feat: add compute_zksync_create2_address (#83)
Browse files Browse the repository at this point in the history
Bumped the version in Cargo.toml from 2.3.0 to 2.4.0 and updated the regex dependency. Added a new module to compute zkSync CREATE2 addresses and included tests. Updated the README to reflect the new version.
  • Loading branch information
shuhuiluo authored Oct 6, 2024
1 parent 84b773c commit ce6fcf6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-sdk-core"
version = "2.3.0"
version = "2.4.0"
edition = "2021"
authors = ["malik <[email protected]>", "Shuhui Luo <twitter.com/aureliano_law>"]
description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"
Expand All @@ -14,7 +14,7 @@ lazy_static = "1.5"
num-bigint = "0.4"
num-integer = "0.1"
num-traits = "0.2"
regex = { version = "1.10", optional = true }
regex = { version = "1.11", optional = true }
rustc-hash = "2.0"
thiserror = { version = "1.0", optional = true }

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Add this to your Cargo.toml

```
[dependencies]
uniswap-sdk-core = "1.0.0"
uniswap-sdk-core = "2.4.0"
```

And this to your code:
Expand Down
44 changes: 44 additions & 0 deletions src/utils/compute_zksync_create2_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use alloy_primitives::{keccak256, Address, Bytes, B256};

#[inline]
#[must_use]
pub fn compute_zksync_create2_address(
sender: Address,
bytecode_hash: B256,
salt: B256,
input: Option<Bytes>,
) -> Address {
let prefix = keccak256("zksyncCreate2");
let input_hash = keccak256(input.unwrap_or_default());
let mut bytes = [0; 160];
bytes[0..32].copy_from_slice(prefix.as_slice());
bytes[44..64].copy_from_slice(sender.as_slice());
bytes[64..96].copy_from_slice(salt.as_slice());
bytes[96..128].copy_from_slice(bytecode_hash.as_slice());
bytes[128..160].copy_from_slice(input_hash.as_slice());
Address::from_word(keccak256(bytes))
}

#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{address, b256};

#[test]
fn test_compute_zksync_create2_address() {
const USDCE: Address = address!("3355df6D4c9C3035724Fd0e3914dE96A5a83aaf4");
const WETH: Address = address!("5AEa5775959fBC2557Cc8789bC1bf90A239D9a91");
let mut bytes = [0; 96];
bytes[12..32].copy_from_slice(USDCE.as_slice());
bytes[44..64].copy_from_slice(WETH.as_slice());
bytes[92..96].copy_from_slice(3000_u32.to_be_bytes().as_slice());
let salt = keccak256(&bytes);
let result = compute_zksync_create2_address(
address!("8FdA5a7a8dCA67BBcDd10F02Fa0649A937215422"),
b256!("010013f177ea1fcbc4520f9a3ca7cd2d1d77959e05aa66484027cb38e712aeed"),
salt,
None,
);
assert_eq!(result, address!("ff577f0E828a878743Ecc5E2632cbf65ceCf17cF"));
}
}
4 changes: 2 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod compute_price_impact;

pub mod compute_zksync_create2_address;
pub mod sorted_insert;

pub mod sqrt;

#[cfg(feature = "validate_parse_address")]
pub mod validate_and_parse_address;

0 comments on commit ce6fcf6

Please sign in to comment.