Project type: Decentralised Exchange

GitHub - raydium-io/raydium-amm: Constant product AMM integrated with the OpenBook CLOB
Write better code with AI](
Find and fix vulnerabilities](
Plan and track work](
Collaborate outside of code](
Find more, search less](
+ Small and medium teams 
+ View all use cases
+ View all industries
+ White papers, Ebooks, Webinars 
Fund open source developers](/sponsors)
+ [The ReadME Project
Search or jump to...
# Search code, repositories, users, issues, pull requests...
We read every piece of feedback, and take your input very seriously.
Include my email address so I can be contacted
## Use saved searches to filter your results more quickly
To see all available qualifiers, see our documentation 
You signed in with another tab or window. Reload to refresh your session.
You signed out in another tab or window. Reload to refresh your session.
You switched accounts on another tab or window. Reload to refresh your session.
* Notifications You must be signed in to change notification settings
Constant product AMM integrated with the OpenBook CLOB
forks](/raydium-io/raydium-amm/forks) Branches Tags Activity
Notifications You must be signed in to change notification settings
masterBranchesTagsGo to fileCode## Folders and files
| Name | | Name | Last commit message | Last commit date |
| --- | --- | --- | --- | --- |
| Latest commit History19 Commits | | |
| program | | program | | |
| .gitignore | | .gitignore | | |
| LICENSE | | LICENSE | | |
| README.md | | README.md | | |
| SECURITY.md | | SECURITY.md | | |
| View all files | | |
## Repository files navigation
| Environment | PROGRAM |
| --- | --- |
| Mainnet | `675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8` |
| Devnet | `HWy1jotHpo6UqeQxx49dpYYdQB8wj9Qk9MdxwjLvDHB8` |
* **The Raydium AMM is an on-chain smart contract based on the “constant product” in a permissionless and decentralized manner built on the Solana blockchain.And it also shares its liquidity according to the Fibonacci sequence in the form of limit orders on OpenBook the primary central limit order book (CLOB) of Solana**
* **The audit process is here 
* **The dev document is here 
2. Install Solana and then run `solana-keygen new` to create a keypair at the default location.
Clone the repository and enter the source code directory.
cargo build-bpf --features devnet
You must update these pubkeys in the "config_feature" as yours over the localnet feature before build;
cargo build-bpf --features localnet
After building, the smart contract files are all located in the target directory.
Attention, check your configuration and confirm the environment you want to deploy.
1. You must have an openbook market not associated to any amm pool if you want to initialize a new amm pool.
And you can refer to ListMarket to create a new market.
2. Add dependencies in your Cargo.toml
raydium-library = { git = "" }
spl-token = { version = "4.0.0", features = ["no-entrypoint"] }
spl-associated-token-account = { version = "2.2.0", features = [
spl-token-2022 = { version = "0.9.0", features = ["no-entrypoint"] }
3. Import dependent libraries
2. initialize a new amm pool with an associate openbook market
let wallet_file_path = "id.json";
let cluster_url = "";
let amm_program = Pubkey::from_str("HWy1jotHpo6UqeQxx49dpYYdQB8wj9Qk9MdxwjLvDHB8")?;
let market_program = Pubkey::from_str("EoTcMgcDRTJVZDMZWBoU6rhYHZfkNTVEAfz3uUJRcYGj")?;
let market = Pubkey::from_str("74yqm5ihhMg5XJeqvC6oPsHaczjF6U9Rc8zs4wMnAGUL")?;
let amm_coin_mint = Pubkey::from_str("2SiSpNowr7zUv5ZJHuzHszskQNaskWsNukhivCtuVLHo")?;
let amm_pc_mint = Pubkey::from_str("GfmdKWR1KrttDsQkJfwtXovZw9bUBHYkPAEwB6wZqQvJ")?;
let create_fee_destination = Pubkey::from_str("3XMrhbv989VxAMi3DErLV9eJht1pHppW5LbKxe9fkEFR")?;
let client = RpcClient::new(cluster_url.to_string());
let wallet = solana_sdk::signature::read_keypair_file(wallet_file_path)
.map_err(|_| format_err!("failed to read keypair from {}", wallet_file_path))?;
let input_pc_amount = 10000_000000;
let input_coin_amount = 10000_000000;
// generate amm keys
let amm_keys = raydium_library::amm::utils::get_amm_pda_keys(
// build initialize instruction
let build_init_instruction = raydium_library::amm::commands::initialize_amm_pool(
3. deposit assets to an amm pool
let wallet_file_path = "id.json";
let cluster_url = "";
let amm_program = Pubkey::from_str("HWy1jotHpo6UqeQxx49dpYYdQB8wj9Qk9MdxwjLvDHB8")?;
let amm_pool_id = Pubkey::from_str("BbZjQanvSaE9me4adAitmTTaSgASvzaVignt4HRSM7ww")?;
let slippage_bps = 50u64; // 0.5%
let input_amount = 10000_000000;
let base_side = 0; // 0: base coin; 1: base pc
let client = RpcClient::new(cluster_url.to_string());
let wallet = solana_sdk::signature::read_keypair_file(wallet_file_path)
.map_err(|_| format_err!("failed to read keypair from {}", wallet_file_path))?;
// load amm keys
let amm_keys = raydium_library::amm::utils::load_amm_keys(&client, &amm_program, &amm_pool_id)?;
// load market keys
let market_keys = raydium_library::amm::openbook::get_keys_for_market(
// calculate amm pool vault with load data at the same time or use simulate to calculate
let result = raydium_library::amm::calculate_pool_vault_amounts(
let (max_coin_amount, max_pc_amount) =
// build deposit instruction
let build_deposit_instruction = raydium_library::amm::commands::deposit(
4. withdraw assets from amm pool
let wallet_file_path = "id.json";
let cluster_url = "";
let amm_program = Pubkey::from_str("HWy1jotHpo6UqeQxx49dpYYdQB8wj9Qk9MdxwjLvDHB8")?;
let amm_pool_id = Pubkey::from_str("BbZjQanvSaE9me4adAitmTTaSgASvzaVignt4HRSM7ww")?;
// let slippage_bps = 50u64; // 0.5%
let withdraw_lp_amount = 10000_000000;
let client = RpcClient::new(cluster_url.to_string());
let wallet = solana_sdk::signature::read_keypair_file(wallet_file_path)
.map_err(|_| format_err!("failed to read keypair from {}", wallet_file_path))?;
// load amm keys
let amm_keys = raydium_library::amm::utils::load_amm_keys(&client, &amm_program, &amm_pool_id)?;
// load market keys
let market_keys = raydium_library::amm::openbook::get_keys_for_market(
// build withdraw instruction
let build_withdraw_instruction = raydium_library::amm::commands::withdraw(
let wallet_file_path = "id.json";
let cluster_url = "";
let amm_program = Pubkey::from_str("HWy1jotHpo6UqeQxx49dpYYdQB8wj9Qk9MdxwjLvDHB8")?;
let amm_pool_id = Pubkey::from_str("BbZjQanvSaE9me4adAitmTTaSgASvzaVignt4HRSM7ww")?;
let input_token_mint = Pubkey::from_str("GfmdKWR1KrttDsQkJfwtXovZw9bUBHYkPAEwB6wZqQvJ")?;
let output_token_mint = Pubkey::from_str("2SiSpNowr7zUv5ZJHuzHszskQNaskWsNukhivCtuVLHo")?;
let slippage_bps = 50u64; // 0.5%
let amount_specified = 2000_000000u64;
let swap_base_in = false;
let client = RpcClient::new(cluster_url.to_string());
let wallet = solana_sdk::signature::read_keypair_file(wallet_file_path)
.map_err(|_| format_err!("failed to read keypair from {}", wallet_file_path))?;
// load amm keys
let amm_keys = raydium_library::amm::utils::load_amm_keys(&client, &amm_program, &amm_pool_id)?;
// load market keys
let market_keys = raydium_library::amm::openbook::get_keys_for_market(
// calculate amm pool vault with load data at the same time or use simulate to calculate
let result = raydium_library::amm::calculate_pool_vault_amounts(
let direction = if input_token_mint == amm_keys.amm_coin_mint
&& output_token_mint == amm_keys.amm_pc_mint
let other_amount_threshold = raydium_library::amm::swap_with_slippage(
// build swap instruction
let build_swap_instruction = raydium_library::amm::commands::swap(
Constant product AMM integrated with the OpenBook CLOB
© 2024 GitHub, Inc.
* Do not share my personal information
You can’t perform that action at this time.