-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathLock.ts
More file actions
204 lines (162 loc) · 6.97 KB
/
Lock.ts
File metadata and controls
204 lines (162 loc) · 6.97 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
import {
time,
loadFixture,
} from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
import { expect } from "chai";
import hre from "hardhat";
describe("Lock", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deployOneYearLockFixture() {
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
const ONE_GWEI = 1_000_000_000;
const lockedAmount = ONE_GWEI;
const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await hre.ethers.getSigners();
const Lock = await hre.ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
return { lock, unlockTime, lockedAmount, owner, otherAccount };
}
describe("Deployment", function () {
it("Should set the right unlockTime", async function () {
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
expect(await lock.i_unlockTime()).to.equal(unlockTime);
});
it("Should set the right owner", async function () {
const { lock, owner } = await loadFixture(deployOneYearLockFixture);
expect(await lock.i_owner()).to.equal(owner.address);
});
it("Should receive and store the funds to lock", async function () {
const { lock, lockedAmount } = await loadFixture(
deployOneYearLockFixture
);
expect(await hre.ethers.provider.getBalance(lock.target)).to.equal(
lockedAmount
);
});
it("Should fail if the unlockTime is not in the future", async function () {
// We don't use the fixture here because we want a different deployment
const latestTime = await time.latest();
const Lock = await hre.ethers.getContractFactory("Lock");
await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWithCustomError(
Lock,
"Lock__UnlockTimeNotInFuture"
);
});
it("Should fail if no funds are provided", async function () {
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
const Lock = await hre.ethers.getContractFactory("Lock");
await expect(Lock.deploy(unlockTime, { value: 0 })).to.be.revertedWithCustomError(
Lock,
"Lock__NoFundsProvided"
);
});
});
describe("Withdrawals", function () {
describe("Validations", function () {
it("Should revert with the right error if called too soon", async function () {
const { lock } = await loadFixture(deployOneYearLockFixture);
await expect(lock.withdraw()).to.be.revertedWithCustomError(
lock,
"Lock__WithdrawalTooEarly"
);
});
it("Should revert with the right error if called from another account", async function () {
const { lock, unlockTime, otherAccount } = await loadFixture(
deployOneYearLockFixture
);
// We can increase the time in Hardhat Network
await time.increaseTo(unlockTime);
// We use lock.connect() to send a transaction from another account
await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWithCustomError(
lock,
"Lock__NotOwner"
);
});
it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
const { lock, unlockTime } = await loadFixture(
deployOneYearLockFixture
);
// Transactions are sent using the first signer by default
await time.increaseTo(unlockTime);
await expect(lock.withdraw()).not.to.be.reverted;
});
});
describe("Events", function () {
it("Should emit an event on withdrawals", async function () {
const { lock, unlockTime, lockedAmount } = await loadFixture(
deployOneYearLockFixture
);
await time.increaseTo(unlockTime);
await expect(lock.withdraw())
.to.emit(lock, "Withdrawal")
.withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
});
});
describe("Transfers", function () {
it("Should transfer the funds to the owner", async function () {
const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
deployOneYearLockFixture
);
await time.increaseTo(unlockTime);
await expect(lock.withdraw()).to.changeEtherBalances(
[owner, lock],
[lockedAmount, -lockedAmount]
);
});
});
});
describe("Additional Functions", function () {
it("Should return the correct balance", async function () {
const { lock, lockedAmount } = await loadFixture(deployOneYearLockFixture);
expect(await lock.getBalance()).to.equal(lockedAmount);
});
it("Should return the correct time remaining", async function () {
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
const currentTime = await time.latest();
const expectedTimeRemaining = unlockTime - currentTime;
expect(await lock.getTimeRemaining()).to.be.closeTo(expectedTimeRemaining, 2);
});
it("Should return 0 time remaining after unlock time", async function () {
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
await time.increaseTo(unlockTime + 1);
expect(await lock.getTimeRemaining()).to.equal(0);
});
});
describe("Deposits", function () {
it("Should emit Deposit event on construction", async function () {
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
const ONE_GWEI = 1_000_000_000;
const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
const [owner] = await hre.ethers.getSigners();
const Lock = await hre.ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: ONE_GWEI });
const receipt = await lock.deploymentTransaction()?.wait();
// Check that a Deposit event was emitted during deployment
const depositEvents = receipt?.logs.filter(log => {
try {
const parsed = lock.interface.parseLog(log);
return parsed?.name === 'Deposit';
} catch {
return false;
}
});
expect(depositEvents).to.have.length(1);
});
it("Should accept additional deposits via receive function", async function () {
const { lock, owner, lockedAmount } = await loadFixture(deployOneYearLockFixture);
const additionalAmount = 500_000_000;
await expect(owner.sendTransaction({
to: lock.target,
value: additionalAmount
}))
.to.emit(lock, "Deposit")
.withArgs(owner.address, additionalAmount);
expect(await lock.getBalance()).to.equal(lockedAmount + additionalAmount);
});
});
});