An open API service indexing awesome lists of open source software.

https://github.com/zuyux/sphinx-canisters

canisters for interacting with sphinx stacks contracts
https://github.com/zuyux/sphinx-canisters

Last synced: 9 months ago
JSON representation

canisters for interacting with sphinx stacks contracts

Awesome Lists containing this project

README

          

# sphinx-canisters

πŸ“¦ Canisters for interacting with Sphinx πŸ”— Stacks contracts

## πŸ“ Overview

The **sphinx-canisters** repository contains 🌐 Internet Computer (ICP) canisters that interact with **Stacks blockchain contracts** for managing 🌍 decentralized question-and-response sessions. The canisters automate operations like βœ… checking if a question has timed out, β›” closing questions, πŸ€– calling OpenAI for evaluation, and πŸ’Έ transferring rewards to participants.

The main components of the repository include:
- `cerebellum.mo`: Handles interactions with the Stacks contracts, calls πŸ€– OpenAI API to evaluate responses, and manages the flow of the Sphinx process.
- `secman.mo`: A πŸ”’ secret manager canister for securely storing API πŸ”‘ keys and private πŸ”‘ keys.

## βš™οΈ Functionality

The canisters provide functionality for:

1. **⏰ Timer-based Automation**
- A recurring ⏱️ timer is set to run every ⏳ hour using the `Timer` module from the πŸ› οΈ Motoko base library. This timer triggers the main operation of checking whether a question is timed out and, if necessary, closes it and evaluates responses.

2. **πŸ”— Stacks API Integration**
- The `cerebellum.mo` canister interacts with the **Stacks blockchain** using 🌐 HTTP outcalls. It checks if a question contract is open or closed, closes it, and initiates πŸ’° fund transfers.
- For interaction, HTTPS πŸ“‘ GET and POST requests are used to call the Stacks contracts.

3. **πŸ€– OpenAI Integration**
- The canister integrates with the **OpenAI API** to generate insights or rank user responses for specific questions. The chosen response is used to decide the πŸ† winner in the decentralized Sphinx process.

4. **πŸ”’ Secret Management**
- The `secman.mo` canister is responsible for securely managing sensitive information such as the **OpenAI API key** and **Stacks contract owner’s private key**.

## πŸš€ How it Works

1. **⏰ Recurring Timer**
- A timer runs every ⏳ hour to check the status of questions. If a question is found to be closed (timed out), it proceeds to initiate closure operations.

2. **πŸ” Question Status Check**
- The canister sends a πŸ“‘ GET request to the **Stacks blockchain API** to check if the current question is open or closed. If it is open, the canister continues monitoring until a timeout occurs.

3. **β›” Close Question and Evaluate Responses**
- Once a question is deemed timed out, the canister sends a πŸ“‘ POST request to close it on the **Stacks blockchain**.
- After the question is closed, it retrieves all user responses stored in a contract. If there are responses available, the canister builds a prompt and calls the **OpenAI API** to evaluate and determine the best response.

4. **πŸ’Έ Fund Transfer to Winner**
- The πŸ† winner of the question (based on πŸ€– OpenAI evaluation) receives a reward. The canister initiates a πŸ“‘ POST request to the Stacks contract to transfer πŸ’° funds to the winning participant's address.
- If no responses are available in the contract, no winner is declared, and $PHI tokens on the contract's pool are burned.

## πŸ› οΈ Setup Instructions

1. **πŸ“₯ Clone the Repository**
```sh
git clone https://github.com/zuyux/sphinx-canisters.git
cd sphinx-canisters
```

2. **πŸ”§ Install Dependencies**
Ensure that you have **DFX** (the 🌐 Internet Computer SDK) installed. You also need the **Motoko compiler**.

3. **πŸ“ Update Configuration**
Update the **dfx.json** file with the appropriate πŸ†” Principal IDs for the Stacks contracts, πŸ€– OpenAI API keys, and other necessary configurations.

4. **πŸš€ Deploy Canisters**
Use the following commands to deploy the canisters:
```sh
dfx start --background
dfx deploy
```

## βš™οΈ Configuration Details

- **πŸ”— Stacks API URLs**: The canister uses the Stacks API to interact with contracts. Make sure to use the correct endpoints for reading contract data (`GET`) and writing to contracts (`POST`).
- **πŸ”’ Authorization**: To interact with the Stacks blockchain, the owner's private key is used. It is stored securely in the `secman.mo` canister.

## πŸ“‚ Key Files

- **`cerebellum.mo`**: Manages the core logic for interacting with the Sphinx contracts, πŸ€– OpenAI API calls, and flow management.
- **`secman.mo`**: A πŸ”’ secret manager for storing sensitive credentials like API keys.

## πŸ“ Example Workflow

1. **⏱️ Timer triggers** every hour to check the question status.
2. **πŸ” Status check**: Sends a πŸ“‘ GET request to determine if the question is timed out.
3. If timed out, **β›” closes the question** using a πŸ“‘ POST request.
4. Retrieves responses and **evaluates with πŸ€– OpenAI**.
5. **πŸ’Έ Transfers funds** to the selected winning address via the Stacks contract.

## πŸ“„ License
This repository is licensed under the πŸ“ MIT License.