Skip to content

Commit

Permalink
add coverage support (#81)
Browse files Browse the repository at this point in the history
* bump snforge to `0.30.0`

* add coverage support

* only run coverage CI on main push

* rename CI
  • Loading branch information
0xChqrles authored Sep 19, 2024
1 parent 9579a1f commit 2dc0abf
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 33 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [push, pull_request]
permissions: read-all

jobs:
check:
fmt:
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -14,7 +14,7 @@ jobs:
- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.0"
scarb-version: "2.8.2"

- name: Check cairo format
run: scarb fmt --check
Expand All @@ -24,22 +24,22 @@ jobs:
run: scarb build
working-directory: contracts

tests:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@main

- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.0"
scarb-version: "2.8.2"

- name: Set up SNForge
uses: foundry-rs/setup-snfoundry@v3
with:
starknet-foundry-version: "0.27.0"
starknet-foundry-version: "0.30.0"

- name: Run cairo tests
run: scarb test
- name: Run tests and generate report
run: snforge test
working-directory: contracts
40 changes: 40 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Contracts Coverage

on:
# Trigger the workflow on push,
# but only for the main branch
push:
branches:
- main

# manual trigger
workflow_dispatch:

permissions: read-all

jobs:
coverage:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@main

- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.2"

- name: Set up SNForge
uses: foundry-rs/setup-snfoundry@v3
with:
starknet-foundry-version: "0.30.0"

- name: Run tests and generate report
run: snforge test --coverage
working-directory: contracts

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.lcov
token: ${{ secrets.CODECOV_TOKEN }}
7 changes: 6 additions & 1 deletion contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
target
.snfoundry_cache
.snfoundry_cache

# snforge --coverage
.snfoundry_versioned_programs
snfoundry_trace
coverage
14 changes: 12 additions & 2 deletions contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,20 @@ name = "openzeppelin_utils"
version = "0.16.0"
source = "git+https://github.com/openzeppelin/cairo-contracts?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb"

[[package]]
name = "snforge_scarb_plugin"
version = "0.2.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:2e4ce3ebe3f49548bd26908391b5d78537a765d827df0d96c32aeb88941d0d67"

[[package]]
name = "snforge_std"
version = "0.27.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.27.0#2d99b7c00678ef0363881ee0273550c44a9263de"
version = "0.30.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:2f3c4846881813ac0f5d1460981249c9f5e2a6831e752beedf9b70975495b4ec"
dependencies = [
"snforge_scarb_plugin",
]

[[package]]
name = "zkramp"
Expand Down
11 changes: 6 additions & 5 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
[package]
name = "zkramp"
version = "0.1.0"
edition = "2023_11"
cairo-version = "2.8.0"
scarb-version = "2.8.0"
edition = "2024_07"
cairo-version = "2.8.2"
scarb-version = "2.8.2"

[dependencies]
starknet = "2.8.0"
starknet = "2.8.2"
openzeppelin = { git = "https://github.com/openzeppelin/cairo-contracts", tag = "v0.16.0" }

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.27.0" }
assert_macros = "2.8.2"
snforge_std = "0.30.0"

[tool.fmt]
max-line-length = 120
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/components/escrow/escrow.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[starknet::component]
pub mod EscrowComponent {
use openzeppelin_token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use starknet::storage::Map;
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::{ContractAddress, get_contract_address};
use zkramp::components::escrow::interface;

Expand All @@ -10,9 +10,9 @@ pub mod EscrowComponent {
//

#[storage]
struct Storage {
pub struct Storage {
// (owner, token) -> amount
deposits: Map::<(ContractAddress, ContractAddress), u256>,
pub deposits: Map::<(ContractAddress, ContractAddress), u256>,
}

//
Expand Down
5 changes: 1 addition & 4 deletions contracts/src/components/escrow/escrow_test.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use core::starknet::get_contract_address;

use core::starknet::storage::StorageMapReadAccess;
use openzeppelin::presets::interfaces::ERC20UpgradeableABIDispatcherTrait;
use snforge_std::start_cheat_caller_address;
use zkramp::components::escrow::escrow::EscrowComponent::EscrowImpl;
use zkramp::components::escrow::escrow_mock::{TestingStateDefault, ComponentState};
use zkramp::tests::constants;
use zkramp::tests::utils;


//
// Externals
//
Expand All @@ -32,7 +31,6 @@ fn test_lock() {
assert_eq!(token_dispatcher.allowance(constants::SPENDER(), constants::RECIPIENT()), 0);
}


#[test]
fn test_lock_unlock() {
let token_dispatcher = utils::setup_erc20(recipient: constants::OWNER());
Expand Down Expand Up @@ -62,7 +60,6 @@ fn test_lock_unlock() {
assert_eq!(escrow.deposits.read((constants::SPENDER(), token_dispatcher.contract_address)), 0);
}


#[test]
#[should_panic(expected: 'Insufficient deposit balance')]
fn test_lock_unlock_greater_than_balance() {
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/components/registry/registry.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[starknet::component]
pub mod RegistryComponent {
use core::num::traits::Zero;
use starknet::storage::Map;
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::{ContractAddress, get_caller_address};
use zkramp::components::registry::interface::OffchainId;
use zkramp::components::registry::interface;
Expand All @@ -11,8 +11,8 @@ pub mod RegistryComponent {
//

#[storage]
struct Storage {
Registry_registrations: Map::<(ContractAddress, OffchainId), bool>,
pub struct Storage {
pub Registry_registrations: Map::<(ContractAddress, OffchainId), bool>,
}

//
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/components/registry/registry_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn test_is_registered() {

registry.register(offchain_id: constants::REVOLUT_ID());

assert_eq!(registry.is_registered(constants::CALLER(), constants::REVOLUT_ID()), true);
assert!(registry.is_registered(constants::CALLER(), constants::REVOLUT_ID()));
}

#[test]
Expand Down
5 changes: 3 additions & 2 deletions contracts/src/contracts/ramps/revolut/revolut.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[starknet::contract]
pub mod RevolutRamp {
use core::num::traits::Zero;
use core::starknet::storage::{StoragePointerReadAccess};
use openzeppelin::access::ownable::OwnableComponent;
use starknet::storage::Map;
use starknet::storage::{
Map, StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess, StorageMapWriteAccess
};
use starknet::{ContractAddress, get_caller_address, get_block_timestamp};
use zkramp::components::escrow::escrow::EscrowComponent;
use zkramp::components::registry::interface::{OffchainId, IRegistry};
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/contracts/ramps/revolut/revolut_test.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use core::starknet::{ContractAddress, get_caller_address};
use openzeppelin::utils::serde::SerializedAppend;
use snforge_std::{declare, ContractClassTrait, start_cheat_caller_address, test_address};
use snforge_std::{declare, start_cheat_caller_address, test_address, DeclareResultTrait, ContractClassTrait};
use zkramp::contracts::ramps::revolut::interface::{ZKRampABIDispatcher, ZKRampABIDispatcherTrait, LiquidityKey};
use zkramp::tests::constants;

fn deploy_revolut_ramp() -> ZKRampABIDispatcher {
let contract = declare("RevolutRamp").unwrap();
let contract = declare("RevolutRamp").unwrap().contract_class();

let mut calldata = array![];

Expand Down
4 changes: 2 additions & 2 deletions contracts/src/tests/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use openzeppelin::presets::interfaces::{ERC20UpgradeableABIDispatcher};

use openzeppelin::utils::serde::SerializedAppend;

use snforge_std::{declare, ContractClassTrait};
use snforge_std::{declare, DeclareResultTrait, ContractClassTrait};
use starknet::ContractAddress;

use super::constants;
Expand All @@ -17,7 +17,7 @@ pub fn setup_erc20(recipient: ContractAddress) -> ERC20UpgradeableABIDispatcher
calldata.append_serde(recipient);
calldata.append_serde(recipient);

let contract = declare("ERC20Upgradeable").unwrap();
let contract = declare("ERC20Upgradeable").unwrap().contract_class();
let (contract_address, _) = contract.deploy(@calldata).unwrap();

ERC20UpgradeableABIDispatcher { contract_address: contract_address }
Expand Down

0 comments on commit 2dc0abf

Please sign in to comment.