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
#![cfg_attr(not(feature = "std"), no_std)]
use core::marker::PhantomData;

use currency::Amount;
use frame_support::{
    pallet_prelude::Get,
    traits::{Currency, OnTimestampSet, OnUnbalanced, ProcessMessageError, TryDrop},
};
use primitives::{BlockNumber, UnsignedFixedPoint};
use sp_runtime::{DispatchError, FixedPointNumber};
use sp_std::prelude::*;
use xcm_executor::traits::Properties;

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

pub mod evm;

// The relay chain is limited to 12s to include parachain blocks.
pub const MILLISECS_PER_BLOCK: u64 = 12000;
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;
pub const WEEKS: BlockNumber = DAYS * 7;
pub const YEARS: BlockNumber = DAYS * 365;

pub type AccountId<T> = <T as frame_system::Config>::AccountId;
pub type VaultId<T> = primitives::VaultId<AccountId<T>, currency::CurrencyId<T>>;
pub use currency::CurrencyId;
use primitives::{Balance, Nonce};
use xcm::latest::{Instruction, MultiLocation, Weight};
use xcm_executor::traits::ShouldExecute;

fn native_currency_id<T: currency::Config>() -> CurrencyId<T> {
    T::GetNativeCurrencyId::get()
}

pub fn estimate_escrow_reward_rate<T, EscrowAnnuityInstance, EscrowRewardsApi, EscrowCurrency>(
    account_id: AccountId<T>,
    amount: Option<Balance>,
    lock_time: Option<BlockNumber>,
) -> Result<UnsignedFixedPoint, DispatchError>
where
    T: currency::Config
        + escrow::Config<Currency = EscrowCurrency>
        + annuity::Config<EscrowAnnuityInstance, Currency = EscrowCurrency>,
    EscrowAnnuityInstance: 'static,
    EscrowRewardsApi: reward::RewardsApi<(), AccountId<T>, Balance, CurrencyId = CurrencyId<T>>,
    EscrowCurrency: Currency<<T as frame_system::Config>::AccountId, Balance = Balance>,
{
    let native_currency = native_currency_id::<T>();
    // withdraw previous rewards
    EscrowRewardsApi::withdraw_reward(&(), &account_id, native_currency)?;
    // increase amount and/or lock_time
    escrow::Pallet::<T>::round_height_and_deposit_for(
        &account_id,
        amount.unwrap_or_default(),
        lock_time.unwrap_or_default().into(),
    )?;
    // distribute rewards accrued over block count
    let reward = annuity::Pallet::<T, EscrowAnnuityInstance>::min_reward_per_block().saturating_mul(YEARS.into());
    EscrowRewardsApi::distribute_reward(&(), native_currency, reward)?;
    let received = EscrowRewardsApi::compute_reward(&(), &account_id, native_currency)?;
    // NOTE: total_locked is same currency as rewards
    let total_locked = escrow::Pallet::<T>::locked_balance(&account_id).amount;
    // rate is received / total_locked
    Ok(UnsignedFixedPoint::checked_from_rational(received, total_locked).unwrap_or_default())
}

