# BBSubscriptions

{% embed url="<https://github.com/backedby/v1-contracts/blob/main/contracts/BBSubscriptions.sol>" %}

For each ERC20 token accepted by the protocol, there is a corresponding BBSubscriptions contract.

## Functions

### subscribe

```solidity
function subscribe(
    uint256 profileId,
    uint256 tierId
) external payable returns (uint256 subscriptionId)
```

Create a subscription to a Profile at a specific Tier.

#### Parameters

| Name      | Type    | Description                         |
| --------- | ------- | ----------------------------------- |
| profileId | uint256 | Profile to create a subscription to |
| tierId    | uint256 | Subscription Tier to subscribe to.  |

#### Return Values

| Name           | Type    | Description                              |
| -------------- | ------- | ---------------------------------------- |
| subscriptionId | uint256 | The ID of the newly created subscription |

### unsubscribe

```solidity
unsubscribe(
    uint256 profileId, 
    uint256 tierId
) external
```

Unsubscribe from a Profile

| Name      | Type    | Description                 |
| --------- | ------- | --------------------------- |
| profileId | uint256 | Profile to unsubscribe from |
| tierId    | uint256 | Tier to unsubscribe from    |

### checkUpkeep

```solidity
function checkUpkeep(
    bytes calldata checkData
) external view override returns (bool, bytes memory)
```

Generate input for performUpkeep

#### Parameters

| Name      | Type  | Description       |
| --------- | ----- | ----------------- |
| checkData | bytes | api.encoded input |

#### checkData Structure

```solidity
abi.decode(checkData, (uint256, uint256, uint256, uint256, address))
```

| Name           | Type    | Description                                           |
| -------------- | ------- | ----------------------------------------------------- |
| lowerBound     | uint256 | Starting subscriptionId to check for renewal          |
| upperBoard     | uint256 | Ending subscriptionId to check for renewal            |
| minRenews      | uint256 | Minimum number of renewals to find                    |
| maxRenews      | uint256 | Maximum number of renewals to find                    |
| refundReceiver | address | Account to receive the gas refund from performUpkeep. |

#### Return Values

| Name         | Type  | Description                                                                     |
| ------------ | ----- | ------------------------------------------------------------------------------- |
| upkeepNeeded | bool  | Returns true if the number of subscriptions to renew is greater than minRenews. |
| performData  | bytes | Data to pass thru to performUpkeep if upkeepNeeded is true.                     |

#### performData Structure

```solidity
abi.encode(renewIndexes, refundReceiver)
```

| Name           | Type       | Description                   |
| -------------- | ---------- | ----------------------------- |
| renewIndexes   | uint256\[] | Subscription IDs to renew     |
| refundReceiver | address    | Account to send gas refund to |

### performUpkeep

```solidity
function performUpkeep(
    bytes calldata renewalData
) external
```

PerformUpkeep is a standard `AutomationCompatibleInterface` function.  Input can be automatically generated by the checkUpkeep() function or manually built.

#### Parameters

| Name        | Type  | Description            |
| ----------- | ----- | ---------------------- |
| renewalData | bytes | abi.encoded parameters |

#### RenewalData Structure

```solidity
abi.decode(renewalData, (uint256[], address))
```

| Name           | Type       | Description                   |
| -------------- | ---------- | ----------------------------- |
| renewIndexes   | uint256\[] | Subscriber IDs to renew       |
| refundReceiver | address    | Address to send gas refund to |

### withdrawToTreasury

```solidity
function withdrawToTreasury(
) public
```

Sends the ERC20 token balance the instance of BBSubscriptions handles to the treasury of the BBSubscriptionsFactory.

### getSubscriptionFromProfile

```solidity
function getSubscriptionFromProfile(
    uint256 profileId, 
    uint256 tierId, 
    address subscriber
) external view returns (uint256 subscriptionId, uint256 price, uint256 expiration, bool cancelled)
```

Get the subscription data on an account for a given profile and tier.

#### Parameters

| Name       | Type    | Description         |
| ---------- | ------- | ------------------- |
| profileId  | uint256 | Profile to look at  |
| tierId     | uint256 | Tier to look at     |
| subscriber | address | Account to look for |

#### Return Values

| Name           | Type    | Description                                             |
| -------------- | ------- | ------------------------------------------------------- |
| subscriptionId | uint256 | Subscription ID                                         |
| price          | uint256 | Monthly price of subscription                           |
| expiration     | uint256 | Unix timestamp of the when the subscription will expire |
| cancelled      | bool    | Whether or not the subscription is cancelled            |

### getSubscriptionFromId

```solidity
function getSubscriptionFromId(
    uint256 subscriptionId
) external view returns (uint256 profileId, uint256 tierId, address subscriber, uint256 price, uint256 expiration, bool cancelled)
```

Get the details of a subscription based on an ID.

#### Parameters

| Name           | Type    | Description                      |
| -------------- | ------- | -------------------------------- |
| subscriptionId | uint256 | ID of the subscription to return |

#### Return Values

| Name       | Type    | Description                                             |
| ---------- | ------- | ------------------------------------------------------- |
| profileId  | uint256 | ID of the profile                                       |
| tierId     | uint256 | ID of the subscription tier                             |
| subscriber | address | Address of the subscriber                               |
| price      | uint256 | Monthly price of subscription                           |
| expiration | uint256 | Unix timestamp of the when the subscription will expire |
| cancelled  | bool    | Whether or not the subscription is cancelled            |
