1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! # Replace Pallet
//! Based on the [specification](https://spec.interlay.io/spec/replace.html).

#![deny(warnings)]
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

mod default_weights;
mod ext;

pub mod types;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[cfg(test)]
extern crate mocktopus;

#[cfg(test)]
use mocktopus::macros::mockable;

use crate::types::{BalanceOf, ReplaceRequestExt, Version};
pub use crate::types::{DefaultReplaceRequest, ReplaceRequest, ReplaceRequestStatus};
use bitcoin::types::FullTransactionProof;
use btc_relay::BtcAddress;
use currency::Amount;
pub use default_weights::WeightInfo;
use frame_support::{
    dispatch::{DispatchError, DispatchResult},
    ensure,
    pallet_prelude::Weight,
    traits::Get,
    transactional,
};
use frame_system::{ensure_root, ensure_signed};
use sp_core::H256;
use sp_std::vec::Vec;
use types::DefaultVaultId;
use vault_registry::{types::CurrencyId, CurrencySource};

pub use pallet::*;

/// Complexity:
/// - `O(H + I + O + B)` where:
///   - `H` is the number of hashes in the merkle tree
///   - `I` is the number of transaction inputs
///   - `O` is the number of transaction outputs
///   - `B` is `transaction` size in bytes (length-fee-bounded)
fn weight_for_execute_replace<T: Config>(proof: &FullTransactionProof) -> Weight {
    {
        let h = proof.user_tx_proof.merkle_proof.hashes.len() as u32;
        let i = proof.user_tx_proof.transaction.inputs.len() as u32;
        let o = proof.user_tx_proof.transaction.outputs.len() as u32;
        let b = proof.user_tx_proof.tx_encoded_len;
        <T as Config>::WeightInfo::execute_pending_replace(h, i, o, b)
            .max(<T as Config>::WeightInfo::execute_cancelled_replace(h, i, o, b))
    }
    .saturating_add({
        let h = proof.coinbase_proof.merkle_proof.hashes.len() as u32;
        let i = proof.coinbase_proof.transaction.inputs.len() as u32;
        let o = proof.coinbase_proof.transaction.outputs.len() as u32;
        let b = proof.coinbase_proof.tx_encoded_len;
        <T as Config>::WeightInfo::execute_pending_replace(h, i, o, b)
            .max(<T as Config>::WeightInfo::execute_cancelled_replace(h, i, o, b))
    })
}

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_support::pallet_prelude::*;
    use frame_system::pallet_prelude::*;
    use primitives::VaultId;
    use vault_registry::types::DefaultVaultCurrencyPair;

    /// ## Configuration
    /// The pallet's configuration trait.
    #[pallet::config]
    pub trait Config:
        frame_system::Config
        + vault_registry::Config
        + btc_relay::Config
        + oracle::Config
        + fee::Config
        + nomination::Config
    {
        /// The overarching event type.
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// Weight information for the extrinsics in this module.
        type WeightInfo: WeightInfo;
    }

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        RequestReplace {
            old_vault_id: DefaultVaultId<T>,
            amount: BalanceOf<T>,
            griefing_collateral: BalanceOf<T>,
        },
        WithdrawReplace {
            old_vault_id: DefaultVaultId<T>,
            withdrawn_tokens: BalanceOf<T>,
            withdrawn_griefing_collateral: BalanceOf<T>,
        },
        AcceptReplace {
            replace_id: H256,
            old_vault_id: DefaultVaultId<T>,
            new_vault_id: DefaultVaultId<T>,
            amount: BalanceOf<T>,
            collateral: BalanceOf<T>,
            btc_address: BtcAddress,
        },
        ExecuteReplace {
            replace_id: H256,
            old_vault_id: DefaultVaultId<T>,
            new_vault_id: DefaultVaultId<T>,
        },
        CancelReplace {
            replace_id: H256,
            new_vault_id: DefaultVaultId<T>,
            old_vault_id: DefaultVaultId<T>,
            griefing_collateral: BalanceOf<T>,
        },
        ReplacePeriodChange {
            period: BlockNumberFor<T>,
        },
    }

    #[pallet::error]
    pub enum Error<T> {
        /// Replace requires non-zero increase.
        ReplaceAmountZero,
        /// Replace amount is too small.
        AmountBelowDustAmount,
        /// No replace request found.
        NoPendingRequest,
        /// Unexpected vault account.
        UnauthorizedVault,
        /// Cannot replace self.
        ReplaceSelfNotAllowed,
        /// Cannot replace with nominated collateral.
        VaultHasEnabledNomination,
        /// Replace request has not expired.
        ReplacePeriodNotExpired,
        /// Replace request already completed.
        ReplaceCompleted,
        /// Replace request already cancelled.
        ReplaceCancelled,
        /// Replace request not found.
        ReplaceIdNotFound,
        /// Vault cannot replace different currency.
        InvalidWrappedCurrency,
    }

    /// Vaults create replace requests to transfer locked collateral.
    /// This mapping provides access from a unique hash to a `ReplaceRequest`.
    #[pallet::storage]
    pub(super) type ReplaceRequests<T: Config> =
        StorageMap<_, Blake2_128Concat, H256, DefaultReplaceRequest<T>, OptionQuery>;

    /// The time difference in number of blocks between when a replace request is created
    /// and required completion time by a vault. The replace period has an upper limit
    /// to prevent griefing of vault collateral.
    #[pallet::storage]
    #[pallet::getter(fn replace_period)]
    pub(super) type ReplacePeriod<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;

    /// The minimum amount of btc that is accepted for replace requests; any lower values would
    /// risk the bitcoin client to reject the payment
    #[pallet::storage]
    #[pallet::getter(fn replace_btc_dust_value)]
    pub(super) type ReplaceBtcDustValue<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

    #[pallet::type_value]
    pub(super) fn DefaultForStorageVersion() -> Version {
        Version::V0
    }

    /// Build storage at V1 (requires default 0).
    #[pallet::storage]
    #[pallet::getter(fn storage_version)]
    pub(super) type StorageVersion<T: Config> = StorageValue<_, Version, ValueQuery, DefaultForStorageVersion>;

    #[pallet::genesis_config]
    #[derive(frame_support::DefaultNoBound)]
    pub struct GenesisConfig<T: Config> {
        pub replace_period: BlockNumberFor<T>,
        pub replace_btc_dust_value: BalanceOf<T>,
    }

    #[pallet::genesis_build]
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
        fn build(&self) {
            ReplacePeriod::<T>::put(self.replace_period);
            ReplaceBtcDustValue::<T>::put(self.replace_btc_dust_value);
        }
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

    #[pallet::pallet]
    pub struct Pallet<T>(_);

    // The pallet's dispatchable functions.
    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Request the replacement of a new vault ownership
        ///
        /// # Arguments
        ///
        /// * `origin` - sender of the transaction
        /// * `amount` - amount of issued tokens
        /// * `griefing_collateral` - amount of collateral
        #[pallet::call_index(0)]
        #[pallet::weight(<T as Config>::WeightInfo::request_replace())]
        #[transactional]
        pub fn request_replace(
            origin: OriginFor<T>,
            currency_pair: DefaultVaultCurrencyPair<T>,
            #[pallet::compact] amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            let old_vault = VaultId::new(ensure_signed(origin)?, currency_pair.collateral, currency_pair.wrapped);
            Self::_request_replace(old_vault, amount)?;
            Ok(().into())
        }

        /// Withdraw a request of vault replacement
        ///
        /// # Arguments
        ///
        /// * `origin` - sender of the transaction: the old vault
        #[pallet::call_index(1)]
        #[pallet::weight(<T as Config>::WeightInfo::withdraw_replace())]
        #[transactional]
        pub fn withdraw_replace(
            origin: OriginFor<T>,
            currency_pair: DefaultVaultCurrencyPair<T>,
            #[pallet::compact] amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            let old_vault = VaultId::new(ensure_signed(origin)?, currency_pair.collateral, currency_pair.wrapped);
            Self::_withdraw_replace_request(old_vault, amount)?;
            Ok(().into())
        }

        /// Accept request of vault replacement
        ///
        /// # Arguments
        ///
        /// * `origin` - the initiator of the transaction: the new vault
        /// * `old_vault` - id of the old vault that we are (possibly partially) replacing
        /// * `collateral` - the collateral for replacement
        /// * `btc_address` - the address that old-vault should transfer the btc to
        #[pallet::call_index(2)]
        #[pallet::weight(<T as Config>::WeightInfo::accept_replace())]
        #[transactional]
        pub fn accept_replace(
            origin: OriginFor<T>,
            currency_pair: DefaultVaultCurrencyPair<T>,
            old_vault: DefaultVaultId<T>,
            #[pallet::compact] amount_btc: BalanceOf<T>,
            #[pallet::compact] collateral: BalanceOf<T>,
            btc_address: BtcAddress,
        ) -> DispatchResultWithPostInfo {
            let new_vault = VaultId::new(ensure_signed(origin)?, currency_pair.collateral, currency_pair.wrapped);
            Self::_accept_replace(old_vault, new_vault, amount_btc, collateral, btc_address)?;
            Ok(().into())
        }

        /// Execute vault replacement
        ///
        /// # Arguments
        ///
        /// * `origin` - sender of the transaction: the new vault
        /// * `replace_id` - the ID of the replacement request
        /// * 'merkle_proof' - the merkle root of the block
        /// * `raw_tx` - the transaction id in bytes
        #[pallet::call_index(3)]
        #[pallet::weight(weight_for_execute_replace::<T>(unchecked_transaction))]
        #[transactional]
        pub fn execute_replace(
            origin: OriginFor<T>,
            replace_id: H256,
            unchecked_transaction: FullTransactionProof,
        ) -> DispatchResultWithPostInfo {
            let _ = ensure_signed(origin)?;

            Self::_execute_replace(replace_id, unchecked_transaction)?;
            Ok(().into())
        }

        /// Cancel vault replacement
        ///
        /// # Arguments
        ///
        /// * `origin` - sender of the transaction: anyone
        /// * `replace_id` - the ID of the replacement request
        #[pallet::call_index(4)]
        #[pallet::weight(<T as Config>::WeightInfo::cancel_replace())]
        #[transactional]
        pub fn cancel_replace(origin: OriginFor<T>, replace_id: H256) -> DispatchResultWithPostInfo {
            let _ = ensure_signed(origin)?;
            Self::_cancel_replace(replace_id)?;
            Ok(().into())
        }

        /// Set the default replace period for tx verification.
        ///
        /// # Arguments
        ///
        /// * `origin` - the dispatch origin of this call (must be _Root_)
        /// * `period` - default period for new requests
        ///
        /// # Weight: `O(1)`
        #[pallet::call_index(5)]
        #[pallet::weight(<T as Config>::WeightInfo::set_replace_period())]
        #[transactional]
        pub fn set_replace_period(origin: OriginFor<T>, period: BlockNumberFor<T>) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            <ReplacePeriod<T>>::set(period);
            Self::deposit_event(Event::ReplacePeriodChange { period });
            Ok(().into())
        }
    }
}

