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

Feature/implement cario coverage & increase coverage #6

Merged
merged 26 commits into from
Sep 18, 2024
Merged
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
16 changes: 16 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ jobs:
- uses: asdf-vm/actions/install@v3
- run: scarb test
working-directory: contracts

coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: asdf-vm/actions/install@v3
- run: curl -L https://raw.githubusercontent.com/software-mansion/cairo-coverage/main/scripts/install.sh | sh

- name: Run tests and generate report
run: cd contracts/ && snforge test --coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.lcov
token: ${{ secrets.CODECOV_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
contracts/target
contracts/.snfoundry_cache/

# snforge --coverage
contracts/.snfoundry_versioned_programs
contracts/snfoundry_trace
contracts/coverage

# `mac`
**/.DS_Store
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Kudos

[![Check Workflow Status](https://github.com/keep-starknet-strange/kudos/actions/workflows/check.yml/badge.svg)](https://github.com/keep-starknet-strange/kudos/actions/workflows/check.yml)
[![codecov](https://codecov.io/gh/keep-starknet-strange/kudos/blob/branch/main/graph/badge.svg)](https://codecov.io/gh/keep-starknet-strange/kudos)

An internal application to delegate `kudos` on Starknet by using Google OAuth.

Expand Down Expand Up @@ -32,3 +33,11 @@ Starknet contracts to handle:
```bash
cd contracts && scarb test
```
***Code Coverage***

Check out the [installation](https://github.com/software-mansion/cairo-coverage#installation) section of `cairo-coverage` for the detailed installation instructions.

```bash
cd contracts && snforge test --coverage
```

5 changes: 5 additions & 0 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ starknet = "2.8.2"
assert_macros = "2.8.2"
snforge_std = "0.30.0"

[profile.dev.cairo]
unstable-add-statements-functions-debug-info = true
unstable-add-statements-code-locations-debug-info = true
inlining-strategy= "avoid"

[[target.starknet-contract]]
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ fn test_register_credential() {
assert!(registry.get_credential_address(CREDENTIAL_HASH) == CALLER());
}

#[test]
fn test_register_credential_address_zero() {
let mut registry: ComponentState = Default::default();
registry._register_credential(CREDENTIAL_HASH, ZERO_ADDRESS());
registry._register_user(ZERO_ADDRESS(), CREDENTIAL_HASH);

assert!(registry.get_credential(ZERO_ADDRESS()) == CREDENTIAL_HASH);
assert!(registry.is_registered(ZERO_ADDRESS()) == false);
assert!(registry.get_credential_address(CREDENTIAL_HASH) == ZERO_ADDRESS());
}

#[test]
#[should_panic(expected: 'User already registered cred')]
fn test_double_register_credential() {
Expand Down
20 changes: 1 addition & 19 deletions contracts/src/kudos.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub mod Kudos {
use kudos::credential_registry::{ICredentialRegistry, CredentialRegistryComponent};
use kudos::oz16::IERC20ReadOnly;
use kudos::oz16::erc20::{ERC20Component, ERC20HooksEmptyImpl, ERC20Component::InternalTrait};
use kudos::oz16::ownable::OwnableComponent;
use kudos::utils::constants::REGISTRATION_AMOUNT;
use starknet::storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map
Expand All @@ -17,7 +16,6 @@ pub mod Kudos {
event: CredentialRegistryEvent
);
component!(path: ERC20Component, storage: erc20, event: ERC20Event);
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

#[abi(embed_v0)]
impl CredentialRegistryImpl =
Expand All @@ -26,18 +24,12 @@ pub mod Kudos {
impl ERC20Impl = ERC20Component::ERC20Impl<ContractState>;
impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;

#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
credential_registry: CredentialRegistryComponent::Storage,
#[substorage(v0)]
erc20: ERC20Component::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
total_given: Map<ContractAddress, u256>,
total_received: Map<ContractAddress, u256>
}
Expand All @@ -50,8 +42,6 @@ pub mod Kudos {
#[flat]
ERC20Event: ERC20Component::Event,
KudosGiven: KudosGiven,
#[flat]
OwnableEvent: OwnableComponent::Event,
}

#[derive(Drop, starknet::Event)]
Expand All @@ -70,11 +60,8 @@ pub mod Kudos {
}

#[constructor]
fn constructor(
ref self: ContractState, name: ByteArray, symbol: ByteArray, owner: ContractAddress
) {
fn constructor(ref self: ContractState, name: ByteArray, symbol: ByteArray) {
self.erc20.initializer(name, symbol);
self.ownable.initializer(owner);
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -135,10 +122,5 @@ pub mod Kudos {
fn balance_of(self: @ContractState, account: ContractAddress) -> u256 {
self.erc20.balance_of(account)
}
fn allowance(
self: @ContractState, owner: ContractAddress, spender: ContractAddress
) -> u256 {
self.erc20.allowance(owner, spender)
}
}
}
1 change: 0 additions & 1 deletion contracts/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub mod oz16 {
pub mod erc20;

mod interfaces;
pub mod ownable;
pub use interfaces::{
IERC20, IERC20Dispatcher, IERC20DispatcherTrait, IOwnable, IOwnableDispatcher,
IOwnableDispatcherTrait, IERC20ReadOnly
Expand Down
10 changes: 0 additions & 10 deletions contracts/src/oz16/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ pub mod ERC20Component {
self.ERC20_balances.read(account)
}

/// Returns the remaining number of tokens that `spender` is
/// allowed to spend on behalf of `owner` through `transfer_from`.
/// This is zero by default.
/// This value changes when `approve` or `transfer_from` are called.
fn allowance(
self: @ComponentState<TContractState>, owner: ContractAddress, spender: ContractAddress
) -> u256 {
self.ERC20_allowances.read((owner, spender))
}

/// Moves `amount` tokens from the caller's token balance to `to`.
///
/// Requirements:
Expand Down
2 changes: 0 additions & 2 deletions contracts/src/oz16/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub trait IERC20<TState> {
fn decimals(self: @TState) -> u8;
fn total_supply(self: @TState) -> u256;
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
fn transfer_from(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
Expand All @@ -26,7 +25,6 @@ pub trait IERC20ReadOnly<TState> {
fn decimals(self: @TState) -> u8;
fn total_supply(self: @TState) -> u256;
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
}

#[starknet::interface]
Expand Down
154 changes: 0 additions & 154 deletions contracts/src/oz16/ownable.cairo

This file was deleted.

4 changes: 0 additions & 4 deletions contracts/src/utils/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ pub fn DUMMY() -> ContractAddress {
contract_address_const::<'DUMMY'>()
}

pub fn OWNER() -> ContractAddress {
contract_address_const::<'OWNER'>()
}

pub fn ZERO_ADDRESS() -> ContractAddress {
contract_address_const::<0>()
}
Expand Down
3 changes: 1 addition & 2 deletions contracts/tests/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use kudos::utils::constants::{
NAME, SYMBOL, OWNER, CALLER, RECEIVER, CREDENTIAL_HASH, CREDENTIAL_HASH_2, ONE
NAME, SYMBOL, CALLER, RECEIVER, CREDENTIAL_HASH, CREDENTIAL_HASH_2, ONE
};
use kudos::{IKudosDispatcher, IKudosDispatcherTrait};
use snforge_std::{declare, ContractClassTrait, DeclareResultTrait, start_cheat_caller_address};
Expand All @@ -25,7 +25,6 @@ pub fn setup() -> ContractAddress {
let mut calldata: Array<felt252> = array![];
calldata.append_serde(NAME());
calldata.append_serde(SYMBOL());
calldata.append_serde(OWNER());

declare_deploy("Kudos", calldata)
}
Expand Down