d by the IDL.
</Note>
## Common CPI Calls
| CPI call | Account context | Parameters | Notes |
| - | - | - | - |
| `cpi::initialize_fee_vault` | `InitializeFeeVaultCtx` | `InitializeFeeVaultParameters` | Creates a keypair-based fee vault. The fee vault account signs. |
| `cpi::initialize_fee_vault_pda` | `InitializeFeeVaultPdaCtx` | `InitializeFeeVaultParameters` | Creates a PDA fee vault from `base` and `token_mint`. The base account signs. |
| `cpi::fund_fee` | `FundFeeCtx` | `max_amount` | Transfers from `fund_token_vault` and updates fee-per-share accounting. |
| `cpi::fund_by_claiming_fee` | `FundByClaimingFeeCtx` | `payload` | Invokes a whitelisted DAMM v2 or DBC instruction using the PDA fee vault as signer. |
| `cpi::claim_fee` | `ClaimFeeCtx` | `index` | Transfers the user's claimable amount from the token vault. |
## Account Requirements
| Flow | Required signer | Important accounts |
| - | - | - |
| Non-PDA initialization | `fee_vault`, `payer` | Fee vault account, fee vault authority PDA, token vault PDA, token mint, owner, token program, system program. |
| PDA initialization | `base`, `payer` | Fee vault PDA, fee vault authority PDA, token vault PDA, token mint, owner, token program, system program. |
| Direct funding | `funder` | Fee vault, token vault, token mint, funder's token account, token program. |
| Source-program funding | `signer` shareholder | PDA fee vault, token vault, source program, downstream remaining accounts. |
| User claim | `user` | Fee vault, fee vault authority PDA, token vault, token mint, user token destination, token program. |
## Source-Program Funding
`fund_by_claiming_fee` is the most account-order-sensitive CPI path. The handler validates:
| Check | Requirement |
| - | - |
| Fee vault type | `fee_vault_type` must be PDA account (`1`). |
| Signer | The `signer` must be one of the configured shareholders. |
| Payload | First 8 bytes must match a whitelisted downstream instruction discriminator. |
| Remaining accounts | The configured remaining-account index for that downstream action must equal the fee vault token vault. |
| Credited amount | The program credits the token vault balance increase after the downstream CPI. |
Whitelisted actions are DAMM v2 `claim_position_fee`, DAMM v2 `claim_reward`, and DBC creator/partner trading fee, surplus, and migration fee withdrawals.
## Token Programs
| Token behavior | Guidance |
| - | - |
| SPL Token | Pass the SPL Token program for `token_program`. |
| Token 2022 | Pass Token 2022 when the fee vault mint is Token 2022. Only transfer-fee, metadata-pointer, and token-metadata extensions are supported by mint validation. |
| Transfer-fee mints | Direct funding credits the transfer-fee excluded amount. Claim transfers use checked transfers from the vault. |
| Native SOL | CPI callers must prepare wrapped SOL token accounts explicitly. The TypeScript SDK wraps and unwraps SOL for top-level transactions. |
## Common CPI Failures
| Error area | CPI-side fix |
| - | - |
| Missing event accounts | Include generated `event_authority` and `program` accounts for `#[event_cpi]` instructions. |
| Invalid user index | Read `FeeVault.users` and pass the index for the signer address. |
| Source funding with non-PDA vault | Create the vault with `initialize_fee_vault_pda`. |
| Invalid source action | Match the source program, discriminator, and remaining-account order from the whitelist. |
| Token vault mismatch | Derive `token_vault` from `["token_vault", fee_vault]` and pass that same account in the downstream remaining-account slot. |
| Token 2022 mint rejection | Use only supported Token 2022 extensions for the fee vault mint. |
## Best Practices
| Practice | Why it matters |
| - | - |
| Prefer generated bindings | Use the Anchor crate with the `cpi` feature or generated IDL bindings instead of hand-typing account order. |
| Read fee vault state first | Validate `token_mint`, `token_vault`, `fee_vault_type`, `base`, `total_share`, and user indexes before building CPI. |
| Keep PDA derivation explicit | Use `fee_vault` seeds from `base` and `token_mint`, and token vault seeds from the fee vault. |
| Treat source funding as advanced | Downstream DAMM v2 and DBC account lists must match the whitelisted instruction exactly. |
| Simulate before sending | Source-program funding performs a downstream CPI and then credits the balance delta, so simulation catches most account-order mistakes. |
| Test each token program variant | SPL Token and Token 2022 paths differ in mint validation and transfer-fee behavior. |
# Dynamic Fee Sharing TS SDK Examples
Source: https://docs.meteora.ag/developer-guides/dynamic-fee-sharing/typescript-sdk/examples
Learn how to build common Dynamic Fee Sharing TypeScript SDK flows for vault creation, funding, source-program claims, user claims, and state reads.
These examples show the SDK flow shape. They return unsigned transactions that your app should sign, simulate, submit, and confirm through its own wallet or backend signer flow.
## Create A Non-PDA Fee Vault
```typescript theme={"system"}
import { Keypair } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { DynamicFeeSharingClient } from "@meteora-ag/dynamic-fee-sharing-sdk";
const client = new DynamicFeeSharingClient(connection, "confirmed");
const feeVault = Keypair.generate();
const tx = await client.createFeeVault({
feeVault: feeVault.publicKey,
tokenMint,
tokenProgram: TOKEN_PROGRAM_ID,
owner: owner.publicKey,
payer: payer.publicKey,
userShare: [
{ address: userA.publicKey, share: 600 },
{ address: userB.publicKey, share: 400 },
],
});
// Sign with payer and feeVault.
```
Use this flow when the vault address can be generated and stored by your integration.
## Create A PDA Fee Vault
```typescript theme={"system"}
import {
deriveFeeVaultPdaAddress,
DynamicFeeSharingClient,
} from "@meteora-ag/dynamic-fee-sharing-sdk";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
const client = new DynamicFeeSharingClient(connection, "confirmed");
const feeVault = deriveFeeVaultPdaAddress(base.publicKey, tokenMint);
const tx = await client.createFeeVaultPda({
base: base.publicKey,
tokenMint,
tokenProgram: TOKEN_PROGRAM_ID,
owner: owner.publicKey,
payer: payer.publicKey,
userShare: [
{ address: userA.publicKey, share: 600 },
{ address: userB.publicKey, share: 400 },
],
});
// Sign with payer and base.
```
Use this flow when the vault must be recoverable from `(base, tokenMint)` or must sign source-program fee claim instructions.
## Fund A Fee Vault Directly
```typescript theme={"system"}
import BN from "bn.js";
const tx = await client.fundFeeVault({
feeVault,
funder: funder.publicKey,
fundAmount: new BN("1000000"),
});
// Sign with funder.
```
The SDK creates the funder's associated token account if needed. For native SOL, it wraps SOL before funding.
## Claim User Fees
```typescript theme={"system"}
const tx = await client.claimUserFee({
feeVault,
user: userA.publicKey,
payer: payer.publicKey,
});
// Sign with payer and userA.
```
Use `claimUserFee2` when the claimant signs but receives into another receiver's associated token account:
```typescript theme={"system"}
const tx = await client.claimUserFee2({
feeVault,
user: userA.publicKey,
payer: payer.publicKey,
receiver: receiver.publicKey,
});
// Sign with payer and userA. The receiver does not sign.
```
## Read Vault State
```typescript theme={"system"}
const feeVaultState = await client.getFeeVault(feeVault);
console.log(feeVaultState.totalFundedFee.toString());
console.log(feeVaultState.totalShare);
```
```typescript theme={"system"}
const breakdown = await client.getFeeBreakdown(feeVault);
for (const userFee of breakdown.userFees) {
console.log(userFee.address.toBase58(), userFee.feeUnclaimed.toString());
}
```
## Fund From DAMM v2
PDA fee vaults can receive DAMM v2 position fees or rewards through `fund_by_claiming_fee`. The fee vault must own the DAMM v2 position NFT account before claiming.
```typescript theme={"system"}
const tx = await client.fundByClaimDammV2Fee({
signer: shareholder.publicKey,
owner: owner.publicKey,
feeVault,
dammV2Pool,
dammV2Position,
dammV2PositionNftAccount,
});
// Sign with shareholder. Include any other signers required by your transaction flow.
```
```typescript theme={"system"}
const tx = await client.fundByClaimDammV2Reward({
signer: shareholder.publicKey,
rewardIndex: 0,
feeVault,
dammV2Pool,
dammV2Position,
dammV2PositionNftAccount,
});
```
If the position NFT account still belongs to another owner, use `setTokenAccountOwnerTx` to transfer its token-account authority to the fee vault before using these claim helpers.
## Fund From DBC
PDA fee vaults can receive DBC creator fees, partner fees, surplus withdrawals, and migration fees through source-program funding wrappers.
| Flow | SDK method | Vault must be configured as |
| - | - | - |
| Creator trading fee | `fundByClaimDbcCreatorTradingFee` | `VirtualPool.creator` |
| Partner trading fee | `fundByClaimDbcPartnerTradingFee` | `PoolConfig.feeClaimer` |
| Creator surplus | `fundByWithdrawDbcCreatorSurplus` | `VirtualPool.creator` |
| Partner surplus | `fundByWithdrawDbcPartnerSurplus` | `PoolConfig.feeClaimer` |
| Migration fee | `fundByWithdrawDbcMigrationFee` | Creator or partner, based on `isPartner` |
```typescript theme={"system"}
const tx = await client.fundByClaimDbcCreatorTradingFee({
signer: shareholder.publicKey,
creator: creator.publicKey,
feeVault,
poolConfig,
virtualPool,
});
```
The SDK fetches `PoolConfig` and `VirtualPool` state when you do not provide cached state.
# Dynamic Fee Sharing TS SDK Getting Started
Source: https://docs.meteora.ag/developer-guides/dynamic-fee-sharing/typescript-sdk/getting-started
Learn how to install @meteora-ag/dynamic-fee-sharing-sdk, create a client, derive fee vault PDAs, and build your first transactions.
This guide shows how to install and initialize the official Dynamic Fee Sharing TypeScript SDK, `@meteora-ag/dynamic-fee-sharing-sdk`.
Before you begin, here are the main resources:
<CardGroup>
<Card title="TypeScript SDK" icon="node-js" href="https://github.com/MeteoraAg/dynamic-fee-sharing-sdk">
Dynamic Fee Sharing TypeScript SDK repository.
</Card>
<Card title="Dynamic Fee Sharing NPM Package" icon="npm" href="https://www.npmjs.com/package/@meteora-ag/dynamic-fee-sharing-sdk">
Published package for transaction builders, state reads, PDA helpers, and examples.
</Card>
</CardGroup>
## Install
To use the SDK in your project, install it with your preferred package manager:
<Tabs>
<Tab title="npm">
```bash theme={"system"}
npm install @meteora-ag/dynamic-fee-sharing-sdk @solana/web3.js @solana/spl-token bn.js
```
</Tab>
<Tab title="pnpm">
```bash theme={"system"}
pnpm install @meteora-ag/dynamic-fee-sharing-sdk @solana/web3.js @solana/spl-token bn.js
```
</Tab>
<Tab title="yarn">
```bash theme={"system"}
yarn add @meteora-ag/dynamic-fee-sharing-sdk @solana/web3.js @solana/spl-token bn.js
```
</Tab>
</Tabs>
## Dependencies
| Package | Version |
| - | - |
| `@coral-xyz/anchor` | `^0.31.0` |
| `@meteora-ag/cp-amm-sdk` | `^1.2.0` |
| `@meteora-ag/dynamic-bonding-curve-sdk` | `^1.4.5` |
| `@solana/spl-token` | `^0.4.13` |
| `@solana/web3.js` | `^1.98.0` |
| `bn.js` | `^5.2.1` |
| `decimal.js` | `^10.5.0` |
## Create A Client
```typescript theme={"system"}
import { Connection } from "@solana/web3.js";
import { DynamicFeeSharingClient } from "@meteora-ag/dynamic-fee-sharing-sdk";
const connection = new Connection(process.env.RPC_URL!, "confirmed");
const client = new DynamicFeeSharingClient(connection, "confirmed");
```
The client builds unsigned transactions. Your application is responsible for signing, simulating, sending, and confirming them.
## Program IDs
| Constant | Value |
| - | - |
| `DYNAMIC_FEE_SHARING_PROGRAM_ID` | `dfsdo2UqvwfN8DuUVrMRNfQe11VaiNoKcMqLHVvDPzh` |
## Derive Addresses
```typescript theme={"system"}
import {
deriveFeeVaultAuthorityAddress,
deriveFeeVaultPdaAddress,
deriveTokenVaultAddress,
} from "@meteora-ag/dynamic-fee-sharing-sdk";
const feeVault = deriveFeeVaultPdaAddress(base.publicKey, tokenMint);
const tokenVault = deriveTokenVaultAddress(feeVault);
const feeVaultAuthority = deriveFeeVaultAuthorityAddress();
```
## Create A Fee Vault
Use `createFeeVault` for a keypair-based fee vault:
```typescript theme={"system"}
import { Keypair } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
const feeVault = Keypair.generate();
const tx = await client.createFeeVault({
feeVault: feeVault.publicKey,
tokenMint,
tokenProgram: TOKEN_PROGRAM_ID,
owner: owner.publicKey,
payer: payer.publicKey,
userShare: [
{ address: userA.publicKey, share: 600 },
{ address: userB.publicKey, share: 400 },
],
});
// Sign with payer and feeVault.
```
Use `createFeeVaultPda` when the vault must be deterministic or must sign whitelisted source-program fee claims:
```typescript theme={"system"}
const tx = await client.createFeeVaultPda({
base: base.publicKey,
tokenMint,
tokenProgram: TOKEN_PROGRAM_ID,
owner: owner.publicKey,
payer: payer.publicKey,
userShare: [
{ address: userA.publicKey, share: 600 },
{ address: userB.publicKey, share: 400 },
],
});
// Sign with payer and base.
```
## Fund And Claim
```typescript theme={"system"}
import BN from "bn.js";
const fundTx = await client.fundFeeVault({
feeVault,
funder: funder.publicKey,
fundAmount: new BN("1000000"),
});
// Sign with funder.
```
```typescript theme={"system"}
const claimTx = await client.claimUserFee({
feeVault,
user: userA.publicKey,
payer: payer.publicKey,
});
// Sign with payer and userA.
```
Use `claimUserFee2` when the user signs but the claimed fee should be delivered to a separate receiver token account owner.
## SDK Examples
<CardGroup>
<Card title="Dynamic Fee Sharing Examples" icon="code" href="https://github.com/MeteoraAg/dynamic-fee-sharing-sdk/tree/main/scripts">
Example scripts for creating fee vaults, funding vaults, claiming user fees, reading state, and source-program funding.
</Card>
</CardGroup>
## Testing The SDK
If you have cloned the SDK repository, start with package-level checks and tests:
```bash theme={"system"}
pnpm install
pnpm run build
bun test
```
# Dynamic Fee Sharing TS SDK Reference
Source: https://docs.meteora.ag/developer-guides/dynamic-fee-sharing/typescript-sdk/reference
Explore Dynamic Fee Sharing TypeScript SDK exports, client methods, PDA helpers, constants, types, and source-program funding wrappers.
The TypeScript SDK package is `@meteora-ag/dynamic-fee-sharing-sdk`. It exports the `DynamicFeeSharingClient` class, PDA helpers, token helpers, constants, IDL-backed account types, and parameter types.
```typescript theme={"system"}
import {
DynamicFeeSharingClient,
DYNAMIC_FEE_SHARING_PROGRAM_ID,
deriveFeeVaultPdaAddress,
deriveTokenVaultAddress,
} from "@meteora-ag/dynamic-fee-sharing-sdk";
```
## Client Construction
| API | Signature | Use |
| - | - | - |
| `new DynamicFeeSharingClient` | `(connection: Connection, commitment: Commitment)` | Creates an Anchor program client and caches the fee vault authority PDA. |
| `createDfsProgram` | `(connection: Connection, commitment?: Commitment) => DynamicFeeSharingProgram` | Creates the raw Anchor program client from the SDK IDL. |
## State Methods
| API | Signature | Use |
| - | - | - |
| `getFeeVault` | `(feeVault: PublicKey) => Promise<FeeVault>` | Fetches and decodes one fee vault account. |
| `getFeeBreakdown` | `(feeVault: PublicKey) => Promise<{ totalFundedFee; totalClaimedFee; totalUnclaimedFee; userFees }>` | Calculates total funded, claimed, unclaimed, and per-user fee totals from fee vault state. |
`getFeeBreakdown` filters out empty user slots where `share` is zero.
## Vault Creation Methods
| API | Parameters | Signers | Use |
| - | - | - | - |
| `createFeeVault` | `feeVault`, `tokenMint`, `tokenProgram`, `owner`, `payer`, `userShare` | `payer`, `feeVault` | Builds a transaction for a keypair-based fee vault. |
| `createFeeVaultPda` | `base`, `tokenMint`, `tokenProgram`, `owner`, `payer`, `userShare` | `payer`, `base` | Builds a transaction for a deterministic PDA fee vault. |
`userShare` maps to the on-chain `UserShare[]` type:
| Field | Type | Rule |
| - | - | - |
| `address` | `PublicKey` | Must not be the default public key. |
| `share` | `number` | Must be greater than zero and fit `u32`. |
The program requires 2 to 5 users.
## Funding Methods
| API | Parameters | Signer | Use |
| - | - | - | - |
| `fundFeeVault` | `fundAmount`, `funder`, `feeVault`, optional `feeVaultState` | `funder` | Directly funds a fee vault from the funder's associated token account. |
| `fundByClaimDammV2Fee` | `signer`, `owner`, `feeVault`, `dammV2Pool`, `dammV2Position`, `dammV2PositionNftAccount`, optional `dammV2PoolState` | `signer` | Claims DAMM v2 position fees into a PDA fee vault. |
| `fundByClaimDammV2Reward` | `signer`, `rewardIndex`, `feeVault`, `dammV2Pool`, `dammV2Position`, `dammV2PositionNftAccount`, optional `dammV2PoolState` | `signer` | Claims one DAMM v2 reward into a PDA fee vault. |
| `fundByClaimDbcCreatorTradingFee` | `signer`, `creator`, `feeVault`, `poolConfig`, `virtualPool`, optional cached states | `signer` | Claims DBC creator trading fees into a PDA fee vault. |
| `fundByClaimDbcPartnerTradingFee` | `signer`, `feeClaimer`, `feeVault`, `poolConfig`, `virtualPool`, optional cached states | `signer` | Claims DBC partner trading fees into a PDA fee vault. |
| `fundByWithdrawDbcCreatorSurplus` | `signer`, `feeVault`, `poolConfig`, `virtualPool`, optional cached states | `signer` | Withdraws DBC creator surplus into a PDA fee vault. |
| `fundByWithdrawDbcPartnerSurplus` | `signer`, `feeVault`, `poolConfig`, `virtualPool`, optional cached states | `signer` | Withdraws DBC partner surplus into a PDA fee vault. |
| `fundByWithdrawDbcMigrationFee` | `signer`, `isPartner`, `feeVault`, `poolConfig`, `virtualPool`, optional cached states | `signer` | Withdraws DBC migration fees into a PDA fee vault. |
The source-program wrappers call the on-chain `fund_by_claiming_fee` instruction. That instruction requires a PDA fee vault and a `signer` that is one of the configured shareholders.
## Claim Methods
| API | Parameters | Signers | Use |
| - | - | - | - |
| `claimUserFee` | `feeVault`, `user`, `payer` | `payer`, `user` | Claims the user's available amount into the user's associated token account. |
| `claimUserFee2` | `feeVault`, `user`, `payer`, `receiver` | `payer`, `user` | Claims the user's available amount into a receiver-associated token account. The receiver does not sign. |
Both methods fetch the fee vault, find the user's share index, create the destination associated token account if needed, and build `claim_fee`.
## PDA Helpers
| Helper | Seeds | Returns |
| - | - | - |
| `deriveFeeVaultAuthorityAddress()` | `fee_vault_authority` | `PublicKey` |
| `deriveTokenVaultAddress(feeVault)` | `token_vault`, fee vault | `PublicKey` |
| `deriveFeeVaultPdaAddress(base, tokenMint)` | `fee_vault`, base, token mint | `PublicKey` |
| `deriveDammV2EventAuthorityAddress()` | `__event_authority` under DAMM v2 | `PublicKey` |
## Token And Transaction Helpers
| Helper | Use |
| - | - |
| `getTokenProgram(tokenFlag)` | Maps SDK `TokenType.SPL` or `TokenType.Token2022` to the SPL Token or Token 2022 program ID. |
| `getOrCreateATAInstruction(connection, tokenMint, owner, payer, allowOwnerOffCurve, tokenProgram)` | Returns an associated token account and optional idempotent create instruction. |
| `wrapSOLInstruction(from, to, amount)` | Builds native SOL wrap instructions for a WSOL token account. |
| `unwrapSOLInstruction(owner, receiver, allowOwnerOffCurve?)` | Builds a WSOL close-account instruction when claiming native SOL. |
| `setTokenAccountOwnerTx(tokenAccount, from프로젝트로 돌아가기