-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
115 lines (95 loc) · 3.54 KB
/
test.js
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
const { expect } = require("chai");
const { parseEther } = require("ethers/lib/utils");
const { ethers } = require("hardhat");
describe("Campaign", function () {
let campaignFactoryContract;
let owner, creator1, contributor1, contributor2, withdrawalRecipient;
const createACampaign = () => {
return campaignFactoryContract
.connect(creator1)
.createCampaign(
"Campaign One",
"king",
parseEther("0.1"),
"This is the very first campaign created from this factory"
);
};
beforeEach(async () => {
[owner, creator1, contributor1, contributor2, withdrawalRecipient] =
await ethers.getSigners();
const CampaignFactory = await hre.ethers.getContractFactory(
"CampaignFactory"
);
campaignFactoryContract = await CampaignFactory.deploy();
await campaignFactoryContract.initialize();
});
it("should create the CampaignFactory Contract", async () => {
expect(await campaignFactoryContract.address).to.contains("0x");
});
it("should create a Campaign", async () => {
await createACampaign();
//get creator campaigns
const campaigns = await campaignFactoryContract.getCreatorCampaigns(
creator1.address
);
expect(campaigns.length).to.equal(1);
});
it("should create a Campaign and contribute to it", async () => {
await createACampaign();
//get deployed campaigns
const deployedCampaigns =
await campaignFactoryContract.getDeployedCampaigns();
//contribute to a campaign
await campaignFactoryContract
.connect(contributor1)
.contribute(deployedCampaigns[0], { value: parseEther("10") });
//get contributed campaigns for contributor1
const contributedCampaigns =
await campaignFactoryContract.getContributedCampaigns(
contributor1.address
);
expect(contributedCampaigns[0]).to.equal(deployedCampaigns[0]);
});
it("should create a Campaign, contribute to it and make a withdrawal", async () => {
await createACampaign();
//get deployed campaigns
const deployedCampaigns =
await campaignFactoryContract.getDeployedCampaigns();
//contribute to a campaign
await campaignFactoryContract
.connect(contributor1)
.contribute(deployedCampaigns[0], { value: parseEther("10") });
//contribute again
await campaignFactoryContract
.connect(contributor2)
.contribute(deployedCampaigns[0], { value: parseEther("20") });
const abi = [
"function createRequest(string memory description, uint256 value, address recipient) public",
"function approveRequest(uint256 index) public",
"function finalizeRequest(uint256 index) public",
"function getSummary() public view returns (uint256, uint256, uint256, uint256, uint256, uint256, string memory, string memory, string memory, address)",
"function requests(uint) view public returns (address, bool, uint40, string, uint256)",
];
const campaignContract = new ethers.Contract(
deployedCampaigns[0],
abi,
owner
);
await campaignContract
.connect(creator1)
.createRequest(
"I need to buy cat food",
parseEther("10"),
withdrawalRecipient.address
);
await campaignContract
.connect(contributor1)
.approveRequest(0, { gasLimit: 200000 });
await campaignContract
.connect(contributor2)
.approveRequest(0, { gasLimit: 200000 });
await campaignContract.connect(creator1).finalizeRequest(0);
const request = await campaignContract.requests(0);
expect(request[1]).true;
});
});