Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More examples #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/ci-pr-main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CPI example

on:
pull_request:
branches:
- main

jobs:
program_changed_files:
runs-on: ubuntu-20.04
outputs:
program: ${{steps.changed-files-specific.outputs.any_changed}}
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Get specific changed files
id: changed-files-specific
uses: tj-actions/changed-files@v18.6
with:
files: |
programs/cpi-example

cpi_example_test:
runs-on: ubuntu-20.04
needs: program_changed_files
if: needs.program_changed_files.outputs.program == 'true'
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.76
override: true
- uses: Swatinem/rust-cache@v2
- run: cargo test -- --nocapture
shell: bash
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Meteora-CPI-examples

The repository containing examples for CPI (Cross-Program Invocation) to swap at [DLMM](https://github.com/meteoraAg/dlmm-sdk) and [Dynamic AMM](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk) programs.
The repository containing examples for CPI (Cross-Program Invocation) to [DLMM](https://github.com/meteoraAg/dlmm-sdk) and [Dynamic AMM](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk) programs.

Disclaimer: This repository only serves as examples and is not intended for production use.

Expand All @@ -18,6 +18,9 @@ Disclaimer: This repository only serves as examples and is not intended for prod
- [CPI to Dynamic AMM initialize pool example](programs/cpi-example/src/instructions/dynamic_amm_cpi/initialize_customizable_permissionless_pool.rs)
- [CPI to Dynamic AMM initialize pool with config example](programs/cpi-example/src/instructions/dynamic_amm_cpi/initialize_permissionless_pool_with_config.rs)

- [CPI to Dynamic AMM lock liquidity example](programs/cpi-example/src/instructions/dynamic_amm_cpi/lock_liquidity.rs)
- [CPI to Dynamic AMM claim fee example](programs/cpi-example/src/instructions/dynamic_amm_cpi/claim_fee.rs)

- [CPI to M3m3 initialize vault example](programs/cpi-example/src/instructions/m3m3_cpi/initialize_vault.rs)

- [Tests](programs/cpi-example/tests/)
Expand Down
1 change: 1 addition & 0 deletions programs/cpi-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ assert_matches = "1.5.0"
solana-client = "1.16.0"
solana-account-decoder = "1.16.0"
bincode = "1.3.3"
spl-associated-token-account = "2.2.0"
274 changes: 274 additions & 0 deletions programs/cpi-example/src/instructions/dynamic_amm_cpi/claim_fee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
use anchor_lang::prelude::*;
use anchor_spl::associated_token::get_associated_token_address_with_program_id;
use dynamic_amm::state::Pool;

#[derive(Accounts)]
pub struct DynamicAmmClaimFee<'info> {
/// Check: Pool account (PDA)
#[account(mut)]
pub pool: UncheckedAccount<'info>,

/// Check: Pool LP mint
#[account(mut)]
pub lp_mint: UncheckedAccount<'info>,

/// CHECK: Lock escrow of user
#[account(mut)]
pub lock_escrow: UncheckedAccount<'info>,

/// CHECK: Lock escrow owner
#[account(mut)]
pub owner: Signer<'info>,

/// CHECK: Token vault of lock escrow
#[account(mut)]
pub escrow_vault: UncheckedAccount<'info>,

/// CHECK: Token account of vault A
#[account(mut)]
pub a_token_vault: UncheckedAccount<'info>,

/// CHECK: Token account of vault B
#[account(mut)]
pub b_token_vault: UncheckedAccount<'info>,

/// CHECK: Vault account for token a. token a of the pool will be deposit / withdraw from this vault account.
#[account(mut)]
pub a_vault: UncheckedAccount<'info>,

/// CHECK: Vault account for token b. token b of the pool will be deposit / withdraw from this vault account.
#[account(mut)]
pub b_vault: UncheckedAccount<'info>,

/// CHECK: LP token account of vault A. Used to receive/burn the vault LP upon deposit/withdraw from the vault.
#[account(mut)]
pub a_vault_lp: UncheckedAccount<'info>,

/// CHECK: LP token account of vault B. Used to receive/burn the vault LP upon deposit/withdraw from the vault.
#[account(mut)]
pub b_vault_lp: UncheckedAccount<'info>,

/// CHECK: LP token mint of vault a
#[account(mut)]
pub a_vault_lp_mint: UncheckedAccount<'info>,

/// CHECK: LP token mint of vault b
#[account(mut)]
pub b_vault_lp_mint: UncheckedAccount<'info>,

#[account(mut)]
/// User token A account. Used to receive fee
pub user_a_token: UncheckedAccount<'info>,
#[account(mut)]
/// User token B account. Used to receive fee
pub user_b_token: UncheckedAccount<'info>,

/// CHECK: Token program
pub token_program: UncheckedAccount<'info>,

/// CHECK: Dynamic AMM
#[account(
address = dynamic_amm::ID
)]
pub dynamic_amm: UncheckedAccount<'info>,

/// CHECK: Dynamic vault
pub dynamic_vault: UncheckedAccount<'info>,
}

