Skip to content

Commit

Permalink
Merge pull request #80 from web3wagers/dev
Browse files Browse the repository at this point in the history
Merge dev into Main
  • Loading branch information
adrianvrj authored Sep 13, 2024
2 parents 4680aa9 + 6537d77 commit 0326faa
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 41 deletions.
23 changes: 23 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Pull Request

- [ ] Closes #
- [ ] Added tests (if necessary)
- [ ] Run tests
- [ ] Run formatting
- [ ] Commented the code

## Changes description

Describe what your changes will do and any TODOs there might be in the future related to the issue you are closing.

## Current output

Give evidence like screenshots of what your job looks like.

## Time spent breakdown

Give a breakdown on the time you used to solve the issue you are closing.

## Comments

Any other details you might want to share.
29 changes: 29 additions & 0 deletions .github/workflows/contract_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,32 @@ jobs:
run: |
cd contracts
scarb check
run-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Check for changes
uses: dorny/paths-filter@v3
id: changes
with:
filters: |
contracts_dir:
- 'contracts/src/**'
- name: Install scarb
if: steps.changes.outputs.contracts_dir == 'true'
run: |
cd contracts
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh -s -- -v 2.6.5
- name: Install snfoundry
uses: foundry-rs/setup-snfoundry@v3
with:
starknet-foundry-version: "0.20.1"

