0

I have the below code, if you see I am storing the ContractSender struct as OptionQuery.

Later on, I am trying to read the data from the Struct, but was not able to.

#[pallet::storage]
    #[pallet::getter(fn contract_sender)]
    pub(super) type ContractSender<T: Config> =
    StorageMap<_, Blake2_128Concat, T::AccountId, Contract<T::AccountId, T::AccountId, BalanceOf<T>>, OptionQuery>;


    #[pallet::storage]
    #[pallet::getter(fn contract_receiver)]
    pub(super) type ContractReceiver<T: Config> =
    StorageMap<_, Blake2_128Concat, T::AccountId, Contract<T::AccountId, T::AccountId, BalanceOf<T>>, OptionQuery>;

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// Sign Contract
        ContractEvent(T::AccountId, T::AccountId, BalanceOf<T>, u64, u64, u64),
        /// Lock funds
        Locked(T::AccountId, BalanceOf<T>),

        /// Unlock funds
        UnLock(T::AccountId, BalanceOf<T>),

        /// Transfer
        Transfer(T::AccountId, T::AccountId, BalanceOf<T>),
    }

    #[pallet::error]
    pub enum Error<T> {
        /// The requested user has not stored a value yet
        NoValueStored,
        /// Expiring Date was wrong/older than current date
        WrongExpiringDate,
        /// Contract is signed by the same addresses
        SameAddressError
    }

    #[pallet::call]
    impl<T: Config> Pallet<T> {

        /// Sign contract between two addresses
        #[pallet::weight(T::WeightInfo::sign_contract())]
        pub fn sign_contract(
            origin: OriginFor<T>,
            to: T::AccountId,
            amount: BalanceOf<T>,
            work_days: u64,
            take_action_days: u64,
        ) -> DispatchResultWithPostInfo {
            // Check if Tx is signed
            let from = ensure_signed(origin)?;
            // Check if the sender and receiver have not the same address
            ensure!(from != to, Error::<T>::SameAddressError);

            // calculate how many blocks per day gets generated
            let prod_block_per_sec = 6;
            let day_per_second = 86400;
            let prod_block_per_day = day_per_second / prod_block_per_sec;

            let current_block_number: u64 = frame_system::Pallet::<T>::block_number().try_into().unwrap_or(0);
            let work_days_in_block_number = current_block_number + (work_days * prod_block_per_day);
            let take_action_days_in_block = work_days_in_block_number + (take_action_days * prod_block_per_day);

            //Creating a Contract object
            let contract = Contract {
                origin: from.clone(),
                to: to.clone(),
                amount: amount.clone(),
                current_block_number: current_block_number.clone(),
                work_days_in_block_number: work_days_in_block_number.clone(),
                take_action_days_in_block: take_action_days_in_block.clone(),
            };

            // Save in storage the sender and the contract
            <ContractSender<T>>::insert(from.clone(), &contract);
            // Save in storage the reciever and the contract
            <ContractReceiver<T>>::insert(to.clone(), contract);
            //Throw Contract event

            let work_days_in_block_number = <ContractSender<T>>::get(&from).work_days_in_block_number;

            Ok(().into())
        }

When I am trying to read from the storage and get the work_days_in_block_number value but not possible. Error:

 error[E0609]: no field `work_days_in_block_number` on type `Option<Contract<<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId, <<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance>>`
     --> /Users/elioprifti/substrate-node-template/pallets/escrow/src/lib.rs:161:68
      |
  161 |             let work_days_in_block_number = <ContractSender<T>>::get(&from).work_days_in_block_number;
      |                                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^

  For more information about this error, try `rustc --explain E0609`.
  warning: `escrow` (lib) generated 3 warnings
  error: could not compile `escrow` due to previous error; 3 warnings emitted

What should I do?

1 Answer 1

1

As you said, you are using OptionQuery.

So the value you get is an Option value.

let maybe_contract_sender = <ContractSender<T>>::get(&from);

if let Some(contract_sender) = maybe_contract_sender {
    let work_days_in_block_number = contract_sender.work_days_in_block_number;
}

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.