npm package: @hellomoon/api
Description: SDK for Hello Moon's REST API, Streaming, and Subscription Management
Author: Hello Moon
Latest version: 1.23.2

# Hello Moon API SDK

## Installation

### Package Manager
Using npm:
```
$ npm install @hellomoon/api
```

Using yarn:
```
$ yarn add @hellomoon/api
```

## Managing subscriptions

### Listing your subscriptions
```ts
const { RestClient } = require("@hellomoon/api");

const client = new RestClient(process.env.API_KEY);
client.send(new ListStreamsRequest()).then(subscriptions => {
    // Do something with your subscriptions
}).catch(console.error);
```

### Create a subscription
```ts
const { RestClient, TokenSwapStream, dataStreamFilters } = require("@hellomoon/api");

const stream = new TokenSwapStream({
    target: {
        targetType: "WEBSOCKET",
    },
    filters: {
        programId: dataStreamFilters.text.equals("whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc")
    },
    name: "SDK Test"
});

const client = new RestClient(process.env.API_KEY);
client.send(new CreateStreamsRequest(stream)).then(subscription => {
    // Do something with your subscription
}).catch(console.error);
```

### Update a subscription
```ts
const { RestClient, TokenSwapStream, dataStreamFilters } = require("@hellomoon/api");

const stream = new TokenSwapStream({
    id: "YOUR_SUBSCRIPTION_ID",
    target: {
        targetType: "WEBSOCKET",
    },
    filters: {
        programId: dataStreamFilters.text.equals("whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc")
    },
    name: "SDK Test"
});

const client = new RestClient(process.env.API_KEY);
client.send(new UpdateStreamsRequest(stream)).then(subscription => {
    // Do something with your subscription
}).catch(console.error);
```

### Delete a subscription
```ts
const { RestClient } = require("@hellomoon/api");

const client = new RestClient(process.env.API_KEY);
client.send(new DeleteStreamsRequest("YOUR_SUBSCRIPTION_ID")).then(() => {
}).catch(console.error);
```

### Available Streams
#### [BalanceChangeStream](https://docs.hellomoon.io/docs/balance-change)
> Balance changes for all accounts at a transaction level. Each transaction will have 0 or more balance changes.
#### [LpBalanceChangeStream](https://docs.hellomoon.io/docs/lp-balance-changes)
> Balance changes for Liquidity Pool (LP) at a transaction level.
#### [LpCreationStream](https://docs.hellomoon.io/docs/lp-creations)
>	Creation of new Liquidity Pools (LPs)
#### [LpDepositWithdrawalStream](https://docs.hellomoon.io/docs/lp-deposits-withdrawals)
> Liquidity has been deposited or withdrawn from the Liquidity Pool (LP).
#### [NftCollectionListingStatsStream](https://docs.hellomoon.io/docs/nft-collection-listing-stats)
> Listing stats for the NFT collection. This is at the collection level. Each collection is emitted at most once per block. If a block contains multiple instruction that modify the stats for a collection, this describes the last instruction.
#### [NftMarketActionStream](https://docs.hellomoon.io/docs/nft-secondary-market-actions)
>  Actions taken on secondary NFT markets, e.g. MagicEden. These are generally emitted in the order as they happen on-chain, but new collections may be delayed.
#### [NftPrimarySaleWithCollectionStream](https://docs.hellomoon.io/docs/nft-collection-listing-stats-1)
> NFT tokens that are minted or sold from the collection to the first owner during the mint phase. These are not emitted in the order as they happen on-chain. To receive the events as they happen on-chain, use NftPrimarySaleWithoutCollection.
#### [NftPrimarySaleWithoutCollectionStream](https://docs.hellomoon.io/docs/nft-primary-sales-without-collection)
> NFT tokens that are minted or sold from the collection to the first owner during the mint phase. These are emitted in the order as they happen on-chain. To receive the events with the collection name, use NftPrimarySaleWithCollection.
#### [SharkyEventStream](https://docs.hellomoon.io/docs/events-for-sharky-protocol)
> Events related to the Sharky protocol
#### [TokenPriceStream](https://docs.hellomoon.io/docs/token-price)
> This stream emits events everytime Hello Moon determines a liquidity pool has changed the valuation of a tokens
#### [TokenSwapStream](https://docs.hellomoon.io/docs/token-swap)
> This stream emits events anytime a token is swapped on a DEX that Hello Moon monitors.
#### [TokenTransferStream](https://docs.hellomoon.io/docs/token-transfer)
> Balance changes for all accounts at a transaction level. Each transaction will have 0 or more balance changes.

## Streaming

### Subscribe to a Websocket Subscription
```ts
const { StreamClient, isStreamError } = require("@hellomoon/api");

/** 
* The format of the data you expect to receive back from your stream.
* We don't know what a stream will return ahead of time, it is optional to define for typing support
*/
type MyDataType {
    mint: string;
}

const client = new StreamClient(process.env.API_KEY);
client.connect(data => {
    // A fallback message catcher.  This shouldn't fire, but can be used for system messages come through
    console.log(data);
}).then(disconnect => {
    const unsubscribe = client.subscribe("YOUR_SUBSCRIPTION_ID", data => {
        // An array of streamed events
        console.log(data);
    });
    // Disconnect after 60 seconds
    setTimeout(disconnect, 60000);
}, err => {
    // Handle error
    console.log(err);
}).catch(console.error);
```

