# Blockchain Integration

#### Blockchain Integration

**SynaptiQ Systems** integrates blockchain technology for secure, on-chain decision-making.

It allows agents to interact with blockchain networks like Ethereum and Solana, enabling trustless operations and verifiable task execution.

**Key Features**

* **Smart Contract Deployment**: Deploy contracts to automate task verification and execution.
* **On-Chain Decision-Making**: Nodes interact with blockchain for trustless operations and decentralized coordination.
* **Secure Wallet Management**: Wallet configurations are securely managed using environment variables for paths and private keys.

***

#### Example: Deploying a Smart Contract

Here’s how to deploy and interact with a smart contract on Ethereum using **SynaptiQ Systems**' Blockchain Integration module:

**Code Example:**

```python
pythonCopy codefrom src.utils.blockchain_manager import BlockchainManager

# Initialize the Blockchain Manager
blockchain = BlockchainManager()

# Define contract ABI and Bytecode
abi = [
    {
        "constant": True,
        "inputs": [],
        "name": "getValue",
        "outputs": [{"name": "", "type": "uint256"}],
        "payable": False,
        "stateMutability": "view",
        "type": "function",
    }
]
bytecode = "0x608060405234801561001057600080fd5b5060405161010038038061010083398101806040528101908080518201929190505050806000819055505060d58061003e6000396000f3fe6080604052600080fdfea165627a7a723058202cfa2cf67d..."

# Deploy the contract
contract_address = blockchain.deploy_contract(abi, bytecode)
print(f"Contract deployed at: {contract_address}")

# Interact with the contract
result = blockchain.call_contract_function(contract_address, abi, "getValue")
print(f"Result from contract: {result}")
```

***

#### Managing Wallets Securely

**SynaptiQ Systems** supports secure wallet management by dynamically retrieving wallet configurations using environment variables.

**Solana Wallet**

Set the wallet path using the `SOLANA_WALLET_PATH` environment variable:

**Code Example:**

```bash
bashCopy codeSOLANA_WALLET_PATH=/path/to/solana-wallet.json
```

**Ethereum Wallet**

Set the private key using the `ETHEREUM_WALLET_PRIVATE_KEY` environment variable:

**Code Example:**

```bash
bashCopy codeETHEREUM_WALLET_PRIVATE_KEY=your_ethereum_private_key_here
```

***

#### Code Snippet for Wallet Loading

**Code Example:**

```python
pythonCopy codeimport os

# Load wallets securely
solana_wallet_path = os.getenv("SOLANA_WALLET_PATH")
ethereum_private_key = os.getenv("ETHEREUM_WALLET_PRIVATE_KEY")

print(f"Solana Wallet Path: {solana_wallet_path}")
print("Ethereum Private Key Loaded.")
```

***

#### Common Issues and Troubleshooting

| **Problem**                                          | **Solution**                                                                                                 |
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| **FileNotFoundError: Solana wallet path not found.** | Ensure the `SOLANA_WALLET_PATH` environment variable is set correctly.                                       |
| **ValueError: Ethereum private key not configured.** | Set the `ETHEREUM_WALLET_PRIVATE_KEY` environment variable.                                                  |
| **Contract deployment failed.**                      | Verify your RPC URL is correct, ensure sufficient funds for gas fees, and check the ABI/bytecode for errors. |

***

#### Wallet Configuration

**Environment Variables for Wallets**

Wallet paths and private keys are securely managed using environment variables. This approach ensures sensitive data is protected and avoids hardcoding values in source files.

**Referencing Wallets in YAML**

In the `config.yaml` file, wallets are referenced dynamically using their respective environment variables:

**Code Example:**

```yaml
yamlCopy codeblockchain:
  solana:
    wallet_env_var: "SOLANA_WALLET_PATH"
  ethereum:
    wallet_env_var: "ETHEREUM_WALLET_PRIVATE_KEY"
```

***

#### Instructions for Developers

1.  **Set the Environment Variables**: Add variables to your `.env` file or export them in your shell:

    **Code Example:**

    ```bash
    bashCopy codeexport SOLANA_WALLET_PATH=/path/to/solana-wallet.json
    export ETHEREUM_WALLET_PRIVATE_KEY=your_private_key_here
    ```
2.  **Edit the YAML File**: Specify the environment variable names for wallet configurations:

    **Code Example:**

    ```yaml
    yamlCopy codesolana:
      wallet_env_var: "SOLANA_WALLET_PATH"
    ethereum:
      wallet_env_var: "ETHEREUM_WALLET_PRIVATE_KEY"
    ```
3.  **Run the Framework**: The framework automatically loads these variables at runtime:

    **Code Example:**

    ```python
    pythonCopy codefrom src.utils.blockchain_manager import BlockchainManager
    blockchain = BlockchainManager()
    balance = blockchain.solana_get_balance("your_solana_address_here")
    print(f"Solana balance: {balance}")
    ```

***

#### Security Benefits

* **Environment-Specific**: Easily set different wallets for development, staging, and production environments.
* **No Hardcoding**: Protects sensitive data from being exposed in source control.
* **Scalability**: Works seamlessly in CI/CD pipelines and cloud-based deployments.
