-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add 101 template for zksync-101
- Loading branch information
1 parent
db28b15
commit 9ac3028
Showing
43 changed files
with
2,559 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
.DS_Store | ||
|
||
package-lock.json | ||
node_modules | ||
|
||
era_test_node.log | ||
|
||
# Compilation output | ||
**/.upgradable/ | ||
build-test/ | ||
dist | ||
artifacts-zk/ | ||
cache-zk/ | ||
deployments-zk/ | ||
deployments/ | ||
|
||
typechain-types | ||
typechain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# DO NOT PUT REAL PRIVATE KEYS INTO THIS FILE | ||
# This is a local rich wallet private key for development and learning purposes only | ||
WALLET_PRIVATE_KEY=0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# dotenv environment variable files | ||
|
||
.env | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env.local | ||
.env.* | ||
!.env.example | ||
|
||
# Finder (MacOS) folder config | ||
.DS_Store | ||
|
||
node_modules/ | ||
|
||
era_test_node.log | ||
|
||
package-lock.json | ||
node_modules | ||
typechain-types | ||
typechain | ||
|
||
**/.upgradable/ | ||
build-test/ | ||
dist | ||
artifacts-zk/ | ||
cache-zk/ | ||
deployments-zk/ | ||
deployments/ | ||
|
||
# Hardhat files | ||
cache | ||
artifacts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"recommendations": [ | ||
"christian-kohler.npm-intellisense", | ||
"christian-kohler.path-intellisense", | ||
"dbaeumer.vscode-eslint", | ||
"editorconfig.editor", | ||
"esbenp.prettier-vscode", | ||
"mikestead.dotenv", | ||
"yoavbls.pretty-ts-errors" | ||
], | ||
"unwantedRecommendations": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"files.trimTrailingWhitespace": true, | ||
"editor.formatOnType": false, | ||
"editor.formatOnPaste": false, | ||
"editor.formatOnSave": true, | ||
"editor.formatOnSaveMode": "file", | ||
"editor.tabSize": 2, | ||
"editor.insertSpaces": true, | ||
"files.eol": "\n", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Matter Labs | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# ZKsync 101 | ||
|
||
This project was scaffolded with [ZKsync CLI](https://github.com/matter-labs/zksync-cli). | ||
|
||
ZKsync 101 is a quick start template to help you learn how to | ||
be a super developer using ZKsync CLI to build projects in the | ||
ZKsync ecosystem! | ||
|
||
This project is a Nodejs project that uses hardhat, viem, and solidity. | ||
You will learn how to build, deploy and test smart contracts with hardhat | ||
onto the ZKsync in memory node and use ZKsync CLI to interact with the contracts. | ||
|
||
## Project Layout | ||
|
||
- `/contracts`: Contains solidity smart contracts. | ||
- `/deploy`: Scripts for contract deployment and interaction. | ||
- `/test`: Test files. | ||
- `hardhat.config.ts`: Configuration settings, the default network is set to "inMemoryNode". | ||
|
||
## How to Use | ||
|
||
1. Install dependencies with `npm install --force`. | ||
|
||
2. Configure your ZKsync CLI to use the In memory node settings for dev. | ||
|
||
```bash | ||
zksync-cli dev config | ||
``` | ||
|
||
3. Start up a local in-memory node with ZKsync CLI with the following command: | ||
|
||
```bash | ||
zksync-cli dev start | ||
``` | ||
|
||
4. Follow along on ZKsync Docs in our [ZKsync 101](https://docs.zksync.io/build/start-coding/zksync-101)! | ||
|
||
### Environment Settings | ||
|
||
This project pulls in environment variables from `.env` files. | ||
|
||
Rename `.env.example` to `.env`, the provided private key is a local rich wallet | ||
that is available in the local in-memory node. | ||
|
||
```txt | ||
WALLET_PRIVATE_KEY=your_private_key_here... | ||
``` | ||
|
||
### Local Tests | ||
|
||
Running `npm run test` by default runs the [zkSync In-memory Node](https://docs.zksync.io/build/test-and-debug/in-memory-node) provided by the [@matterlabs/hardhat-zksync-node](https://docs.zksync.io/build/tooling/hardhat/hardhat-zksync-node) tool. | ||
|
||
Important: ZKsync In-memory Node currently supports only the L2 node. If contracts also need L1, use another testing environment like Dockerized Node. | ||
Refer to [test documentation](https://docs.zksync.io/build/test-and-debug) for details. | ||
|
||
## Useful Links | ||
|
||
- [Docs](https://docs.zksync.io) | ||
- [Official Site](https://zksync.io/) | ||
- [GitHub](https://github.com/matter-labs) | ||
- [Twitter](https://twitter.com/zksync) | ||
- [Discord](https://join.zksync.dev/) | ||
|
||
## License | ||
|
||
This project is under the [MIT](./LICENSE) license. |
48 changes: 48 additions & 0 deletions
48
templates/101/contracts/1-hello-zksync/CrowdfundingCampaign.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract CrowdfundingCampaign { | ||
address public owner; | ||
uint256 private fundingGoal; | ||
uint256 private totalFundsRaised; | ||
mapping(address => uint256) private contributions; | ||
|
||
event ContributionReceived(address contributor, uint256 amount); | ||
event GoalReached(uint256 totalFundsRaised); | ||
|
||
constructor(uint256 _fundingGoal) { | ||
owner = msg.sender; | ||
fundingGoal = _fundingGoal; | ||
} | ||
|
||
function contribute() public payable { | ||
require(msg.value > 0, "Contribution must be greater than 0"); | ||
contributions[msg.sender] += msg.value; | ||
totalFundsRaised += msg.value; | ||
|
||
emit ContributionReceived(msg.sender, msg.value); | ||
|
||
if (totalFundsRaised >= fundingGoal) { | ||
emit GoalReached(totalFundsRaised); | ||
} | ||
} | ||
|
||
function withdrawFunds() public { | ||
require(msg.sender == owner, "Only the owner can withdraw funds"); | ||
require(totalFundsRaised >= fundingGoal, "Funding goal not reached"); | ||
|
||
uint256 amount = address(this).balance; | ||
totalFundsRaised = 0; | ||
|
||
(bool success, ) = payable(owner).call{value: amount}(""); | ||
require(success, "Transfer failed."); | ||
} | ||
|
||
function getTotalFundsRaised() public view returns (uint256) { | ||
return totalFundsRaised; | ||
} | ||
|
||
function getFundingGoal() public view returns (uint256) { | ||
return fundingGoal; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
templates/101/contracts/2-contract-factory/CrowdfundingFactory.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
// Crowdfunding campaign contract | ||
import "../1-hello-zksync/CrowdfundingCampaign.sol"; | ||
|
||
// Factory contract to create and manage crowdfunding campaigns | ||
contract CrowdfundingFactory { | ||
CrowdfundingCampaign[] private campaigns; | ||
|
||
event CampaignCreated(address campaignAddress, uint256 fundingGoal); | ||
|
||
function createCampaign(uint256 fundingGoal) public { | ||
CrowdfundingCampaign newCampaign = new CrowdfundingCampaign( | ||
fundingGoal | ||
); | ||
campaigns.push(newCampaign); | ||
|
||
emit CampaignCreated(address(newCampaign), fundingGoal); | ||
} | ||
|
||
function getCampaigns() | ||
public | ||
view | ||
returns (CrowdfundingCampaign[] memory) | ||
{ | ||
return campaigns; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
templates/101/contracts/3-proxy-contracts/beacon/BeaconCrowdfundingCampaign.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import "@openzeppelin/contracts/proxy/utils/Initializable.sol"; | ||
|
||
// The BeaconCrowdfundingCampaign contract implements | ||
// the Initializable interface from OpenZeppelin | ||
contract BeaconCrowdfundingCampaign is Initializable { | ||
address public owner; | ||
uint256 private fundingGoal; | ||
uint256 private totalFundsRaised; | ||
mapping(address => uint256) private contributions; | ||
|
||
event ContributionReceived(address contributor, uint256 amount); | ||
event GoalReached(uint256 totalFundsRaised); | ||
|
||
// The `initialize` function replaces the constructor in upgradeable contracts | ||
function initialize(uint256 _fundingGoal) public initializer { | ||
owner = msg.sender; | ||
fundingGoal = _fundingGoal; | ||
} | ||
|
||
function contribute() public payable { | ||
require(msg.value > 0, "Contribution must be greater than 0"); | ||
contributions[msg.sender] += msg.value; | ||
totalFundsRaised += msg.value; | ||
|
||
emit ContributionReceived(msg.sender, msg.value); | ||
|
||
if (totalFundsRaised >= fundingGoal) { | ||
emit GoalReached(totalFundsRaised); | ||
} | ||
} | ||
|
||
function withdrawFunds() public { | ||
require(msg.sender == owner, "Only the owner can withdraw funds"); | ||
require(totalFundsRaised >= fundingGoal, "Funding goal not reached"); | ||
|
||
uint256 amount = address(this).balance; | ||
totalFundsRaised = 0; | ||
|
||
(bool success, ) = payable(owner).call{value: amount}(""); | ||
require(success, "Transfer failed."); | ||
} | ||
|
||
function getTotalFundsRaised() public view returns (uint256) { | ||
return totalFundsRaised; | ||
} | ||
|
||
function getFundingGoal() public view returns (uint256) { | ||
return fundingGoal; | ||
} | ||
} |
Oops, something went wrong.