Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Added tests for starknet_simulate_transactions and bump starknet.rs version to maintain compatibility with Deoxys #29

Merged
merged 10 commits into from
Jan 12, 2024
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions unit_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ serde = "1.0.194"
serde_json = "1.0.110"
tokio = { version = "1", features = ["full"] }
url = "2.5.0"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "a35ce22", default-features = false }
starknet-core = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "a35ce22", default-features = false }
starknet-providers = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "a35ce22", default-features = false }
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false }
starknet-core = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false }
starknet-providers = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false }
starknet-accounts = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false }
env_logger = "0.10.1"

[dev-dependencies]
Expand Down
6 changes: 6 additions & 0 deletions unit_tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,11 @@ pub const ETHEREUM_ADDRESS: &str = "";
pub const INVALID_ETHEREUM_ADDRESS: &str = "";
pub const SELECTOR_NAME: &str = "";

///
/// Value to be used as a signer for simulate_transaction tests.
///
pub const SIGNER_PRIVATE: &str = "";
pub const ARGENT_CONTRACT_ADDRESS: &str = "";

pub const ERR_DEOXYS: &str = "Error waiting for response from Deoxys client";
pub const ERR_PATHFINDER: &str = "Error waiting for response from Pathfinder client";
104 changes: 104 additions & 0 deletions unit_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ use std::{fs::File, io::Read};

use constants::*;
use serde::Deserialize;
use starknet::signers::{LocalWallet, SigningKey};
Tbelleng marked this conversation as resolved.
Show resolved Hide resolved
use starknet_accounts::{Account, Call, ConnectedAccount, Execution, SingleOwnerAccount};
use starknet_core::chain_id;
use starknet_core::{
types::{BroadcastedInvokeTransaction, BroadcastedTransaction, FieldElement},
utils::get_selector_from_name,
};
use starknet_providers::{jsonrpc::HttpTransport, JsonRpcClient};

pub mod constants;
pub mod fixtures;
Expand Down Expand Up @@ -70,3 +74,103 @@ impl TransactionFactory for BadTransactionFactory {
})
}
}

pub struct MaxFeeTransactionFactory;

impl TransactionFactory for MaxFeeTransactionFactory {
fn build(_: Option<FieldElement>) -> BroadcastedTransaction {
BroadcastedTransaction::Invoke(BroadcastedInvokeTransaction {
max_fee: FieldElement::from_hex_be("0x100000000000000000000000000000000").unwrap(),
signature: vec![],
nonce: FieldElement::ZERO,
sender_address: FieldElement::from_hex_be(ACCOUNT_CONTRACT).unwrap(),
calldata: vec![
FieldElement::from_hex_be(TEST_CONTRACT_ADDRESS).unwrap(),
get_selector_from_name("sqrt").unwrap(),
FieldElement::from_hex_be("1").unwrap(),
FieldElement::from(81u8),
],
is_query: false,
})
}
}

type RpcAccount<'a> = SingleOwnerAccount<&'a JsonRpcClient<HttpTransport>, LocalWallet>;

pub fn build_single_owner_account<'a>(
rpc: &'a JsonRpcClient<HttpTransport>,
private_key: &str,
account_address: &str,
is_legacy: bool,
) -> RpcAccount<'a> {
let signer = LocalWallet::from(SigningKey::from_secret_scalar(
FieldElement::from_hex_be(private_key).unwrap(),
));
let account_address =
FieldElement::from_hex_be(account_address).expect("Invalid Contract Address");
let execution_encoding = if is_legacy {
starknet_accounts::ExecutionEncoding::Legacy
} else {
starknet_accounts::ExecutionEncoding::New
};
SingleOwnerAccount::new(
rpc,
signer,
account_address,
chain_id::TESTNET,
execution_encoding,
)
}

#[allow(async_fn_in_trait)]
pub trait PrepareInvoke {
async fn prepare_invoke(
&self,
calls: Vec<Call>,
nonce: FieldElement,
max_fee: FieldElement,
query_only: bool,
) -> BroadcastedInvokeTransaction;
}

impl PrepareInvoke for SingleOwnerAccount<&JsonRpcClient<HttpTransport>, LocalWallet> {
async fn prepare_invoke(
&self,
calls: Vec<Call>,
nonce: FieldElement,
max_fee: FieldElement,
query_only: bool,
) -> BroadcastedInvokeTransaction
where
Self: Account + ConnectedAccount,
{
let prepared_execution = Execution::new(calls, self)
.nonce(nonce)
.max_fee(max_fee)
.prepared()
.unwrap();
prepared_execution
.get_invoke_request(query_only)
.await
.unwrap()
}
}

pub fn generate_call(
contract_address: &str,
function_name: &str,
calldata_values: Vec<u8>,
) -> Call {
let to = FieldElement::from_hex_be(contract_address).unwrap();
let selector = get_selector_from_name(function_name).unwrap();
let calldata = calldata_values
.into_iter()
.map(FieldElement::from)
.collect();

Call {
to,
selector,
calldata,
}
}
Loading
Loading