diff --git a/crates/core/src/contracts/account.rs b/crates/core/src/contracts/account.rs index 0b70ae714..d62dbfd4c 100644 --- a/crates/core/src/contracts/account.rs +++ b/crates/core/src/contracts/account.rs @@ -46,7 +46,13 @@ pub trait Account<'a, P: Provider + Send + Sync + 'a> { _ => Err(EthApiError::from(err)), })?; - Ok(vec_felt_to_bytes(bytecode)) + if bytecode.is_empty() { + return Ok(Bytes::default()); + } + + // bytecode_len is the first element of the returned array + // TODO: Remove Manual Decoding + Ok(vec_felt_to_bytes(bytecode[1..].to_vec())) } } diff --git a/crates/core/src/contracts/tests/account.rs b/crates/core/src/contracts/tests/account.rs new file mode 100644 index 000000000..69704de58 --- /dev/null +++ b/crates/core/src/contracts/tests/account.rs @@ -0,0 +1,27 @@ +use rstest::*; +use starknet::core::types::{BlockId, BlockTag}; + +use crate::client::api::KakarotStarknetApi; +use crate::contracts::account::{Account, KakarotAccount}; +use crate::test_utils::deploy_helpers::{get_contract, get_contract_deployed_bytecode, KakarotTestEnvironmentContext}; +use crate::test_utils::fixtures::kakarot_test_env_ctx; + +#[rstest] +#[tokio::test(flavor = "multi_thread")] +async fn test_bytecode(kakarot_test_env_ctx: KakarotTestEnvironmentContext) { + // Given + let contract_name = "Counter"; + let counter_starknet_address = kakarot_test_env_ctx.evm_contract(contract_name).addresses.starknet_address; + let counter_contract = get_contract(contract_name); + let expected_bytecode = get_contract_deployed_bytecode(counter_contract); + + let starknet_block_id = BlockId::Tag(BlockTag::Latest); + let starknet_provider = kakarot_test_env_ctx.client().starknet_provider(); + let counter_contract_account = KakarotAccount::new(counter_starknet_address, starknet_provider.as_ref()); + + // When + let actual_bytecode = counter_contract_account.bytecode(&starknet_block_id).await.unwrap(); + + // Then + assert_eq!(expected_bytecode.to_vec(), actual_bytecode.to_vec()); +} diff --git a/crates/core/src/contracts/tests/erc20.rs b/crates/core/src/contracts/tests/erc20.rs new file mode 100644 index 000000000..6ba289e3b --- /dev/null +++ b/crates/core/src/contracts/tests/erc20.rs @@ -0,0 +1,26 @@ +use reth_primitives::U256; +use starknet::core::types::BlockId; +use starknet::providers::SequencerGatewayProvider; +use starknet_crypto::FieldElement; + +use crate::client::api::KakarotStarknetApi; +use crate::client::constants::{DUMMY_ARGENT_GAS_PRICE_ACCOUNT_ADDRESS, STARKNET_NATIVE_TOKEN}; +use crate::contracts::erc20::starknet_erc20::StarknetErc20; +use crate::mock::mock_starknet::init_testnet_client; + +#[tokio::test] +async fn test_balance_of() { + // Given + let client = init_testnet_client(); + let starknet_native_token_address = FieldElement::from_hex_be(STARKNET_NATIVE_TOKEN).unwrap(); + let provider = client.starknet_provider(); + let eth = StarknetErc20::::new(&provider, starknet_native_token_address); + + let random_block = BlockId::Number(838054); + + // When + let balance = eth.balance_of(&DUMMY_ARGENT_GAS_PRICE_ACCOUNT_ADDRESS, &random_block).await.unwrap(); + + // Then + assert_eq!(U256::from(983627765290549u64), balance); +} diff --git a/crates/core/src/contracts/tests/mod.rs b/crates/core/src/contracts/tests/mod.rs index 6ba289e3b..370ce69bd 100644 --- a/crates/core/src/contracts/tests/mod.rs +++ b/crates/core/src/contracts/tests/mod.rs @@ -1,26 +1,2 @@ -use reth_primitives::U256; -use starknet::core::types::BlockId; -use starknet::providers::SequencerGatewayProvider; -use starknet_crypto::FieldElement; - -use crate::client::api::KakarotStarknetApi; -use crate::client::constants::{DUMMY_ARGENT_GAS_PRICE_ACCOUNT_ADDRESS, STARKNET_NATIVE_TOKEN}; -use crate::contracts::erc20::starknet_erc20::StarknetErc20; -use crate::mock::mock_starknet::init_testnet_client; - -#[tokio::test] -async fn test_balance_of() { - // Given - let client = init_testnet_client(); - let starknet_native_token_address = FieldElement::from_hex_be(STARKNET_NATIVE_TOKEN).unwrap(); - let provider = client.starknet_provider(); - let eth = StarknetErc20::::new(&provider, starknet_native_token_address); - - let random_block = BlockId::Number(838054); - - // When - let balance = eth.balance_of(&DUMMY_ARGENT_GAS_PRICE_ACCOUNT_ADDRESS, &random_block).await.unwrap(); - - // Then - assert_eq!(U256::from(983627765290549u64), balance); -} +mod account; +mod erc20;