pub fn estimate_vault_reward_rate<T, VaultAnnuityInstance, VaultStakingApi, VaultCapacityApi, VaultAnnuityCurrency>(
    vault_id: VaultId<T>,
) -> Result<UnsignedFixedPoint, DispatchError>
where
    T: oracle::Config
        + currency::Config<UnsignedFixedPoint = UnsignedFixedPoint, Balance = Balance>
        + fee::Config<UnsignedFixedPoint = UnsignedFixedPoint>
        + annuity::Config<VaultAnnuityInstance, Currency = VaultAnnuityCurrency>,
    VaultStakingApi: reward::RewardsApi<(Option<Nonce>, VaultId<T>), AccountId<T>, Balance, CurrencyId = CurrencyId<T>>,
    VaultCapacityApi: reward::RewardsApi<(), CurrencyId<T>, Balance, CurrencyId = CurrencyId<T>>,
    VaultAnnuityInstance: 'static,
    VaultAnnuityCurrency: Currency<<T as frame_system::Config>::AccountId, Balance = Balance>,
{
    // distribute and withdraw previous rewards
    let native_currency = native_currency_id::<T>();
    fee::Pallet::<T>::distribute_vault_rewards(&vault_id, native_currency)?;
    // distribute rewards accrued over block count
    VaultStakingApi::withdraw_reward(&(None, vault_id.clone()), &vault_id.account_id, native_currency)?;
    let reward = annuity::Pallet::<T, VaultAnnuityInstance>::min_reward_per_block().saturating_mul(YEARS.into());
    VaultCapacityApi::distribute_reward(&(), native_currency, reward)?;
    Amount::<T>::new(reward, native_currency).mint_to(&fee::Pallet::<T>::fee_pool_account_id())?;
    // calculate the collateral
    let collateral = VaultStakingApi::get_stake(&(None, vault_id.clone()), &vault_id.account_id)?;
    // compute and convert rewards to the same currency
    let received = fee::Pallet::<T>::compute_vault_rewards(&vault_id, &vault_id.account_id, native_currency)?;
    let received_value = received.convert_to(vault_id.collateral_currency())?;
    Ok(UnsignedFixedPoint::checked_from_rational(received_value.amount(), collateral).unwrap_or_default())
}

pub struct AndBarrier<T: ShouldExecute, U: ShouldExecute>(PhantomData<(T, U)>);
impl<T: ShouldExecute, U: ShouldExecute> ShouldExecute for AndBarrier<T, U> {
    fn should_execute<Call>(
        origin: &MultiLocation,
        instructions: &mut [Instruction<Call>],
        max_weight: Weight,
        properties: &mut Properties,
    ) -> Result<(), ProcessMessageError> {
        T::should_execute(origin, instructions, max_weight, properties)?;
        U::should_execute(origin, instructions, max_weight, properties)?;
        // only if both returned ok, we return ok
        Ok(())
    }
}

pub struct Transactless<T: ShouldExecute>(PhantomData<T>);

impl<T: ShouldExecute> ShouldExecute for Transactless<T> {
    fn should_execute<Call>(
        origin: &MultiLocation,
        instructions: &mut [Instruction<Call>],
        max_weight: Weight,
        properties: &mut Properties,
    ) -> Result<(), ProcessMessageError> {
        // filter any outer-level Transacts. Any Transact calls sent to other chain should still work.
        let has_transact = instructions.iter().any(|x| matches!(x, Instruction::Transact { .. }));
        if has_transact {
            return Err(ProcessMessageError::Unsupported);
        }
        // No transact - return result of the wrapped barrier
        T::should_execute(origin, instructions, max_weight, properties)
    }
}

pub struct ToTreasury<T, TreasuryAccount, NativeCurrency>(PhantomData<(T, TreasuryAccount, NativeCurrency)>);

impl<T, TreasuryAccount, NativeCurrency, NegImbalance> OnUnbalanced<NegImbalance>
    for ToTreasury<T, TreasuryAccount, NativeCurrency>
where
    T: frame_system::Config,
    NativeCurrency: Currency<T::AccountId, NegativeImbalance = NegImbalance>,
    NegImbalance: TryDrop,
    TreasuryAccount: Get<T::AccountId>,
{
    fn on_nonzero_unbalanced(amount: NegImbalance) {
        // Must resolve into existing but better to be safe.
        let _ = NativeCurrency::resolve_creating(&TreasuryAccount::get(), amount);
    }
}

pub struct ConsensusOnTimestampSet<T, EnableManualSeal>(PhantomData<(T, EnableManualSeal)>);
impl<T: pallet_aura::Config, EnableManualSeal: Get<bool>> OnTimestampSet<T::Moment>
    for ConsensusOnTimestampSet<T, EnableManualSeal>
{
    fn on_timestamp_set(moment: T::Moment) {
        if EnableManualSeal::get() {
            return;
        }
        <pallet_aura::Pallet<T> as OnTimestampSet<T::Moment>>::on_timestamp_set(moment)
    }
}