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
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
// This file is part of Centrifuge chain project.

// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).

// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

use cfg_traits::{investments::ForeignInvestmentHooks, liquidity_pools::OutboundMessageHandler};
use cfg_types::domain_address::Domain;
use frame_support::traits::{
	fungibles::Mutate,
	tokens::{Fortitude, Precision, Preservation},
};
use sp_core::Get;
use sp_runtime::DispatchResult;

use crate::{pallet::Config, Message, Pallet};

impl<T: Config> ForeignInvestmentHooks<T::AccountId> for Pallet<T> {
	type Amount = T::Balance;
	type CurrencyId = T::CurrencyId;
	type InvestmentId = (T::PoolId, T::TrancheId);
	type TrancheAmount = T::Balance;

	fn fulfill_cancel_investment(
		who: &T::AccountId,
		(pool_id, tranche_id): (T::PoolId, T::TrancheId),
		currency_id: Self::CurrencyId,
		amount_cancelled: Self::Amount,
		fulfilled: Self::Amount,
	) -> DispatchResult {
		let currency = Pallet::<T>::try_get_general_index(currency_id)?;
		let (chain_id, ..) = Pallet::<T>::try_get_wrapped_token(&currency_id)?;
		let domain = Domain::Evm(chain_id);

		T::Tokens::burn_from(
			currency_id,
			who,
			amount_cancelled,
			Precision::Exact,
			Fortitude::Polite,
		)?;

		let message = Message::FulfilledCancelDepositRequest {
			pool_id: pool_id.into(),
			tranche_id: tranche_id.into(),
			investor: who.clone().into(),
			currency,
			currency_payout: amount_cancelled.into(),
			fulfilled_invest_amount: fulfilled.into(),
		};

		T::OutboundMessageHandler::handle(T::TreasuryAccount::get(), domain, message)
	}

	fn fulfill_collect_investment(
		who: &T::AccountId,
		(pool_id, tranche_id): (T::PoolId, T::TrancheId),
		currency_id: Self::CurrencyId,
		amount_collected: Self::Amount,
		tranche_tokens_payout: Self::TrancheAmount,
	) -> DispatchResult {
		let currency = Pallet::<T>::try_get_general_index(currency_id)?;
		let (chain_id, ..) = Pallet::<T>::try_get_wrapped_token(&currency_id)?;
		let domain = Domain::Evm(chain_id);

		T::Tokens::transfer(
			(pool_id, tranche_id).into(),
			who,
			&domain.into_account(),
			tranche_tokens_payout,
			Preservation::Expendable,
		)?;

		let message = Message::FulfilledDepositRequest {
			pool_id: pool_id.into(),
			tranche_id: tranche_id.into(),
			investor: who.clone().into(),
			currency,
			currency_payout: amount_collected.into(),
			tranche_tokens_payout: tranche_tokens_payout.into(),
		};

		T::OutboundMessageHandler::handle(T::TreasuryAccount::get(), domain, message)
	}

	fn fulfill_collect_redemption(
		who: &T::AccountId,
		(pool_id, tranche_id): (T::PoolId, T::TrancheId),
		currency_id: Self::CurrencyId,
		tranche_tokens_collected: Self::TrancheAmount,
		amount_payout: Self::Amount,
	) -> DispatchResult {
		let currency = Pallet::<T>::try_get_general_index(currency_id)?;
		let (chain_id, ..) = Pallet::<T>::try_get_wrapped_token(&currency_id)?;
		let domain = Domain::Evm(chain_id);

		T::Tokens::burn_from(
			currency_id,
			who,
			amount_payout,
			Precision::Exact,
			Fortitude::Polite,
		)?;

		let message = Message::FulfilledRedeemRequest {
			pool_id: pool_id.into(),
			tranche_id: tranche_id.into(),
			investor: who.clone().into(),
			currency,
			currency_payout: amount_payout.into(),
			tranche_tokens_payout: tranche_tokens_collected.into(),
		};

		T::OutboundMessageHandler::handle(T::TreasuryAccount::get(), domain, message)
	}
}