// "Internal" functions, callable by code.
#[cfg_attr(test, mockable)]
impl<T: Config> Pallet<T> {
    fn _request_replace(vault_id: DefaultVaultId<T>, amount_btc: BalanceOf<T>) -> DispatchResult {
        // check vault is not banned
        ext::vault_registry::ensure_not_banned::<T>(&vault_id)?;

        let amount_btc = Amount::new(amount_btc, vault_id.wrapped_currency());

        ensure!(
            !ext::nomination::is_nominatable::<T>(&vault_id),
            Error::<T>::VaultHasEnabledNomination
        );

        let requestable_tokens = ext::vault_registry::requestable_to_be_replaced_tokens::<T>(&vault_id)?;
        let to_be_replaced_increase = amount_btc.min(&requestable_tokens)?;

        ensure!(!to_be_replaced_increase.is_zero(), Error::<T>::ReplaceAmountZero);

        // increase to-be-replaced tokens. This will fail if the vault does not have enough tokens available
        let total_to_be_replaced =
            ext::vault_registry::try_increase_to_be_replaced_tokens::<T>(&vault_id, &to_be_replaced_increase)?;

        // check that total-to-be-replaced is above the minimum
        ensure!(
            total_to_be_replaced.ge(&Self::dust_value(vault_id.wrapped_currency()))?,
            Error::<T>::AmountBelowDustAmount
        );

        // get the griefing collateral increase
        let griefing_collateral = ext::fee::get_replace_griefing_collateral::<T>(
            &to_be_replaced_increase.convert_to(T::GetGriefingCollateralCurrencyId::get())?,
        )?;

        // Lock the oldVault’s griefing collateral
        ext::vault_registry::transfer_funds(
            CurrencySource::FreeBalance(vault_id.account_id.clone()),
            CurrencySource::AvailableReplaceCollateral(vault_id.clone()),
            &griefing_collateral,
        )?;

        // Emit RequestReplace event
        Self::deposit_event(Event::<T>::RequestReplace {
            old_vault_id: vault_id,
            amount: to_be_replaced_increase.amount(),
            griefing_collateral: griefing_collateral.amount(),
        });
        Ok(())
    }

