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

https://github.com/ivanzzeth/probable-go-clob-client

Go CLOB SDK for probable.markets
https://github.com/ivanzzeth/probable-go-clob-client

Last synced: about 1 month ago
JSON representation

Go CLOB SDK for probable.markets

Awesome Lists containing this project

README

          

# Probable Markets Go CLOB Client

A Go SDK for interacting with the Probable Markets CLOB (Central Limit Order Book) API.

## Features

- **Dual Authentication Support**:
- L1 Authentication: Wallet-based authentication using EIP-712 signatures
- L2 Authentication: API Key-based authentication using HMAC-SHA256 signatures (faster, no gas fees)

- **Type Safety**: Strong typing throughout the SDK, no `interface{}` or `any` types
- **Error Handling**: Comprehensive error definitions in the `errs` package
- **Examples**: Complete examples for all authentication operations

## Installation

```bash
go get github.com/ivanzzeth/probable-go-clob-client
```

## Quick Start

### 1. Generate API Key (L1 Authentication)

```go
package main

import (
"context"
"log"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ivanzzeth/ethsig"
probableclob "github.com/ivanzzeth/probable-go-clob-client"
)

func main() {
// Parse private key
privateKey, err := crypto.HexToECDSA("your_private_key_hex")
if err != nil {
log.Fatal(err)
}

// Create signer
signer := ethsig.NewEthPrivateKeySigner(privateKey)

// Create client
client, err := probableclob.NewClient(
probableclob.WithSigner(signer),
)
if err != nil {
log.Fatal(err)
}

// Generate L2 API key
ctx := context.Background()
credentials, err := client.GenerateL2APIKey(ctx)
if err != nil {
log.Fatal(err)
}

log.Printf("API Key: %s", credentials.Key)
log.Printf("Secret: %s", credentials.Secret)
log.Printf("Passphrase: %s", credentials.Passphrase)
}
```

### 2. Use L2 Authentication (API Key + HMAC)

```go
package main

import (
"context"
"log"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ivanzzeth/ethsig"
probableclob "github.com/ivanzzeth/probable-go-clob-client"
)

func main() {
// Parse private key (required for prob_address header)
privateKey, err := crypto.HexToECDSA("your_private_key_hex")
if err != nil {
log.Fatal(err)
}

signer := ethsig.NewEthPrivateKeySigner(privateKey)

// Create client with API credentials
client, err := probableclob.NewClient(
probableclob.WithAPICredentials(
"your_api_key",
"your_api_secret",
"your_api_passphrase",
),
probableclob.WithSigner(signer),
)
if err != nil {
log.Fatal(err)
}

// Verify L2 credentials
ctx := context.Background()
result, err := client.VerifyL2Credentials(ctx)
if err != nil {
log.Fatal(err)
}

if result.Valid {
log.Println("L2 credentials verified!")
}
}
```

## Authentication Methods

### L1 Authentication (Wallet Signature)

- Uses EIP-712 signature with your private key
- Required for generating API keys
- Requires gas fees for on-chain operations
- More secure but slower

### L2 Authentication (API Key + HMAC)

- Uses HMAC-SHA256 signature with API credentials
- No gas fees required
- Faster and more efficient for API calls
- Requires API credentials generated via L1 authentication

## Examples

See the [examples](./examples) directory for complete examples:

### Authentication Examples
- [Generate API Key](./examples/auth/generate-api-key) - Generate API credentials using L1 authentication
- [Get API Key](./examples/auth/get-api-key) - Retrieve current API key
- [Delete API Key](./examples/auth/delete-api-key) - Delete current API key
- [Verify Credentials](./examples/auth/verify-credentials) - Verify API credentials are valid

### Events Examples
- [List Events](./examples/events/list-events) - List all events with filtering and sorting
- [Get Event by ID](./examples/events/get-event-by-id) - Get event details by numeric ID
- [Get Event by Slug](./examples/events/get-event-by-slug) - Get event details by slug
- [Get Event Tags](./examples/events/get-event-tags) - Get tags for an event

### Markets Examples
- [List Markets](./examples/markets/list-markets) - List all markets with filtering
- [Get Market by ID](./examples/markets/get-market-by-id) - Get market details by ID
- [Get Market by Polymarket ID](./examples/markets/get-market-by-polymarket-id) - Get market by Polymarket ID
- [Get Market by BSC Question ID](./examples/markets/get-market-by-bsc-question-id) - Get market by BSC question ID