/// Claims the fee for a user from the locked liquidity in the pool.
///
/// # Arguments
///
/// * `ctx` - The context containing accounts and programs.
///
/// # Returns
///
/// Returns a `Result` indicating success or failure.

pub fn handle_claim_fee(ctx: Context<DynamicAmmClaimFee>) -> Result<()> {
let accounts = dynamic_amm::cpi::accounts::ClaimFee {
pool: ctx.accounts.pool.to_account_info(),
lp_mint: ctx.accounts.lp_mint.to_account_info(),
lock_escrow: ctx.accounts.lock_escrow.to_account_info(),
owner: ctx.accounts.owner.to_account_info(),
// Unused anymore, but still remained for compatibility. Passing escrow_vault can save 1 account
source_tokens: ctx.accounts.escrow_vault.to_account_info(),
a_vault: ctx.accounts.a_vault.to_account_info(),
b_vault: ctx.accounts.b_vault.to_account_info(),
a_vault_lp: ctx.accounts.a_vault_lp.to_account_info(),
b_vault_lp: ctx.accounts.b_vault_lp.to_account_info(),
a_vault_lp_mint: ctx.accounts.a_vault_lp_mint.to_account_info(),
b_vault_lp_mint: ctx.accounts.b_vault_lp_mint.to_account_info(),
user_a_token: ctx.accounts.user_a_token.to_account_info(),
user_b_token: ctx.accounts.user_b_token.to_account_info(),
vault_program: ctx.accounts.dynamic_vault.to_account_info(),
escrow_vault: ctx.accounts.escrow_vault.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
a_token_vault: ctx.accounts.a_token_vault.to_account_info(),
b_token_vault: ctx.accounts.b_token_vault.to_account_info(),
};

let cpi_context = CpiContext::new(ctx.accounts.dynamic_amm.to_account_info(), accounts);

// Claim max fee
dynamic_amm::cpi::claim_fee(cpi_context, u64::MAX)
}

#[derive(Accounts)]
pub struct DynamicAmmClaimFeePdaCreator<'info> {
/// Check: Pool account (PDA)
#[account(mut)]
pub pool: Box<Account<'info, Pool>>,

/// Check: Pool LP mint
#[account(mut)]
pub lp_mint: UncheckedAccount<'info>,

/// CHECK: Pool creator authority. PDA.
#[account(
mut,
seeds = [b"creator"],
bump
)]
pub creator_authority: UncheckedAccount<'info>,

/// CHECK: Lock escrow of creator PDA
#[account(mut)]
pub lock_escrow: UncheckedAccount<'info>,

/// CHECK: Only admin can claim fee for creator PDA.
#[account(
constraint = crate::assert_eq_admin(cpi_example_admin.key())
)]
pub cpi_example_admin: Signer<'info>,

/// CHECK: Token vault of lock escrow
#[account(mut)]
pub escrow_vault: UncheckedAccount<'info>,

/// CHECK: Token account of vault A
#[account(mut)]
pub a_token_vault: UncheckedAccount<'info>,

/// CHECK: Token account of vault B
#[account(mut)]
pub b_token_vault: UncheckedAccount<'info>,

/// CHECK: Vault account for token a. token a of the pool will be deposit / withdraw from this vault account.
#[account(mut)]
pub a_vault: UncheckedAccount<'info>,

/// CHECK: Vault account for token b. token b of the pool will be deposit / withdraw from this vault account.
#[account(mut)]
pub b_vault: UncheckedAccount<'info>,

/// CHECK: LP token account of vault A. Used to receive/burn the vault LP upon deposit/withdraw from the vault.
#[account(mut)]
pub a_vault_lp: UncheckedAccount<'info>,

/// CHECK: LP token account of vault B. Used to receive/burn the vault LP upon deposit/withdraw from the vault.
#[account(mut)]
pub b_vault_lp: UncheckedAccount<'info>,

/// CHECK: LP token mint of vault a
#[account(mut)]
pub a_vault_lp_mint: UncheckedAccount<'info>,

/// CHECK: LP token mint of vault b
#[account(mut)]
pub b_vault_lp_mint: UncheckedAccount<'info>,

#[account(mut)]
/// Creator token A account. Used to receive fee
pub creator_a_token: UncheckedAccount<'info>,
#[account(mut)]
/// Creator token B account. Used to receive fee
pub creator_b_token: UncheckedAccount<'info>,

/// CHECK: Token program
pub token_program: UncheckedAccount<'info>,

/// CHECK: Dynamic AMM
#[account(
address = dynamic_amm::ID
)]
pub dynamic_amm: UncheckedAccount<'info>,

/// CHECK: Dynamic vault
pub dynamic_vault: UncheckedAccount<'info>,
}