    fn _withdraw_replace_request(vault_id: DefaultVaultId<T>, amount: BalanceOf<T>) -> Result<(), DispatchError> {
        let amount = Amount::new(amount, vault_id.wrapped_currency());
        // decrease to-be-replaced tokens, so that the vault is free to use its issued tokens again.
        let (withdrawn_tokens, to_withdraw_collateral) =
            ext::vault_registry::withdraw_replace_request::<T>(&vault_id, &amount)?;

        if withdrawn_tokens.is_zero() {
            return Err(Error::<T>::NoPendingRequest.into());
        }

        // Emit WithdrawReplaceRequest event.
        Self::deposit_event(Event::<T>::WithdrawReplace {
            old_vault_id: vault_id,
            withdrawn_tokens: withdrawn_tokens.amount(),
            withdrawn_griefing_collateral: to_withdraw_collateral.amount(),
        });
        Ok(())
    }

    fn accept_replace_tokens(
        old_vault_id: &DefaultVaultId<T>,
        new_vault_id: &DefaultVaultId<T>,
        redeemable_tokens: &Amount<T>,
    ) -> DispatchResult {
        // increase old-vault's to-be-redeemed tokens - this should never fail
        ext::vault_registry::try_increase_to_be_redeemed_tokens::<T>(old_vault_id, redeemable_tokens)?;

        // increase new-vault's to-be-issued tokens - this will fail if there is insufficient collateral
        ext::vault_registry::try_increase_to_be_issued_tokens::<T>(new_vault_id, redeemable_tokens)?;

        Ok(())
    }

