> For the complete documentation index, see [llms.txt](https://aether-framework.gitbook.io/aetherframework/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aether-framework.gitbook.io/aetherframework/blockchain-smart-contract-interaction.md).

# Blockchain Smart Contract Interaction

**Overview**

Aether Framework includes built-in support for interacting with blockchain smart contracts. This feature enables agents to execute trustless, verifiable actions such as task logging, resource allocation, and decentralized governance.

The framework supports **Ethereum** and **Solana**, with additional functionality for deploying, calling, and managing smart contracts.

***

#### **Key Features**

* **Smart Contract Deployment**: Deploy contracts directly from agents to enable automation and on-chain verification.
* **Function Invocation**: Call contract functions to perform tasks or retrieve data.
* **On-Chain Task Logging**: Record task results securely for auditing and collaboration.
* **Multi-Chain Support**: Use Ethereum for complex computations and Solana for high-speed, low-cost transactions.

***

#### **Examples**

**1. Deploying a Smart Contract**

Agents can deploy smart contracts on Ethereum to automate tasks and establish secure workflows.

```python
from 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 smart contract
contract_address = blockchain.deploy_contract(abi, bytecode)
print(f"Smart contract deployed at: {contract_address}")
```

***

**2. Calling a Smart Contract Function**

Once deployed, agents can interact with the contract's functions to retrieve or modify on-chain data.

```python
# Call a function from the deployed contract
result = blockchain.call_contract_function(contract_address, abi, "getValue")
print(f"Contract result: {result}")
```

***

**3. Logging Tasks on the Blockchain**

Agents can log task results on-chain for secure, immutable storage and auditing.

```python
# Log a task result on-chain
transaction_hash = blockchain.log_task(
    sender_keypair="path/to/solana_keypair.json",
    task_description="Analyze energy consumption data",
    task_result="Task completed successfully"
)
print(f"Task logged on blockchain. Transaction hash: {transaction_hash}")
```

***

#### **Wallet Configuration**

To interact with blockchains, agents need to securely configure wallets. Use **environment variables** to store sensitive information like wallet paths and private keys.

**Solana Wallet**

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

```plaintext
SOLANA_WALLET_PATH=/path/to/solana-wallet.json
```

**Ethereum Wallet**

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

```plaintext
ETHEREUM_WALLET_PRIVATE_KEY=your_private_key_here
```

**Accessing Wallets in Code**

```python
import os

# Load Solana wallet path
solana_wallet_path = os.getenv("SOLANA_WALLET_PATH")
print(f"Solana Wallet Path: {solana_wallet_path}")

# Load Ethereum private key
ethereum_private_key = os.getenv("ETHEREUM_WALLET_PRIVATE_KEY")
print("Ethereum Private Key Loaded.")
```

***

#### **Common Use Cases**

1. **Task Verification**: Deploy contracts to verify that tasks were executed correctly.
2. **Resource Allocation**: Use contracts to manage on-chain resource distribution.
3. **Decentralized Governance**: Implement voting mechanisms for swarm decision-making.

***

#### **Common Issues and Solutions**

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

***

#### **Best Practices**

* Always use environment variables to manage sensitive information like private keys.
* For high-security applications, encrypt wallets and only decrypt them during runtime.
* Test smart contracts thoroughly on testnets (e.g., Ethereum’s Goerli or Solana’s Devnet) before deploying to mainnets.
* Use multi-signature wallets for tasks involving significant resources or sensitive data.

***