### Orderbook Examples
- [Get Orderbook](./examples/orderbook/get-orderbook) - Get orderbook for a market
- [Get Price](./examples/orderbook/get-price) - Get price for a token
- [Get Prices](./examples/orderbook/get-prices) - Get prices for multiple tokens
- [Get Midpoint](./examples/orderbook/get-midpoint) - Get midpoint price
- [Get Price History](./examples/orderbook/get-price-history) - Get price history for a market

### Search Examples
- [Search](./examples/search/search) - Full-text search across events and markets

### Chain Operation Examples
- [Enable Trading](./examples/chain/enable-trading) - Enable trading by approving necessary tokens
- [Split](./examples/chain/split) - Split collateral into outcome tokens
- [Merge](./examples/chain/merge) - Merge outcome tokens back into collateral
- [Redeem](./examples/chain/redeem) - Redeem positions for resolved markets

All examples support loading environment variables from a `.env` file:

```bash
PRIVATE_KEY=your_private_key_hex
API_KEY=your_api_key
API_SECRET=your_api_secret
API_PASSPHRASE=your_api_passphrase
```

Run examples:

```bash
# Generate API key
PRIVATE_KEY=your_key go run ./examples/auth/generate-api-key

# Get API key (L2 auth)
API_KEY=your_key API_SECRET=your_secret API_PASSPHRASE=your_passphrase PRIVATE_KEY=your_key go run ./examples/auth/get-api-key

# Verify credentials
API_KEY=your_key API_SECRET=your_secret API_PASSPHRASE=your_passphrase PRIVATE_KEY=your_key go run ./examples/auth/verify-credentials
```

## API Reference

### Client Options

- `WithHost(host string)` - Set the API host URL (default: `https://api.probable.markets/public/api/v1`)
- `WithAPIKey(apiKey string)` - Set API key for L2 authentication
- `WithAPICredentials(apiKey, apiSecret, apiPassphrase string)` - Set API credentials for L2 authentication
- `WithChainID(chainID *big.Int)` - Set chain ID (default: 56 for BNB Chain)
- `WithRPCURL(rpcURL string)` - Set RPC URL for chain operations
- `WithSigner(signer Signer)` - Set signer for L1 authentication
- `WithSafeTradingSigner(safeSigner SafeTradingSigner, safeAddress common.Address)` - Set Safe trading signer and Safe wallet address for chain operations
- `WithHTTPTimeout(timeout time.Duration)` - Set HTTP client timeout (default: 30s)

### Authentication Methods

- `GenerateL2APIKey(ctx context.Context) (*APIKeyCredential, error)` - Generate L2 API key using L1 authentication
- `GetAPIKey(ctx context.Context) (*APIKey, error)` - Get current API key (supports L1/L2)
- `DeleteAPIKey(ctx context.Context) (*DeleteAPIKey, error)` - Delete current API key (supports L1/L2)
- `VerifyL2Credentials(ctx context.Context) (*VerifyL2Credentials, error)` - Verify L2 authentication headers (HMAC signature)

### Search Methods

- `Search(ctx context.Context, opts SearchOptions) (*SearchResponse, error)` - Full-text search across events and markets with comprehensive filtering, sorting, and caching options

### Chain Operation Methods

- `EnableTrading(ctx context.Context) ([]string, error)` - Enable trading by approving necessary tokens (returns transaction hashes)
- `Split(ctx context.Context, conditionID common.Hash, amount decimal.Decimal) (string, error)` - Split collateral into outcome tokens
- `SplitByMarketID(ctx context.Context, marketID int64, amount decimal.Decimal, checkApproval bool) (string, error)` - Split by market ID
- `Merge(ctx context.Context, conditionID common.Hash, amount decimal.Decimal) (string, error)` - Merge outcome tokens back into collateral
- `MergeByMarketID(ctx context.Context, marketID int64, amount decimal.Decimal, checkApproval bool) (string, error)` - Merge by market ID
- `Redeem(ctx context.Context, conditionID common.Hash) (string, error)` - Redeem positions for resolved markets
- `RedeemByMarketID(ctx context.Context, marketID int64, checkApproval bool) (string, error)` - Redeem by market ID
- `CheckBalanceAndAllowance(ctx context.Context) (*BalanceAllowanceInfo, error)` - Check balance and allowances
- `PrintBalanceAndAllowance(ctx context.Context) error` - Print balance and allowance status

## Development

This SDK follows the development principles outlined in `agent/golang/go_sdk_dev.md`:

- All types are strongly typed (no `interface{}` or `any`)
- Comprehensive error handling with custom error types
- All API responses are properly typed
- Examples for all operations
- Environment variable support via `.env` files

## License

MIT License - see [LICENSE](./LICENSE) file for details.