> For the complete documentation index, see [llms.txt](https://verifund.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://verifund.gitbook.io/docs/getting-started/smart-contracts.md).

# Smart Contracts

Verifund's smart contract ecosystem is built on Solidity ^0.8.20 and utilizes OpenZeppelin's battle-tested contracts for security and reliability. Our contracts are deployed on the Lisk blockchain and handle all core platform functionality.

### Core Contracts

#### 1. **IDRX Token (ERC20)**

* **Purpose**: Native currency for all platform transactions
* **Symbol**: IDRX
* **Decimals**: 2 (following Indonesian Rupiah format)
* **Features**:
  * Unlimited minting for testing environments
  * `mint10k()` function for quick test funding
  * Standard ERC20 functionality with burn capability

#### 2. **CampaignFactory**

* **Purpose**: Central deployment point for all campaigns
* **Key Functions**:
  * `createCampaign()` - Deploy new campaign contracts
  * `getDeployedCampaigns()` - Retrieve all campaign addresses
* **Features**:
  * Automated parameter validation
  * Event logging for monitoring
  * Integration with IDRX and SBT contracts

#### 3. **Campaign**

* **Purpose**: Individual campaign logic and fund management
* **Key Functions**:
  * `donate()` - Accept IDRX donations
  * `withdraw()` - Owner withdrawal with conditions
  * `refund()` - Donor refund for failed campaigns
  * `syncIDRXDonations()` - Sync external transfers
* **Advanced Features**:
  * Peak balance tracking for external donations
  * Verification-based withdrawal privileges
  * Time-locked operations

#### 4. **VerifundSBT (ERC721 Soulbound)**

* **Purpose**: Non-transferable identity verification tokens
* **Key Functions**:
  * `klaimLencanaSaya()` - Claim verification badge
  * `isVerified()` - Check user verification status
  * `getBadgeInfo()` - Comprehensive badge information
* **Features**:
  * Whitelist-based permission system
  * Non-transferable token enforcement
  * IPFS metadata integration

### Contract Interactions

#### Campaign Creation Flow

```solidity
// 1. User calls CampaignFactory
CampaignFactory.createCampaign(
    "Campaign Name",
    1000000, // 10,000.00 IDRX target
    86400,   // 24 hours duration
    "QmIPFSHash..."
);

// 2. Factory deploys new Campaign contract
Campaign newCampaign = new Campaign(
    msg.sender,     // owner
    name,           // campaign name
    targetAmount,   // funding goal
    deadline,       // end time
    ipfsHash,       // metadata
    idrxAddress,    // token contract
    sbtAddress      // verification contract
);
```

#### Donation Flow

```solidity
// 1. Donor approves IDRX spending
IDRX.approve(campaignAddress, donationAmount);

// 2. Donor calls campaign donate function
Campaign.donate(donationAmount);

// 3. Campaign transfers IDRX and updates state
// - Transfers tokens from donor to campaign
// - Updates amountRaised
// - Records donation in mapping
// - Emits Donated event
```

#### Verification-Based Withdrawal

```solidity
// Campaign withdrawal logic considers:
bool targetReached = actualBalance >= targetAmount;
bool ownerVerified = verifundSBT.isVerified(owner);

// Withdrawal allowed if:
require(
    targetReached || ownerVerified,
    "Target not reached and owner not verified"
);
```

### Security Features

#### Access Control

* **Owner-only functions**: Protected by `onlyOwner` modifier
* **Time-based restrictions**: `beforeDeadline` and `afterDeadline` modifiers
* **Verification checks**: Integration with SBT contract for privileged operations

#### Fund Safety

* **Escrow mechanism**: Funds held in campaign contracts until conditions met
* **Automatic refunds**: Available for failed campaigns with unverified owners
* **Peak balance tracking**: Prevents fund manipulation in external transfers

#### Reentrancy Protection

* **Checks-Effects-Interactions**: Proper state updates before external calls
* **OpenZeppelin guards**: Utilizing battle-tested security patterns
* **Safe token transfers**: Using `transferFrom` with proper validation

### Contract Addresses

#### Lisk Sepolia Testnet

```
IDRX Token:        0x31c0C6e0F048d259Cd8597e1e3594F842555b235
VerifundSBT:       0x388878A2e2c404a2567c070a4C39D9A75EFFeb61
CampaignFactory:   0x5fc7BB8359fE43Ad330711708A573b9A502f0534
```

#### Lisk Mainnet

```
IDRX Token:        0x18Bc5bcC660cf2B9cE3cd51a404aFe1a0cBD3C22
VerifundSBT:       0x3eb9972beA8f2fDE0C011c77E432f3706078E65E
CampaignFactory:   0x72c4C531d16458622Fbd3B050923a1A2c7E69E3E
```

### Gas Optimization

#### Efficient Data Storage

* **Packed structs**: Minimize storage slots usage
* **Immutable variables**: Reduce SSTORE operations for constants
* **Batch operations**: Group related state changes

#### Function Optimization

* **View functions**: Read-only operations don't consume gas
* **Event emission**: Cost-effective data logging
* **Minimal external calls**: Reduce inter-contract communication overhead

***

Continue to IDRX Token Contract for detailed contract specifications.
