dgf-prototype/ethereum/test/Lock.js

132 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-03-04 19:33:06 -06:00
const {
time,
loadFixture,
2024-03-05 21:21:27 -06:00
} = require('@nomicfoundation/hardhat-toolbox/network-helpers');
const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
const { expect } = require('chai');
const { ethers } = require('hardhat');
2024-03-04 19:33:06 -06:00
2024-03-05 21:21:27 -06:00
describe('Lock', () => {
2024-03-04 19:33:06 -06:00
// 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 ethers.getSigners();
2024-03-05 21:21:27 -06:00
const Lock = await ethers.getContractFactory('Lock');
2024-03-04 19:33:06 -06:00
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
2024-03-05 21:21:27 -06:00
return {
lock, unlockTime, lockedAmount, owner, otherAccount,
};
2024-03-04 19:33:06 -06:00
}
2024-03-05 21:21:27 -06:00
describe('Deployment', () => {
it('Should set the right unlockTime', async () => {
2024-03-04 19:33:06 -06:00
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
expect(await lock.unlockTime()).to.equal(unlockTime);
});
2024-03-05 21:21:27 -06:00
it('Should set the right owner', async () => {
2024-03-04 19:33:06 -06:00
const { lock, owner } = await loadFixture(deployOneYearLockFixture);
expect(await lock.owner()).to.equal(owner.address);
});
2024-03-05 21:21:27 -06:00
it('Should receive and store the funds to lock', async () => {
2024-03-04 19:33:06 -06:00
const { lock, lockedAmount } = await loadFixture(
2024-03-05 21:21:27 -06:00
deployOneYearLockFixture,
2024-03-04 19:33:06 -06:00
);
expect(await ethers.provider.getBalance(lock.target)).to.equal(
2024-03-05 21:21:27 -06:00
lockedAmount,
2024-03-04 19:33:06 -06:00
);
});
2024-03-05 21:21:27 -06:00
it('Should fail if the unlockTime is not in the future', async () => {
2024-03-04 19:33:06 -06:00
// We don't use the fixture here because we want a different deployment
const latestTime = await time.latest();
2024-03-05 21:21:27 -06:00
const Lock = await ethers.getContractFactory('Lock');
2024-03-04 19:33:06 -06:00
await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith(
2024-03-05 21:21:27 -06:00
'Unlock time should be in the future',
2024-03-04 19:33:06 -06:00
);
});
});
2024-03-05 21:21:27 -06:00
describe('Withdrawals', () => {
describe('Validations', () => {
it('Should revert with the right error if called too soon', async () => {
2024-03-04 19:33:06 -06:00
const { lock } = await loadFixture(deployOneYearLockFixture);
await expect(lock.withdraw()).to.be.revertedWith(
2024-03-05 21:21:27 -06:00
"You can't withdraw yet",
2024-03-04 19:33:06 -06:00
);
});
2024-03-05 21:21:27 -06:00
it('Should revert with the right error if called from another account', async () => {
2024-03-04 19:33:06 -06:00
const { lock, unlockTime, otherAccount } = await loadFixture(
2024-03-05 21:21:27 -06:00
deployOneYearLockFixture,
2024-03-04 19:33:06 -06:00
);
// 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.revertedWith(
2024-03-05 21:21:27 -06:00
"You aren't the owner",
2024-03-04 19:33:06 -06:00
);
});
2024-03-05 21:21:27 -06:00
it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async () => {
2024-03-04 19:33:06 -06:00
const { lock, unlockTime } = await loadFixture(
2024-03-05 21:21:27 -06:00
deployOneYearLockFixture,
2024-03-04 19:33:06 -06:00
);
// Transactions are sent using the first signer by default
await time.increaseTo(unlockTime);
await expect(lock.withdraw()).not.to.be.reverted;
});
});
2024-03-05 21:21:27 -06:00
describe('Events', () => {
it('Should emit an event on withdrawals', async () => {
2024-03-04 19:33:06 -06:00
const { lock, unlockTime, lockedAmount } = await loadFixture(
2024-03-05 21:21:27 -06:00
deployOneYearLockFixture,
2024-03-04 19:33:06 -06:00
);
await time.increaseTo(unlockTime);
await expect(lock.withdraw())
2024-03-05 21:21:27 -06:00
.to.emit(lock, 'Withdrawal')
2024-03-04 19:33:06 -06:00
.withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
});
});
2024-03-05 21:21:27 -06:00
describe('Transfers', () => {
it('Should transfer the funds to the owner', async () => {
const {
lock, unlockTime, lockedAmount, owner,
} = await loadFixture(
deployOneYearLockFixture,
2024-03-04 19:33:06 -06:00
);
await time.increaseTo(unlockTime);
await expect(lock.withdraw()).to.changeEtherBalances(
[owner, lock],
2024-03-05 21:21:27 -06:00
[lockedAmount, -lockedAmount],
2024-03-04 19:33:06 -06:00
);
});
});
});
});