Skip to content

Commit

Permalink
[fix] Using ByteArray to represent reason
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianvrj committed Sep 12, 2024
1 parent 7a326a4 commit db3aac9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
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
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

0 comments on commit db3aac9

Please sign in to comment.