### Query a Websocket Subscription
```ts
const { StreamClient, TokenPriceRequest } = require("@hellomoon/api");

const client = new StreamClient(process.env.API_KEY);
client.connect(data => {
    // A fallback message catcher.  This shouldn't fire, but can be used for system messages come through
    console.log(data);
}).then(disconnect => {
    client.send(new TokenPriceRequest({
        
    }).then(responseData => {
        // The response object
        console.log(responseData);
        // Disconnect when we are done
        disconnect();
    }, err => {
        // Handle error
    }));
    
}).catch(console.error);
```

### Combining streaming with Query
```ts
const { StreamClient, isStreamError } = require("@hellomoon/api");

/** 
* The format of the data you expect to receive back from your stream.
* We don't know what a stream will return ahead of time, it is optional to define for typing support
*/
type MyDataType {
    mint: string;
}

const client = new StreamClient(process.env.API_KEY);
client.connect(data => {
    // A fallback message catcher.  This shouldn't fire, but can be used for system messages come through
    console.log(data);
}).then(disconnect => {
    const unsubscribe = client.subscribe<MyDataType>("YOUR_SUBSCRIPTION_ID", data => {        
    client.send(new TokenPriceRequest({
            mint: data[0].mint
        }).then(price => {
            // The response object
            console.log(data);
            // Disconnect when we are done        
        }, err => {
            // Handle error
        }));
    });
    // Disconnect after 10 seconds
    setTimeout(disconnect, 10000);
}).catch(console.error);
```

## Rest API

```ts
const { RestClient, NftListingStatusRequest } = require("@hellomoon/api");

const client = new RestClient(process.env.API_KEY);
client
  .send(new NftListingStatusRequest())
  .then(console.log)
  .catch(console.error);
```

### Available Rest Requests
- AllTimeTxnsByUserRequest
- BlockRewardsRequest
- CitrusLoanEventsRequest
- CitrusLoanSummaryRequest
- CollaterizedLoanSummaryRequest
- CollectionAllTimeRequest
- CollectionFloorpriceRequest
- CollectionListingCandlesticksRequest
- CollectionListingStatsRequest
- CollectionMintsRequest
- CollectionNameRequest
- CollectionProgramUsageRequest
- CollectionStatsRequest
- CollectionVolatilityRequest
- CumulativeTokenVolumeRequest
- CurrentLpEmissionsRequest
- DailyNftAvgMedianSalesRequest
- DefiLendingRequest
- DefiLiquidityPoolsBalancesRequest
- DefiLiquidityPoolsMetadataRequest
- DefiLiquidityPoolsRequest
- DefiProgramLeaderboardV2Request
- DefiProgramNetNewUsersDailyRequest
- DefiProgramOverlapRequest
- DefiSwapProgramsRequest
- DefiTokenLeaderboardV3Request
- DefiTokenNetNewPurchasesRequest
- DefiTokenUsersDailyRequest
- FilterSetRequest
- FraktLoanEventsRequest
- FraktLoanSummaryRequest
- JupiterCurrentStatsRequest
- JupiterHistoricalTradingStatsRequest
- JupiterPairsBrokenDownWeeklyRequest
- JupiterSwapStatsRequest
- LeaderboardStatsRequest
- NftCollectionOwnersCurrentV1Request
- NftDistinctOwnersInTimeDailyV1Request
- NftHoldingPeriodV1Request
- NftListingStatusRequest
- NftListingsRequest
- NftMarketStatsRequest
- NftMintInformationRequest
- NftMintPriceByCreatorAvgRequest
- NftMintsByOwnerRequest
- NftOverlappingOwnersV1Request
- NftOwnersCumulativeV1Request
- NftPrimarySaleCollectionStatsRequest
- NftSocialRequest
- NftTopHoldersRequest
- OverlapTopRequest
- SalesPerMarketDailyRequest
- SalesPrimaryRequest
- SalesSecondaryRequest
- SharkyApyRequest
- SharkyDefaultStatsRequest
- SharkyHistoricalDefaultsRequest
- SharkyLoanEventsRequest
- SharkyLoanSummaryRequest
- SolanaArtemisProgramStatsRequest
- SolanaTxnsByUserRequest
- SplTokenListRequest
- Stakeaccountsv2Request
- Stakeactionsv2Request
- Staketransfersv2Request
- StreamCreateRequest
- StreamUpdateRequest
- TokenBalancesByOwnerRequest
- TokenPriceCandlesticksRequest
- TokenPriceRequest
- TokenSwapsRequest
- TokenTransfersWithOwnerRequest
- Tokencreationv2Request
- Tokensupplyv2Request
- TopTokenSellBuyJupRequest
- TopTokensPerProgram24hrRequest
- WashtradingCollectionIndexV7Request