Example 1

Creating and Trading in a Market

gopackage main

import (
    "context"
    "fmt"
    "log"
    "os"
    "time"
    
    "github.com/ayush78490/Go-Predix/gopredix-sdk"
)

func main() {
    // Initialize client
    client, err := gopredix.NewClient("https://your-rpc-endpoint.com")
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    ctx := context.Background()
    privateKey := os.Getenv("PRIVATE_KEY")

    // Step 1: Create a new market
    createReq := &gopredix.CreateMarketRequest{
        Question:        "Will ETH reach $5000 by end of 2025?",
        Category:        "crypto",
        ResolutionTime:  time.Now().AddDate(0, 0, 365).Unix(),
        Outcomes:        []string{"YES", "NO"},
        InitialLiquidity: 1000,
        PrivateKey:      privateKey,
    }

    marketID, err := client.CreateMarket(ctx, createReq)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Created market: %s\n", marketID)

    // Step 2: Place a buy order
    orderReq := &gopredix.PlaceOrderRequest{
        MarketID:   marketID,
        Outcome:    "YES",
        OrderType:  "LIMIT",
        Side:       "BUY",
        Quantity:   50,
        Price:      0.60,
        PrivateKey: privateKey,
    }

    orderID, err := client.PlaceOrder(ctx, orderReq)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Order placed: %s\n", orderID)

    // Step 3: Check your position
    userAddr := "0x..." // Your wallet address
    position, err := client.GetPosition(ctx, userAddr, marketID, "YES")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Position - Shares: %f, Value: %f\n", position.Shares, position.CurrentValue)
}

Last updated