Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Commit 825416e

Browse files
authored
feat: add new_call_contract_syscall to Cairo1Helpers class (#1023)
1 parent a24ea25 commit 825416e

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

crates/contracts/src/cairo1_helpers.cairo

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::starknet::{EthAddress, secp256_trait::Signature};
1+
use core::starknet::{EthAddress, secp256_trait::Signature, ContractAddress};
22

33
#[starknet::interface]
44
pub trait IPrecompiles<T> {
@@ -88,7 +88,7 @@ pub trait IHelpers<T> {
8888
/// * The recovered Ethereum address.
8989
fn recover_eth_address(self: @T, msg_hash: u256, signature: Signature) -> (bool, EthAddress);
9090

91-
/// Performs signature verification in the secp256r1 ellipitic curve.
91+
/// Performs signature verification in the secp256r1 elliptic curve.
9292
///
9393
/// # Arguments
9494
///
@@ -103,20 +103,41 @@ pub trait IHelpers<T> {
103103
fn verify_signature_secp256r1(
104104
self: @T, msg_hash: u256, r: u256, s: u256, x: u256, y: u256
105105
) -> bool;
106+
107+
108+
/// Calls a contract using the new Cairo call_contract_syscall.
109+
///
110+
/// Meant to be used from Cairo Zero classes that want to be able to manage the return value of
111+
/// the call.
112+
/// Only applicable from Starknet v0.13.4 and onwwards.
113+
///
114+
/// # Arguments
115+
///
116+
/// * `to` - The address of the contract to call.
117+
/// * `selector` - The selector of the function to call.
118+
/// * `calldata` - The calldata to pass to the function.
119+
///
120+
/// # Returns
121+
/// A tuple containing:
122+
/// * A boolean indicating whether the call was successful.
123+
/// * The output of the call.
124+
fn new_call_contract_syscall(
125+
ref self: T, to: ContractAddress, selector: felt252, calldata: Span<felt252>
126+
) -> (bool, Span<felt252>);
106127
}
107128

108129

109130
pub mod embeddable_impls {
110131
use core::keccak::{cairo_keccak, keccak_u256s_be_inputs};
111132
use core::num::traits::Zero;
112-
use core::starknet::EthAddress;
113133
use core::starknet::eth_signature::{verify_eth_signature};
114134
use core::starknet::secp256_trait::{
115135
Signature, recover_public_key, Secp256PointTrait, is_valid_signature
116136
};
117137
use core::starknet::secp256_trait::{Secp256Trait};
118138
use core::starknet::secp256k1::Secp256k1Point;
119139
use core::starknet::secp256r1::{Secp256r1Point};
140+
use core::starknet::{ContractAddress, EthAddress};
120141
use core::traits::Into;
121142
use core::{starknet, starknet::SyscallResultTrait};
122143
use evm::errors::EVMError;
@@ -149,7 +170,7 @@ pub mod embeddable_impls {
149170
}
150171

151172
#[starknet::embeddable]
152-
pub impl Helpers<TContractState> of super::IHelpers<TContractState> {
173+
pub impl Helpers<TContractState, +Drop<TContractState>> of super::IHelpers<TContractState> {
153174
fn get_block_hash(self: @TContractState, block_number: u64) -> felt252 {
154175
starknet::syscalls::get_block_hash_syscall(block_number).unwrap_syscall()
155176
}
@@ -217,6 +238,22 @@ pub mod embeddable_impls {
217238

218239
return is_valid_signature(msg_hash, r, s, public_key);
219240
}
241+
242+
fn new_call_contract_syscall(
243+
ref self: TContractState,
244+
to: ContractAddress,
245+
selector: felt252,
246+
calldata: Span<felt252>
247+
) -> (bool, Span<felt252>) {
248+
// Note: until Starknet v0.13.4, the transaction will fail if the call reverted.
249+
let result = starknet::syscalls::call_contract_syscall(to, selector, calldata);
250+
match result {
251+
Result::Ok(output) => { return (true, output); },
252+
// This will need to be manually tested and enabled once contract calls can be
253+
// handled.
254+
Result::Err(error) => { return panic(error); }
255+
}
256+
}
220257
}
221258
}
222259

0 commit comments

Comments
 (0)