11use crate :: address:: BitcoinAddress ;
22use crate :: logs:: { P0 , P1 } ;
33use crate :: management:: CallError ;
4- use crate :: memo:: Status ;
54use crate :: queries:: WithdrawalFee ;
6- use crate :: state:: ReimbursementReason ;
75use crate :: updates:: update_balance:: UpdateBalanceError ;
86use async_trait:: async_trait;
97use candid:: { CandidType , Deserialize , Principal } ;
@@ -14,8 +12,7 @@ use ic_btc_interface::{
1412use ic_canister_log:: log;
1513use ic_management_canister_types:: DerivationPath ;
1614use icrc_ledger_types:: icrc1:: account:: Account ;
17- use icrc_ledger_types:: icrc1:: transfer:: { Memo , TransferError } ;
18- use num_traits:: ToPrimitive ;
15+ use icrc_ledger_types:: icrc1:: transfer:: Memo ;
1916use scopeguard:: { guard, ScopeGuard } ;
2017use serde:: Serialize ;
2118use serde_bytes:: ByteBuf ;
@@ -464,40 +461,6 @@ fn finalized_txids(candidates: &[state::SubmittedBtcTransaction], new_utxos: &[U
464461 . collect ( )
465462}
466463
467- async fn reimburse_failed_kyt ( ) {
468- let try_to_reimburse = state:: read_state ( |s| s. pending_reimbursements . clone ( ) ) ;
469- for ( burn_block_index, entry) in try_to_reimburse {
470- let ( memo_status, kyt_fee) = match entry. reason {
471- ReimbursementReason :: TaintedDestination { kyt_fee, .. } => ( Status :: Rejected , kyt_fee) ,
472- ReimbursementReason :: CallFailed => ( Status :: CallFailed , 0 ) ,
473- } ;
474- let reimburse_memo = crate :: memo:: MintMemo :: KytFail {
475- kyt_fee : Some ( kyt_fee) ,
476- status : Some ( memo_status) ,
477- associated_burn_index : Some ( burn_block_index) ,
478- } ;
479- if let Ok ( block_index) = crate :: updates:: update_balance:: mint (
480- entry
481- . amount
482- . checked_sub ( kyt_fee)
483- . expect ( "reimburse underflow" ) ,
484- entry. account ,
485- crate :: memo:: encode ( & reimburse_memo) . into ( ) ,
486- )
487- . await
488- {
489- state:: mutate_state ( |s| {
490- state:: audit:: reimbursed_failed_deposit (
491- s,
492- burn_block_index,
493- block_index,
494- & IC_CANISTER_RUNTIME ,
495- )
496- } ) ;
497- }
498- }
499- }
500-
501464async fn finalize_requests ( ) {
502465 if state:: read_state ( |s| s. submitted_transactions . is_empty ( ) ) {
503466 return ;
@@ -1119,96 +1082,6 @@ fn distribute(amount: u64, n: u64) -> Vec<u64> {
11191082 shares
11201083}
11211084
1122- pub async fn distribute_kyt_fees ( ) {
1123- use icrc_ledger_client_cdk:: CdkRuntime ;
1124- use icrc_ledger_client_cdk:: ICRC1Client ;
1125- use icrc_ledger_types:: icrc1:: transfer:: TransferArg ;
1126-
1127- enum MintError {
1128- TransferError ( TransferError ) ,
1129- CallError ( i32 , String ) ,
1130- }
1131-
1132- impl std:: fmt:: Debug for MintError {
1133- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
1134- match self {
1135- MintError :: TransferError ( e) => write ! ( f, "TransferError({:?})" , e) ,
1136- MintError :: CallError ( code, msg) => write ! ( f, "CallError({}, {:?})" , code, msg) ,
1137- }
1138- }
1139- }
1140-
1141- async fn mint ( amount : u64 , to : candid:: Principal , memo : Memo ) -> Result < u64 , MintError > {
1142- debug_assert ! ( memo. 0 . len( ) <= CKBTC_LEDGER_MEMO_SIZE as usize ) ;
1143-
1144- let client = ICRC1Client {
1145- runtime : CdkRuntime ,
1146- ledger_canister_id : state:: read_state ( |s| s. ledger_id . get ( ) . into ( ) ) ,
1147- } ;
1148- client
1149- . transfer ( TransferArg {
1150- from_subaccount : None ,
1151- to : Account {
1152- owner : to,
1153- subaccount : None ,
1154- } ,
1155- fee : None ,
1156- created_at_time : None ,
1157- memo : Some ( memo) ,
1158- amount : candid:: Nat :: from ( amount) ,
1159- } )
1160- . await
1161- . map_err ( |( code, msg) | MintError :: CallError ( code, msg) ) ?
1162- . map_err ( MintError :: TransferError )
1163- . map ( |n| n. 0 . to_u64 ( ) . expect ( "nat does not fit into u64" ) )
1164- }
1165-
1166- let fees_to_distribute = state:: read_state ( |s| s. owed_kyt_amount . clone ( ) ) ;
1167- for ( provider, amount) in fees_to_distribute {
1168- let memo = crate :: memo:: MintMemo :: Kyt ;
1169- match mint ( amount, provider, crate :: memo:: encode ( & memo) . into ( ) ) . await {
1170- Ok ( block_index) => {
1171- state:: mutate_state ( |s| {
1172- if let Err ( state:: Overdraft ( overdraft) ) = state:: audit:: distributed_kyt_fee (
1173- s,
1174- provider,
1175- amount,
1176- block_index,
1177- & IC_CANISTER_RUNTIME ,
1178- ) {
1179- // This should never happen because:
1180- // 1. The fee distribution task is guarded (at most one copy is active).
1181- // 2. Fee distribution is the only way to decrease the balance.
1182- log ! (
1183- P0 ,
1184- "BUG[distribute_kyt_fees]: distributed {} to {} but the balance is only {}" ,
1185- tx:: DisplayAmount ( amount) ,
1186- provider,
1187- tx:: DisplayAmount ( amount - overdraft) ,
1188- ) ;
1189- } else {
1190- log ! (
1191- P0 ,
1192- "[distribute_kyt_fees]: minted {} to {}" ,
1193- tx:: DisplayAmount ( amount) ,
1194- provider,
1195- ) ;
1196- }
1197- } ) ;
1198- }
1199- Err ( error) => {
1200- log ! (
1201- P0 ,
1202- "[distribute_kyt_fees]: failed to mint {} to {} with error: {:?}" ,
1203- tx:: DisplayAmount ( amount) ,
1204- provider,
1205- error
1206- ) ;
1207- }
1208- }
1209- }
1210- }
1211-
12121085pub fn timer < R : CanisterRuntime + ' static > ( runtime : R ) {
12131086 use tasks:: { pop_if_ready, run_task} ;
12141087
0 commit comments