# Subscriptions

## 📡 Subscriptions API

The GoFundMeme SDK provides a WebSocket-based subscription system that allows real-time updates for Fair Launches, Bonding Curve swaps, Market Capitalization changes, Pool State updates, and more.

With *gfmSDK.api.subscription*, developers can efficiently listen to live events without polling.

### 🔥 Key Features

✔️ Real-Time Pool State Updates – Get notified when a pool’s status changes.

✔️ Live Market Cap Updates – Track the market cap of a token dynamically.

✔️ Bonding Curve Swaps – Receive buy/sell events in real-time.

✔️ Fair Launch Funding Events – Monitor funding and defunding actions.

✔️ New & Migrated Pools – Be alerted when a pool is created or migrated.

✔️ Seamless Disconnection – Easily unsubscribe when needed.

📜 How to Use Subscriptions

<br>

To start listening to real-time events, you can use the gfmSDK.api.subscription object and call one of the available listeners.

### 🚀 Quick Example

```typescript
import { Connection } from "@solana/web3.js";
import { initGoFundMemeSDK, NETWORK } from "@gofundmeme/sdk";

const connection = new Connection("https://api.mainnet-beta.solana.com");

(async () => {
    const gfmSDK = await initGoFundMemeSDK({ connection });

    // Listen for funding events on Fair Launch pools in Mainnet
    const listener = gfmSDK.api.subscription.fairLaunch.funding.all(NETWORK.MAINNET);

    listener.subscription.subscribe((event) => {
        console.log("📡 Fair Launch Funding Event:", event);
    });
})();
```

🛠️ Explanation:

1️⃣ Initialize the SDK with a Solana RPC connection.

2️⃣ Subscribe to a WebSocket event (gfmSDK.api.subscription.fairLaunch.funding.all).

3️⃣ Log incoming events in real time.

📡 Available Subscription Methods<br>

***

## 1️⃣ Pool State Updates

Listens for pool state changes, such as raising, launching, or launched.

Subscribe by Network

```typescript
const listener = gfmSDK.api.subscription.poolState.all(NETWORK.MAINNET);
listener.subscription.subscribe((event) => console.log("📡 Pool State Update:", event));
```

Subscribe by Mint Address

```typescript
const listener = gfmSDK.api.subscription.poolState.byMintAddress("YOUR_MINT_ADDRESS");
listener.subscription.subscribe((event) => console.log("📡 Pool State Update:", event));
```

📌 Event Type:

```typescript
type PoolStateEvent = {
    status: PoolStatus;
    totalRaised: number;
    raisePercent: number;
    token: TokenInfo;
    pool: PoolInfo;
    raiseType: RaiseType;
    dexType: DexType;
    network: NETWORK;
}
```

***

## 2️⃣ Market Cap Updates

Tracks market capitalization changes in real-time.

Subscribe by Network

```typescript
const listener = gfmSDK.api.subscription.marketcap.all(NETWORK.MAINNET);
listener.subscription.subscribe((event) => console.log("📡 Market Cap Update:", event));
```

Subscribe by Mint Address

```typescript
const listener = gfmSDK.api.subscription.marketcap.byMintAddress("YOUR_MINT_ADDRESS");
listener.subscription.subscribe((event) => console.log("📡 Market Cap Update:", event));
```

📌 Event Type:

```typescript
type MarketcapUpdateEvent = {
    network: NETWORK;
    mintAddress: string;
    mcSOL: number;
}
```

***

## 3️⃣ Bonding Curve Swaps

Listens for buy and sell transactions on Bonding Curve pools.

Subscribe by Network

```typescript
const listener = gfmSDK.api.subscription.bondingCurve.swaps.all(NETWORK.MAINNET);
listener.subscription.subscribe((event) => console.log("📡 Swap Event:", event));
```

Subscribe by Mint Address

```typescript
const listener = gfmSDK.api.subscription.bondingCurve.swaps.byMintAddress("YOUR_MINT_ADDRESS");
listener.subscription.subscribe((event) => console.log("📡 Swap Event:", event));
```

