Skip to content

Commit dd80b3f

Browse files
authored
Use OZ 0.8.1 implementation of STRK and ETH ERC20 token contracts (#750)
1 parent ff3c030 commit dd80b3f

File tree

7 files changed

+27
-56
lines changed

7 files changed

+27
-56
lines changed

crates/starknet-devnet-core/contracts/system_artifacts/ERC20_Mintable_OZ_0.20.0.sierra

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/starknet-devnet-core/contracts/system_artifacts/ERC20_Mintable_OZ_0.8.1.sierra

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

crates/starknet-devnet-core/contracts/system_artifacts/compilation_info.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/starknet-devnet-core/src/constants.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ pub const CAIRO_1_ACCOUNT_CONTRACT_SIERRA_HASH: &str =
2626

2727
pub const CAIRO_1_ERC20_CONTRACT: &str = include_str!(concat!(
2828
env!("CARGO_MANIFEST_DIR"),
29-
"/contracts/system_artifacts/ERC20_Mintable_OZ_0.20.0.sierra"
29+
"/contracts/system_artifacts/ERC20_Mintable_OZ_0.8.1.sierra"
3030
));
3131

32-
/// Unlike in previous Devnet versions, now using the actual hash of the predeployed artifact
32+
/// Hardcoded to match the hash of the unchanged OZ ERC20.cairo even despite the changes: commented
33+
/// attributes in struct Approval (owner, spender), the addition of mintability.
3334
pub const CAIRO_1_ERC20_CONTRACT_CLASS_HASH: Felt =
34-
Felt::from_hex_unchecked("0x011374319a6e07b4f2738fa3bfa8cf2181bfb0dbb4d800215baa87b83a57877e");
35+
Felt::from_hex_unchecked("0x046ded64ae2dead6448e247234bab192a9c483644395b66f2155f2614e5804b0");
3536

3637
/// only used in tests; if artifact needed in production, add a new constant that uses include_str!
3738
pub const CAIRO_0_ERC20_CONTRACT_PATH: &str =

crates/starknet-devnet-core/src/starknet/predeployed.rs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use starknet_rs_core::types::Felt;
33
use starknet_rs_core::utils::cairo_short_string_to_felt;
44
use starknet_types::contract_address::ContractAddress;
55
use starknet_types::felt::felt_from_prefixed_hex;
6-
use starknet_types_core::hash::Poseidon;
76

87
use crate::constants::{
98
CHARGEABLE_ACCOUNT_ADDRESS, UDC_CONTRACT, UDC_CONTRACT_ADDRESS, UDC_CONTRACT_CLASS_HASH,
@@ -23,40 +22,6 @@ pub(crate) fn create_erc20_at_address_extended(
2322
Ok(erc20_fee_contract)
2423
}
2524

26-
fn store_short_string_as_byte_array(
27-
state: &mut StarknetState,
28-
contract_address: ContractAddress,
29-
storage_var_name: &str,
30-
short_str: &str,
31-
) -> DevnetResult<()> {
32-
let storage_var_address = get_storage_var_address(storage_var_name, &[])?.try_into()?;
33-
34-
let felt_value =
35-
cairo_short_string_to_felt(short_str).map_err(|_| Error::UnexpectedInternalError {
36-
msg: format!("Cannot create a ByteArray from {short_str}"),
37-
})?;
38-
39-
state.set_storage_at(
40-
contract_address.try_into()?,
41-
storage_var_address,
42-
short_str.len().into(),
43-
)?;
44-
45-
// That's how ByteArray is defined
46-
let capacity_arg = cairo_short_string_to_felt("ByteArray")
47-
.map_err(|e| Error::UnexpectedInternalError { msg: e.to_string() })?;
48-
49-
let chunk_index = Felt::ZERO;
50-
let mut hashable = [storage_var_address.into(), chunk_index, capacity_arg];
51-
Poseidon::hades_permutation(&mut hashable);
52-
let chunk_base = starknet_api::state::StorageKey::try_from(hashable[0])?;
53-
54-
// no offset in chunk because the word is expected to be short
55-
state.set_storage_at(contract_address.try_into()?, chunk_base, felt_value)?;
56-
57-
Ok(())
58-
}
59-
6025
/// Set initial values of ERC20 contract storage
6126
pub(crate) fn initialize_erc20_at_address(
6227
state: &mut StarknetState,
@@ -66,11 +31,17 @@ pub(crate) fn initialize_erc20_at_address(
6631
) -> DevnetResult<()> {
6732
let contract_address = ContractAddress::new(contract_address)?;
6833

69-
for (storage_var_name, string) in [("ERC20_name", erc20_name), ("ERC20_symbol", erc20_symbol)] {
70-
store_short_string_as_byte_array(state, contract_address, storage_var_name, string)?;
71-
}
72-
7334
for (storage_var_name, storage_value) in [
35+
(
36+
"ERC20_name",
37+
cairo_short_string_to_felt(erc20_name)
38+
.map_err(|err| Error::UnexpectedInternalError { msg: err.to_string() })?,
39+
),
40+
(
41+
"ERC20_symbol",
42+
cairo_short_string_to_felt(erc20_symbol)
43+
.map_err(|err| Error::UnexpectedInternalError { msg: err.to_string() })?,
44+
),
7445
("ERC20_decimals", 18.into()),
7546
// necessary to set - otherwise minting txs cannot be executed
7647
("Ownable_owner", felt_from_prefixed_hex(CHARGEABLE_ACCOUNT_ADDRESS)?),

tests/integration/common/constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub const CAIRO_1_ACCOUNT_CONTRACT_0_8_0_SIERRA_PATH: &str = concat!(
4141

4242
// system contracts
4343
pub const CAIRO_1_ERC20_CONTRACT_CLASS_HASH: Felt =
44-
Felt::from_hex_unchecked("0x011374319a6e07b4f2738fa3bfa8cf2181bfb0dbb4d800215baa87b83a57877e");
44+
Felt::from_hex_unchecked("0x046ded64ae2dead6448e247234bab192a9c483644395b66f2155f2614e5804b0");
4545
pub const ETH_ERC20_CONTRACT_ADDRESS: Felt =
4646
Felt::from_hex_unchecked("0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7");
4747
pub const STRK_ERC20_CONTRACT_ADDRESS: Felt =

tests/integration/general_integration_tests.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde_json::json;
2-
use starknet_rs_core::codec::Decode;
3-
use starknet_rs_core::types::{BlockId, BlockTag, ByteArray, Felt, FunctionCall};
4-
use starknet_rs_core::utils::{get_selector_from_name, get_storage_var_address};
2+
use starknet_rs_core::types::{BlockId, BlockTag, Felt, FunctionCall};
3+
use starknet_rs_core::utils::{
4+
get_selector_from_name, get_storage_var_address, parse_cairo_short_string,
5+
};
56
use starknet_rs_providers::Provider;
67

78
use crate::common::background_devnet::BackgroundDevnet;
@@ -105,14 +106,14 @@ async fn test_config() {
105106
assert_eq!(fetched_config, expected_config);
106107
}
107108

108-
/// Part of the responsibility of this test was transferred to test
109-
/// `predeployed_erc20_tokens_return_expected_values_from_property_getters`
110109
#[tokio::test]
111110
async fn predeployed_erc20_tokens_have_expected_storage() {
112111
let devnet = BackgroundDevnet::spawn().await.unwrap();
113112
for (token_address, var_name, expected_value) in [
114-
(ETH_ERC20_CONTRACT_ADDRESS, "ERC20_decimals", Felt::from(18)),
115-
(STRK_ERC20_CONTRACT_ADDRESS, "ERC20_decimals", Felt::from(18)),
113+
(ETH_ERC20_CONTRACT_ADDRESS, "ERC20_name", "Ether"),
114+
(ETH_ERC20_CONTRACT_ADDRESS, "ERC20_symbol", "ETH"),
115+
(STRK_ERC20_CONTRACT_ADDRESS, "ERC20_name", "StarkNet Token"),
116+
(STRK_ERC20_CONTRACT_ADDRESS, "ERC20_symbol", "STRK"),
116117
] {
117118
let actual_value = devnet
118119
.json_rpc_client
@@ -124,7 +125,7 @@ async fn predeployed_erc20_tokens_have_expected_storage() {
124125
.await
125126
.unwrap();
126127

127-
assert_eq!(actual_value, expected_value);
128+
assert_eq!(parse_cairo_short_string(&actual_value).unwrap().as_str(), expected_value);
128129
}
129130
}
130131

@@ -150,8 +151,7 @@ async fn predeployed_erc20_tokens_return_expected_values_from_property_getters()
150151
.await
151152
.unwrap();
152153

153-
// We expect the felt vector to contain the encoded ByteArray, thus we decode it
154-
let actual_string: String = ByteArray::decode(&actual_felts).unwrap().try_into().unwrap();
155-
assert_eq!(&actual_string, expected_value);
154+
assert_eq!(actual_felts.len(), 1);
155+
assert_eq!(parse_cairo_short_string(&actual_felts[0]).unwrap(), expected_value);
156156
}
157157
}

0 commit comments

Comments
 (0)