5

I have a function using the following 2 types:

pub type BalanceOf<T> = <<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

and I use them to implement a method in a struct using generics:

#[derive(Debug, PartialEq, Encode, Decode)]
pub struct Investor<T,U>{
    pub account_id:T,
    pub nft:U,
}

impl<T:pallet::Config,U> Investor<T,U>{
    
    pub fn contribute(origin:OriginFor<T>,acc:AccountIdOf<T> ,value:BalanceOf<T>) -> DispatchResult{
        let c1=Contribution::new(&acc,&value);
        let _who = ensure_signed(origin)?;
        let _now = <frame_system::Pallet<T>>::block_number();
        if ContributionsLog::<T>::contains_key(c1.account){
            ContributionsLog::<T>::mutate(c1.account, |val|{
                *val += *c1.amount;
            })
        } else {
            ContributionsLog::<T>::insert(&acc,value);
            ContAccounts::<T>::mutate(|val|{
                val.push(acc);
            })
        }
        
        //function taking contribution storage and amount as inputs here
        Ok(().into())
    }
}

the problem is when I try to call the contribute method as below

Investor::contribute(who,dev.account_id,rent);

I get the following error:

Cannot infer type for type parameter 'T'

how/where should I define an explicit type for T?

1 Answer 1

5

Rather than call Investor::contribute(who,dev.account_id,rent);

Seems you should call:

Investor::<T::AccountId, u32>::contribute(who,dev.account_id,rent);

And just be explicit with your type definitions of the generics when accessing the contribute function.

4
  • Thank you! I have a follow up question: let's say that I call the contribute method inside a function that has the following parameters : fn do_something(origin: OriginFor<T>, something: u32, acc:T::AccountId ,rent:BalanceOf<T>,cd:CID,prop:Properties,start:Option<BlockNumberOf<T>>,end:Option<BlockNumberOf<T>>) -> DispatchResult Rust considers that T is a frame_system::Config>::AccountId and therefore does not have the right T to correctly define for example let _who = ensure_signed(origin)?; Commented Mar 15, 2022 at 10:29
  • I'm not sure what you are asking. Can you open a new question with a bit more clarity?
    – Shawn Tabrizi
    Commented Mar 15, 2022 at 12:32
  • Sorry for the confusion, I think a look a the full code will make it clear: github.com/Fair-Squares/fair-squares/blob/structure_template/… On the branch "structure_template" the line 144 when uncommented prevents me from using OriginOf. This line is calling the method "contribute" mentioned earlier. Commented Mar 15, 2022 at 13:12
  • 1
    In that case, It should be Investor::<T::AccountId, u32>::contribute(who,dev.account_id,rent);, or pub account_id:T::AccountId,
    – Shawn Tabrizi
    Commented Mar 15, 2022 at 14:36

Your Answer

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

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