# NGOLisFactory

The contract includes functionality for creating new NGO instances, associating them with owners, and emitting events to log these actions. Additionally, it provides a method for the contract owner to withdraw fees in the form of staked Ether (stETH), ensuring proper fund management and transparency. The contract leverages interfaces and events to interact with other smart contracts and to notify external systems of significant actions. It utilizes the ERC1967 proxy pattern to create upgradeable NGO instances, manages ownership associations, and handles platform fee collection.

* [Source code](https://github.com/Launchnodes-Ltd/LIS/blob/main/contracts/NGOLisFactory.sol)
* [Deployed contract](https://holesky.etherscan.io/address/0x4510dad49ec1b72bfb3cfcad3aab8b6c6bb097fc#writeContract)

### Contract Architecture

#### Proxy Implementation

* Uses OpenZeppelin's `ERC1967Proxy` for creating upgradeable NGO instances
* Maintains a reference to the current NGOLis implementation contract
* Allows implementation updates through owner-controlled function
* Each deployed NGO is a separate proxy pointing to the same implementation

#### Core Components

1. **Implementation Management**

   ```solidity
   address private _ngoImplementation;    // Current NGO implementation
   ```

   * Stores the reference implementation for all NGO proxies
   * Can be updated by factory owner through `setImplementation`
2. **Protocol Addresses**

   ```solidity
   address _lidoSCAddress;        // Lido staking contract
   address _withdrawalSCAddress;  // Withdrawal queue contract
   address wstETHSC;             // Wrapped stETH contract
   ```

   * Core protocol contracts used by NGO instances
   * Set during factory construction
   * Immutable after deployment
3. **Ownership Tracking**

   ```solidity
   mapping(address => address) public ownerToNgo;
   ```

   * Maps NGO owners to their respective NGO contract addresses
   * One-to-one relationship between owners and NGO

## Sequential Diagram

<figure><img src="/files/gcKPWCByA608q3cwfMpd" alt=""><figcaption></figcaption></figure>

### Core Logic and Operations

#### NGO Deployment Process

1. **Proxy Creation**
   * Creates new `ERC1967Proxy` instance
   * Points to current implementation
   * Initializes with provided parameters
2. **Ownership Association**
   * Records owner to NGO address mapping
   * Enables lookup of NGO contracts by owner
3. **Configuration**
   * Sets initial rewards owner
   * Configures oracle permissions
   * Associates with Lido protocol contracts

#### Fee Management

* Collects platform fees in wstETH
* Owner can withdraw accumulated fees
* Emits events for fee collection transparency

### Function Documentation

#### Administrative Functions

### `constructor`

```solidity
constructor(
    address _lidoSC,
    address _withdrawalSC,
    address _ngoImplementationAddress,
    address _wstETHSC
) Ownable(msg.sender)
```

Initializes the factory with required protocol addresses.

* **Parameters**
  * `_lidoSC`: Lido protocol contract address
  * `_withdrawalSC`: Withdrawal queue contract address
  * `_ngoImplementationAddress`: NGOLis implementation contract
  * `_wstETHSC`: Wrapped stETH contract address
* **Effects**
  * Sets immutable protocol addresses
  * Establishes initial implementation reference
  * Sets contract owner

### `setImplementation`

```solidity
function setImplementation(address newImplementation) public onlyOwner
```

Updates the NGO implementation contract address.

* **Parameters**
  * `newImplementation`: Address of new implementation contract
* **Access Control**
  * Restricted to factory owner
* **Effects**
  * Updates implementation reference for future deployments
  * Does not affect existing NGO proxies

#### Deployment Functions

### `createNGO`

```solidity
function createNGO(
    string memory _name,
    string memory _imageLink,
    string memory _description,
    string memory _link,
    string memory _location,
    address _rewardsOwner,
    address owner,
    address oracle
) public onlyOwner
```

Creates and initializes a new NGO contract instance.

* **Parameters**
  * `_name`: NGO name identifier
  * `_imageLink`: URL to NGO image
  * `_description`: NGO description
  * `_link`: External NGO website/resource
  * `_location`: Physical/operational location
  * `_rewardsOwner`: Address to receive NGO rewards
  * `owner`: NGO contract administrator
  * `oracle`: Address with oracle privileges
* **Access Control**
  * Restricted to factory owner
* **Effects**
  * Deploys new proxy contract
  * Initializes NGO configuration
  * Records owner association
  * Emits `NGOCreated` event

#### Fee Management Functions

### `withdrawFeeStEth`

```solidity
function withdrawFeeStEth() public onlyOwner
```

Withdraws accumulated platform fees.

* **Access Control**
  * Restricted to factory owner
* **Effects**
  * Transfers all wstETH balance to owner
  * Emits `LisFeeClaimed` event
* **Requirements**
  * Factory must have non-zero wstETH balance

### Events

#### `NGOCreated`

```solidity
event NGOCreated(
    string _name,
    string _imageLink,
    string _description,
    string _link,
    address _rewardsOwner,
    address _ngoAddress,
    string _location,
    address oracle
);
```

Emitted when new NGO is deployed.

* Includes all NGO metadata
* Records contract addresses
* Enables frontend/indexer tracking

#### `LisFeeClaimed`

```solidity
event LisFeeClaimed(uint256 _value);
```

Emitted when platform fees are withdrawn.

* Records withdrawal amount
* Provides transparency for fee collection

### Security Features

1. **Access Control**
   * Owner-only administrative functions
   * Protected implementation updates
   * Controlled NGO deployment
2. **Proxy Pattern**
   * Upgradeable NGO instances
   * Consistent implementation reference
   * State isolation between NGOs
3. **Address Validation**
   * Protocol contract verification
   * Owner-NGO relationship tracking
   * Unique NGO deployment per owner

### Integration Guidelines

1. **Deployment Process**

   ```javascript
   // Example deployment sequence
   const factory = await NGOLisFactory.deploy(
     lidoAddress,
     withdrawalAddress,
     implementationAddress,
     wstETHAddress
   );

   // Create NGO instance
   await factory.createNGO(
     "NGO Name",
     "image.url",
     "Description",
     "website.url",
     "Location",
     rewardsOwner,
     ngoAdmin,
     oracleAddress
   );
   ```
2. **Fee Management**

   ```javascript
   // Withdraw platform fees
   await factory.withdrawFeeStEth();
   ```
3. **Implementation Updates**

   ```javascript
   // Update NGO implementation
   await factory.setImplementation(newImplementationAddress);
   ```

### Common Operations

1. **Finding NGO Address**

   ```solidity
   address ngoAddress = factory.ownerToNgo(ownerAddress);
   ```
2. **Verifying Ownership**

   ```solidity
   bool hasNGO = factory.ownerToNgo(address) != address(0);
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://launchnodes.gitbook.io/lido-impact-staking-lis/contracts/ngolisfactory.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
