Skip to content

Commit

Permalink
feat: various lib improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Aug 26, 2024
1 parent a1d9f12 commit c43f3e0
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use contract_printing::IHelloStarknetDispatcherTrait;

fn deploy_contract(name: ByteArray) -> ContractAddress {
let contract = declare(name).unwrap();
let (address, _) = contract.deploy(@ArrayTrait::new()).unwrap();
let (address, _) = contract.deploy([].span()).unwrap();
address
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait IHelloStarknet<TContractState> {
fn declare_and_interact() {
assert(1 == 1, 'simple check');
let contract = declare("HelloStarknet").unwrap();
let (contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap();
let (contract_address, _) = contract.deploy([].span()).unwrap();
let dispatcher = IHelloStarknetDispatcher { contract_address };

dispatcher.get_balance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn deploy_erc20(
) -> ContractAddress {
let contract = declare("ERC20").unwrap();

let mut constructor_calldata: Array::<felt252> = array![name, symbol, decimals.into()];
let mut constructor_calldata: Array<felt252> = array![name, symbol, decimals.into()];

let mut initial_supply_serialized = array![];
initial_supply.serialize(ref initial_supply_serialized);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use simple_package::hello_starknet::IHelloStarknetDispatcherTrait;
#[test]
fn call_and_invoke() {
let contract = declare("HelloStarknet").unwrap();
let constructor_calldata = @ArrayTrait::new();
let constructor_calldata = [].span();
let (contract_address, _) = contract.deploy(constructor_calldata).unwrap();
let dispatcher = IHelloStarknetDispatcher { contract_address };

Expand Down
14 changes: 7 additions & 7 deletions sncast_std/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub impl DisplayContractAddress of Display<ContractAddress> {

#[derive(Drop, Clone, Debug, Serde)]
pub struct CallResult {
pub data: Array::<felt252>,
pub data: Array<felt252>,
}

impl DisplayCallResult of Display<CallResult> {
Expand All @@ -114,7 +114,7 @@ impl DisplayCallResult of Display<CallResult> {
}

pub fn call(
contract_address: ContractAddress, function_selector: felt252, calldata: Array::<felt252>
contract_address: ContractAddress, function_selector: felt252, calldata: Array<felt252>
) -> Result<CallResult, ScriptCommandError> {
let contract_address_felt: felt252 = contract_address.into();
let mut inputs = array![contract_address_felt, function_selector];
Expand All @@ -135,7 +135,7 @@ pub fn call(
result_data
}

#[derive(Drop, Clone, Debug, Serde)]
#[derive(Drop, Copy, Debug, Serde)]
pub struct DeclareResult {
pub class_hash: ClassHash,
pub transaction_hash: felt252,
Expand Down Expand Up @@ -175,7 +175,7 @@ pub fn declare(
result_data
}

#[derive(Drop, Clone, Debug, Serde)]
#[derive(Drop, Copy, Debug, Serde)]
pub struct DeployResult {
pub contract_address: ContractAddress,
pub transaction_hash: felt252,
Expand All @@ -194,7 +194,7 @@ impl DisplayDeployResult of Display<DeployResult> {

pub fn deploy(
class_hash: ClassHash,
constructor_calldata: Array::<felt252>,
constructor_calldata: Array<felt252>,
salt: Option<felt252>,
unique: bool,
max_fee: Option<felt252>,
Expand Down Expand Up @@ -232,7 +232,7 @@ pub fn deploy(
result_data
}

#[derive(Drop, Clone, Debug, Serde)]
#[derive(Drop, Copy, Debug, Serde)]
pub struct InvokeResult {
pub transaction_hash: felt252,
}
Expand All @@ -246,7 +246,7 @@ impl DisplayInvokeResult of Display<InvokeResult> {
pub fn invoke(
contract_address: ContractAddress,
entry_point_selector: felt252,
calldata: Array::<felt252>,
calldata: Array<felt252>,
max_fee: Option<felt252>,
nonce: Option<felt252>
) -> Result<InvokeResult, ScriptCommandError> {
Expand Down
23 changes: 14 additions & 9 deletions snforge_std/src/cheatcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ mod tx_info;
mod fork;
mod storage;

#[derive(Drop, Serde, PartialEq, Clone, Debug, Display)]
#[derive(Drop, Serde, PartialEq, Clone, Debug)]
enum CheatTarget {
All: (),
One: ContractAddress,
Multiple: Array<ContractAddress>
}

#[derive(Drop, Serde, PartialEq, Clone, Debug, Display)]
#[derive(Drop, Serde, PartialEq, Copy, Debug)]
enum CheatSpan {
Indefinite: (),
TargetCalls: usize,
Expand Down Expand Up @@ -150,14 +150,16 @@ fn stop_elect(target: CheatTarget) {
}


/// Mocks contract call to a `function_selector` of a contract at the given address, for `n_times` first calls that are made
/// to the contract.
/// Mocks contract call to a `function_selector` of a contract at the given address, for `n_times`
/// first calls that are made to the contract.
/// A call to function `function_selector` will return data provided in `ret_data` argument.
/// An address with no contract can be mocked as well.
/// An entrypoint that is not present on the deployed contract is also possible to mock.
/// Note that the function is not meant for mocking internal calls - it works only for contract entry points.
/// Note that the function is not meant for mocking internal calls - it works only for contract
/// entry points.
/// - `contract_address` - target contract address
/// - `function_selector` - hashed name of the target function (can be obtained with `selector!` macro)
/// - `function_selector` - hashed name of the target function (can be obtained with `selector!`
/// macro)
/// - `ret_data` - data to return by the function `function_selector`
/// - `n_times` - number of calls to mock the function for
fn mock_call<T, impl TSerde: core::serde::Serde<T>, impl TDestruct: Destruct<T>>(
Expand All @@ -182,7 +184,8 @@ fn mock_call<T, impl TSerde: core::serde::Serde<T>, impl TDestruct: Destruct<T>>
/// Mocks contract call to a function of a contract at the given address, indefinitely.
/// See `mock_call` for comprehensive definition of how it can be used.
/// - `contract_address` - targeted contracts' address
/// - `function_selector` - hashed name of the target function (can be obtained with `selector!` macro)
/// - `function_selector` - hashed name of the target function (can be obtained with `selector!`
/// macro)
/// - `ret_data` - data to be returned by the function
fn start_mock_call<T, impl TSerde: core::serde::Serde<T>, impl TDestruct: Destruct<T>>(
contract_address: ContractAddress, function_selector: felt252, ret_data: T
Expand All @@ -200,9 +203,11 @@ fn start_mock_call<T, impl TSerde: core::serde::Serde<T>, impl TDestruct: Destru
handle_cheatcode(cheatcode::<'mock_call'>(inputs.span()));
}

/// Cancels the `mock_call` / `start_mock_call` for the function with given name and contract address.
/// Cancels the `mock_call` / `start_mock_call` for the function with given name and contract
/// address.
/// - `contract_address` - targeted contracts' address
/// - `function_selector` - hashed name of the target function (can be obtained with `selector!` macro)
/// - `function_selector` - hashed name of the target function (can be obtained with `selector!`
/// macro)
fn stop_mock_call(contract_address: ContractAddress, function_selector: felt252) {
let contract_address_felt: felt252 = contract_address.into();
handle_cheatcode(
Expand Down
33 changes: 16 additions & 17 deletions snforge_std/src/cheatcodes/contract_class.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ trait ContractClassTrait {
/// `constructor_calldata` - serialized calldata for the deploy constructor
/// Returns the precalculated `ContractAddress`
fn precalculate_address(
self: @ContractClass, constructor_calldata: @Array::<felt252>
self: @ContractClass, constructor_calldata: Span<felt252>
) -> ContractAddress;

/// Deploys a contract
/// `self` - an instance of the struct `ContractClass` which is obtained by calling `declare`
/// `constructor_calldata` - serialized calldata for the constructor
/// Returns the address the contract was deployed at and serialized constructor return data, or panic data if it failed
/// Returns the address the contract was deployed at and serialized constructor return data, or
/// panic data if it failed
fn deploy(
self: @ContractClass, constructor_calldata: @Array::<felt252>
self: @ContractClass, constructor_calldata: Span<felt252>
) -> SyscallResult<(ContractAddress, Span<felt252>)>;

/// Deploys a contract at a given address
/// `self` - an instance of the struct `ContractClass` which is obtained by calling `declare`
/// `constructor_calldata` - serialized calldata for the constructor
/// `contract_address` - address the contract should be deployed at
/// Returns the address the contract was deployed at and serialized constructor return data, or panic data if it failed
/// Returns the address the contract was deployed at and serialized constructor return data, or
/// panic data if it failed
fn deploy_at(
self: @ContractClass,
constructor_calldata: @Array::<felt252>,
contract_address: ContractAddress
self: @ContractClass, constructor_calldata: Span<felt252>, contract_address: ContractAddress
) -> SyscallResult<(ContractAddress, Span<felt252>)>;

/// Utility method for creating a new `ContractClass` instance
Expand All @@ -48,16 +48,16 @@ trait ContractClassTrait {

impl ContractClassImpl of ContractClassTrait {
fn precalculate_address(
self: @ContractClass, constructor_calldata: @Array::<felt252>
self: @ContractClass, constructor_calldata: Span<felt252>
) -> ContractAddress {
let mut inputs: Array::<felt252> = _prepare_calldata(self.class_hash, constructor_calldata);
let mut inputs: Array<felt252> = _prepare_calldata(self.class_hash, constructor_calldata);

let outputs = handle_cheatcode(cheatcode::<'precalculate_address'>(inputs.span()));
(*outputs[0]).try_into().unwrap()
}

fn deploy(
self: @ContractClass, constructor_calldata: @Array::<felt252>
self: @ContractClass, constructor_calldata: Span<felt252>
) -> SyscallResult<(ContractAddress, Span<felt252>)> {
let mut inputs = _prepare_calldata(self.class_hash, constructor_calldata);

Expand All @@ -76,9 +76,7 @@ impl ContractClassImpl of ContractClassTrait {
}

fn deploy_at(
self: @ContractClass,
constructor_calldata: @Array::<felt252>,
contract_address: ContractAddress
self: @ContractClass, constructor_calldata: Span<felt252>, contract_address: ContractAddress
) -> SyscallResult<(ContractAddress, Span<felt252>)> {
let mut inputs = _prepare_calldata(self.class_hash, constructor_calldata);
inputs.append(contract_address.into());
Expand All @@ -103,7 +101,8 @@ impl ContractClassImpl of ContractClassTrait {
}

/// Declares a contract
/// `contract` - name of a contract as Cairo string. It is a name of the contract (part after mod keyword) e.g. "HelloStarknet"
/// `contract` - name of a contract as Cairo string. It is a name of the contract (part after mod
/// keyword) e.g. "HelloStarknet"
/// Returns the `ContractClass` which was declared or panic data if declaration failed
fn declare(contract: ByteArray) -> Result<ContractClass, Array<felt252>> {
let mut span = handle_cheatcode(
Expand Down Expand Up @@ -138,10 +137,10 @@ fn get_class_hash(contract_address: ContractAddress) -> ClassHash {
}

fn _prepare_calldata(
class_hash: @ClassHash, constructor_calldata: @Array::<felt252>
) -> Array::<felt252> {
class_hash: @ClassHash, constructor_calldata: Span<felt252>
) -> Array<felt252> {
let class_hash: felt252 = class_hash.clone().into();
let mut inputs: Array::<felt252> = array![class_hash];
let mut inputs: Array<felt252> = array![class_hash];
constructor_calldata.serialize(ref inputs);
inputs
}
16 changes: 8 additions & 8 deletions snforge_std/src/cheatcodes/events.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum SpyOn {
Multiple: Array<ContractAddress>
}

fn spy_events(spy_on: SpyOn) -> EventSpy {
pub fn spy_events(spy_on: SpyOn) -> EventSpy {
let mut inputs = array![];
spy_on.serialize(ref inputs);
let output = handle_cheatcode(cheatcode::<'spy_events'>(inputs.span()));
Expand All @@ -24,8 +24,8 @@ fn event_name_hash(name: felt252) -> felt252 {
*output[0]
}

#[derive(Drop, Clone, Serde)]
struct Event {
#[derive(Drop, Clone, Serde, Debug)]
pub struct Event {
keys: Array<felt252>,
data: Array<felt252>
}
Expand Down Expand Up @@ -57,15 +57,15 @@ impl EventFetcherImpl of EventFetcher {
}
}

trait EventAssertions<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>> {
fn assert_emitted(ref self: EventSpy, events: @Array<(ContractAddress, T)>);
fn assert_not_emitted(ref self: EventSpy, events: @Array<(ContractAddress, T)>);
pub trait EventAssertions<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>> {
fn assert_emitted(ref self: EventSpy, events: Span<(ContractAddress, T)>);
fn assert_not_emitted(ref self: EventSpy, events: Span<(ContractAddress, T)>);
}

impl EventAssertionsImpl<
T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>
> of EventAssertions<T> {
fn assert_emitted(ref self: EventSpy, events: @Array<(ContractAddress, T)>) {
fn assert_emitted(ref self: EventSpy, events: Span<(ContractAddress, T)>) {
self.fetch_events();

let mut i = 0;
Expand All @@ -86,7 +86,7 @@ impl EventAssertionsImpl<
};
}

fn assert_not_emitted(ref self: EventSpy, events: @Array<(ContractAddress, T)>) {
fn assert_not_emitted(ref self: EventSpy, events: Span<(ContractAddress, T)>) {
self.fetch_events();

let mut i = 0;
Expand Down
6 changes: 3 additions & 3 deletions snforge_std/src/cheatcodes/l1_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use core::serde::Serde;
use starknet::{ContractAddress, testing::cheatcode, SyscallResult};
use super::super::_cheatcode::handle_cheatcode;

#[derive(Drop, Clone)]
#[derive(Drop, Copy)]
struct L1Handler {
contract_address: ContractAddress,
function_selector: felt252,
from_address: felt252,
payload: Span::<felt252>,
payload: Span<felt252>,
}

trait L1HandlerTrait {
Expand All @@ -24,7 +24,7 @@ impl L1HandlerImpl of L1HandlerTrait {
}

fn execute(self: L1Handler) -> SyscallResult<()> {
let mut inputs: Array::<felt252> = array![
let mut inputs: Array<felt252> = array![
self.contract_address.into(), self.function_selector, self.from_address,
];
self.payload.serialize(ref inputs);
Expand Down
2 changes: 1 addition & 1 deletion starknet_forge_template/tests/test_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {{ PROJECT_NAME }}::IHelloStarknetDispatcherTrait;

fn deploy_contract(name: ByteArray) -> ContractAddress {
let contract = declare(name).unwrap();
let (contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap();
let (contract_address, _) = contract.deploy([].span()).unwrap();
contract_address
}

Expand Down

0 comments on commit c43f3e0

Please sign in to comment.