-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReedemCollateralScript.s.sol
More file actions
243 lines (217 loc) · 10 KB
/
Copy pathReedemCollateralScript.s.sol
File metadata and controls
243 lines (217 loc) · 10 KB
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import {Test, console2} from "../../lib/forge-std/src/Test.sol";
import {TroveManager} from "../../contracts/core/TroveManager.sol";
import {DebtToken} from "../../contracts/core/DebtToken.sol";
import {MultiCollateralHintHelpers} from "../../contracts/core/helpers/MultiCollateralHintHelpers.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract RedeemUSBDTest is Test {
// Contract interfaces
TroveManager public troveManager;
DebtToken public debtToken;
MultiCollateralHintHelpers public hintHelpers;
// Constants - Fill these with your actual addresses
address public constant TROVE_MANAGER_ADDRESS =
0x55796b02a7650C9fEF78bC2E73De1EF6dA719283;
address public constant USBD_ADDRESS =
0x165d03D3Df6443B87b4B7a6268fd13d37C5e3127;
address public constant HINT_HELPER_ADDRESS =
0xfbb5dc670379677f1e79BA25e48aC974ad71D76a;
// Test configuration
uint256 public constant LST_PRICE = 119272.24 ether; // LST price in wei format
uint256 public constant DEBT_AMOUNT = 80 ether; // Amount to redeem in wei format
address public userAddress; // Will be set to msg.sender
function setUp() public {
// Connect to deployed contracts
troveManager = TroveManager(TROVE_MANAGER_ADDRESS);
debtToken = DebtToken(USBD_ADDRESS);
hintHelpers = MultiCollateralHintHelpers(HINT_HELPER_ADDRESS);
// Set user address to the specific address provided
userAddress = 0x39d2770AbcC456f6C6be820705eD966592E0ad96;
// Get RPC URL and API key for the network
string memory RPC_URL = vm.envString("BITLAYER_TESTNET_RPC_URL");
// Fork from Bitlayer testnet
vm.createSelectFork(RPC_URL);
// pretend to be user
vm.startPrank(userAddress);
// Print connected addresses for verification
console2.log("TroveManager address:", address(troveManager));
console2.log("DebtToken address:", address(debtToken));
console2.log("HintHelpers address:", address(hintHelpers));
console2.log("Impersonating user:", userAddress);
}
function testRedeemUSBD() public {
// Log starting balances
console2.log("Starting test with LST price:", LST_PRICE / 1e18);
console2.log("Amount to redeem:", DEBT_AMOUNT / 1e18, "USBD");
console2.log("User address:", userAddress);
uint256 initialBalance = debtToken.balanceOf(userAddress);
console2.log("Initial USBD balance:", initialBalance / 1e18);
// Check if user has deposited amount (stake in trove)
uint256 depositedAmount = troveManager.getTroveStake(userAddress);
console2.log("Deposited amount in trove:", depositedAmount / 1e18);
// Ensure we have enough USBD tokens
if (debtToken.balanceOf(userAddress) < DEBT_AMOUNT) {
console2.log(
"Insufficient USBD balance. Current:",
debtToken.balanceOf(userAddress) / 1e18
);
console2.log("Needed:", DEBT_AMOUNT / 1e18);
return;
}
// Note: No approval needed - redeemCollateral burns tokens directly from caller
if (depositedAmount > 0) {
// User has stake in trove - proceed with direct redemption
console2.log(
"User has stake in trove, proceeding with direct redemption..."
);
try
hintHelpers.getRedemptionHints(
troveManager, // Pass the contract, not address
DEBT_AMOUNT,
LST_PRICE,
0
)
returns (
address firstRedemptionHint,
uint256 partialRedemptionHintNICR,
uint256 truncatedUSBDamount
) {
console2.log(
"Redemption hints - FirstRedemptionHint:",
firstRedemptionHint
);
console2.log(
"Redemption hints - PartialRedemptionHintNICR:",
partialRedemptionHintNICR
);
console2.log(
"Truncated USBD amount:",
truncatedUSBDamount / 1e18
);
// Execute redemption
try
troveManager.redeemCollateral(
DEBT_AMOUNT,
firstRedemptionHint,
address(0), // upperPartialRedemptionHint
address(0), // lowerPartialRedemptionHint
partialRedemptionHintNICR,
0, // maxIterations (0 = unlimited)
1 ether // maxFeePercentage (1 ether = 100%)
)
{
console2.log("Redemption successful!");
// Check final balance
uint256 finalBalance = debtToken.balanceOf(userAddress);
console2.log("Final USBD balance:", finalBalance / 1e18);
console2.log(
"USBD redeemed:",
(initialBalance - finalBalance) / 1e18
);
} catch Error(string memory reason) {
console2.log("Redemption failed with reason:", reason);
} catch (bytes memory lowLevelData) {
console2.log("Redemption failed with low-level error");
console2.logBytes(lowLevelData);
}
} catch Error(string memory reason) {
console2.log("GetRedemptionHints failed with reason:", reason);
} catch (bytes memory lowLevelData) {
console2.log("GetRedemptionHints failed with low-level error");
console2.logBytes(lowLevelData);
}
} else {
// User doesn't have stake - handle fee transfer first
console2.log(
"User has no stake in trove, handling fee transfer..."
);
uint256 feePercentage = 29; // 29% fee
uint256 feeAmount = (DEBT_AMOUNT * feePercentage) / 100;
console2.log("Fee amount:", feeAmount / 1e18);
// Get fee receiver (owner)
address feeReceiver = troveManager.owner();
console2.log("Fee receiver address:", feeReceiver);
// Transfer fee to receiver
try debtToken.transfer(feeReceiver, feeAmount) {
console2.log("Fee transfer successful");
// Calculate remaining debt after fee
uint256 remainingDebtValue = DEBT_AMOUNT - feeAmount;
console2.log(
"Remaining debt value to redeem:",
remainingDebtValue / 1e18
);
// Get redemption hints for remaining debt
try
hintHelpers.getRedemptionHints(
troveManager, // Pass the contract, not address
remainingDebtValue,
LST_PRICE,
0
)
returns (
address firstRedemptionHint,
uint256 partialRedemptionHintNICR,
uint256 truncatedUSBDamount
) {
console2.log(
"Redemption hints - FirstRedemptionHint:",
firstRedemptionHint
);
console2.log(
"Redemption hints - PartialRedemptionHintNICR:",
partialRedemptionHintNICR
);
console2.log(
"Truncated USBD amount:",
truncatedUSBDamount / 1e18
);
// Execute redemption for remaining debt
try
troveManager.redeemCollateral(
remainingDebtValue,
firstRedemptionHint,
address(0), // upperPartialRedemptionHint
address(0), // lowerPartialRedemptionHint
partialRedemptionHintNICR,
0, // maxIterations (0 = unlimited)
1 ether // maxFeePercentage (1 ether = 100%)
)
{
console2.log("Redemption successful!");
// Check final balance
uint256 finalBalance = debtToken.balanceOf(userAddress);
console2.log(
"Final USBD balance:",
finalBalance / 1e18
);
console2.log(
"USBD redeemed:",
(initialBalance - finalBalance) / 1e18
);
} catch Error(string memory reason) {
console2.log("Redemption failed with reason:", reason);
} catch (bytes memory lowLevelData) {
console2.log("Redemption failed with low-level error");
console2.logBytes(lowLevelData);
}
} catch Error(string memory reason) {
console2.log(
"GetRedemptionHints failed with reason:",
reason
);
} catch (bytes memory lowLevelData) {
console2.log(
"GetRedemptionHints failed with low-level error"
);
console2.logBytes(lowLevelData);
}
} catch Error(string memory reason) {
console2.log("Fee transfer failed with reason:", reason);
} catch (bytes memory lowLevelData) {
console2.log("Fee transfer failed with low-level error");
console2.logBytes(lowLevelData);
}
}
}
}