    fn _accept_replace(
        old_vault_id: DefaultVaultId<T>,
        new_vault_id: DefaultVaultId<T>,
        amount_btc: BalanceOf<T>,
        collateral: BalanceOf<T>,
        btc_address: BtcAddress,
    ) -> Result<(), DispatchError> {
        let new_vault_currency_id = new_vault_id.collateral_currency();
        let amount_btc = Amount::new(amount_btc, old_vault_id.wrapped_currency());
        let collateral = Amount::new(collateral, new_vault_currency_id);

        // don't allow vaults to replace themselves
        ensure!(old_vault_id != new_vault_id, Error::<T>::ReplaceSelfNotAllowed);

        // probably this check is not strictly required, but it's better to give an
        // explicit error rather than insufficient balance
        ensure!(
            old_vault_id.wrapped_currency() == new_vault_id.wrapped_currency(),
            Error::<T>::InvalidWrappedCurrency
        );

        // Check that new vault is not currently banned
        ext::vault_registry::ensure_not_banned::<T>(&new_vault_id)?;

        // decrease old-vault's to-be-replaced tokens
        let (redeemable_tokens, griefing_collateral) =
            ext::vault_registry::decrease_to_be_replaced_tokens::<T>(&old_vault_id, &amount_btc)?;

        // check amount_btc is above the minimum
        ensure!(
            redeemable_tokens.ge(&Self::dust_value(old_vault_id.wrapped_currency()))?,
            Error::<T>::AmountBelowDustAmount
        );

        // Calculate and lock the new-vault's additional collateral
        let actual_new_vault_collateral =
            ext::vault_registry::calculate_collateral::<T>(&collateral, &redeemable_tokens, &amount_btc)?;

        ext::vault_registry::try_deposit_collateral::<T>(&new_vault_id, &actual_new_vault_collateral)?;

        Self::accept_replace_tokens(&old_vault_id, &new_vault_id, &redeemable_tokens)?;

        ext::vault_registry::transfer_funds(
            CurrencySource::AvailableReplaceCollateral(old_vault_id.clone()),
            CurrencySource::ActiveReplaceCollateral(old_vault_id.clone()),
            &griefing_collateral,
        )?;

        let replace_id = ext::security::get_secure_id::<T>(&old_vault_id.account_id);

        let replace = ReplaceRequest {
            old_vault: old_vault_id,
            new_vault: new_vault_id,
            accept_time: ext::security::active_block_number::<T>(),
            collateral: actual_new_vault_collateral.amount(),
            btc_address,
            griefing_collateral: griefing_collateral.amount(),
            amount: redeemable_tokens.amount(),
            period: Self::replace_period(),
            btc_height: ext::btc_relay::get_best_block_height::<T>(),
            status: ReplaceRequestStatus::Pending,
        };

        Self::insert_replace_request(&replace_id, &replace);

        // Emit AcceptReplace event
        Self::deposit_event(Event::<T>::AcceptReplace {
            replace_id: replace_id,
            old_vault_id: replace.old_vault,
            new_vault_id: replace.new_vault,
            amount: replace.amount,
            collateral: replace.collateral,
            btc_address: replace.btc_address,
        });

        Ok(())
    }

