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

add cairo 1 contracts #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ cairo-lang
venv
# Hint Dependency symlink
starkware

.DS_Store
12 changes: 10 additions & 2 deletions scripts/setup-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ then
exit 1
fi

if ! command -v starknet-compile > /dev/null
then
echo "please install scarb https://docs.swmansion.com/scarb/download.html#install-via-installation-script"
echo "curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh"
exit 1
fi

echo -e "\ninitializing cairo-lang($CAIRO_VER)...\n"
git submodule update --init

Expand All @@ -40,7 +47,7 @@ cp tests/dependencies/initializable.cairo cairo-lang/src/starkware/starknet/std_


# compile cairo programs
echo -e "compmiling cairo programs...\n"
echo -e "compiling cairo programs...\n"
mkdir -p build/programs
cairo-format -i tests/programs/*
cairo-compile tests/programs/bad_output.cairo --output build/programs/bad_output.json
Expand All @@ -51,7 +58,7 @@ cairo-compile tests/programs/load_deprecated_class.cairo --output build/programs
cairo-compile cairo-lang/src/starkware/starknet/core/os/os.cairo --output build/os_debug.json --cairo_path cairo-lang/src

# compile starknet contract
echo -e "compiling starknet contrarcts...\n"
echo -e "compiling starknet contracts...\n"
mkdir -p build/contracts
ln -s cairo-lang/src/starkware starkware
starknet-compile-deprecated --no_debug_info tests/contracts/token_for_testing.cairo --output build/contracts/token_for_testing.json --cairo_path cairo-lang/src --account_contract
Expand All @@ -60,3 +67,4 @@ starknet-compile-deprecated --no_debug_info tests/contracts/dummy_token.cairo --
starknet-compile-deprecated --no_debug_info tests/contracts/delegate_proxy.cairo --output build/contracts/delegate_proxy.json --cairo_path cairo-lang/src
starknet-compile-deprecated --no_debug_info tests/contracts/test_contract.cairo --output build/contracts/test_contract.json --cairo_path cairo-lang/src
starknet-compile-deprecated --no_debug_info tests/contracts/test_contract2.cairo --output build/contracts/test_contract2.json --cairo_path cairo-lang/src
cd ./tests/contracts/cairo1/ && scarb build && mv target/dev/* ../../../
1 change: 1 addition & 0 deletions tests/contracts/cairo1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
12 changes: 12 additions & 0 deletions tests/contracts/cairo1/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "cairo1"
version = "0.1.0"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet = "2.2.0"

[[target.starknet-contract]]
sierra = true
casm = true
19 changes: 19 additions & 0 deletions tests/contracts/cairo1/src/counter.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[starknet::contract]
mod HelloStarknet {
#[storage]
struct Storage {
balance: felt252,
}

// Increases the balance by the given amount.
#[external(v0)]
fn increase_balance(ref self: ContractState, amount: felt252) {
self.balance.write(self.balance.read() + amount);
}

// Returns the current balance.
#[external(v0)]
fn get_balance(self: @ContractState) -> felt252 {
self.balance.read()
}
}
4 changes: 4 additions & 0 deletions tests/contracts/cairo1/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod no_validate_account;
mod counter;
use no_validate_account::Account;
use counter::HelloStarknet;
80 changes: 80 additions & 0 deletions tests/contracts/cairo1/src/no_validate_account.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#[starknet::contract]
mod Account {
use array::{ArrayTrait, SpanTrait};
use box::BoxTrait;
use ecdsa::check_ecdsa_signature;
use option::OptionTrait;
use starknet::account::Call;
use starknet::{ContractAddress, call_contract_syscall};
use zeroable::Zeroable;
use array::ArraySerde;

#[storage]
struct Storage {
public_key: felt252
}

#[constructor]
fn constructor(ref self: ContractState, public_key_: felt252) {
self.public_key.write(public_key_);
}

trait StorageTrait {
fn validate_transaction(self: @ContractState) -> felt252;
}
impl StorageImpl of StorageTrait {
fn validate_transaction(self: @ContractState) -> felt252 {
starknet::VALIDATED
}
}


#[external(v0)]
fn __validate_deploy__(
self: @ContractState,
class_hash: felt252,
contract_address_salt: felt252,
public_key_: felt252
) -> felt252 {
self.validate_transaction()
}

#[external(v0)]
impl AccountContractImpl of starknet::account::AccountContract<ContractState> {
fn __validate_declare__(self: @ContractState, class_hash: felt252) -> felt252 {
self.validate_transaction()
}

fn __validate__(ref self: ContractState, calls: Array<Call>) -> felt252 {
self.validate_transaction()
}

fn __execute__(ref self: ContractState, mut calls: Array<Call>) -> Array<Span<felt252>> {
// Validate caller.
assert(starknet::get_caller_address().is_zero(), 'INVALID_CALLER');

// Check the tx version here, since version 0 transaction skip the __validate__ function.
let tx_info = starknet::get_tx_info().unbox();
assert(tx_info.version != 0, 'INVALID_TX_VERSION');

let mut result = ArrayTrait::new();
loop {
match calls.pop_front() {
Option::Some(call) => {
let mut res = call_contract_syscall(
address: call.to,
entry_point_selector: call.selector,
calldata: call.calldata.span()
)
.unwrap();
result.append(res);
},
Option::None(()) => {
break; // Can't break result; because of 'variable was previously moved'
},
};
};
result
}
}
}
Loading