-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTestCollateralLiquidatorJig.sol
137 lines (112 loc) · 4.58 KB
/
TestCollateralLiquidatorJig.sol
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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import "@openzeppelin/contracts/proxy/Clones.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import "../LoanReceipt.sol";
import "../interfaces/ICollateralLiquidator.sol";
import "../interfaces/ICollateralLiquidationReceiver.sol";
/**
* @title Testing Jig for Collateral Liquidators
* @author MetaStreet Labs
*/
contract TestCollateralLiquidatorJig is ERC165, ERC721Holder, ICollateralLiquidationReceiver {
using SafeERC20 for IERC20;
/**************************************************************************/
/* Errors */
/**************************************************************************/
/**
* @notice Force a revert
*/
error ForceRevert();
/**************************************************************************/
/* Events */
/**************************************************************************/
/**
* @notice Emitted when loan collateral is liquidated
* @param proceeds Liquidation proceeds in currency tokens
*/
event CollateralLiquidated(uint256 proceeds);
/**************************************************************************/
/* State */
/**************************************************************************/
/**
* @dev Currency token
*/
IERC20 private _currencyToken;
/**
* @dev Collateral liquidator instance
*/
address private _collateralLiquidator;
/**************************************************************************/
/* Constructor */
/**************************************************************************/
/**
* @notice TestLiquidator
*/
constructor(IERC20 currencyToken_, address collateralLiquidator_) {
_currencyToken = currencyToken_;
_collateralLiquidator = collateralLiquidator_;
}
/**************************************************************************/
/* Getters */
/**************************************************************************/
/**
* @notice Get currency token
* @return Currency token contract
*/
function currencyToken() external view returns (address) {
return address(_currencyToken);
}
/**
* @notice Get collateral liquidator
* @return Collateral liquidator contract
*/
function collateralLiquidator() external view returns (address) {
return address(_collateralLiquidator);
}
/**************************************************************************/
/* Methods */
/**************************************************************************/
/**
* @notice Liquidate collateral with liquidator
* @param encodedLoanReceipt Encoded loan receipt
*/
function liquidate(bytes calldata encodedLoanReceipt) external {
LoanReceipt.LoanReceiptV2 memory loanReceipt = LoanReceipt.decode(encodedLoanReceipt);
IERC721(loanReceipt.collateralToken).approve(_collateralLiquidator, loanReceipt.collateralTokenId);
/* Start liquidation with collateral liquidator */
ICollateralLiquidator(_collateralLiquidator).liquidate(
address(_currencyToken),
loanReceipt.collateralToken,
loanReceipt.collateralTokenId,
loanReceipt.collateralWrapperContext,
encodedLoanReceipt
);
}
/**
* @notice Callback on loan collateral liquidated
* @param loanReceipt Loan receipt
* @param proceeds Liquidation proceeds in currency tokens
*/
function onCollateralLiquidated(bytes calldata loanReceipt, uint256 proceeds) external {
LoanReceipt.LoanReceiptV2 memory decodedLoanReceipt = LoanReceipt.decode(loanReceipt);
/* Force a revert to test try...catch in English Auction */
if (decodedLoanReceipt.collateralTokenId == 130) {
revert ForceRevert();
}
emit CollateralLiquidated(proceeds);
}
/******************************************************/
/* ERC165 interface */
/******************************************************/
/**
* @inheritdoc IERC165
*/
function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
return interfaceId == type(ICollateralLiquidationReceiver).interfaceId || super.supportsInterface(interfaceId);
}
}