    fn _execute_replace(replace_id: H256, unchecked_transaction: FullTransactionProof) -> DispatchResult {
        // retrieve the replace request using the id parameter
        // we can still execute cancelled requests
        let replace = Self::get_open_or_cancelled_replace_request(&replace_id)?;

        let griefing_collateral: Amount<T> = replace.griefing_collateral();
        let amount = replace.amount();
        let collateral = replace.collateral()?;

        // NOTE: anyone can call this method provided the proof is correct
        let new_vault_id = replace.new_vault;
        let old_vault_id = replace.old_vault;

        // check the transaction inclusion and validity
        ext::btc_relay::verify_and_validate_op_return_transaction::<T, _>(
            unchecked_transaction,
            replace.btc_address,
            replace.amount,
            replace_id,
        )?;

        // only return griefing collateral if not already slashed
        let collateral = match replace.status {
            ReplaceRequestStatus::Pending => {
                // give old-vault the griefing collateral
                ext::vault_registry::transfer_funds(
                    CurrencySource::ActiveReplaceCollateral(old_vault_id.clone()),
                    CurrencySource::FreeBalance(old_vault_id.account_id.clone()),
                    &griefing_collateral,
                )?;
                // NOTE: this is just the additional collateral already locked on accept
                // it is only used in the ReplaceTokens event
                collateral
            }
            ReplaceRequestStatus::Cancelled => {
                // we need to re-accept first, this will check that the vault is over the secure threshold
                Self::accept_replace_tokens(&old_vault_id, &new_vault_id, &amount)?;
                // no additional collateral locked for this
                Amount::zero(collateral.currency())
            }
            ReplaceRequestStatus::Completed => {
                // we never enter this branch as completed requests are filtered
                return Err(Error::<T>::ReplaceCompleted.into());
            }
        };

        // decrease old-vault's issued & to-be-redeemed tokens, and
        // change new-vault's to-be-issued tokens to issued tokens
        ext::vault_registry::replace_tokens::<T>(&old_vault_id, &new_vault_id, &amount, &collateral)?;

        // Emit ExecuteReplace event.
        Self::deposit_event(Event::<T>::ExecuteReplace {
            replace_id: replace_id,
            old_vault_id: old_vault_id,
            new_vault_id: new_vault_id,
        });

        // Remove replace request
        Self::set_replace_status(&replace_id, ReplaceRequestStatus::Completed);
        Ok(())
    }

    fn _cancel_replace(replace_id: H256) -> Result<(), DispatchError> {
        // Retrieve the ReplaceRequest as per the replaceId parameter from Vaults in the VaultRegistry
        let replace = Self::get_open_replace_request(&replace_id)?;

        let griefing_collateral: Amount<T> = replace.griefing_collateral();
        let amount = replace.amount();
        let collateral = replace.collateral()?;

        // only cancellable after the request has expired
        ensure!(
            ext::btc_relay::has_request_expired::<T>(
                replace.accept_time,
                replace.btc_height,
                Self::replace_period().max(replace.period)
            )?,
            Error::<T>::ReplacePeriodNotExpired
        );

        let new_vault_id = replace.new_vault;

        // decrease old-vault's to-be-redeemed tokens, and
        // decrease new-vault's to-be-issued tokens
        ext::vault_registry::cancel_replace_tokens::<T>(&replace.old_vault, &new_vault_id, &amount)?;

        // slash old-vault's griefing collateral
        ext::vault_registry::transfer_funds::<T>(
            CurrencySource::ActiveReplaceCollateral(replace.old_vault.clone()),
            CurrencySource::FreeBalance(new_vault_id.account_id.clone()),
            &griefing_collateral,
        )?;

        // if the new_vault locked additional collateral especially for this replace,
        // release it if it does not cause them to be undercollateralized
        if !ext::vault_registry::is_vault_liquidated::<T>(&new_vault_id)?
            && ext::vault_registry::is_allowed_to_withdraw_collateral::<T>(&new_vault_id, Some(collateral.clone()))?
        {
            ext::vault_registry::force_withdraw_collateral::<T>(&new_vault_id, &collateral)?;
        }

        // Remove the ReplaceRequest from ReplaceRequests
        Self::set_replace_status(&replace_id, ReplaceRequestStatus::Cancelled);

        // Emit CancelReplace event.
        Self::deposit_event(Event::<T>::CancelReplace {
            replace_id: replace_id,
            new_vault_id: new_vault_id,
            old_vault_id: replace.old_vault,
            griefing_collateral: replace.griefing_collateral,
        });
        Ok(())
    }

