-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Init backend #8
Init backend #8
Conversation
`npm init` refs #2
✅ Deploy Preview for web3-sponsors canceled.
|
`npm install -D hardhat` refs #1
WalkthroughSignificant updates include the introduction of a new backend project setup. It incorporates a Changes
Recent review detailsConfiguration used: CodeRabbit UI Files selected for processing (2)
Files skipped from review due to trivial changes (1)
Additional comments not posted (5)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
`npx hardhat init` refs #1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
@@ -0,0 +1,17 @@ | |||
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure correct import path.
The import path uses @nomicfoundation/hardhat-ignition/modules
which seems incorrect. It should probably be @nomicfoundation/hardhat-ignition
.
contract Lock { | ||
uint public unlockTime; | ||
address payable public owner; | ||
|
||
event Withdrawal(uint amount, uint when); | ||
|
||
constructor(uint _unlockTime) payable { | ||
require( | ||
block.timestamp < _unlockTime, | ||
"Unlock time should be in the future" | ||
); | ||
|
||
unlockTime = _unlockTime; | ||
owner = payable(msg.sender); | ||
} | ||
|
||
function withdraw() public { | ||
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal | ||
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp); | ||
|
||
require(block.timestamp >= unlockTime, "You can't withdraw yet"); | ||
require(msg.sender == owner, "You aren't the owner"); | ||
|
||
emit Withdrawal(address(this).balance, block.timestamp); | ||
|
||
owner.transfer(address(this).balance); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smart contract logic appears robust and secure.
- The constructor sets the
unlockTime
andowner
correctly with appropriate validations. - The
withdraw
function includes necessary checks for time and ownership, which are crucial for security. - The use of events for logging is a good practice.
However, consider adding revert reasons for all require
statements for better error handling and user feedback.
} | ||
|
||
describe("Deployment", function () { | ||
it("Should set the right unlockTime", async function () { | ||
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture); | ||
|
||
expect(await lock.unlockTime()).to.equal(unlockTime); | ||
}); | ||
|
||
it("Should set the right owner", async function () { | ||
const { lock, owner } = await loadFixture(deployOneYearLockFixture); | ||
|
||
expect(await lock.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.revertedWith( | ||
"Unlock time should be in the future" | ||
); | ||
}); | ||
}); | ||
|
||
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.revertedWith( | ||
"You can't withdraw yet" | ||
); | ||
}); | ||
|
||
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.revertedWith( | ||
"You aren't the owner" | ||
); | ||
}); | ||
|
||
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] | ||
); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comprehensive test coverage.
The tests cover a wide range of scenarios:
- Correct setting of contract parameters during deployment.
- Handling of funds.
- Time-based restrictions.
- Ownership validation.
- Withdrawal functionality and event emissions.
Consider adding more comments to explain the purpose of each test for better maintainability.
refs #1
closes #2