# Ad Manager SDK

Ad Manager SDK is a TypeScript library for managing advertising campaigns and credential definitions on a blockchain network. This SDK provides an easy-to-use interface for interacting with smart contracts related to ad management.

## Features

- List credential definitions owned by a specific address
- Update campaigns in the registry
- List active ad sets
- List campaigns owned by a specific address or the current user
- Get budget balance of a specific ad set
- Fund ad sets
- Get the contract address of the asset used to fund ad sets

## Installation

To install the Ad Manager SDK, use npm:

```bash
npm install @moojo/ad-manager-sdk
```

## Usage

First, import the `AdManagerSdk` class and create an instance:

```typescript
import { AdManagerSdk } from '@moojo/ad-manager-sdk'

// Create a private key (you should securely manage this in a real application)
const privateKey = new AdManagerSdk.PrivateKey('your-private-key')

// Create an instance of AdManagerSdk
const adManagerSdk = new AdManagerSdk(privateKey, {
  network: 'mainnet', // or 'testnet'
  debug: true, // Set to false in production
})
```

### List Credential Definitions

```typescript
const ownerAddress = '0x1234...'
const credentialDefinitions = await adManagerSdk.listCredentialDefinitions(ownerAddress)
console.log(credentialDefinitions)
```

### Update a Campaign

```typescript
import { Campaign } from '@moojo/protocol-core'

const campaign: Campaign = {
  // ... campaign details
}

const result = await adManagerSdk.updateCampaign(campaign)
console.log(`Campaign update submitted. Transaction hash: ${result.hash}`)
```

### List Active Ad Sets

```typescript
const activeAdSets = await adManagerSdk.listActiveAdSets()
console.log(activeAdSets)
```

### List Campaigns

```typescript
// List campaigns owned by the current user
const ownCampaigns = await adManagerSdk.listCampaigns('own')
console.log(ownCampaigns)

// List campaigns owned by a specific address
const address = '0x5678...'
const campaigns = await adManagerSdk.listCampaigns(address)
console.log(campaigns)
```

### Get Balance of Ad Set

```typescript
const adSetId = '0x9ABC...'
const balance = await adManagerSdk.getBalance(adSetId)
console.log(`Ad set ${adSetId} has balance ${balance}`)
```

### Fund an Ad Set

```typescript
// funding ad set #0xDEF0... with 1000 USDC
const adSetId = '0xDEF0...'
const funding = 1000

const hash = await adManagerSdk.fund(adSetId, funding)
console.log(`Ad Set funding submitted. Transaction hash: ${hash}`)
```

### Get Contract Address of Funding Asset

```typescript
const asset = await adManagerSdk.getFundingAsset()
console.log(`Funding contract address: ${asset}`)
```

## Error Handling

The SDK uses a custom error resolution mechanism. All methods can throw errors, so it's recommended to use try-catch blocks:

```typescript
try {
  const campaigns = await adManagerSdk.listCampaigns('own')
  console.log(campaigns)
} catch (error) {
  console.error('An error occurred:', error)
}
```

## License

This project is licensed under the [MIT License](../../LICENSE).
