pub(crate) fn try_distribute(&self, total_seeds: &Balance) -> Option<SimpleFarmRewardDistribution> {

        if let SimpleFarmStatus::Running = self.status {
            if env::block_timestamp() < to_nano(self.terms.start_at) {
                // a farm haven't start yet
                return None;
            }
            let mut dis = self.last_distribution.clone();
            // calculate rr according to cur_timestamp
            dis.rr = (to_sec(env::block_timestamp()) - self.terms.start_at) / self.terms.session_interval;
            let mut reward_added = (dis.rr - self.last_distribution.rr) as u128 
                * self.terms.reward_per_session;
            if self.last_distribution.undistributed < reward_added {
                // all undistribution would be distributed this time
                reward_added = self.last_distribution.undistributed;
                // recalculate rr according to undistributed
                let increased_rr = (reward_added / self.terms.reward_per_session) as u32;
                dis.rr = self.last_distribution.rr + increased_rr;
                let reward_caculated = increased_rr as u128 * self.terms.reward_per_session;
                if reward_caculated < reward_added {
                    // add the tail round
                    dis.rr += 1;

                }
                // env::log(
                //     format!(
                //         "Farm ends at Round #{}, unclaimed reward: {}.",
                //         dis.rr, reward_added + dis.unclaimed
                //     )
                //     .as_bytes(),
                // );
            }
            dis.unclaimed += reward_added;
            dis.undistributed -= reward_added;

            // calculate rps
            if total_seeds == &0 {
                U256::from(0).to_little_endian(&mut dis.rps);
            } else {
                (
                    U256::from_little_endian(&self.last_distribution.rps) + 
                    U256::from(reward_added) 
                    * U256::from(DENOM) 
                    / U256::from(*total_seeds)
                ).to_little_endian(&mut dis.rps);
            }
            Some(dis)
        } else {
            None
        }

    }
/// Try to distribute reward according to current timestamp
/// return None if farm is not in Running state or haven't start farming yet;
/// return new dis :SimpleFarmRewardDistribution 
/// Note, if total_seed is 0, the rps in new dis would be reset to 0 too.
Try to distribute reward according to current timestamp
return None if farm is not in Running state or haven't start farming yet;
return new dis :SimpleFarmRewardDistribution
Note, if total_seed is 0, the rps in new dis would be reset to 0 too.
Try to distribute reward according to current timestamp return None if farm is not in Running state or haven ' t start farming yet ; return new dis : SimpleFarmRewardDistribution Note if total_seed is 0 the rps in new dis would be reset to 0 too .
pub(crate) fn try_distribute(&self, total_seeds: &Balance) -> Option<SimpleFarmRewardDistribution> {
        if let SimpleFarmStatus::Running = self.status {
            if env::block_timestamp() < to_nano(self.terms.start_at) {
                // a farm haven't start yet
                return None;
            }
            let mut dis = self.last_distribution.clone();
            // calculate rr according to cur_timestamp
            dis.rr = (to_sec(env::block_timestamp()) - self.terms.start_at) / self.terms.session_interval;
            let mut reward_added = (dis.rr - self.last_distribution.rr) as u128 
                * self.terms.reward_per_session;
            if self.last_distribution.undistributed < reward_added {
                // all undistribution would be distributed this time
                reward_added = self.last_distribution.undistributed;
                // recalculate rr according to undistributed
                let increased_rr = (reward_added / self.terms.reward_per_session) as u32;
                dis.rr = self.last_distribution.rr + increased_rr;
                let reward_caculated = increased_rr as u128 * self.terms.reward_per_session;
                if reward_caculated < reward_added {
                    // add the tail round
                    dis.rr += 1;
                }
                // env::log(
                //     format!(
                //         "Farm ends at Round #{}, unclaimed reward: {}.",
                //         dis.rr, reward_added + dis.unclaimed
                //     )
                //     .as_bytes(),
                // );
            }
            dis.unclaimed += reward_added;
            dis.undistributed -= reward_added;
            // calculate rps
            if total_seeds == &0 {
                U256::from(0).to_little_endian(&mut dis.rps);
            } else {
                (
                    U256::from_little_endian(&self.last_distribution.rps) + 
                    U256::from(reward_added) 
                    * U256::from(DENOM) 
                    / U256::from(*total_seeds)
                ).to_little_endian(&mut dis.rps);
            }
            Some(dis)
        } else {
            None
        }
    }
pub ( crate ) fn try_distribute ( & self , total_seeds : & Balance ) -> Option < SimpleFarmRewardDistribution > { if let SimpleFarmStatus :: Running = self . status { if env :: block_timestamp ( ) < to_nano ( self . terms . start_at ) { // a farm haven't start yet return None ; } let mut dis = self . last_distribution . clone ( ) ; // calculate rr according to cur_timestamp dis . rr = ( to_sec ( env :: block_timestamp ( ) ) - self . terms . start_at ) / self . terms . session_interval ; let mut reward_added = ( dis . rr - self . last_distribution . rr ) as u128 * self . terms . reward_per_session ; if self . last_distribution . undistributed < reward_added { // all undistribution would be distributed this time reward_added = self . last_distribution . undistributed ; // recalculate rr according to undistributed let increased_rr = ( reward_added / self . terms . reward_per_session ) as u32 ; dis . rr = self . last_distribution . rr + increased_rr ; let reward_caculated = increased_rr as u128 * self . terms . reward_per_session ; if reward_caculated < reward_added { // add the tail round dis . rr += 1 ; } // env::log( //     format!( //         "Farm ends at Round #{}, unclaimed reward: {}.", //         dis.rr, reward_added + dis.unclaimed //     ) //     .as_bytes(), // ); } dis . unclaimed += reward_added ; dis . undistributed -= reward_added ; // calculate rps if total_seeds == & 0 { U256 :: from ( 0 ) . to_little_endian ( & mut dis . rps ) ; } else { ( U256 :: from_little_endian ( & self . last_distribution . rps ) + U256 :: from ( reward_added ) * U256 :: from ( DENOM ) / U256 :: from ( * total_seeds ) ) . to_little_endian ( & mut dis . rps ) ; } Some ( dis ) } else { None } }
Try to distribute reward according to current timestamp
return None if farm is not in Running state or haven't start farming yet;
return new dis :SimpleFarmRewardDistribution
Note, if total_seed is 0, the rps in new dis would be reset to 0 too.
Try to distribute reward according to current timestamp return None if farm is not in Running state or haven ' t start farming yet ; return new dis : SimpleFarmRewardDistribution Note if total_seed is 0 the rps in new dis would be reset to 0 too .
&self
total_seeds
&Balance
&self
total_seeds
&Balance