📌 Event Type:

```typescript
type SwapEvent = {
    direction: 'buy' | 'sell';
    solAmountChange: number;
    tokenAmountChange: number;
    price: number;
    pooledSOL: number;
    targetSOL: number;
    poolAddress: string;
    funderAddress: string;
    mintAddress: string;
    txid: string;
}
```

***

## 4️⃣ Fair Launch Funding Events

Tracks contributions (funding) and withdrawals (defunding) from Fair Launch pools.

Subscribe by Network

```typescript
const listener = gfmSDK.api.subscription.fairLaunch.funding.all(NETWORK.MAINNET);
listener.subscription.subscribe((event) => console.log("📡 Funding Event:", event));
```

Subscribe by Mint Address

```typescript
const listener = gfmSDK.api.subscription.fairLaunch.funding.byMintAddress("YOUR_MINT_ADDRESS");
listener.subscription.subscribe((event) => console.log("📡 Funding Event:", event));
```

📌 Event Type:

```typescript
type FundingEvent = {
    direction: 'fund' | 'defund';
    solAmountChange: number;
    pooledSOL: number;
    targetSOL: number;
    poolAddress: string;
    funderAddress: string;
    mintAddress: string;
    txid: string;
}
```

***

## 5️⃣ New & Migrated Pools

These subscriptions notify you when a pool is created or migrated.

New Pools (All Networks)

```typescript
const listener = gfmSDK.api.subscription.pool.newPools(NETWORK.MAINNET);
listener.subscription.subscribe((event) => console.log("📡 New Pool Created:", event));
```

Migrated Pools (All Networks)

```typescript
const listener = gfmSDK.api.subscription.pool.migratedPools(NETWORK.MAINNET);
listener.subscription.subscribe((event) => console.log("📡 Pool Migrated:", event));
```

📌 Event Type:

```typescript
type PoolStateEvent = {
    status: PoolStatus;
    totalRaised: number;
    raisePercent: number;
    token: TokenInfo;
    pool: PoolInfo;
    raiseType: RaiseType;
    dexType: DexType;
    network: NETWORK;
}
```

***

## 🔌 Unsubscribing & Disconnecting

You can unsubscribe from a specific subscription or disconnect from all WebSocket events.

Unsubscribe from a Specific Event

```
const listener = gfmSDK.api.subscription.fairLaunch.funding.all(NETWORK.MAINNET);
listener.subscription.subscribe((event) => console.log("📡 Funding Event:", event));

// After 30 seconds, unsubscribe
setTimeout(() => {
    console.log("🔌 Disconnecting from funding events...");
    listener.disconnect();
}, 30000);
```

Disconnect from All Subscriptions

```
gfmSDK.api.subscription.disconnectAll();
console.log("🚫 All WebSocket connections closed.");
```

***

## 🎯 Final Summary

| Subscription Type   | Method                                                                         |
| ------------------- | ------------------------------------------------------------------------------ |
| Pool State Updates  | poolState.all(network), poolState.byMintAddress(mintAddress)                   |
| Market Cap Updates  | marketcap.all(network), marketcap.byMintAddress(mintAddress)                   |
| Bonding Curve Swaps | bondingCurve.swaps.all(network), bondingCurve.swaps.byMintAddress(mintAddress) |
| Fair Launch Funding | fairLaunch.funding.all(network), fairLaunch.funding.byMintAddress(mintAddress) |
| New Pools           | pool.newPools(network)                                                         |
| Migrated Pools      | pool.migratedPools(network)                                                    |
| Unsubscribe         | listener.disconnect()                                                          |
| Disconnect All      | gfmSDK.api.subscription.disconnectAll()                                        |

🚀 Instant Updates – No need for manual polling!

⚡ Efficient WebSockets – Optimized real-time data streams.

🔗 Simple API – Easy to integrate, minimal effort required.
