Skip to content

Ambrosians420/SplinterSwap

Repository files navigation

Splinterlands Trade API

A Flask-based API for handling Splinterlands asset trades on the Hive blockchain with automatic fee collection.

Features

  • Secure trade creation and confirmation
  • Automatic DEC fee collection
  • Transaction ID logging
  • Comprehensive error handling
  • Authentication system
  • Trade status tracking
  • Database persistence

Setup

1. Install Dependencies

pip install -r requirements.txt

2. Environment Configuration

Create a .env file with the following variables:

Flask Configuration

SECRET_KEY=your-super-secret-key-change-this-in-production

Hive Configuration

ADMIN_WALLET=splintermarket_admin FEE_AMOUNT=100 HIVE_NODE=https://api.hive.blog

Splinterlands API Configuration

SPLINTERLANDS_API=https://api.splinterlands.io MARKET_API=https://api.splinterlands.io/market AUTH_API=https://api.splinterlands.io/auth/login


### 3. Run the Application

```bash
python app.py

The application will start on http://localhost:5000

API Endpoints

Health Check

  • GET /health
  • Returns application status

Create Trade

  • POST /create_trade
  • Body:
{
  "proposer": "user1",
  "receiver": "user2",
  "proposer_assets": ["card1", "card2"],
  "receiver_assets": ["card3", "card4"],
  "proposer_value": 100.0,
  "receiver_value": 100.0
}

Get Trade Details

  • GET /get_trade/<trade_id>
  • Returns complete trade information

Confirm Trade

  • POST /confirm_trade/<trade_id>
  • Body:
{
  "user": "username",
  "is_proposer": true,
  "private_key": "user_private_key"
}

List Trades

  • GET /list_trades
  • Query Parameters:
    • user: Filter by user
    • status: Filter by status (pending, completed, cancelled)

Cancel Trade

  • POST /cancel_trade/<trade_id>
  • Body:
{
  "user": "username"
}

Database Schema

The application uses SQLite with the following schema:

CREATE TABLE trades (
    trade_id INTEGER PRIMARY KEY AUTOINCREMENT,
    proposer TEXT NOT NULL,
    receiver TEXT NOT NULL,
    proposer_assets TEXT NOT NULL,
    receiver_assets TEXT NOT NULL,
    proposer_value REAL DEFAULT 0,
    receiver_value REAL DEFAULT 0,
    status TEXT DEFAULT 'pending',
    last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    transaction_id TEXT,
    proposer_confirmed BOOLEAN DEFAULT FALSE,
    receiver_confirmed BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Security Features

  • Authentication decorator for protected endpoints
  • Input validation for all trade data
  • Error handling and logging
  • SQL injection prevention with parameterized queries
  • Environment variable configuration

Trade Flow

  1. Create Trade: User creates a trade with assets and values
  2. Confirm Trade: Both parties must confirm the trade
  3. Execute Trade: Once both parties confirm, the trade is executed on Hive
  4. Fee Collection: DEC fees are automatically transferred to admin wallet
  5. Status Update: Trade status is updated to 'completed'

Error Handling

The application includes comprehensive error handling for:

  • Invalid trade data
  • Insufficient authority (RC)
  • Missing private keys
  • Database errors
  • Network issues

Logging

All operations are logged with appropriate levels:

  • INFO: Successful operations
  • ERROR: Failed operations with details
  • DEBUG: Detailed debugging information

Development

To run in development mode:

export FLASK_ENV=development
python app.py```env

Production Deployment

For production deployment:

  1. Set proper environment variables
  2. Use a production WSGI server (gunicorn, uwsgi)
  3. Set up proper logging
  4. Configure database backups
  5. Set up monitoring and alerting

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors