Skip to content
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

Merged
merged 4 commits into from
Jun 25, 2024
Merged

Init backend #8

merged 4 commits into from
Jun 25, 2024

Conversation

jo-elimu
Copy link
Member

closes #2

`npm init`

refs #2
@jo-elimu jo-elimu self-assigned this Jun 25, 2024
@jo-elimu jo-elimu requested a review from a team as a code owner June 25, 2024 08:47
Copy link

netlify bot commented Jun 25, 2024

Deploy Preview for web3-sponsors canceled.

Name Link
🔨 Latest commit 143a237
🔍 Latest deploy log https://app.netlify.com/sites/web3-sponsors/deploys/667a92db3650bf0008da87bb

`npm install -D hardhat`

refs #1
Copy link

coderabbitai bot commented Jun 25, 2024

Walkthrough

Significant updates include the introduction of a new backend project setup. It incorporates a .gitignore file, a Hardhat project showcasing a simple contract for locking funds (Lock.sol), tests for the contract, a Hardhat configuration, deployment module, and a GitHub Actions workflow for CI/CD. The README.md provides detailed instructions on the usage, while the workflow ensures code quality through automated builds and tests.

Changes

File/Path Summary
backend/.gitignore Introduces an exclusion rule for node_modules within the backend directory.
backend/README.md Contains instructions for running various Hardhat tasks, showcasing a sample Hardhat project.
backend/contracts/Lock.sol Adds a smart contract with a lock mechanism for funds, allowing withdrawal after a specified unlock time only by the owner.
backend/hardhat.config.ts Introduces Hardhat configuration with Solidity version 0.8.24 and enabling the gas reporter.
backend/ignition/modules/Lock.ts Defines a module with lock functionality using Hardhat Ignition, including parameters for unlockTime and lockedAmount.
backend/test/Lock.ts Adds test cases for the Lock contract covering scenarios like deployment, fund handling, ownership checks, and time validations.
.github/.../backend.yml GitHub Actions workflow for building and testing the backend application on pushes and pull requests to the main branch.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9044018 and 143a237.

Files selected for processing (2)
  • .github/workflows/backend.yml (1 hunks)
  • backend/hardhat.config.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • backend/hardhat.config.ts
Additional comments not posted (5)
.github/workflows/backend.yml (5)

3-7: Well-configured workflow triggers.

The workflow is correctly set to trigger on both push and pull request events to the main branch, ensuring that all changes are continuously integrated and tested.


12-17: Robust matrix configuration for cross-environment testing.

The use of a matrix strategy to test across ubuntu-latest and windows-latest with Node.js versions 18.x and 20.x is excellent for ensuring broad compatibility of the backend application.


19-21: Proper configuration of default working directory.

Setting the working directory to backend for all run commands is a good practice to ensure consistency in the execution environment of the workflow steps.


24-30: Efficient setup and caching for Node.js.

Using actions/setup-node@v3 for Node.js setup with caching enabled and specifying the dependency path backend/package-lock.json are best practices that enhance the efficiency of the workflow.


31-36: Comprehensive build and test steps.

The inclusion of steps for installation, compilation, testing, and coverage checking (with a coverage threshold of 80%) ensures a thorough validation process for the backend application.


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

`npx hardhat init`

refs #1
Copy link

@coderabbitai coderabbitai bot left a 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";
Copy link

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.

Comment on lines +7 to +33
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);
}
Copy link

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 and owner 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.

Comment on lines +9 to +126
}

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]
);
});
});
});
Copy link

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.

@jo-elimu jo-elimu merged commit efa1ee9 into main Jun 25, 2024
12 checks passed
@jo-elimu jo-elimu deleted the 2-init-backend branch June 25, 2024 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Init backend
2 participants