fn finalize_payment(amount_spent: U512, account: PublicKey) {
    let caller = contract_api::get_caller();
    if caller.value() != SYSTEM_ACCOUNT {
        contract_api::revert(Error::SystemFunctionCalledByUserAccount.into());
    }

    let payment_purse = get_payment_purse().unwrap_or_revert();
    let total = contract_api::get_balance(payment_purse)
        .unwrap_or_else(|| contract_api::revert(Error::PaymentPurseBalanceNotFound.into()));
    if total < amount_spent {
        contract_api::revert(Error::InsufficientPaymentForAmountSpent.into());
    }
    let refund_amount = total - amount_spent;

    let rewards_purse = get_rewards_purse().unwrap_or_revert();
    let refund_purse = get_refund_purse();
    contract_api::remove_uref(REFUND_PURSE_KEY); //unset refund purse after reading it

    // pay validators
    if let contract_api::PurseTransferResult::TransferError =
        contract_api::transfer_from_purse_to_purse(payment_purse, rewards_purse, amount_spent)
    {
        contract_api::revert(Error::FailedTransferToRewardsPurse.into());
    }

    // give refund
    if !refund_amount.is_zero() {
        if let Some(purse) = refund_purse {
            if let contract_api::PurseTransferResult::TransferError =
                contract_api::transfer_from_purse_to_purse(payment_purse, purse, refund_amount)
            {
                // on case of failure to transfer to refund purse we fall back on the account's
                // main purse
                refund_to_account(payment_purse, account, refund_amount)
            }
        } else {
            refund_to_account(payment_purse, account, refund_amount);
        }
    }
}
/// Transfers funds from the payment purse to the validator rewards
/// purse, as well as to the refund purse, depending on how much
/// was spent on the computation. This function maintains the invariant
/// that the balance of the payment purse is zero at the beginning and
/// end of each deploy and that the refund purse is unset at the beginning
/// and end of each deploy.
Transfers funds from the payment purse to the validator rewards
purse, as well as to the refund purse, depending on how much
was spent on the computation. This function maintains the invariant
that the balance of the payment purse is zero at the beginning and
end of each deploy and that the refund purse is unset at the beginning
and end of each deploy.
Transfers funds from the payment purse to the validator rewards purse as well as to the refund purse depending on how much was spent on the computation . This function maintains the invariant that the balance of the payment purse is zero at the beginning and end of each deploy and that the refund purse is unset at the beginning and end of each deploy .
fn finalize_payment(amount_spent: U512, account: PublicKey) {
    let caller = contract_api::get_caller();
    if caller.value() != SYSTEM_ACCOUNT {
        contract_api::revert(Error::SystemFunctionCalledByUserAccount.into());
    }
    let payment_purse = get_payment_purse().unwrap_or_revert();
    let total = contract_api::get_balance(payment_purse)
        .unwrap_or_else(|| contract_api::revert(Error::PaymentPurseBalanceNotFound.into()));
    if total < amount_spent {
        contract_api::revert(Error::InsufficientPaymentForAmountSpent.into());
    }
    let refund_amount = total - amount_spent;
    let rewards_purse = get_rewards_purse().unwrap_or_revert();
    let refund_purse = get_refund_purse();
    contract_api::remove_uref(REFUND_PURSE_KEY); //unset refund purse after reading it
    // pay validators
    if let contract_api::PurseTransferResult::TransferError =
        contract_api::transfer_from_purse_to_purse(payment_purse, rewards_purse, amount_spent)
    {
        contract_api::revert(Error::FailedTransferToRewardsPurse.into());
    }
    // give refund
    if !refund_amount.is_zero() {
        if let Some(purse) = refund_purse {
            if let contract_api::PurseTransferResult::TransferError =
                contract_api::transfer_from_purse_to_purse(payment_purse, purse, refund_amount)
            {
                // on case of failure to transfer to refund purse we fall back on the account's
                // main purse
                refund_to_account(payment_purse, account, refund_amount)
            }
        } else {
            refund_to_account(payment_purse, account, refund_amount);
        }
    }
}
fn finalize_payment ( amount_spent : U512 , account : PublicKey ) { let caller = contract_api :: get_caller ( ) ; if caller . value ( ) != SYSTEM_ACCOUNT { contract_api :: revert ( Error :: SystemFunctionCalledByUserAccount . into ( ) ) ; } let payment_purse = get_payment_purse ( ) . unwrap_or_revert ( ) ; let total = contract_api :: get_balance ( payment_purse ) . unwrap_or_else ( | | contract_api :: revert ( Error :: PaymentPurseBalanceNotFound . into ( ) ) ) ; if total < amount_spent { contract_api :: revert ( Error :: InsufficientPaymentForAmountSpent . into ( ) ) ; } let refund_amount = total - amount_spent ; let rewards_purse = get_rewards_purse ( ) . unwrap_or_revert ( ) ; let refund_purse = get_refund_purse ( ) ; contract_api :: remove_uref ( REFUND_PURSE_KEY ) ; //unset refund purse after reading it // pay validators if let contract_api :: PurseTransferResult :: TransferError = contract_api :: transfer_from_purse_to_purse ( payment_purse , rewards_purse , amount_spent ) { contract_api :: revert ( Error :: FailedTransferToRewardsPurse . into ( ) ) ; } // give refund if ! refund_amount . is_zero ( ) { if let Some ( purse ) = refund_purse { if let contract_api :: PurseTransferResult :: TransferError = contract_api :: transfer_from_purse_to_purse ( payment_purse , purse , refund_amount ) { // on case of failure to transfer to refund purse we fall back on the account's // main purse refund_to_account ( payment_purse , account , refund_amount ) } } else { refund_to_account ( payment_purse , account , refund_amount ) ; } } }
Transfers funds from the payment purse to the validator rewards
purse, as well as to the refund purse, depending on how much
was spent on the computation.
Transfers funds from the payment purse to the validator rewards purse as well as to the refund purse depending on how much was spent on the computation .
amount_spent
U512
account
PublicKey
amount_spent
U512
account
PublicKey