0

on writing to: &mut AccountId, it throws warning to use explicit lifetime name needed here.

enter image description here

    #[ink(message)] 
    pub fn delegate(&mut self, to: &mut AccountId) {
        let caller = self.env().caller();
        let voters = self.voters.get(caller).unwrap_or_default();
        let voters_to = self.voters.get(*to).unwrap_or_default();
        assert!(!voters.voted, "You already voted.");

        assert!(*to != caller, "Self-delegation is disallowed");

        while voters_to.delegate != [0; 32].into() {
            *to = voters_to.delegate;
        }
    }

including lifetime, but it throws ink message must not be generic. I remove #[ink(message)] and it works fine and #[inline] as well

enter image description here

    #[ink(message)] 
    pub fn delegate<'a>(&mut self, to: &'a mut AccountId) {
        let caller = self.env().caller();
        let voters = self.voters.get(caller).unwrap_or_default();
        let voters_to = self.voters.get(*to).unwrap_or_default();
        assert!(!voters.voted, "You already voted.");

        assert!(*to != caller, "Self-delegation is disallowed");

        while voters_to.delegate != [0; 32].into() {
            *to = voters_to.delegate;
        }
    }

Anybody explain me why this is happening? Or am I doing something wrong?

2 Answers 2

0

You should change to: &'a mut AccountId to to: AccountId, and just use the AccountId directly in your ink! message.

1

You can not use references in [ink(message)].

I think you want this function to be public, so it needs to be [ink(message)]. You can just simply remove the reference of the parameter and work with its value.

4
  • should I omit #[ink(message)] then?
    – Ganesh11
    Dec 16, 2022 at 1:21
  • I updated my answer)
    – kriko727
    Dec 16, 2022 at 10:02
  • thanks for help I need to define pub fn delegate(&mut self, to: AccountId) to as mut, I have only found to use &mut as pub fn delegate(&mut self, to: &mut AccountId) but #[ink(message)] doesn't use references.
    – Ganesh11
    Dec 16, 2022 at 11:30
  • the &mut self or &self part is necessary for a message. mut self means that the message will mutate the contract storage, while self is only a view function
    – kriko727
    Dec 16, 2022 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.