Skip to content

Commit 49eb266

Browse files
authored
test: add CoverBroker upgrade + basic functionality (#1373)
2 parents 94e1764 + 121e230 commit 49eb266

File tree

6 files changed

+451
-60
lines changed

6 files changed

+451
-60
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"homepage": "https://github.com/NexusMutual/smart-contracts",
2222
"dependencies": {
23-
"@nexusmutual/deployments": "^2.11.1",
23+
"@nexusmutual/deployments": "^2.12.0",
2424
"@nexusmutual/ethers-v5-aws-kms-signer": "^0.0.1",
2525
"@nomicfoundation/hardhat-network-helpers": "^1.0.12",
2626
"@openzeppelin/contracts-v4": "npm:@openzeppelin/contracts@^4.7.3",

test/fork/basic-functionality-tests.js

Lines changed: 127 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const { parseEther, defaultAbiCoder, toUtf8Bytes, formatEther, parseUnits } = et
2727

2828
const ASSESSMENT_VOTER_COUNT = 3;
2929
const { USDC_ADDRESS } = Address;
30-
const { NXM_WHALE_1, NXM_WHALE_2, DAI_NXM_HOLDER, NXMHOLDER, DAI_HOLDER, USDC_HOLDER, HUGH } = UserAddress;
30+
const { NXM_WHALE_1, NXM_WHALE_2, DAI_NXM_HOLDER, NXMHOLDER, DAI_HOLDER, USDC_HOLDER, NXM_AB_MEMBER } = UserAddress;
3131

3232
let custodyProductId, custodyCoverId;
3333
let protocolProductId, protocolCoverId;
@@ -59,12 +59,13 @@ const setTime = async timestamp => {
5959

6060
async function castAssessmentVote() {
6161
// vote
62-
for (const abMember of this.abMembers.slice(0, ASSESSMENT_VOTER_COUNT)) {
63-
await this.assessment.connect(abMember).castVotes([assessmentId], [true], [''], 0);
64-
}
62+
await Promise.all(
63+
this.abMembers
64+
.slice(0, ASSESSMENT_VOTER_COUNT)
65+
.map(abMember => this.assessment.connect(abMember).castVotes([assessmentId], [true], [''], 0)),
66+
);
6567

66-
const { poll: pollResult } = await this.assessment.assessments(assessmentId);
67-
const poll = pollResult;
68+
const { poll } = await this.assessment.assessments(assessmentId);
6869

6970
const payoutCooldown = (await this.assessment.getPayoutCooldown()).toNumber();
7071

@@ -92,25 +93,24 @@ describe('basic functionality tests', function () {
9293
});
9394

9495
it('Impersonate addresses', async function () {
95-
await evm.impersonate(NXM_WHALE_1);
96-
await evm.impersonate(NXM_WHALE_2);
97-
await evm.impersonate(NXMHOLDER);
98-
await evm.impersonate(NEW_POOL_MANAGER);
99-
await evm.setBalance(NXM_WHALE_1, parseEther('100000'));
100-
await evm.setBalance(NXM_WHALE_2, parseEther('100000'));
101-
await evm.setBalance(NXMHOLDER, parseEther('100000'));
102-
await evm.setBalance(NEW_POOL_MANAGER, parseEther('100000'));
103-
await evm.setBalance(DAI_HOLDER, parseEther('100000'));
104-
await evm.setBalance(DAI_NXM_HOLDER, parseEther('100000'));
105-
106-
this.members = [];
107-
this.members.push(await getSigner(NXM_WHALE_1));
108-
this.members.push(await getSigner(NXM_WHALE_2));
109-
this.members.push(await getSigner(NXMHOLDER));
96+
await Promise.all([
97+
// Impersonate addresses
98+
evm.impersonate(NXM_WHALE_1),
99+
evm.impersonate(NXM_WHALE_2),
100+
evm.impersonate(NXMHOLDER),
101+
evm.impersonate(DAI_HOLDER),
102+
evm.impersonate(NEW_POOL_MANAGER),
103+
// Set balances
104+
evm.setBalance(NXM_WHALE_1, parseEther('100000')),
105+
evm.setBalance(NXM_WHALE_2, parseEther('100000')),
106+
evm.setBalance(NXMHOLDER, parseEther('100000')),
107+
evm.setBalance(NEW_POOL_MANAGER, parseEther('100000')),
108+
evm.setBalance(DAI_HOLDER, parseEther('100000')),
109+
evm.setBalance(DAI_NXM_HOLDER, parseEther('100000')),
110+
]);
110111

112+
this.members = await Promise.all([NXM_WHALE_1, NXM_WHALE_2, NXMHOLDER].map(address => getSigner(address)));
111113
this.manager = await getSigner(NEW_POOL_MANAGER);
112-
113-
await evm.impersonate(DAI_HOLDER);
114114
this.daiHolder = await getSigner(DAI_HOLDER);
115115
this.usdcHolder = await getSigner(USDC_HOLDER);
116116
});
@@ -139,35 +139,41 @@ describe('basic functionality tests', function () {
139139
return latestAddresses[contractCode];
140140
}
141141

142-
for (const contractCode of Object.keys(dependenciesToVerify)) {
143-
const dependencies = dependenciesToVerify[contractCode];
142+
await Promise.all(
143+
Object.keys(dependenciesToVerify).map(async contractCode => {
144+
const dependencies = dependenciesToVerify[contractCode];
144145

145-
const masterAwareV2 = await ethers.getContractAt('IMasterAwareV2', await getLatestAddress(contractCode));
146+
const masterAwareV2 = await ethers.getContractAt('IMasterAwareV2', await getLatestAddress(contractCode));
146147

147-
for (const dependency of dependencies) {
148-
const dependencyAddress = await getLatestAddress(dependency);
148+
await Promise.all(
149+
dependencies.map(async dependency => {
150+
const dependencyAddress = await getLatestAddress(dependency);
149151

150-
const contractId = InternalContractsIDs[dependency];
151-
const storedDependencyAddress = await masterAwareV2.internalContracts(contractId);
152-
expect(storedDependencyAddress).to.be.equal(
153-
dependencyAddress,
154-
`Dependency ${dependency} for ${contractCode} is not set correctly ` +
155-
`(expected ${dependencyAddress}, got ${storedDependencyAddress})`,
152+
const contractId = InternalContractsIDs[dependency];
153+
const storedDependencyAddress = await masterAwareV2.internalContracts(contractId);
154+
expect(storedDependencyAddress).to.be.equal(
155+
dependencyAddress,
156+
`Dependency ${dependency} for ${contractCode} is not set correctly ` +
157+
`(expected ${dependencyAddress}, got ${storedDependencyAddress})`,
158+
);
159+
}),
156160
);
157-
}
158-
}
161+
}),
162+
);
159163
});
160164

161165
it('Stake for assessment', async function () {
162166
// stake
163167
const amount = parseEther('200');
164-
for (const abMember of this.abMembers.slice(0, ASSESSMENT_VOTER_COUNT)) {
165-
const memberAddress = await abMember.getAddress();
166-
const { amount: stakeAmountBefore } = await this.assessment.stakeOf(memberAddress);
167-
await this.assessment.connect(abMember).stake(amount);
168-
const { amount: stakeAmountAfter } = await this.assessment.stakeOf(memberAddress);
169-
expect(stakeAmountAfter).to.be.equal(stakeAmountBefore.add(amount));
170-
}
168+
await Promise.all(
169+
this.abMembers.slice(0, ASSESSMENT_VOTER_COUNT).map(async abMember => {
170+
const memberAddress = await abMember.getAddress();
171+
const { amount: stakeAmountBefore } = await this.assessment.stakeOf(memberAddress);
172+
await this.assessment.connect(abMember).stake(amount);
173+
const { amount: stakeAmountAfter } = await this.assessment.stakeOf(memberAddress);
174+
expect(stakeAmountAfter).to.be.equal(stakeAmountBefore.add(amount));
175+
}),
176+
);
171177
});
172178

173179
it('Swap NXM for ETH', async function () {
@@ -584,8 +590,8 @@ describe('basic functionality tests', function () {
584590
});
585591

586592
it('Buy protocol USDC cover', async function () {
587-
await evm.impersonate(HUGH);
588-
const coverBuyer = await getSigner(HUGH);
593+
await evm.impersonate(NXM_AB_MEMBER);
594+
const coverBuyer = await getSigner(NXM_AB_MEMBER);
589595
const coverBuyerAddress = await coverBuyer.getAddress();
590596

591597
const coverAsset = 6; // USDC
@@ -628,8 +634,8 @@ describe('basic functionality tests', function () {
628634
});
629635

630636
it('Submit claim for protocol cover in USDC', async function () {
631-
await evm.impersonate(HUGH);
632-
const coverBuyer = await getSigner(HUGH);
637+
await evm.impersonate(NXM_AB_MEMBER);
638+
const coverBuyer = await getSigner(NXM_AB_MEMBER);
633639

634640
const claimsCountBefore = await this.individualClaims.getClaimsCount();
635641
const assessmentCountBefore = await this.assessment.getAssessmentsCount();
@@ -678,10 +684,81 @@ describe('basic functionality tests', function () {
678684
expect(payoutRedeemed).to.be.equal(true);
679685
});
680686

687+
it('buy cover through CoverBroker using ETH', async function () {
688+
const coverBuyer = await ethers.Wallet.createRandom().connect(ethers.provider);
689+
690+
await evm.setBalance(coverBuyer.address, parseEther('1000000'));
691+
692+
const amount = parseEther('1');
693+
const coverCountBefore = await this.cover.getCoverDataCount();
694+
695+
await this.coverBroker.connect(coverBuyer).buyCover(
696+
{
697+
coverId: 0,
698+
owner: coverBuyer.address,
699+
productId: protocolProductId,
700+
coverAsset: 0, // ETH
701+
amount,
702+
period: 3600 * 24 * 30, // 30 days
703+
maxPremiumInAsset: parseEther('1').mul(260).div(10000),
704+
paymentAsset: 0, // ETH
705+
payWithNXM: false,
706+
commissionRatio: '500', // 5%,
707+
commissionDestination: coverBuyer.address,
708+
ipfsData: '',
709+
},
710+
[{ poolId, coverAmountInAsset: amount }],
711+
{ value: amount },
712+
);
713+
714+
const coverCountAfter = await this.cover.getCoverDataCount();
715+
const coverId = coverCountAfter;
716+
const isCoverBuyerOwner = await this.coverNFT.isApprovedOrOwner(coverBuyer.address, coverId);
717+
718+
expect(isCoverBuyerOwner).to.be.equal(true);
719+
expect(coverCountAfter).to.be.equal(coverCountBefore.add(1));
720+
});
721+
722+
it('buy cover through CoverBroker using ERC20 (USDC)', async function () {
723+
await evm.impersonate(USDC_HOLDER);
724+
await evm.setBalance(USDC_HOLDER, parseEther('1000000'));
725+
726+
const coverBuyer = await getSigner(USDC_HOLDER);
727+
const coverBuyerAddress = await coverBuyer.getAddress();
728+
729+
const amount = parseUnits('1000', 6);
730+
const coverCountBefore = await this.cover.getCoverDataCount();
731+
732+
await this.usdc.connect(coverBuyer).approve(this.coverBroker.address, MaxUint256);
733+
await this.coverBroker.connect(coverBuyer).buyCover(
734+
{
735+
coverId: 0,
736+
owner: coverBuyerAddress,
737+
productId: protocolProductId,
738+
coverAsset: 6, // USDC
739+
amount,
740+
period: 3600 * 24 * 30, // 30 days
741+
maxPremiumInAsset: amount.mul(260).div(10000),
742+
paymentAsset: 6, // USDC
743+
payWithNXM: false,
744+
commissionRatio: '500', // 5%,
745+
commissionDestination: coverBuyerAddress,
746+
ipfsData: '',
747+
},
748+
[{ poolId, coverAmountInAsset: amount }],
749+
);
750+
751+
const coverCountAfter = await this.cover.getCoverDataCount();
752+
const coverId = coverCountAfter;
753+
const isCoverBuyerOwner = await this.coverNFT.isApprovedOrOwner(coverBuyerAddress, coverId);
754+
755+
expect(isCoverBuyerOwner).to.be.equal(true);
756+
expect(coverCountAfter).to.be.equal(coverCountBefore.add(1));
757+
});
758+
681759
it('Edit cover', async function () {
682-
// buying cover with USDC
683-
await evm.impersonate(HUGH);
684-
const coverBuyer = await getSigner(HUGH);
760+
await evm.impersonate(NXM_AB_MEMBER);
761+
const coverBuyer = await getSigner(NXM_AB_MEMBER);
685762
const coverBuyerAddress = await coverBuyer.getAddress();
686763

687764
const coverAsset = 6; // USDC

0 commit comments

Comments
 (0)