Skip to content

Commit 658ae23

Browse files
authored
test: migrate Cover integration tests to ethers v6 (#1465)
2 parents 48c355c + 8146046 commit 658ae23

27 files changed

+2987
-227
lines changed

contracts/mocks/tokens/ERC20Mock.sol

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,21 @@ contract ERC20Mock is ERC20 {
1717
}
1818

1919
function setMetadata(
20-
string memory name,
21-
string memory symbol,
22-
uint8 __decimals
20+
string memory tokenName,
21+
string memory tokenSymbol,
22+
uint8 tokenDecimals
2323
) public {
24-
_name = name;
25-
_symbol = symbol;
26-
_decimals = __decimals;
24+
_name = tokenName;
25+
_symbol = tokenSymbol;
26+
_decimals = tokenDecimals;
2727
}
2828

29-
function setName(string memory name) public {
30-
_name = name;
29+
function name() public view override returns (string memory) {
30+
return _name;
3131
}
3232

33-
function setSymbol(string memory symbol) public {
34-
_symbol = symbol;
35-
}
36-
37-
function setDecimals(uint8 __decimals) public {
38-
_decimals = __decimals;
33+
function symbol() public view override returns (string memory) {
34+
return _symbol;
3935
}
4036

4137
function decimals() public view override returns (uint8) {

contracts/modules/cover/Cover.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ contract Cover is ICover, EIP712, RegistryAware, ReentrancyGuard, Multicall {
246246
{
247247
uint nxmPriceInCoverAsset = pool.getInternalTokenPriceInAssetAndUpdateTwap(params.coverAsset);
248248
uint amountDueInNXM = _createCover(params, poolAllocationRequests, coverId, nxmPriceInCoverAsset);
249-
premiumInPaymentAsset = nxmPriceInCoverAsset * amountDueInNXM / ONE_NXM;
249+
premiumInPaymentAsset = params.coverAsset == params.paymentAsset
250+
? nxmPriceInCoverAsset * amountDueInNXM / ONE_NXM
251+
: amountDueInNXM;
250252
}
251253

252254
_retrievePayment(

lib/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,14 @@ const Assets = {
137137

138138
const PoolAsset = {
139139
ETH: 0,
140-
DAI: 1,
140+
DAI: 1, // deprecated
141141
stETH: 2,
142142
NXMTY: 3, // NXM Treasury Yield (Enzyme)
143143
rETH: 4,
144144
SafeTracker: 5,
145145
USDC: 6,
146146
cbBTC: 7,
147+
NXM: 255, // used as payment asset but technically not added to Pool assets
147148
};
148149

149150
const ContractTypes = {

lib/helpers.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,23 @@ const waitForInput = query => {
4444
};
4545

4646
const BigIntMath = {
47-
min: (a, b) => (a < b ? a : b),
48-
max: (a, b) => (a > b ? a : b),
47+
min: (a, b) => {
48+
const aBigInt = BigInt(a);
49+
const bBigInt = BigInt(b);
50+
return aBigInt < bBigInt ? aBigInt : bBigInt;
51+
},
52+
max: (a, b) => {
53+
const aBigInt = BigInt(a);
54+
const bBigInt = BigInt(b);
55+
return aBigInt > bBigInt ? aBigInt : bBigInt;
56+
},
57+
divCeil: (a, b) => (BigInt(a) + BigInt(b) - 1n) / BigInt(b),
58+
sum: arr => arr.reduce((x, y) => BigInt(x) + BigInt(y), 0n),
59+
/**
60+
* Round up to the nearest multiple of a given unit
61+
* Example: roundUpToMultiple(7n, 3n) = 9n
62+
*/
63+
roundUpToMultiple: (value, unit) => BigIntMath.divCeil(value, unit) * BigInt(unit),
4964
};
5065

5166
module.exports = {

lib/protocol.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { ethers } = require('ethers');
22
const { parseEther, ZeroAddress } = ethers;
3+
const { BigIntMath } = require('./helpers');
4+
const { PoolAsset } = require('./constants');
35

46
const COVER_PRICE_DENOMINATOR = 10000n;
57

@@ -33,15 +35,15 @@ function calculateInternalPrice(currentState, observations, capital, supply, cur
3335

3436
const averagePriceB = (currentObservation.priceCumulativeBelow - firstObservation.priceCumulativeBelow) / elapsed;
3537

36-
const priceA = averagePriceA > spotPriceA ? spotPriceA : averagePriceA;
37-
const priceB = averagePriceB > spotPriceB ? averagePriceB : spotPriceB;
38+
const priceA = BigIntMath.min(averagePriceA, spotPriceA);
39+
const priceB = BigIntMath.max(averagePriceB, spotPriceB);
40+
const bookValue = (parseEther('1') * capital) / supply;
3841

39-
const internalPrice = ((priceA + priceB - parseEther('1')) * capital) / supply;
42+
const internalPrice = priceA + priceB - bookValue;
4043
const maxPrice = (parseEther('1') * 3n * capital) / supply; // 300% BV
4144
const minPrice = (parseEther('1') * 35n * capital) / (supply * 100n); // 35% BV
4245

43-
const maxInternalPrice = internalPrice > maxPrice ? internalPrice : maxPrice;
44-
return maxInternalPrice > minPrice ? minPrice : maxInternalPrice;
46+
return BigIntMath.max(BigIntMath.min(internalPrice, maxPrice), minPrice);
4547
}
4648

4749
// TODO: eject from lib - we'll clearly not use it outside of tests
@@ -71,6 +73,7 @@ async function getInternalPrice(ramm, pool, tokenController, timestamp) {
7173
timestamp: observation.timestamp,
7274
};
7375
}
76+
7477
const state = {
7578
nxmA: previousState.nxmA,
7679
nxmB: previousState.nxmB,
@@ -81,22 +84,23 @@ async function getInternalPrice(ramm, pool, tokenController, timestamp) {
8184
};
8285

8386
const [currentState] = await ramm._getReserves(state, context, timestamp);
84-
8587
const observations = await ramm._updateTwap(state, previousObservations, context, timestamp);
88+
8689
return calculateInternalPrice(currentState, observations, capital, supply, timestamp, { GRANULARITY, PERIOD_SIZE });
8790
}
8891

8992
// ======================= COVER ==============================================
9093

91-
function calculatePremium(amount, rate, period, price, allocationUnit) {
94+
function calculatePremium(amount, rate, period, price, allocationUnit, paymentAsset) {
9295
const nxmAmount = (amount * parseEther('1')) / rate;
9396

9497
const coverNXMAmount =
9598
nxmAmount % allocationUnit === 0n ? nxmAmount : (nxmAmount / allocationUnit + 1n) * allocationUnit;
9699

97-
const premiumInNxm = (((coverNXMAmount * price) / COVER_PRICE_DENOMINATOR) * period) / (365n * 24n * 60n * 60n);
100+
const annualizedPremiumNxm = (coverNXMAmount * price) / COVER_PRICE_DENOMINATOR;
101+
const premiumInNxm = (annualizedPremiumNxm * BigInt(period)) / (365n * 24n * 60n * 60n);
98102

99-
const premiumInAsset = (premiumInNxm * rate) / parseEther('1');
103+
const premiumInAsset = paymentAsset === PoolAsset.NXM ? premiumInNxm : (premiumInNxm * rate) / parseEther('1');
100104

101105
return { premiumInNxm, premiumInAsset, coverNXMAmount };
102106
}

release/3.0/config/deployments.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@
104104
"salt": 2295602
105105
},
106106
"Cover": {
107-
"expectedAddress": "0xcafea6DcD8Ef5836E300b4E62E9a90975b0477EA",
108-
"salt": 15283923,
107+
"expectedAddress": "0xcafea49C5a4aBae7731092B95a0B3590e3753891",
108+
"salt": 22678373,
109109
"constructorArgs": [
110110
"0xcafea2c575550512582090AA06d0a069E7236b9e",
111111
"0xcafeade1872f14adc0a03Ec7b0088b61D76ec729",

release/3.0/release-3.0.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ ENABLE_OPTIMIZER=1 npx hardhat verify --network mainnet \
114114
* _stakingPoolImplementation = `0xcafeade1872f14adc0a03Ec7b0088b61D76ec729`
115115
* _verifyingAddress = `0xcafeac0fF5dA0A2777d915531bfA6B29d282Ee62`
116116
* Address brute force command
117-
* Address: `0xcafea6DcD8Ef5836E300b4E62E9a90975b0477EA`
118-
* Salt: 15283923
117+
* Address: `0xcafea4A986A8d88dc63095034cE36bC8387A8534`
118+
* Salt: 2295602
119119
```bash
120120
ENABLE_OPTIMIZER=1 node scripts/create2/find-salt.js \
121121
-t cafea \

0 commit comments

Comments
 (0)