-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCommons.sol
203 lines (173 loc) · 7.45 KB
/
TestCommons.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
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.18;
// solhint-disable no-console
import {console} from "forge-std/console.sol";
import {RayMath} from "../../src/utils/RayMath.sol";
import {BorrowArg, CollateralState, NFToken, Offer, OfferArg} from "../../src/DataStructure/Objects.sol";
import {getSelector} from "../../src/utils/FuncSelectors.h.sol";
import {Loan, Payment, Provision, Auction} from "../../src/DataStructure/Storage.sol";
import {Loggers} from "./Loggers.sol";
import {Money} from "../../src/mock/Money.sol";
import {NFT} from "../../src/mock/NFT.sol";
import {ONE} from "../../src/DataStructure/Global.sol";
import {Ray} from "../../src/DataStructure/Objects.sol";
abstract contract TestCommons is Loggers {
using RayMath for Ray;
Ray internal immutable apr40percent;
error AssertionFailedLoanDontMatch();
error AssertionFailedRayDontMatch(Ray expected, Ray actual);
error AssertionFailedCollatStateDontMatch();
uint256[] internal oneInArray;
uint256[] internal emptyArray;
bytes internal emptyBytes;
uint256 internal constant KEY = 0xA11CE;
uint256 internal constant KEY2 = 0xB0B;
address internal immutable signer;
address internal immutable signer2;
address internal constant BORROWER = address(bytes20(keccak256("borrower")));
address internal constant OWNER = address(bytes20(keccak256("owner")));
bytes4 internal immutable erc721SafeTransferFromSelector;
bytes4 internal immutable erc721SafeTransferFromDataSelector;
Money internal money;
Money internal money2;
NFT internal nft;
NFT internal nft2;
constructor() {
apr40percent = ONE.div(10).mul(4).div(365 days);
oneInArray = new uint256[](1);
oneInArray[0] = 1;
signer = vm.addr(KEY);
signer2 = vm.addr(KEY2);
vm.label(BORROWER, "borrower");
vm.label(OWNER, "owner");
vm.label(signer, "signer");
vm.label(signer2, "signer2");
erc721SafeTransferFromSelector = getSelector("safeTransferFrom(address,address,uint256)");
erc721SafeTransferFromDataSelector = getSelector("safeTransferFrom(address,address,uint256,bytes)");
if (block.timestamp < 365 days) {
vm.warp(365 days);
}
}
function getOfferDigest(Offer memory offer) internal virtual returns (bytes32);
function getSignatureFromKey(Offer memory offer, uint256 pKey) internal returns (bytes memory signature) {
bytes32 digest = getOfferDigest(offer);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(pKey, digest);
signature = bytes.concat(r, s, bytes1(v));
}
function getSignature(Offer memory offer) internal returns (bytes memory signature) {
return getSignatureFromKey(offer, KEY);
}
function getSignature2(Offer memory offer) internal returns (bytes memory signature) {
return getSignatureFromKey(offer, KEY2);
}
function getCollateralState() internal view returns (CollateralState memory state) {
return
CollateralState({
matched: Ray.wrap(0),
assetLent: getOffer().assetToLend,
tranche: 0,
minOfferDuration: getOffer().duration,
minOfferLoanToValue: getOffer().loanToValue,
maxOfferLoanToValue: getOffer().loanToValue,
from: BORROWER,
nft: getOffer().collateral,
loanId: 1
});
}
function getOfferArg(Offer memory offer) internal returns (OfferArg memory arg) {
arg = OfferArg({signature: getSignature(offer), amount: offer.loanToValue / 10, offer: offer});
}
function getOfferArg() internal returns (OfferArg memory) {
return getOfferArg(getOffer());
}
function getOfferArgs() internal returns (OfferArg[] memory) {
return getOfferArgs(getOffer());
}
function getOfferArgs(Offer memory offer) internal returns (OfferArg[] memory) {
OfferArg[] memory ret = new OfferArg[](1);
ret[0] = getOfferArg(offer);
return ret;
}
function getBorrowArgs(OfferArg[] memory offerArgs) internal view returns (BorrowArg[] memory) {
BorrowArg[] memory args = new BorrowArg[](1);
args[0] = BorrowArg({nft: getNft(), args: offerArgs});
return args;
}
/// @notice to modify the default offer, but not the collateral
function getBorrowArgs(Offer memory offer) internal returns (BorrowArg[] memory) {
return getBorrowArgs(getOfferArgs(offer));
}
function getBorrowArgs() internal returns (BorrowArg[] memory) {
return getBorrowArgs(getOffer());
}
function getNft() internal view returns (NFToken memory ret) {
ret = NFToken({implem: nft, id: 1});
}
function getTranche(uint256 trancheId) internal view virtual returns (Ray rate);
function getAuction() internal pure returns (Auction memory) {
return Auction({duration: 3 days, priceFactor: ONE.mul(3)});
}
/// @dev created as override helper, to modify only few elements in loan
function _getLoan() internal view returns (Loan memory) {
Payment memory payment;
return
Loan({
assetLent: getOffer().assetToLend,
lent: 1 ether,
shareLent: ONE,
startDate: block.timestamp - 2 weeks,
endDate: block.timestamp + 2 weeks,
auction: getAuction(),
interestPerSecond: getTranche(0),
borrower: BORROWER,
collateral: getOffer().collateral,
supplyPositionIndex: 1,
payment: payment,
nbOfPositions: 1
});
}
function getLoan() internal view virtual returns (Loan memory) {
return _getLoan();
}
function assertEq(Loan memory actual, Loan memory expected, string memory message) internal view {
if (keccak256(abi.encode(actual)) != keccak256(abi.encode(expected))) {
if (bytes(message).length > 0) {
console.log("error: %s", message);
}
logLoan(expected, "expected");
logLoan(actual, "actual ");
revert AssertionFailedLoanDontMatch();
}
}
function assertEq(Loan memory actual, Loan memory expected) internal view {
assertEq(actual, expected, "");
}
function assertEq(CollateralState memory actual, CollateralState memory expected) internal view {
if (keccak256(abi.encode(actual)) != keccak256(abi.encode(expected))) {
logCollateralState(expected, "expected");
logCollateralState(actual, "actual ");
revert AssertionFailedCollatStateDontMatch();
}
}
function getOffer() internal view returns (Offer memory) {
return
Offer({
assetToLend: money,
loanToValue: 10 ether,
duration: 2 weeks,
expirationDate: block.timestamp + 2 hours,
tranche: 0,
collateral: getNft()
});
}
function assertEq(Ray actual, Ray expected) internal view {
if (Ray.unwrap(actual) != Ray.unwrap(expected)) {
console.log("expected ", Ray.unwrap(expected));
console.log("actual ", Ray.unwrap(actual));
revert AssertionFailedRayDontMatch(expected, actual);
}
}
function getProvision() internal pure returns (Provision memory) {
return Provision({amount: 1 ether, share: ONE, loanId: 1});
}
}