- name: Run Cairo tests
id: cairo_tests
run: bash scripts/run_tests.sh
3 changes: 3 additions & 0 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", ta
[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.20.1" }

[scripts]
test = "snforge test"

[[target.starknet-contract]]
casm = true
19 changes: 7 additions & 12 deletions contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub trait IFund<TContractState> {
fn getOwner(self: @TContractState) -> ContractAddress;
fn setName(ref self: TContractState, name: felt252);
fn getName(self: @TContractState) -> felt252;
fn setReason(ref self: TContractState, reason: felt252);
fn getReason(self: @TContractState) -> felt252;
fn setReason(ref self: TContractState, reason: ByteArray);
fn getReason(self: @TContractState) -> ByteArray;
fn receiveVote(ref self: TContractState);
fn getUpVotes(self: @TContractState) -> u32;
fn setGoal(ref self: TContractState, goal: u64);
Expand Down Expand Up @@ -38,7 +38,7 @@ mod Fund {
id: u128,
owner: ContractAddress,
name: felt252,
reason: felt252,
reason: ByteArray,
up_votes: u32,
voters: LegacyMap::<ContractAddress, u32>,
goal: u64,
Expand All @@ -51,17 +51,12 @@ mod Fund {
// *************************************************************************
#[constructor]
fn constructor(
ref self: ContractState,
id: u128,
owner: ContractAddress,
name: felt252,
reason: felt252,
goal: u64
ref self: ContractState, id: u128, owner: ContractAddress, name: felt252, goal: u64
) {
self.id.write(id);
self.owner.write(owner);
self.name.write(name);
self.reason.write(reason);
self.reason.write(" ");
self.up_votes.write(FundConstants::INITIAL_UP_VOTES);
self.goal.write(goal);
self.current_goal_state.write(FundConstants::INITIAL_GOAL);
Expand All @@ -87,12 +82,12 @@ mod Fund {
fn getName(self: @ContractState) -> felt252 {
return self.name.read();
}
fn setReason(ref self: ContractState, reason: felt252) {
fn setReason(ref self: ContractState, reason: ByteArray) {
let caller = get_caller_address();
assert!(self.owner.read() == caller, "You are not the owner");
self.reason.write(reason);
}
fn getReason(self: @ContractState) -> felt252 {
fn getReason(self: @ContractState) -> ByteArray {
return self.reason.read();
}
fn receiveVote(ref self: ContractState) {
Expand Down
15 changes: 6 additions & 9 deletions contracts/src/fundManager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use starknet::class_hash::ClassHash;

#[starknet::interface]
pub trait IFundManager<TContractState> {
fn newFund(ref self: TContractState, name: felt252, reason: felt252, goal: u64);
fn newFund(ref self: TContractState, name: felt252, goal: u64);
fn getCurrentId(self: @TContractState) -> u128;
fn getFund(self: @TContractState, id: u128) -> ContractAddress;
}
Expand All @@ -20,10 +20,6 @@ mod FundManager {
use starknet::class_hash::ClassHash;
use starknet::get_caller_address;

// This hash will change if fund.cairo file is modified
const FUND_CLASS_HASH: felt252 =
0x046d36e107b9131c6c1ad0f4ffd473adcadb92c2fe10752718b8c079f95dbb0b;

// *************************************************************************
// STORAGE
// *************************************************************************
Expand All @@ -32,14 +28,16 @@ mod FundManager {
owner: ContractAddress,
current_id: u128,
funds: LegacyMap::<u128, ContractAddress>,
fund_class_hash: ClassHash,
}

// *************************************************************************
// CONSTRUCTOR
// *************************************************************************
#[constructor]
fn constructor(ref self: ContractState) {
fn constructor(ref self: ContractState, fund_class_hash: felt252) {
self.owner.write(get_caller_address());
self.fund_class_hash.write(fund_class_hash.try_into().unwrap());
self.current_id.write(0);
}

Expand All @@ -48,15 +46,14 @@ mod FundManager {
// *************************************************************************
#[abi(embed_v0)]
impl FundManagerImpl of super::IFundManager<ContractState> {
fn newFund(ref self: ContractState, name: felt252, reason: felt252, goal: u64) {
fn newFund(ref self: ContractState, name: felt252, goal: u64) {
let mut calldata = ArrayTrait::<felt252>::new();
calldata.append(self.current_id.read().try_into().unwrap());
calldata.append(get_caller_address().try_into().unwrap());
calldata.append(name);
calldata.append(reason);
calldata.append(goal.try_into().unwrap());
let (address_0, _) = deploy_syscall(
FUND_CLASS_HASH.try_into().unwrap(), 12345, calldata.span(), false
self.fund_class_hash.read(), 12345, calldata.span(), false
)
.unwrap();
self.funds.write(self.current_id.read(), address_0);
Expand Down
39 changes: 19 additions & 20 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// *************************************************************************
// *************************
// FUND TEST
// *************************************************************************
// *************************
use starknet::{ContractAddress, contract_address_const};

use snforge_std::{declare, ContractClassTrait, CheatTarget};
Expand All @@ -22,28 +22,27 @@ fn OTHER_USER() -> ContractAddress {
fn NAME() -> felt252 {
'NAME_FUND_TEST'
}
fn REASON() -> felt252 {
'REASON_FUND_TEST'
fn REASON() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn GOAL() -> u64 {
1000
}
fn __setup__() -> ContractAddress {
fn _setup_() -> ContractAddress {
let contract = declare("Fund");
let mut calldata: Array<felt252> = array![];
calldata.append_serde(ID());
calldata.append_serde(OWNER());
calldata.append_serde(NAME());
calldata.append_serde(REASON());
calldata.append_serde(GOAL());
contract.deploy(@calldata).unwrap()
}
// *************************************************************************
// *************************
// TEST
// *************************************************************************
// *************************
#[test]
fn test_constructor() {
let contract_address = __setup__();
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
let id = dispatcher.getId();
let owner = dispatcher.getOwner();
Expand All @@ -56,7 +55,7 @@ fn test_constructor() {
assert(id == ID(), 'Invalid id');
assert(owner == OWNER(), 'Invalid owner');
assert(name == NAME(), 'Invalid name');
assert(reason == REASON(), 'Invalid reason');
assert(reason == " ", 'Invalid reason');
assert(up_votes == 0, 'Invalid up votes');
assert(goal == GOAL(), 'Invalid goal');
assert(current_goal_state == 0, 'Invalid current goal state');
Expand All @@ -65,7 +64,7 @@ fn test_constructor() {

#[test]
fn test_set_name() {
let contract_address = __setup__();
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
let name = dispatcher.getName();
assert(name == NAME(), 'Invalid name');
Expand All @@ -77,19 +76,19 @@ fn test_set_name() {

#[test]
fn test_set_reason() {
let contract_address = __setup__();
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
let reason = dispatcher.getReason();
assert(reason == REASON(), 'Invalid reason');
assert(reason == " ", 'Invalid reason');
snforge_std::start_prank(CheatTarget::One(contract_address), OWNER());
dispatcher.setReason('NEW_REASON');
dispatcher.setReason(REASON());
let new_reason = dispatcher.getReason();
assert(new_reason == 'NEW_REASON', 'Set reason method not working')
assert(new_reason == REASON(), 'Set reason method not working')
}

#[test]
fn test_set_goal() {
let contract_address = __setup__();
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
let goal = dispatcher.getGoal();
assert(goal == GOAL(), 'Invalid goal');
Expand All @@ -101,7 +100,7 @@ fn test_set_goal() {

#[test]
fn test_receive_vote_successful() {
let contract_address = __setup__();
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
dispatcher.receiveVote();
let me = dispatcher.getVoter();
Expand All @@ -114,7 +113,7 @@ fn test_receive_vote_successful() {
#[test]
#[should_panic(expected: ('User already voted!',))]
fn test_receive_vote_unsuccessful_double_vote() {
let contract_address = __setup__();
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
dispatcher.receiveVote();
let me = dispatcher.getVoter();
Expand All @@ -128,7 +127,7 @@ fn test_receive_vote_unsuccessful_double_vote() {

#[test]
fn test_receive_donation_successful() {
let contract_address = __setup__();
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
// Put state as recollecting dons
dispatcher.setIsActive(2);
Expand All @@ -148,7 +147,7 @@ fn test_receive_donation_successful() {
#[test]
#[should_panic(expected: ('Fund not recollecting dons!',))]
fn test_receive_donation_unsuccessful_wrong_state() {
let contract_address = __setup__();
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
// Put a wrong state to receive donations
dispatcher.setIsActive(1);
Expand Down
Binary file modified frontend/gostarkme-web/app/icon.ico
Binary file not shown.
30 changes: 30 additions & 0 deletions scripts/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

set -e

tests_output=$(cd contracts && scarb run test || true)

# Get summary information
echo "::group::Summary"

tests_passed=$(echo "$tests_output" | awk '/Tests:/ {print $2}')
tests_failed=$(echo "$tests_output" | awk '/Tests:/ {print $4}')
skipped_count=$(echo "$tests_output" | awk '/Tests:/ {print $6}')
ignored_count=$(echo "$tests_output" | awk '/Tests:/ {print $6}')
filtered_out_count=$(echo "$tests_output" | awk '/Tests:/ {print $6}')

echo "Tests passed: $tests_passed"
echo "Tests failed: $tests_failed"
echo "Skipped: $skipped_count"
echo "Ignored: $ignored_count"
echo "Filtered out: $filtered_out_count"
echo "::endgroup::"

# Check for failed tests

if [ "$tests_failed" -gt 0 ]; then
failed_tests=$(echo "$tests_output" | awk '/Failures:/{flag=1;next}/^\s*$/{flag=0}flag')
echo "::error::Tests failed:"
echo "$failed_tests"
exit 1
fi

0 comments on commit 0326faa

Please sign in to comment.