async fn refund(
    tn: &TransactionNotification,
    ledger_canister_id: &CanisterId,
    extra_fee: ICPTs,
) -> Result<Option<BlockHeight>, String> {
    let mut refund_block_index = None;

    // Don't refund a negative amount.
    let amount_minus_fee = if let Ok(amount) = tn.amount - TRANSACTION_FEE {
        amount
    } else {
        return Ok(None);
    };

    let (refunded, burned) = if let Ok(amount) = amount_minus_fee - extra_fee {
        (amount, extra_fee)
    } else {
        (ICPTs::ZERO, amount_minus_fee)
    };

    assert_eq!(Ok(amount_minus_fee), refunded + burned);

    if refunded != ICPTs::ZERO {
        let send_args = SendArgs {
            memo: Memo::default(),
            amount: refunded,
            fee: TRANSACTION_FEE,
            from_subaccount: tn.to_subaccount,
            to: AccountIdentifier::new(tn.from, tn.from_subaccount),
            created_at_time: None,
        };

        let res: Result<BlockHeight, (Option<i32>, String)> = dfn_core::api::call_with_cleanup(
            *ledger_canister_id,
            "send_pb",
            protobuf,
            send_args.clone(),
        )
        .await;

        let block = res.map_err(|(code, msg)| {
            format!(
                "Refund to {} failed with code {}: {:?}",
                send_args.to,
                code.unwrap_or_default(),
                msg
            )
        })?;

        print(format!(
            "Refund to {} done in block {}.",
            send_args.to, block
        ));

        refund_block_index = Some(block);
    }

    if burned != ICPTs::ZERO {
        burn_and_log(tn, burned, ledger_canister_id).await;
    }

    Ok(refund_block_index)
}
/// Send the funds for canister creation or top up back to the sender,
/// minus the transaction fee (which is gone) and the fee for the
/// action (which is burned). Returns the index of the block in which
/// the refund was done.
Send the funds for canister creation or top up back to the sender,
minus the transaction fee (which is gone) and the fee for the
action (which is burned). Returns the index of the block in which
the refund was done.
Send the funds for canister creation or top up back to the sender minus the transaction fee ( which is gone ) and the fee for the action ( which is burned ) . Returns the index of the block in which the refund was done .
async fn refund(
    tn: &TransactionNotification,
    ledger_canister_id: &CanisterId,
    extra_fee: ICPTs,
) -> Result<Option<BlockHeight>, String> {
    let mut refund_block_index = None;
    // Don't refund a negative amount.
    let amount_minus_fee = if let Ok(amount) = tn.amount - TRANSACTION_FEE {
        amount
    } else {
        return Ok(None);
    };
    let (refunded, burned) = if let Ok(amount) = amount_minus_fee - extra_fee {
        (amount, extra_fee)
    } else {
        (ICPTs::ZERO, amount_minus_fee)
    };
    assert_eq!(Ok(amount_minus_fee), refunded + burned);
    if refunded != ICPTs::ZERO {
        let send_args = SendArgs {
            memo: Memo::default(),
            amount: refunded,
            fee: TRANSACTION_FEE,
            from_subaccount: tn.to_subaccount,
            to: AccountIdentifier::new(tn.from, tn.from_subaccount),
            created_at_time: None,
        };
        let res: Result<BlockHeight, (Option<i32>, String)> = dfn_core::api::call_with_cleanup(
            *ledger_canister_id,
            "send_pb",
            protobuf,
            send_args.clone(),
        )
        .await;
        let block = res.map_err(|(code, msg)| {
            format!(
                "Refund to {} failed with code {}: {:?}",
                send_args.to,
                code.unwrap_or_default(),
                msg
            )
        })?;
        print(format!(
            "Refund to {} done in block {}.",
            send_args.to, block
        ));
        refund_block_index = Some(block);
    }
    if burned != ICPTs::ZERO {
        burn_and_log(tn, burned, ledger_canister_id).await;
    }
    Ok(refund_block_index)
}
async fn refund ( tn : & TransactionNotification , ledger_canister_id : & CanisterId , extra_fee : ICPTs , ) -> Result < Option < BlockHeight > , String > { let mut refund_block_index = None ; // Don't refund a negative amount. let amount_minus_fee = if let Ok ( amount ) = tn . amount - TRANSACTION_FEE { amount } else { return Ok ( None ) ; } ; let ( refunded , burned ) = if let Ok ( amount ) = amount_minus_fee - extra_fee { ( amount , extra_fee ) } else { ( ICPTs :: ZERO , amount_minus_fee ) } ; assert_eq ! ( Ok ( amount_minus_fee ) refunded burned ) ; if refunded != ICPTs :: ZERO { let send_args = SendArgs { memo : Memo :: default ( ) , amount : refunded , fee : TRANSACTION_FEE , from_subaccount : tn . to_subaccount , to : AccountIdentifier :: new ( tn . from , tn . from_subaccount ) , created_at_time : None , } ; let res : Result < BlockHeight , ( Option < i32 > , String ) > = dfn_core :: api :: call_with_cleanup ( * ledger_canister_id , " " , protobuf , send_args . clone ( ) , ) . await ; let block = res . map_err ( | ( code , msg ) | { format ! ( " " send_args to code unwrap_or_default ( ) msg ) } ) ? ; print ( format ! ( " " send_args to block ) ) ; refund_block_index = Some ( block ) ; } if burned != ICPTs :: ZERO { burn_and_log ( tn , burned , ledger_canister_id ) . await ; } Ok ( refund_block_index ) }
Send the funds for canister creation or top up back to the sender,
minus the transaction fee (which is gone) and the fee for the
action (which is burned).
Send the funds for canister creation or top up back to the sender minus the transaction fee ( which is gone ) and the fee for the action ( which is burned ) .
tn
&TransactionNotification
ledger_canister_id
&CanisterId
extra_fee
ICPTs
tn
&TransactionNotification
ledger_canister_id
&CanisterId
extra_fee
ICPTs