# Blockchain Smart Contract Interaction in Axion Framework

####

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

The **Axion Framework** empowers agents to interact seamlessly with blockchain smart contracts, enabling secure, verifiable, and decentralized operations. This functionality is critical for automating workflows, ensuring trustless execution, and leveraging blockchain features like task logging and resource allocation.

***

#### Key Features

1. **Smart Contract Deployment**\
   Agents can deploy smart contracts on blockchain networks like Ethereum and Solana to automate tasks, enforce workflows, or enable on-chain governance.
2. **Function Invocation**\
   Agents can invoke smart contract functions to perform computations, retrieve data, or trigger actions.
3. **On-Chain Task Logging**\
   Record task results and system states directly on the blockchain for transparency, auditing, and collaboration.
4. **Multi-Chain Support**
   * **Ethereum**: Suitable for complex computations and security-intensive tasks.
   * **Solana**: Optimized for high-speed, low-cost transactions.

***

#### Examples of Blockchain Interaction

**1. Deploying a Smart Contract**

Agents can deploy contracts to enable decentralized workflows or enforce logic on-chain.

**Python Code Example:**

```python
from axion.blockchain.blockchain_manager import BlockchainManager

# Initialize the Blockchain Manager
blockchain = BlockchainManager(network="ethereum")

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

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

***

**2. Calling a Smart Contract Function**

Once deployed, agents can interact with smart contract functions.

**Python Code Example:**

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

***

**3. Logging Tasks On-Chain**

Agents can log task results or important events directly on the blockchain for immutable records.

**Python Code Example (Solana):**

```python
transaction_hash = blockchain.log_task(
    sender_keypair="path/to/solana_keypair.json",
    task_description="Analyze weather patterns",
    task_result="Task completed successfully"
)
print(f"Task logged on blockchain. Transaction hash: {transaction_hash}")
```

***

#### Wallet Configuration

To interact with blockchains, secure wallet configuration is essential. Use environment variables to store sensitive information.

**Solana Wallet Configuration**

Set the wallet path in your environment:

```bash
export SOLANA_WALLET_PATH=/path/to/solana-wallet.json
```

**Ethereum Wallet Configuration**

Set the private key in your environment:

```bash
export ETHEREUM_WALLET_PRIVATE_KEY=your_private_key_here
```

**Access Wallets in Code**

**Python Code Example:**

```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 the correctness of task execution, ensuring accountability.
2. **Resource Allocation:**\
   Manage and distribute resources on-chain to ensure transparency.
3. **Decentralized Governance:**\
   Implement voting mechanisms and decision-making processes for agent swarms.

***

#### Troubleshooting

| **Problem**                           | **Solution**                                           |
| ------------------------------------- | ------------------------------------------------------ |
| `FileNotFoundError: Wallet not found` | Ensure `SOLANA_WALLET_PATH` is correctly set.          |
| `ValueError: Ethereum key missing`    | Add `ETHEREUM_WALLET_PRIVATE_KEY` to your environment. |
| Contract deployment failed            | Check RPC URL, gas fees, ABI, and bytecode for errors. |

***

####


---

# 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://axions-organization.gitbook.io/axion-framework/blockchain-smart-contract-interaction-in-axion-framework.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.
