I am getting a Contract Trapped error on one specific message in my ink contract. I tried putting overflow-checks = false
into the Cargo.toml. That made no difference. This is the first time I have ever seen that error pop up. This is also the first time I have made a message function that both takes in coin and pays out coin. This contract does not call any other contract. It is completely stand alone.
Any ideas on why I am getting a Contract Trapped on this one message? .... Here's the code...
#[ink(message, payable)]
#[openbrush::modifiers(non_reentrant)]
pub fn send_message_to_paid_list (&mut self,
to_list_id: Hash,
new_message: Vec<u8>,
file_url: Vec<u8>,
) -> Result<(), Error> {
// COLLECT PAYMENT FROM THE CALLER
// the 'payable' tag on this message allows the user to send any amount
let staked: Balance = self.env().transferred_value();
// Does this list exist? and do you own it? If so, proceed
let caller = Self::env().caller();
let owned_lists = self.account_owned_paid_lists.get(&caller).unwrap_or_default().hashvector;
if owned_lists.contains(&to_list_id) {
// total the fees required to send to this list
// get the list_accounts from paid_list_details: Mapping<Hash, PaidListDetails>
let listaccounts = self.paid_list_details.get(&to_list_id).unwrap_or_default().list_accounts;
let mut total_fee: Balance = 0;
// for each acccount on the paid list...
for inbox in &listaccounts {
// get their inbox fee from account_settings: Mapping<AccountID, Settings>
let fee: Balance = self.account_settings.get(&inbox).unwrap_or_default().inbox_fee;
// add it to the total_fee
total_fee += fee;
}
// if staked is more than total_fee, proceed
if staked > total_fee {
// set up clones
let new_message_clone = new_message.clone();
// set up the data that will go into the new_message_id hash
let new_timestamp = self.env().block_timestamp();
// create the new_message_id by hashing the above data
let encodable = (caller, to_list_id, new_message, new_timestamp); // Implements `scale::Encode`
let mut new_message_id_u8 = <Sha2x256 as HashOutput>::Type::default(); // 256-bit buffer
ink::env::hash_encoded::<Sha2x256, _>(&encodable, &mut new_message_id_u8);
let new_message_id: Hash = Hash::from(new_message_id_u8);
// SET UP THE MESSAGE DETAILS FOR THE NEW MESSAGE
let fromusername = self.account_settings.get(&caller).unwrap_or_default().username;
let listname = self.paid_list_details.get(&to_list_id).unwrap_or_default().list_name;
let new_details = ListMessageDetails {
message_id: new_message_id,
from_acct: Self::env().caller(),
username: fromusername,
to_list_id: to_list_id,
to_list_name: listname,
message: new_message_clone,
file_url: file_url,
timestamp: self.env().block_timestamp(),
};
// add the message to paid_list_message_details: Mapping<Hash, ListMessageDetails>
self.paid_list_message_details.insert(&new_message_id, &new_details);
// add the message ID to sent_messages_to_paid_list: Mapping<Hash, HashVector>
// get the messages vector for this list
let mut current_messages = self.sent_messages_to_paid_list.get(&to_list_id).unwrap_or_default();
// add this message to the messages vector for this list
current_messages.hashvector.push(new_message_id);
// update the sent_messages_to_paid_list map
self.sent_messages_to_paid_list.insert(&to_list_id, ¤t_messages);
// PAY EACH ACCOUNT ON THE PAID LIST
for inbox in &listaccounts {
// get their inbox fee from account_settings: Mapping<AccountID, Settings>
let fee: Balance = self.account_settings.get(&inbox).unwrap_or_default().inbox_fee;
// send them their inbox_fee
self.env().transfer(*inbox, fee).expect("payout failed");
if self.env().transfer(*inbox, fee).is_err() {
return Err(Error::PayoutFailed);
}
}
// then pay the caller what is left from their stake after paying the list
let leftovers: Balance = staked - total_fee;
if leftovers > 0 {
self.env().transfer(caller, leftovers).expect("payout failed");
if self.env().transfer(caller, leftovers).is_err() {
return Err(Error::PayoutFailed);
}
}
Ok(())
}
// if staked is less than or = to the total fee, send an error
else {
return Err(Error::InsufficientStake);
}
}
// if the list does not exist, or you do not own it, send an error
else {
return Err(Error::NonexistentList);
}
}