Skip to content

Commit bfed79e

Browse files
committed
cointoss working
1 parent 67ab1b6 commit bfed79e

13 files changed

+5697
-0
lines changed

integrations/cointoss/README.md

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# CoinToss Agent
2+
3+
A decentralized toss agent built using CDP AgentKit that operates over the XMTP messaging protocol, enabling group toss on custom topics.
4+
5+
## Features
6+
7+
- XMTP group chat support (responds to @toss mentions)
8+
- Natural language bet creation (e.g., "Will it rain tomorrow for 10 USDC")
9+
- Support for custom betting topics and options
10+
- Multiple player support with option-based prize distribution
11+
- Wallet address display for transparency and accountability
12+
- Transaction hash links for payment verification
13+
- Automated prize distribution to all winners
14+
- Real-time messaging through XMTP
15+
16+
## Prerequisites
17+
18+
- Node.js (v20+)
19+
- [OpenAI](https://platform.openai.com/) API key
20+
- [Coinbase Developer Platform](https://portal.cdp.coinbase.com) (CDP) API credentials
21+
22+
## Quick Start Guide
23+
24+
Follow these steps to get your CoinToss agent up and running quickly:
25+
26+
1. **Clone the repository**:
27+
28+
```bash
29+
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
30+
cd integrations/cointoss
31+
```
32+
33+
2. **Install dependencies**:
34+
35+
```bash
36+
yarn install
37+
```
38+
39+
3. **Generate XMTP keys**:
40+
41+
```bash
42+
yarn build
43+
yarn gen:keys
44+
```
45+
46+
This will generate random wallet and encryption keys for your agent and output them to the console. Copy these values to your `.env` file.
47+
48+
49+
4. **Set up your environment variables**:
50+
Create a `.env` file with the following variables:
51+
52+
```
53+
# Required: OpenAI API Key
54+
OPENAI_API_KEY=your_openai_api_key_here
55+
56+
# Required: Coinbase Developer Platform credentials
57+
CDP_KEY_NAME=your_api_key_name_here
58+
CDP_API_KEY_PRIVATE_KEY=your_api_key_private_key_here
59+
60+
# Required: XMTP wallet and encryption keys (from step 3)
61+
WALLET_KEY=your_wallet_private_key_here
62+
ENCRYPTION_KEY=your_encryption_key_here
63+
64+
# Optional: Network ID (defaults to base-sepolia if not specified)
65+
NETWORK_ID=base-sepolia
66+
67+
# Optional: Redis for persistent storage (if not provided, local file storage will be used)
68+
REDIS_URL=redis://localhost:6379
69+
```
70+
71+
5. **Build and start the agent**:
72+
73+
```bash
74+
yarn build
75+
yarn start
76+
```
77+
78+
For development with more detailed logs:
79+
```bash
80+
yarn dev
81+
```
82+
83+
6. **Invite the agent to your XMTP group chat**:
84+
The agent responds when tagged with `@toss` in a group chat.
85+
86+
## Usage Examples
87+
88+
The agent responds to commands in group chats when tagged with `@toss`:
89+
90+
### Available Commands
91+
92+
- `@toss create <amount>` - Create a new betting game with specified USDC amount
93+
- `@toss join <gameId> <option>` - Join a game and select your option
94+
- `@toss execute <gameId>` - Execute the bet resolution (creator only)
95+
- `@toss status <gameId>` - Check game status and participants
96+
- `@toss list` - List all active games
97+
- `@toss balance` - Check your wallet balance
98+
- `@toss <natural language bet>` - Create a bet using natural language
99+
100+
### Natural Language Examples
101+
- `@toss Will it rain tomorrow for 5` - Creates a yes/no bet with 5 USDC
102+
- `@toss Lakers vs Celtics game for 10` - Creates a bet with Lakers and Celtics as options
103+
104+
### Example Flow
105+
106+
1. **Create a game**: `@toss Will Bitcoin hit $100k this year for 5`
107+
2. **Join the game**: `@toss join 1 yes` (each player must choose an option)
108+
3. **Check status**: `@toss status 1`
109+
4. **Execute the bet**: `@toss execute 1` (creator only)
110+
5. **View results**: All players who chose the winning option share the prize pool
111+
112+
## How It Works
113+
114+
This CoinToss agent combines several technologies:
115+
116+
1. **XMTP Protocol**: For group chat messaging interface
117+
2. **Coinbase AgentKit**: For wallet management and payments
118+
3. **Storage Options**: Redis or local file storage for game and wallet data
119+
4. **LLM Integration**: For natural language bet parsing
120+
121+
The agent workflow:
122+
1. Users create or join betting games in group chats
123+
2. Each player is assigned a unique wallet
124+
3. The game creator determines when to execute the bet
125+
4. A random option is selected as the winner
126+
5. Prize money is split among all players who chose the winning option
127+
128+
## Prize Distribution
129+
130+
- All bets are collected in a dedicated game wallet
131+
- When the game is executed, a winning option is randomly selected
132+
- All players who chose the winning option share the prize pool equally
133+
- Automatic transfers are sent to each winner's wallet
134+
- Transaction confirmations are provided in the chat
135+
136+
## Troubleshooting
137+
138+
### Wallet Creation Errors
139+
140+
If you see errors like `Failed to create wallet: APIError`:
141+
142+
1. **Coinbase API Keys**:
143+
- Verify your API key name matches exactly as shown in the Coinbase Developer Dashboard
144+
- Ensure your private key includes the complete PEM format with BEGIN/END lines
145+
- Format multiline keys properly for your .env file
146+
147+
2. **Network Issues**:
148+
- Check your internet connectivity and API endpoint access
149+
- Verify there are no Coinbase service outages
150+
151+
If you're still encountering issues, try clearing your local wallet data:
152+
```bash
153+
rm -rf .data/wallets
154+
```
155+
156+
## Architecture
157+
158+
- **Wallet Management**: Coinbase SDK for wallet creation and transfers
159+
- **XMTP Integration**: Group chat support with @toss tag handling
160+
- **Unified Agent System**:
161+
- Single AI agent for both natural language parsing and wallet operations
162+
- **Game Logic**:
163+
- Random selection of winning option
164+
- Fair prize distribution among winners
165+
- **Storage Options**: Local file storage or Redis
166+
167+
## Security
168+
169+
- Each user and game gets a dedicated Coinbase wallet
170+
- Encrypted wallet storage
171+
- Transparent wallet address display
172+
- Transaction verification through block explorer links
173+
- Advanced randomness for fair winner selection
174+
175+
## License
176+
177+
MIT
178+
179+
## Code Structure
180+
181+
The CoinToss agent codebase is organized with a modular design:
182+
183+
- **src/index.ts**: Main entry point that initializes the system and handles XMTP message routing
184+
- **src/commands.ts**: Command processor that handles all explicit commands and natural language bets
185+
- **src/cdp.ts**: Agent for natural language processing and wallet operations
186+
- **src/game.ts**: Game logic implementation
187+
- **src/storage.ts**: Storage layer for persisting games and wallet data
188+
- **src/walletService.ts**: Wallet functions for creating and managing game wallets
189+
- **src/xmtp.ts**: XMTP integration for messaging
190+
- **src/types.ts**: Type definitions

0 commit comments

Comments
 (0)