Skip to content

Commit

Permalink
fear: add certification enrollment function (#14)
Browse files Browse the repository at this point in the history
* fear: add certification enrollment function

* fix fmt
  • Loading branch information
emarc99 authored Jan 31, 2025
1 parent b2e62d2 commit e8d8e0f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/ISkillNet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait ISkillNet<TContractState> {

// Examinations/Certifications
fn create_certification(ref self: TContractState, name: ByteArray, fee: u256) -> u256;
fn enroll_for_certification(ref self: TContractState, certificate_id: u256, fee: u256);
fn enroll_for_certification(ref self: TContractState, certificate_id: u256);
fn mint_exam_certificate(ref self: TContractState, certificate_id: u256);
fn verify_exam_certificate(
self: @TContractState, certificate_id: u256, student: ContractAddress,
Expand Down
46 changes: 42 additions & 4 deletions src/skillnet/skillnet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub mod SkillNet {
struct Storage {
courses_count: u256,
certifications_count: u256,
course_details: Map<u256, CourseDetails>, // map(course_id, CourseDetails)
course_instructors: Map<u256, ContractAddress>, // map(course_id, CourseInstructor)
course_details: Map<u256, CourseDetails>, // map(course_id, CourseDetails)
course_instructors: Map<u256, ContractAddress>, // map(course_id, CourseInstructor)
certification_details: Map<
u256, CertificationDetails,
>, // map(certification_id, CertificationDetails)
>, // map(certification_id, CertificationDetails)
token_address: ContractAddress,
}

Expand Down Expand Up @@ -147,7 +147,45 @@ pub mod SkillNet {
certification_id
}

fn enroll_for_certification(ref self: ContractState, certificate_id: u256, fee: u256) {}
fn enroll_for_certification(ref self: ContractState, certificate_id: u256) {
// Get certification details and verify certification exists
let certification = self.certification_details.read(certificate_id);
assert(
certification.certification_id == certificate_id, 'Certification does not exist',
);

// Get student and institution addresses
let student = get_caller_address();
let institution = certification.institution;

let token = self.token_address.read();
let erc20 = IERC20Dispatcher { contract_address: token };

// Check user's balance
let user_balance = erc20.balance_of(student).into();
assert(user_balance >= certification.enroll_fee, 'Insufficient balance');

// Execute payment transfer
erc20.transfer_from(student, institution, certification.enroll_fee.try_into().unwrap());

// Update total enrolled count
let new_total = certification.total_enrolled + 1;
let certification_name = certification.name.clone();
let updated_certification = CertificationDetails {
total_enrolled: new_total, ..certification,
};
self.certification_details.write(certificate_id, updated_certification);

// Emit enrollment event
self
.emit(
EnrolledForCertification {
certification_id: certificate_id,
certification: certification_name,
student_address: student,
},
);
}

fn mint_exam_certificate(ref self: ContractState, certificate_id: u256) {}

Expand Down
33 changes: 33 additions & 0 deletions tests/test_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,36 @@ fn test_enroll_for_course() {
spy.assert_emitted(@array![(skillnet_contract_address, expected_event)]);
stop_cheat_caller_address(skillnet_contract_address);
}

#[test]
fn test_enroll_for_certification() {
let (skillnet_contract_address, erc20_contract_address) = __setup__();
let skillnet_dispatcher = ISkillNetDispatcher { contract_address: skillnet_contract_address };
let erc20_dispatcher = IERC20Dispatcher { contract_address: erc20_contract_address };

// Define institution and student addresses
let institution: ContractAddress = BARRETO.try_into().unwrap();
let student: ContractAddress = WESCOT.try_into().unwrap();

// Create certification with fee
start_cheat_caller_address(skillnet_contract_address, institution);
let certification_id = skillnet_dispatcher.create_certification("Blockchain Developer", 100);
stop_cheat_caller_address(skillnet_contract_address);

// Approve token spending before enrollment
start_cheat_caller_address(skillnet_contract_address, student);
erc20_dispatcher.approve(skillnet_contract_address, 100);

// Enroll in certification
let mut spy = spy_events();
skillnet_dispatcher.enroll_for_certification(certification_id);

// Verify enrollment event
let expected_event = SkillNet::Event::EnrolledForCertification(
SkillNet::EnrolledForCertification {
certification_id, certification: "Blockchain Developer", student_address: student,
},
);
spy.assert_emitted(@array![(skillnet_contract_address, expected_event)]);
stop_cheat_caller_address(skillnet_contract_address);
}

0 comments on commit e8d8e0f

Please sign in to comment.