Skip to content

Commit

Permalink
[TEMA 1] QoL change for better clarity
Browse files Browse the repository at this point in the history
- Added an extra require to remove confusing error when calling exchangeNFT without getting your card first
  • Loading branch information
razvan-tanase committed Nov 8, 2024
1 parent 6a83e74 commit 6c04b98
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions assignments/tema-1/src/tema_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub trait Tema1: default_issue_callbacks::DefaultIssueCallbacksModule {
let rand_index = rand_source.next_usize_in_range(1, vec_len + 1);

let rand_card_properties = self.cards_properties().get(rand_index);
self.students_cards(self.blockchain().get_caller()).set(&rand_card_properties);
self.students_cards(student_address).set(&rand_card_properties);

rand_card_properties
}
Expand All @@ -119,8 +119,8 @@ pub trait Tema1: default_issue_callbacks::DefaultIssueCallbacksModule {
require!(nonce > 0, "NFTs can't have a nonce of 0");
require!(nonce <= self.nft_supply().len() as u64, "NFT not found");

let caller_address = self.blockchain().get_caller();
require!(!self.student_address().contains(&caller_address), "Congratulations! You already finished the homework!");
let student_address = self.blockchain().get_caller();
require!(!self.student_address().contains(&student_address), "Congratulations! You already finished the homework!");

let nft_payment = self.call_value().single_esdt();
let nft_student_data = self.blockchain().get_esdt_token_data(
Expand All @@ -129,7 +129,11 @@ pub trait Tema1: default_issue_callbacks::DefaultIssueCallbacksModule {
nft_payment.token_nonce,
);

require!(nft_student_data.try_decode_attributes::<CardProperties>().unwrap() == self.students_cards(caller_address.clone()).get(), "NFT data mismatch");
let is_empty = self.students_cards(student_address.clone()).is_empty();
require!(!is_empty, "You need to get your card first");

let student_card = self.students_cards(student_address.clone()).get();
require!(nft_student_data.try_decode_attributes::<CardProperties>().unwrap() == student_card, "NFT data mismatch");

let mut index_to_remove: usize = 0;
let nft_data = self.token_id().get_all_token_data(nonce);
Expand All @@ -143,7 +147,7 @@ pub trait Tema1: default_issue_callbacks::DefaultIssueCallbacksModule {
if index_to_remove > 0 {
self.cards_properties().swap_remove(index_to_remove + 1);
self.nft_supply().set(nonce as usize, &nft_student_data);
self.student_address().insert(caller_address);
self.student_address().insert(student_address);
self.send_nft_to_caller(nonce);
}
}
Expand Down

0 comments on commit 6c04b98

Please sign in to comment.