Please explain why the following occurs
When this method is called, the tokens are equally divided and distributed to the user. (as expected)
All user balances get updated with the new amounts.
#[ink(message, payable)]
pub fn distribute_tokens(&mut self, to: Vec<AccountId>) -> Result<(), PSP22Error> {
let total_transfer_amount = self.env().transferred_balance();
// Calculate the amount to be sent to each recipient.
let individual_amount = total_transfer_amount / to.len() as Balance;
// Transfer tokens to each recipient.
for recipient in to {
// This will panic if the transfer fails.
self.env().transfer(recipient, individual_amount).unwrap_or_default();
}
Ok(())
}
When I try to do something like this where the user has the option to specify the amount for each recipient, The transaction also succeeds. The balance of the caller (the one distributing the tokens) gets updated with the amount sent as deducted.
The problem occurs here : The receivers balances remain unchanged. I'm assuming the amount sent from the distributor gets to the contract and stays there without it distributing any of the tokens. Any insight would be appreciated!
#[ink(message, payable)]
pub fn distribute_tokens(&mut self, recipients: StorageVec<AccountId>, amounts: StorageVec<Balance>) {
assert!(recipients.len() == amounts.len(), "Recipients and amounts vectors must be of the same length.");
let transferred_amount: Balance = self.env().transferred_value();
// Ensure the transferred amount matches the total of the amounts vector.
let total_amounts: Balance = amounts.iter().sum();
assert!(transferred_amount >= total_amounts, "Transferred amount does not match total of amounts vector.");
for (recipient, amount) in recipients.iter().zip(amounts.iter()) {
self.env().transfer(*recipient, *amount).unwrap_or_else(|err: InkEnvError| panic!("transfer failed: {:?}", err));
}
}
The transfer fail exception does not get thrown.
Note: StorageVec is an alias for Vec