Skip to content

Commit

Permalink
refactor: use ethers
Browse files Browse the repository at this point in the history
  • Loading branch information
ftupas committed Sep 7, 2023
1 parent 51e0328 commit 5825f25
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 44 deletions.
22 changes: 2 additions & 20 deletions crates/core/src/test_utils/deploy_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use ethers_solc::artifacts::CompactContractBytecode;
use foundry_config::utils::{find_project_root_path, load_config};
use katana_core::db::serde::state::SerializableState;
use katana_core::db::Db;
use reth_primitives::{
sign_message, Address, Bytes, Transaction, TransactionKind, TransactionSigned, TxEip1559, H256, U256,
};
use reth_primitives::{sign_message, Address, Bytes, Transaction, TransactionKind, TransactionSigned, TxEip1559, H256};
use serde::{Deserialize, Serialize};
use starknet::accounts::{Account, Call, ConnectedAccount, SingleOwnerAccount};
use starknet::contract::ContractFactory;
Expand Down Expand Up @@ -179,23 +177,7 @@ pub fn to_kakarot_transaction(nonce: u64, to: TransactionKind, value: u128, inpu
///
/// This function creates a transaction which calls a contract function with provided arguments.
/// The transaction is signed using the provided EOA secret.
pub fn create_raw_ethereum_tx(
selector: [u8; 4],
eoa_secret_key: H256,
to: Address,
args: Vec<U256>,
nonce: u64,
) -> Bytes {
// Start with the function selector
// Append each argument
let mut data: Vec<u8> = selector.to_vec();

for arg in args {
// Ethereum uses big-endian encoding
let arg_bytes: [u8; 32] = arg.to_be_bytes();
data.extend_from_slice(&arg_bytes);
}

pub fn create_raw_ethereum_tx(eoa_secret_key: H256, to: Address, data: Vec<u8>, nonce: u64) -> Bytes {
let transaction = to_kakarot_transaction(nonce, TransactionKind::Call(to), Default::default(), data.into());
let signature =
sign_message(eoa_secret_key, transaction.signature_hash()).expect("Signing of ethereum transaction failed.");
Expand Down
19 changes: 8 additions & 11 deletions crates/core/src/test_utils/execution_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
use ethers::abi::Token;
use ethers::signers::{LocalWallet, Signer};
use reth_primitives::{Address, BlockId, H256, U256};
use reth_primitives::{Address, BlockId, H256};

use super::deploy_helpers::{create_eth_transfer_tx, create_raw_ethereum_tx, KakarotTestEnvironmentContext};
use crate::client::api::KakarotEthApi;

pub async fn execute_tx(env: &KakarotTestEnvironmentContext, contract: &str, selector: &str, args: Vec<U256>) -> H256 {
pub async fn execute_tx(env: &KakarotTestEnvironmentContext, contract: &str, selector: &str, args: Vec<Token>) -> H256 {
let (client, kakarot, contract, contract_eth_address) = env.resources_with_contract(contract);

// When
let nonce = client
.nonce(kakarot.eoa_addresses.eth_address, BlockId::Number(reth_primitives::BlockNumberOrTag::Latest))
.await
.unwrap();
let selector = contract.abi.function(selector).unwrap().short_signature();

let tx = create_raw_ethereum_tx(
selector,
kakarot.eoa_private_key,
contract_eth_address,
args,
nonce.try_into().unwrap(),
);

// Encode input, otherwise throw error
let data = contract.abi.function(selector).unwrap().encode_input(&args).expect("Encoding error");

let tx = create_raw_ethereum_tx(kakarot.eoa_private_key, contract_eth_address, data, nonce.try_into().unwrap());

client.send_transaction(tx).await.unwrap()
}
Expand Down
36 changes: 23 additions & 13 deletions crates/core/tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod tests {
use std::str::FromStr;

use ctor::ctor;
use ethers::abi::Token;
use ethers::signers::{LocalWallet, Signer};
use kakarot_rpc_core::client::api::{KakarotEthApi, KakarotStarknetApi};
use kakarot_rpc_core::client::constants::{DEPLOY_FEE, TX_ORIGIN_ZERO};
Expand Down Expand Up @@ -110,9 +111,10 @@ mod tests {
let (client, kakarot, _, erc20_eth_address) = kakarot_test_env_ctx.resources_with_contract("ERC20");

// When
let to = U256::try_from_be_slice(&kakarot.eoa_addresses.eth_address.to_fixed_bytes()[..]).unwrap();
let to = Address::from_slice(&kakarot.eoa_addresses.eth_address.to_fixed_bytes()[..]);
let amount = U256::from(10_000);
execute_tx(&kakarot_test_env_ctx, "ERC20", "mint", vec![to, amount]).await;
execute_tx(&kakarot_test_env_ctx, "ERC20", "mint", vec![Token::Address(to.into()), Token::Uint(amount.into())])
.await;

// Then
let balances = client.token_balances(kakarot.eoa_addresses.eth_address, vec![erc20_eth_address]).await.unwrap();
Expand All @@ -136,13 +138,25 @@ mod tests {
let (client, kakarot, _, erc20_eth_address) = kakarot_test_env_ctx.resources_with_contract("ERC20");

// When
let to = U256::try_from_be_slice(&kakarot.eoa_addresses.eth_address.to_fixed_bytes()[..]).unwrap();
let to = Address::from_slice(&kakarot.eoa_addresses.eth_address.to_fixed_bytes()[..]);
let amount = U256::from(10_000);
let mint_tx_hash = execute_tx(&kakarot_test_env_ctx, "ERC20", "mint", vec![to, amount]).await;
let mint_tx_hash = execute_tx(
&kakarot_test_env_ctx,
"ERC20",
"mint",
vec![Token::Address(to.into()), Token::Uint(amount.into())],
)
.await;

let to = U256::try_from_be_slice(ACCOUNT_ADDRESS_EVM.as_bytes()).unwrap();
let to = Address::from_slice(ACCOUNT_ADDRESS_EVM.as_bytes());
let amount = U256::from(10_000);
let transfer_tx_hash = execute_tx(&kakarot_test_env_ctx, "ERC20", "transfer", vec![to, amount]).await;
let transfer_tx_hash = execute_tx(
&kakarot_test_env_ctx,
"ERC20",
"transfer",
vec![Token::Address(to.into()), Token::Uint(amount.into())],
)
.await;

let filter = Filter {
block_option: FilterBlockOption::Range {
Expand Down Expand Up @@ -335,13 +349,9 @@ mod tests {
// currently there is a bug when there is no return data from the init code execution
// see https://github.com/kkrt-labs/kakarot/issues/726
// for now, we test with bytecode that has return data
let hash = execute_tx(
&kakarot_test_env_ctx,
"PlainOpcodes",
"create",
vec![U256::from_str("0x604260005260206000F3").unwrap(), count],
)
.await;
let args =
vec![Token::Bytes(Bytes::from_str("0x604260005260206000F3").unwrap().to_vec()), Token::Uint(count.into())];
let hash = execute_tx(&kakarot_test_env_ctx, "PlainOpcodes", "create", args).await;
client.transaction_receipt(hash).await.expect("create transaction failed");

let nonce_final =
Expand Down

0 comments on commit 5825f25

Please sign in to comment.