> ## Documentation Index
> Fetch the complete documentation index at: https://docs.story.foundation/llms.txt
> Use this file to discover all available pages before exploring further.

# Claim Revenue

> Learn how to claim due revenue from a child IP Asset in TypeScript.

<CardGroup cols={1}>
  <Card title="Completed Code" href="https://github.com/storyprotocol/typescript-tutorial/blob/main/scripts/royalty/payRevenue.ts" icon="thumbs-up">
    All of this page is covered in this working code example.
  </Card>
</CardGroup>

This section demonstrates how to claim due revenue from an IP Asset.

There are two main ways revenue can be claimed:

1. **Scenario #1**: Someone pays my IP Asset directly, and I claim that revenue.
2. **Scenario #2**: Someone pays a derivative IP Asset of my IP, and I have the right to a % of their revenue based on the `commercialRevShare` in the license terms.

### Prerequisites

There are a few steps you have to complete before you can start the tutorial.

1. Complete the [TypeScript SDK Setup](/developers/typescript-sdk/setup)
2. Have a basic understanding of the [Royalty Module](/concepts/royalty-module)
3. Obviously, there must be a payment to be claimed. Read [Pay an IPA](/developers/typescript-sdk/pay-ipa)

## Before We Start

When payments are made, they eventually end up in an IP Asset's [IP Royalty Vault](/concepts/royalty-module/ip-royalty-vault). From here, they are claimed/transferred to whoever owns the Royalty Tokens associated with it, which represent a % of revenue share for a given IP Asset's IP Royalty Vault.

The IP Account (the smart contract that represents the [IP Asset](/concepts/ip-asset)) is what holds 100% of the Royalty Tokens when it's first registered. So usually, it indeed holds most of the Royalty Tokens.

<Note>
  **Quick Note**. The below scenarios and examples use a [Liquid Absolute Percentage](/concepts/royalty-module/liquid-absolute-percentage) royalty policy. This is currently one of two royalty policies you can use.
</Note>

## Scenario #1

In this scenario, I own IP Asset 3. Someone pays my IP Asset 3 directly, and I claim that revenue. Let's view this in steps:

1. As we can see in the below diagram, when someone pays IP Asset 3 100 \$WIP, 85 \$WIP automatically gets deposited into IP Royalty Vault 3 (based on the license terms, which specifiies a LAP royalty policy and the resulting royalty stack).

   <Frame>
     <img src="https://mintcdn.com/story/KwuxPVrS3UIa7CU1/images/concepts/royalty-module/royalty-module-split-lap.png?fit=max&auto=format&n=KwuxPVrS3UIa7CU1&q=85&s=c24fbc48c17c96574628a8eac087f7d9" alt="Payment flow to IP Royalty Vault" width="2164" height="1218" data-path="images/concepts/royalty-module/royalty-module-split-lap.png" />
   </Frame>

2. Now, IP Asset 3 wants to claim its revenue sitting in the IP Royalty Vault 3. It will look like this:

   <Frame>
     <img src="https://mintcdn.com/story/KwuxPVrS3UIa7CU1/images/concepts/royalty-module/ipa3-claims.png?fit=max&auto=format&n=KwuxPVrS3UIa7CU1&q=85&s=1ff2c5d8750ab747782cbeae050af512" alt="Claiming revenue from IP Royalty Vault" width="2196" height="1272" data-path="images/concepts/royalty-module/ipa3-claims.png" />
   </Frame>

Below is how IP Asset 3 would claim their revenue, as shown in the image above, with the SDK:

