Skip to content

Commit

Permalink
dev: remove bytecode length from return (#546)
Browse files Browse the repository at this point in the history
* dev: rename to er20 test

* dev: return bytecode only and remove its length

* dev: use evm_contract

* chore: whitespace and naming

* Update crates/core/src/contracts/account.rs

Co-authored-by: Elias Tazartes <66871571+Eikix@users.noreply.github.com>

---------

Co-authored-by: Elias Tazartes <66871571+Eikix@users.noreply.github.com>
  • Loading branch information
ftupas and Eikix authored Sep 11, 2023
1 parent 51803e6 commit 4f17d52
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
8 changes: 7 additions & 1 deletion crates/core/src/contracts/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}

Expand Down
27 changes: 27 additions & 0 deletions crates/core/src/contracts/tests/account.rs
Original file line number Diff line number Diff line change
@@ -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());
}
26 changes: 26 additions & 0 deletions crates/core/src/contracts/tests/erc20.rs
Original file line number Diff line number Diff line change
@@ -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::<SequencerGatewayProvider>::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);
}
28 changes: 2 additions & 26 deletions crates/core/src/contracts/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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::<SequencerGatewayProvider>::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;

0 comments on commit 4f17d52

Please sign in to comment.