    /// Fetch all replace requests from the specified vault.
    ///
    /// # Arguments
    ///
    /// * `account_id` - user account id
    pub fn get_replace_requests_for_old_vault(vault_id: T::AccountId) -> Vec<H256> {
        <ReplaceRequests<T>>::iter()
            .filter(|(_, request)| request.old_vault.account_id == vault_id)
            .map(|(key, _)| key)
            .collect::<Vec<_>>()
    }

    /// Fetch all replace requests to the specified vault.
    ///
    /// # Arguments
    ///
    /// * `account_id` - user account id
    pub fn get_replace_requests_for_new_vault(vault_id: T::AccountId) -> Vec<H256> {
        <ReplaceRequests<T>>::iter()
            .filter(|(_, request)| request.new_vault.account_id == vault_id)
            .map(|(key, _)| key)
            .collect::<Vec<_>>()
    }

    /// Get a replace request by id. Completed or cancelled requests are not returned.
    pub fn get_open_replace_request(replace_id: &H256) -> Result<DefaultReplaceRequest<T>, DispatchError> {
        let request = ReplaceRequests::<T>::try_get(replace_id).or(Err(Error::<T>::ReplaceIdNotFound))?;

        // NOTE: temporary workaround until we delete
        match request.status {
            ReplaceRequestStatus::Pending => Ok(request),
            ReplaceRequestStatus::Completed => Err(Error::<T>::ReplaceCompleted.into()),
            ReplaceRequestStatus::Cancelled => Err(Error::<T>::ReplaceCancelled.into()),
        }
    }

    /// Get a open or completed replace request by id. Cancelled requests are not returned.
    pub fn get_open_or_completed_replace_request(id: &H256) -> Result<DefaultReplaceRequest<T>, DispatchError> {
        let request = <ReplaceRequests<T>>::get(id).ok_or(Error::<T>::ReplaceIdNotFound)?;
        match request.status {
            ReplaceRequestStatus::Pending | ReplaceRequestStatus::Completed => Ok(request),
            ReplaceRequestStatus::Cancelled => Err(Error::<T>::ReplaceCancelled.into()),
        }
    }

    /// Get a open or cancelled replace request by id. Completed requests are not returned.
    pub fn get_open_or_cancelled_replace_request(id: &H256) -> Result<DefaultReplaceRequest<T>, DispatchError> {
        let request = <ReplaceRequests<T>>::get(id).ok_or(Error::<T>::ReplaceIdNotFound)?;
        match request.status {
            ReplaceRequestStatus::Pending | ReplaceRequestStatus::Cancelled => Ok(request),
            ReplaceRequestStatus::Completed => Err(Error::<T>::ReplaceCompleted.into()),
        }
    }

    fn insert_replace_request(key: &H256, value: &DefaultReplaceRequest<T>) {
        <ReplaceRequests<T>>::insert(key, value)
    }

    fn set_replace_status(key: &H256, status: ReplaceRequestStatus) {
        <ReplaceRequests<T>>::mutate_exists(key, |request| {
            *request = request.clone().map(|request| DefaultReplaceRequest::<T> {
                status: status.clone(),
                ..request
            });
        });
    }

    pub fn dust_value(currency_id: CurrencyId<T>) -> Amount<T> {
        Amount::new(ReplaceBtcDustValue::<T>::get(), currency_id)
    }
}