<Info>
  Associated Docs:
  [royalty.claimAllRevenue](/sdk-reference/royalty#claimallrevenue)
</Info>

<Note>
  Claiming revenue is permissionless. Any wallet can run the claim revenue transaction for an IP.
</Note>

```typescript main.ts theme={null}
import { WIP_TOKEN_ADDRESS } from "@story-protocol/core-sdk";
// you should already have a client set up (prerequisite)
import { client } from "./client";

async function main() {
  const claimRevenue = await client.royalty.claimAllRevenue({
    // IP Asset 3's ipId
    ancestorIpId: "0x03",
    // the address that owns the royalty tokens,
    // which is the IP Account in this case
    claimer: "0x03",
    currencyTokens: [WIP_TOKEN_ADDRESS],
    childIpIds: [],
    royaltyPolicies: [],
    claimOptions: {
      // If the wallet claiming the revenue is the owner of the
      // IP Account/IP Asset (in other words, the owner of the
      // IP's underlying NFT), `claimAllRevenue` will transfer all
      // earnings to the user's external wallet holding the NFT
      // instead of the IP Account, for convenience. You can disable it here.
      autoTransferAllClaimedTokensFromIp: true,
      // Unwraps the claimed $WIP to $IP for you
      autoUnwrapIpTokens: true,
    },
  });

  console.log(`Claimed revenue: ${claimRevenue.claimedTokens}`);
}

main();
```

## Scenario #2

In this scenario, I own IP Asset 1. Someone pays a derivative IP Asset 3, and I have the right to a % of their revenue based on the `commercialRevShare` in the license terms. This is exactly the same as Scenario #1, except one extra step is added. Let's view this in steps:

1. As we can see in the below diagram, when someone pays IP Asset 3 100 \$WIP, 15 \$WIP automatically gets deposited to the LAP royalty policy contract to be distributed to ancestors.

   <Frame>
     <img src="https://mintcdn.com/story/KwuxPVrS3UIa7CU1/images/concepts/royalty-module/royalty-module-split-lap.png?fit=max&auto=format&n=KwuxPVrS3UIa7CU1&q=85&s=c24fbc48c17c96574628a8eac087f7d9" alt="Revenue distribution to royalty policy contract" width="2164" height="1218" data-path="images/concepts/royalty-module/royalty-module-split-lap.png" />
   </Frame>

2. Then, in a second step, the tokens are transferred to the ancestors' [IP Royalty Vault](/concepts/royalty-module/ip-royalty-vault) based on the negotiated `commercialRevShare` in the license terms.

   <Frame>
     <img src="https://mintcdn.com/story/Ci-RgMKFKns3XzFq/images/concepts/royalty-module/claim-to-ip1.png?fit=max&auto=format&n=Ci-RgMKFKns3XzFq&q=85&s=eda4d640fd26720785e91a9968fb416c" alt="Revenue distribution to ancestors" width="2186" height="1270" data-path="images/concepts/royalty-module/claim-to-ip1.png" />
   </Frame>

3. Lastly, IP Asset 1 wants to claim their revenue sitting in its associated IP Royalty Vault. It will look like this:

   <Frame>
     <img src="https://mintcdn.com/story/Ci-RgMKFKns3XzFq/images/concepts/royalty-module/claim-to-ip1-account.png?fit=max&auto=format&n=Ci-RgMKFKns3XzFq&q=85&s=084d29d3231f6d30449b4d8e1f249840" alt="Claiming revenue from ancestor IP Royalty Vaults" width="2192" height="1304" data-path="images/concepts/royalty-module/claim-to-ip1-account.png" />
   </Frame>

Below is how IP Asset 1 (or 2) would claim their revenue, as shown in the image above, with the SDK:

<Info>
  Associated Docs:
  [royalty.claimAllRevenue](/sdk-reference/royalty#claimallrevenue)
</Info>

<Note>
  Claiming revenue is permissionless. Any wallet can run the claim revenue transaction for an IP.
</Note>

```typescript main.ts theme={null}
import { WIP_TOKEN_ADDRESS } from "@story-protocol/core-sdk";
// you should already have a client set up (prerequisite)
import { client } from "./client";

async function main() {
  const claimRevenue = await client.royalty.claimAllRevenue({
    // IP Asset 1's ipId
    ancestorIpId: "0x01",
    // the address that owns the royalty tokens,
    // which is the IP Account in this case
    claimer: "0x01",
    currencyTokens: [WIP_TOKEN_ADDRESS],
    // IP Asset 3's ipId
    childIpIds: ["0x03"],
    // Aeneid testnet address of RoyaltyPolicyLAP
    royaltyPolicies: ["0xBe54FB168b3c982b7AaE60dB6CF75Bd8447b390E"],
    claimOptions: {
      // If the wallet claiming the revenue is the owner of the
      // IP Account/IP Asset (in other words, the owner of the
      // IP's underlying NFT), `claimAllRevenue` will transfer all
      // earnings to the user's external wallet holding the NFT
      // instead of the IP Account, for convenience. You can disable it here.
      autoTransferAllClaimedTokensFromIp: true,
      // Unwraps the claimed $WIP to $IP for you
      autoUnwrapIpTokens: true,
    },
  });

  console.log(`Claimed revenue: ${claimRevenue.claimedTokens}`);
}

main();
```

## Scenario #3

In this scenario, I own IP Asset 1. Someone pays a derivative IP Asset 3, and I have the right to a % of their revenue based on the `commercialRevShare` in the license terms. The difference here is that I have previously transferred the royalty tokens in the IP Account to an external wallet, most commonly the wallet that owns the IP. This is exactly the same as Scenario #2, except royalty is being claimed to an external wallet instead of the IP Account. Let's view this in steps:

1. As we can see in the below diagram, when someone pays IP Asset 3 100 \$WIP, 15 \$WIP automatically gets deposited to the LAP royalty policy contract to be distributed to ancestors.

   <Frame>
     <img src="https://mintcdn.com/story/KwuxPVrS3UIa7CU1/images/concepts/royalty-module/royalty-module-split-lap.png?fit=max&auto=format&n=KwuxPVrS3UIa7CU1&q=85&s=c24fbc48c17c96574628a8eac087f7d9" alt="Revenue distribution to royalty policy contract" width="2164" height="1218" data-path="images/concepts/royalty-module/royalty-module-split-lap.png" />
   </Frame>

2. Then, in a second step, the tokens are transferred to the ancestors' [IP Royalty Vault](/concepts/royalty-module/ip-royalty-vault) based on the negotiated `commercialRevShare` in the license terms.

   <Frame>
     <img src="https://mintcdn.com/story/Ci-RgMKFKns3XzFq/images/concepts/royalty-module/claim-to-ip1.png?fit=max&auto=format&n=Ci-RgMKFKns3XzFq&q=85&s=eda4d640fd26720785e91a9968fb416c" alt="Revenue distribution to ancestors" width="2186" height="1270" data-path="images/concepts/royalty-module/claim-to-ip1.png" />
   </Frame>

3. Lastly, IP Asset 1 wants to claim their revenue sitting in its associated IP Royalty Vault. It will look like this:

   <Frame>
     <img src="https://mintcdn.com/story/Ci-RgMKFKns3XzFq/images/concepts/royalty-module/claim-to-ip1-owner.png?fit=max&auto=format&n=Ci-RgMKFKns3XzFq&q=85&s=61dcedfdebb7170e07572641a5e752e9" alt="Claiming revenue from ancestor IP Royalty Vaults" width="1756" height="1164" data-path="images/concepts/royalty-module/claim-to-ip1-owner.png" />
   </Frame>

Below is how IP Asset 1 (or 2) would claim their revenue, as shown in the image above, with the SDK:

<Info>
  Associated Docs:
  [royalty.claimAllRevenue](/sdk-reference/royalty#claimallrevenue)
</Info>

<Note>
  Claiming revenue is permissionless. Any wallet can run the claim revenue transaction for an IP.
</Note>

```typescript main.ts theme={null}
import { WIP_TOKEN_ADDRESS } from "@story-protocol/core-sdk";
// you should already have a client set up (prerequisite)
import { client } from "./client";

async function main() {
  // transfer the royalty tokens from the
  // ip account to the external wallet
  // NOTE: this can only be called by the IP owner
  // and only needs to be called once. Any future
  // claims will be to this external wallet
  const royaltyVaultAddress = await client.royalty.getRoyaltyVaultAddress("0x01");
  const transferRoyaltyTokens = await client.ipAccount.transferErc20({
    ipId: "0x01",
    tokens: [
      {
        address: royaltyVaultAddress,
        amount: 100_000_000, // 100% of the royalty tokens
        target: "0x04", // the external wallet
      },
    ],
  });

  // claim the revenue to the external wallet
  const claimRevenue = await client.royalty.claimAllRevenue({
    // IP Asset 1's ipId
    ancestorIpId: "0x01",
    // the address that owns the royalty tokens,
    // which is the external wallet in this case
    claimer: "0x04",
    currencyTokens: [WIP_TOKEN_ADDRESS],
    // IP Asset 3's ipId
    childIpIds: ["0x03"],
    // Aeneid testnet address of RoyaltyPolicyLAP
    royaltyPolicies: ["0xBe54FB168b3c982b7AaE60dB6CF75Bd8447b390E"],
    claimOptions: {
      // If the wallet claiming the revenue is the owner of the
      // IP Account/IP Asset (in other words, the owner of the
      // IP's underlying NFT), `claimAllRevenue` will transfer all
      // earnings to the user's external wallet holding the NFT
      // instead of the IP Account, for convenience. You can disable it here.
      autoTransferAllClaimedTokensFromIp: true,
      // Unwraps the claimed $WIP to $IP for you
      autoUnwrapIpTokens: true,
    },
  });

  console.log(`Claimed revenue: ${claimRevenue.claimedTokens}`);
}

main();
```

## View Completed Code

Congratulations, you claimed revenue using the [Royalty Module](/concepts/royalty-module)!

<CardGroup cols={1}>
  <Card title="Completed Code" href="https://github.com/storyprotocol/typescript-tutorial/blob/main/scripts/royalty/payRevenue.ts" icon="thumbs-up">
    All of this page is covered in this working code example.
  </Card>
</CardGroup>

## Dispute an IP

Now what happens if an IP Asset doesn't pay their due share? We can dispute the IP on-chain, which we will cover on the next page.
