diff --git a/README.md b/README.md index 9d9c471..b942f2a 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,20 @@ **Treee** is the smart contract layer of the *Treee tree-planting protocol*, enabling on-chain verification, organization management, and NFT issuance for planted trees. It powers transparent and auditable sustainability tracking within the **Stability Nexus** ecosystem. +## ๐Ÿ“‹ Table of Contents + +- [Tech Stack](#tech-stack) +- [Architecture Overview](#architecture-overview) +- [Getting Started](#getting-started) +- [Developer Workflow](#developer-workflow) +- [Testing](#testing) +- [Deployment](#deployment) +- [Common Commands](#common-commands) +- [Project Structure](#project-structure) +- [Security](#security) +- [Contributing](#contributing) +- [License](#license) + --- ## Tech Stack @@ -58,12 +72,50 @@ It powers transparent and auditable sustainability tracking within the **Stabili - **Foundry (forge, anvil, cast)** โ€” Testing, simulation & deployment - **OpenZeppelin Contracts** โ€” Token standards and access control -### Contracts +### Libraries & Dependencies -- `TreeNft.sol` โ€” NFT for planted trees -- `Organisation.sol` โ€” Handles organization logic -- `OrganisationFactory.sol` โ€” Deploys and tracks organizations -- `CareToken.sol`, `PlanterToken.sol`, `LegacyToken.sol` โ€” Incentive ERC-20 tokens +- **foundry-devops** โ€” Deployment utilities +- **forge-std** โ€” Foundry standard library for testing + +--- + +## Architecture Overview + +### Core Contracts + +#### ๐ŸŒณ TreeNft.sol +The main NFT contract representing planted trees. Each NFT contains: +- **Metadata** โ€” Tree species, location, planting date +- **Verification status** โ€” On-chain proof of planting +- **Organization tracking** โ€” Links to the planting organization + +#### ๐Ÿข Organisation.sol +Manages individual organizations that plant trees: +- **Role management** โ€” Admin, verifier, and planter roles +- **Tree tracking** โ€” Records trees planted by the organization +- **Token distribution** โ€” Manages incentive token allocation + +#### ๐Ÿญ OrganisationFactory.sol +Factory contract for deploying and managing organizations: +- **Organization deployment** โ€” Creates new organization contracts +- **Registry** โ€” Maintains a list of all active organizations +- **Access control** โ€” Manages who can create organizations + +### Token Contracts + +#### ๐Ÿ’š CareToken.sol +ERC-20 token rewarding tree care and maintenance activities. + +#### ๐ŸŒฑ PlanterToken.sol +ERC-20 token incentivizing tree planting activities. + +#### ๐Ÿ† LegacyToken.sol +ERC-20 token for long-term sustainability contributions. + +### Utilities + +- **errors.sol** โ€” Custom error definitions for gas-efficient reverts +- **structs.sol** โ€” Shared data structures across contracts --- @@ -71,107 +123,384 @@ It powers transparent and auditable sustainability tracking within the **Stabili ### Prerequisites -- [Foundry](https://book.getfoundry.sh/getting-started/installation) -- Rust toolchain (`rustup`) -- Node.js (optional, for deployment scripting) +Ensure you have the following installed: ---- +- **[Foundry](https://book.getfoundry.sh/getting-started/installation)** โ€” Ethereum development toolkit + ```bash + curl -L https://foundry.paradigm.xyz | bash + foundryup + ``` +- **Git** โ€” Version control +- **Rust toolchain** โ€” Required by Foundry (`rustup`) +- **Node.js** (optional) โ€” For additional tooling -### Quickstart +### Environment Setup + +Create a `.env` file in the root directory: +Install dependencies -#### 1. Clone the repository ```bash -git clone https://github.com/StabilityNexus/Treee-Solidity.git -cd Treee-Solidity +forge install ``` -#### 2. Build contracts +#### 3. Build contracts ```bash forge build ``` -#### 3. Run tests +#### 4. Run tests ```bash forge test ``` -#### 4. Start a local node +#### 5. Start a local node ```bash anvil ``` -#### 5. Deploy (example) +#### 6. Deploy locally ```bash -forge script script/Deploy.s.sol:DeployAllContractsAtOnce \ - --rpc-url $SEPOLIA_RPC_URL \ - --private-key $PRIVATE_KEY \ +# In a new terminal, deploy to local Anvil node +forge script script/DeployAllContracts.s.sol:DeployAllContractsAtOnce \ + --rpc-url http://127.0.0.1:8545 \ + --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 +### Local Development + +#### Start local blockchain: + +```bash +anvil +``` + +#### Test in watch mode: + +```bash +forge test --watch +``` + +#### Run specific tests: + +```bash +# Test a specific contract +forge test --match-contract TreeNftTest + +# Test a specific function +forge test --match-test testMintTree + +# Verbose output +forge test -vvv +``` + +#### Gas reporting: + +```bash +forge test --gas-report +``` + +#### Check test coverage: + +```bash +forge coverage +``` + +### Interacting with Contracts + +#### Using Cast (Command Line): + +```bash +# Call a view function +cast call "getOrganisationCount()(uint256)" --rpc-url http://127.0.0.1:8545 + +# Send a transaction +cast send "mintTree(address,string)" "ipfs://metadata" \ + --private-key \ + --rpc-url http://127.0.0.1:8545 + +# Get contract balance +cast balance
--rpc-url http://127.0.0.1:8545 +``` + +#### Using Forge Scripts: + +```bash +# Deploy Organisation Factory +forge script script/DeployOrganisationFactory.s.sol:DeployOrganisationFactory \ + --rpc-url http://127.0.0.1:8545 \ + --broadcast + +# Deploy TreeNft Contract +forge script script/DeployTreeNftContract.s.sol:DeployTreeNft \ + --rpc-url http://127.0.0.1:8545 \ --broadcast ``` --- -## Developer Workflow +## Project Structure + +``` +Treee-Solidity/ +โ”œโ”€โ”€ ๐Ÿ“ src/ # Smart contracts source code +โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ Organisation.sol # Organization management contract +โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ OrganisationFactory.sol # Factory for creating organizations +โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ TreeNft.sol # Tree NFT contract (ERC-721) +โ”‚ โ”œโ”€โ”€ ๐Ÿ“ token-contracts/ # ERC-20 token implementations +โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ CareToken.sol # Care incentive token +โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ PlanterToken.sol # Planter reward token +โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ LegacyToken.sol # Legacy contribution token +โ”‚ โ””โ”€โ”€ ๐Ÿ“ utils/ # Utility contracts +โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ errors.sol # Custom error definitions +โ”‚ โ””โ”€โ”€ ๐Ÿ“„ structs.sol # Shared data structures +โ”‚ +โ”œโ”€โ”€ ๐Ÿ“ script/ # Deployment & interaction scripts +โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ DeployAllContracts.s.sol # Deploy all contracts at once +โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ DeployOrganisationFactory.s.sol +โ”‚ โ””โ”€โ”€ ๐Ÿ“„ DeployTreeNftContract.s.sol +โ”‚ +โ”œโ”€โ”€ ๐Ÿ“ test/ # Test files (Foundry tests) +โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ Organisation.t.sol # Organisation contract tests +โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ OrganisationFactory.t.sol # Factory contract tests +โ”‚ โ””โ”€โ”€ ๐Ÿ“„ TreeNft.t.sol # TreeNft contract tests +โ”‚ +โ”œโ”€โ”€ ๐Ÿ“ lib/ # External dependencies (Git submodules) +โ”‚ โ”œโ”€โ”€ ๐Ÿ“ forge-std/ # Foundry standard library +โ”‚ โ”œโ”€โ”€ ๐Ÿ“ foundry-devops/ # Deployment tools & utilities +โ”‚ โ””โ”€โ”€ ๐Ÿ“ openzeppelin-contracts/ # OpenZeppelin security libraries +โ”‚ +โ”œโ”€โ”€ ๐Ÿ“ img/ # Images & assets +โ”‚ +โ”œโ”€โ”€ ๐Ÿ“„ foundry.toml # Foundry configuration +โ”œโ”€โ”€ ๐Ÿ“„ remappings.txt # Import path remappings +โ”œโ”€โ”€ ๐Ÿ“„ makefile # Build automation +โ”œโ”€โ”€ ๐Ÿ“„ package.json # Node.js package config +โ””โ”€โ”€ ๐Ÿ“„ README.md # This file +``` + +### Key Files Explained + +| File/Directory | Purpose | +|----------------|---------| +| `foundry.toml` | Foundry configuration including Solidity version, optimizer settings, and test paths | +| `remappings.txt` | Maps import paths (e.g., `@openzeppelin/` โ†’ `lib/openzeppelin-contracts/`) | +| `makefile` | Contains shortcuts for common build and deployment commands | +| `lib/` | Contains all external dependencies installed via `forge install` | +| `script/` | Deployment scripts using Foundry's scripting capabilities | +| `test/` | Unit and integration tests using Foundry's testing framework | + +--- + +## Testing + +### Test Structure -Run a local node: +``` +test/ +โ”œโ”€โ”€ Organisation.t.sol # Organisation contract tests +โ”œโ”€โ”€ OrganisationFactory.t.sol # Factory contract tests +โ””โ”€โ”€ TreeNft.t.sol # TreeNft contract tests +``` + +### Running Tests ```bash -anvil +# Run all tests +forge test + +# Run with different verbosity levels +forge test -v # Show stack traces for failing tests +forge test -vv # Show stack traces for all tests +forge test -vvv # Show execution traces for failing tests +forge test -vvvv # Show execution traces for all tests +forge test -vvvvv # Show execution and setup traces for all tests + +# Filter tests by name pattern +forge test --match-test testMint + +# Filter tests by contract +forge test --match-contract TreeNft + +# Run tests on specific file +forge test --match-path test/TreeNft.t.sol + +# Generate gas snapshot +forge snapshot + +# Compare gas usage +forge snapshot --diff +``` + +--- + +## Deployment + +### Testnet Deployment (Sepolia) + +#### 1. Configure environment variables in `.env`: + +```env +SEPOLIA_RPC_URL= +PRIVATE_KEY= +ETHERSCAN_API_KEY= ``` -Test in watch mode: +#### 2. Deploy all contracts: ```bash -forge test --watch +forge script script/DeployAllContracts.s.sol:DeployAllContractsAtOnce \ + --rpc-url $SEPOLIA_RPC_URL \ + --private-key $PRIVATE_KEY \ + --broadcast \ + --verify \ + --etherscan-api-key $ETHERSCAN_API_KEY ``` -Interact manually: +#### 3. Deploy individual contracts: ```bash -cast call "getOrganisationCount()()" --rpc-url http://127.0.0.1:8545 +# Deploy Organisation Factory +forge script script/DeployOrganisationFactory.s.sol \ + --rpc-url $SEPOLIA_RPC_URL \ + --private-key $PRIVATE_KEY \ + --broadcast \ + --verify + +# Deploy TreeNft +forge script script/DeployTreeNftContract.s.sol \ + --rpc-url $SEPOLIA_RPC_URL \ + --private-key $PRIVATE_KEY \ + --broadcast \ + --verify ``` ---- +### Mainnet Deployment -## Common Commands +โš ๏ธ **Critical**: Before mainnet deployment: + +1. โœ… Complete security audit +2. โœ… Test extensively on testnet +3. โœ… Verify all contract addresses +4. โœ… Use hardware wallet or secure key management +5. โœ… Double-check constructor parameters + +```bash +forge script script/DeployAllContracts.s.sol:DeployAllContractsAtOnce \ + --rpc-url $MAINNET_RPC_URL \ + --private-key $PRIVATE_KEY \ + --broadcast \ + --verify \ + --etherscan-api-key $ETHERSCAN_API_KEY +``` + +### Verify Contracts Manually -| Command | Description | -| ---------------- | --------------------------- | -| `forge build` | Compile contracts | -| `forge test` | Run tests | -| `forge fmt` | Format Solidity code | -| `forge snapshot` | Create gas usage reports | -| `anvil` | Start local blockchain node | +If automatic verification fails: + +```bash +forge verify-contract \ + src/TreeNft.sol:TreeNft \ + --chain-id 11155111 \ + --etherscan-api-key $ETHERSCAN_API_KEY \ + --constructor-args $(cast abi-encode "constructor(string,string)" "TreeNFT" "TREE") +``` --- -## Contributing +## Common Commands + +| Command | Description | +| `forge build` | Compile contracts | +| `forge test` | Run all tests | +| `forge test -vvvv` | Run tests with detailed traces | +| `forge fmt` | Format Solidity code | +| `forge clean` | Remove build artifacts | +| `forge snapshot` | Create gas usage snapshot | +| `forge coverage` | Generate test coverage report | +| `anvil` | Start local blockchain node | +| `cast call` | Call a contract function (read-only) | +| `cast send` | Send a transaction to a contract | +| `forge script