fn delegate(
        &mut self,
        delegator_public_key: PublicKey,
        validator_public_key: PublicKey,
        amount: U512,
    ) -> Result<U512, Error> {
        let provided_account_hash =
            AccountHash::from_public_key(&delegator_public_key, |x| self.blake2b(x));

        if amount.is_zero() {
            return Err(Error::BondTooSmall);
        }

        if !self.is_allowed_session_caller(&provided_account_hash) {
            return Err(Error::InvalidContext);
        }

        let source = self.get_main_purse()?;

        let validator_account_hash = AccountHash::from(&validator_public_key);

        let mut bid = match self.read_bid(&validator_account_hash)? {
            Some(bid) => bid,
            None => {
                // Return early if target validator is not in `bids`
                return Err(Error::ValidatorNotFound);
            }
        };

        let delegators = bid.delegators_mut();

        let new_delegation_amount = match delegators.get_mut(&delegator_public_key) {
            Some(delegator) => {
                self.mint_transfer_direct(
                    Some(PublicKey::System.to_account_hash()),
                    source,
                    *delegator.bonding_purse(),
                    amount,
                    None,
                )
                .map_err(|_| Error::TransferToDelegatorPurse)?
                .map_err(|_| Error::TransferToDelegatorPurse)?;
                delegator.increase_stake(amount)?;
                *delegator.staked_amount()
            }
            None => {
                let bonding_purse = self.create_purse()?;
                self.mint_transfer_direct(
                    Some(PublicKey::System.to_account_hash()),
                    source,
                    bonding_purse,
                    amount,
                    None,
                )
                .map_err(|_| Error::TransferToDelegatorPurse)?
                .map_err(|_| Error::TransferToDelegatorPurse)?;
                let delegator = Delegator::unlocked(
                    delegator_public_key.clone(),
                    amount,
                    bonding_purse,
                    validator_public_key,
                );
                delegators.insert(delegator_public_key.clone(), delegator);
                amount
            }
        };

        self.write_bid(validator_account_hash, bid)?;

        Ok(new_delegation_amount)
    }
/// Adds a new delegator to delegators or increases its current stake. If the target validator
/// is missing, the function call returns an error and does nothing.
///
/// The function transfers motes from the source purse to the delegator's bonding purse.
///
/// This entry point returns the number of tokens currently delegated to a given validator.
Adds a new delegator to delegators or increases its current stake. If the target validator
is missing, the function call returns an error and does nothing.

The function transfers motes from the source purse to the delegator's bonding purse.

This entry point returns the number of tokens currently delegated to a given validator.
Adds a new delegator to delegators or increases its current stake . If the target validator is missing the function call returns an error and does nothing . The function transfers motes from the source purse to the delegator ' s bonding purse . This entry point returns the number of tokens currently delegated to a given validator .
fn delegate(
        &mut self,
        delegator_public_key: PublicKey,
        validator_public_key: PublicKey,
        amount: U512,
    ) -> Result<U512, Error> {
        let provided_account_hash =
            AccountHash::from_public_key(&delegator_public_key, |x| self.blake2b(x));
        if amount.is_zero() {
            return Err(Error::BondTooSmall);
        }
        if !self.is_allowed_session_caller(&provided_account_hash) {
            return Err(Error::InvalidContext);
        }
        let source = self.get_main_purse()?;
        let validator_account_hash = AccountHash::from(&validator_public_key);
        let mut bid = match self.read_bid(&validator_account_hash)? {
            Some(bid) => bid,
            None => {
                // Return early if target validator is not in `bids`
                return Err(Error::ValidatorNotFound);
            }
        };
        let delegators = bid.delegators_mut();
        let new_delegation_amount = match delegators.get_mut(&delegator_public_key) {
            Some(delegator) => {
                self.mint_transfer_direct(
                    Some(PublicKey::System.to_account_hash()),
                    source,
                    *delegator.bonding_purse(),
                    amount,
                    None,
                )
                .map_err(|_| Error::TransferToDelegatorPurse)?
                .map_err(|_| Error::TransferToDelegatorPurse)?;
                delegator.increase_stake(amount)?;
                *delegator.staked_amount()
            }
            None => {
                let bonding_purse = self.create_purse()?;
                self.mint_transfer_direct(
                    Some(PublicKey::System.to_account_hash()),
                    source,
                    bonding_purse,
                    amount,
                    None,
                )
                .map_err(|_| Error::TransferToDelegatorPurse)?
                .map_err(|_| Error::TransferToDelegatorPurse)?;
                let delegator = Delegator::unlocked(
                    delegator_public_key.clone(),
                    amount,
                    bonding_purse,
                    validator_public_key,
                );
                delegators.insert(delegator_public_key.clone(), delegator);
                amount
            }
        };
        self.write_bid(validator_account_hash, bid)?;
        Ok(new_delegation_amount)
    }
fn delegate ( & mut self , delegator_public_key : PublicKey , validator_public_key : PublicKey , amount : U512 , ) -> Result < U512 , Error > { let provided_account_hash = AccountHash :: from_public_key ( & delegator_public_key , | x | self . blake2b ( x ) ) ; if amount . is_zero ( ) { return Err ( Error :: BondTooSmall ) ; } if ! self . is_allowed_session_caller ( & provided_account_hash ) { return Err ( Error :: InvalidContext ) ; } let source = self . get_main_purse ( ) ? ; let validator_account_hash = AccountHash :: from ( & validator_public_key ) ; let mut bid = match self . read_bid ( & validator_account_hash ) ? { Some ( bid ) => bid , None => { // Return early if target validator is not in `bids` return Err ( Error :: ValidatorNotFound ) ; } } ; let delegators = bid . delegators_mut ( ) ; let new_delegation_amount = match delegators . get_mut ( & delegator_public_key ) { Some ( delegator ) => { self . mint_transfer_direct ( Some ( PublicKey :: System . to_account_hash ( ) ) , source , * delegator . bonding_purse ( ) , amount , None , ) . map_err ( | _ | Error :: TransferToDelegatorPurse ) ? . map_err ( | _ | Error :: TransferToDelegatorPurse ) ? ; delegator . increase_stake ( amount ) ? ; * delegator . staked_amount ( ) } None => { let bonding_purse = self . create_purse ( ) ? ; self . mint_transfer_direct ( Some ( PublicKey :: System . to_account_hash ( ) ) , source , bonding_purse , amount , None , ) . map_err ( | _ | Error :: TransferToDelegatorPurse ) ? . map_err ( | _ | Error :: TransferToDelegatorPurse ) ? ; let delegator = Delegator :: unlocked ( delegator_public_key . clone ( ) , amount , bonding_purse , validator_public_key , ) ; delegators . insert ( delegator_public_key . clone ( ) , delegator ) ; amount } } ; self . write_bid ( validator_account_hash , bid ) ? ; Ok ( new_delegation_amount ) }
Adds a new delegator to delegators or increases its current stake.
Adds a new delegator to delegators or increases its current stake .
&mut self
delegator_public_key
PublicKey
validator_public_key
PublicKey
amount
U512
&mut self
delegator_public_key
PublicKey
validator_public_key
PublicKey
amount
U512