/// Claims fee for creator PDA.
///
/// This instruction is used to claim fee for creator PDA. The claimed fee will be hold by creator PDA.
///
/// # Arguments
///
/// * `ctx` - The context containing accounts and programs.
///
/// # Returns
///
/// Returns a `Result` indicating success or failure.
pub fn handle_claim_fee_pda_creator(ctx: Context<DynamicAmmClaimFeePdaCreator>) -> Result<()> {
let creator_a_token_key = get_associated_token_address_with_program_id(
&ctx.accounts.creator_authority.key(),
&ctx.accounts.pool.token_a_mint,
&ctx.accounts.token_program.key(),
);

let creator_b_token_key = get_associated_token_address_with_program_id(
&ctx.accounts.creator_authority.key(),
&ctx.accounts.pool.token_b_mint,
&ctx.accounts.token_program.key(),
);

assert_eq!(
creator_a_token_key,
ctx.accounts.creator_a_token.key(),
"Invalid creator_a_token"
);
assert_eq!(
creator_b_token_key,
ctx.accounts.creator_b_token.key(),
"Invalid creator_b_token"
);

let accounts = dynamic_amm::cpi::accounts::ClaimFee {
pool: ctx.accounts.pool.to_account_info(),
lp_mint: ctx.accounts.lp_mint.to_account_info(),
lock_escrow: ctx.accounts.lock_escrow.to_account_info(),
owner: ctx.accounts.creator_authority.to_account_info(),
// Unused anymore, but still remained for compatibility. Passing escrow_vault can save 1 account
source_tokens: ctx.accounts.escrow_vault.to_account_info(),
a_vault: ctx.accounts.a_vault.to_account_info(),
b_vault: ctx.accounts.b_vault.to_account_info(),
a_vault_lp: ctx.accounts.a_vault_lp.to_account_info(),
b_vault_lp: ctx.accounts.b_vault_lp.to_account_info(),
a_vault_lp_mint: ctx.accounts.a_vault_lp_mint.to_account_info(),
b_vault_lp_mint: ctx.accounts.b_vault_lp_mint.to_account_info(),
user_a_token: ctx.accounts.creator_a_token.to_account_info(),
user_b_token: ctx.accounts.creator_b_token.to_account_info(),
vault_program: ctx.accounts.dynamic_vault.to_account_info(),
escrow_vault: ctx.accounts.escrow_vault.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
a_token_vault: ctx.accounts.a_token_vault.to_account_info(),
b_token_vault: ctx.accounts.b_token_vault.to_account_info(),
};

let seeds = [
b"creator".as_ref(),
&[*ctx.bumps.get("creator_authority").unwrap()],
];

let signer_seeds = &[&seeds[..]];

let cpi_context = CpiContext::new_with_signer(
ctx.accounts.dynamic_amm.to_account_info(),
accounts,
signer_seeds,
);

// Claim max fee
dynamic_amm::cpi::claim_fee(cpi_context, u64::MAX)
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ pub fn handle_initialize_customizable_permissionless_pool_with_config(
}

#[derive(Accounts)]
pub struct DynamicAmmInitializePermissionlessPoolWithConfigPoolPdaCreator<'info> {
pub struct DynamicAmmInitializePermissionlessPoolWithConfigPdaCreator<'info> {
/// CHECK: Creator authority
#[account(
mut,
seeds = [b"creator"],
bump
)]
Expand Down Expand Up @@ -301,7 +302,7 @@ pub struct DynamicAmmInitializePermissionlessPoolWithConfigPoolPdaCreator<'info>
///
/// Returns a `Result` indicating success or failure.
pub fn handle_initialize_customizable_permissionless_pool_with_pda_creator(
ctx: Context<DynamicAmmInitializePermissionlessPoolWithConfigPoolPdaCreator>,
ctx: Context<DynamicAmmInitializePermissionlessPoolWithConfigPdaCreator>,
token_a_amount: u64,
token_b_amount: u64,
activation_point: Option<u64>,
Expand Down Expand Up @@ -334,12 +335,12 @@ pub fn handle_initialize_customizable_permissionless_pool_with_pda_creator(
b_vault_lp_mint: ctx.accounts.b_vault_lp_mint.to_account_info(),
a_vault_lp: ctx.accounts.a_vault_lp.to_account_info(),
b_vault_lp: ctx.accounts.b_vault_lp.to_account_info(),
payer_token_a: ctx.accounts.payer_token_a.to_account_info(),
payer_token_b: ctx.accounts.payer_token_b.to_account_info(),
payer_token_a: ctx.accounts.creator_token_a.to_account_info(),
payer_token_b: ctx.accounts.creator_token_b.to_account_info(),
payer_pool_lp: ctx.accounts.creator_pool_lp.to_account_info(),
protocol_token_a_fee: ctx.accounts.protocol_token_a_fee.to_account_info(),
protocol_token_b_fee: ctx.accounts.protocol_token_b_fee.to_account_info(),
payer: ctx.accounts.payer.to_account_info(),
payer: ctx.accounts.creator_authority.to_account_info(),
rent: ctx.accounts.rent.to_account_info(),
mint_metadata: ctx.accounts.mint_metadata.to_account_info(),
metadata_program: ctx.accounts.metadata_program.to_account_info(),
Expand Down
Loading
Loading