This commit is contained in:
Sushant Chandla 2024-10-17 22:09:47 +05:30
parent dc528b3f9c
commit 4de9eae75f
4 changed files with 60 additions and 50 deletions

View File

@ -6,7 +6,7 @@ use anchor_spl::token_2022::spl_token_2022::extension::{
};
use anchor_spl::token_2022::spl_token_2022::state::Mint;
use gamma::curve::{CurveCalculator, TradeDirection};
use gamma::states::{AmmConfig, Observation, ObservationState, PoolState, PoolStatusBitIndex};
use gamma::states::{AmmConfig, ObservationState, PoolState, PoolStatusBitIndex};
use mango_feeds_connector::chain_data::AccountData;
use solana_program::clock::Clock;
use solana_program::pubkey::Pubkey;
@ -57,6 +57,7 @@ pub struct GammaEdge {
pub vault_1_amount: u64,
pub mint_0: Option<TransferFeeConfig>,
pub mint_1: Option<TransferFeeConfig>,
pub observation_state: ObservationState,
}
impl DexEdge for GammaEdge {
@ -84,6 +85,7 @@ pub(crate) fn get_transfer_config(
pub fn swap_base_input(
pool: &PoolState,
amm_config: &AmmConfig,
observation_state: &ObservationState,
input_vault_key: Pubkey,
input_vault_amount: u64,
input_mint: &Option<TransferFeeConfig>,
@ -147,7 +149,7 @@ pub fn swap_base_input(
amm_config.protocol_fee_rate,
amm_config.fund_fee_rate,
block_timestamp,
&ObservationState::default(),
&observation_state,
trade_direction,
) else {
anyhow::bail!("Can't swap");
@ -178,6 +180,7 @@ pub fn swap_base_input(
pub fn swap_base_output(
pool: &PoolState,
amm_config: &AmmConfig,
observation_state: &ObservationState,
input_vault_key: Pubkey,
input_vault_amount: u64,
_input_mint: &Option<TransferFeeConfig>,
@ -246,7 +249,7 @@ pub fn swap_base_output(
amm_config.protocol_fee_rate,
amm_config.fund_fee_rate,
block_timestamp,
&ObservationState::default(),
observation_state,
trade_direction,
) else {
anyhow::bail!("Can't swap");

View File

@ -7,7 +7,7 @@ use anchor_spl::token_2022::spl_token_2022;
use anyhow::Context;
use async_trait::async_trait;
use gamma::program::Gamma;
use gamma::states::{block_timestamp, AmmConfig, PoolState, PoolStatusBitIndex};
use gamma::states::{block_timestamp, AmmConfig, ObservationState, PoolState, PoolStatusBitIndex};
use itertools::Itertools;
use router_feed_lib::router_rpc_client::{RouterRpcClient, RouterRpcClientTrait};
use router_lib::dex::{
@ -133,6 +133,7 @@ impl DexInterface for GammaCpDex {
accounts: Default::default(),
programs: HashSet::from([Gamma::id()]),
token_accounts_for_owner: HashSet::from([Pubkey::from_str(
// TODO: replace with gamma signer authority
"GpMZbSM2GgvTKHJirzeGfMFoaZ8UR2X7F4v8vHTvxFbL", // ?? what is this.
)
.unwrap()]),
@ -170,6 +171,10 @@ impl DexInterface for GammaCpDex {
let transfer_0_fee = crate::edge::get_transfer_config(&mint_0_account)?;
let transfer_1_fee = crate::edge::get_transfer_config(&mint_1_account)?;
let observation_state_account = chain_data.account(&pool.observation_key)?;
let observation_state =
ObservationState::try_deserialize(&mut observation_state_account.account.data())?;
Ok(Arc::new(GammaEdge {
pool,
config,
@ -177,6 +182,7 @@ impl DexInterface for GammaCpDex {
vault_1_amount: vault_1.amount,
mint_0: transfer_0_fee,
mint_1: transfer_1_fee,
observation_state,
}))
}
@ -214,6 +220,7 @@ impl DexInterface for GammaCpDex {
let result = swap_base_input(
&edge.pool,
&edge.config,
&edge.observation_state,
edge.pool.token_0_vault,
edge.vault_0_amount,
&edge.mint_0,
@ -234,6 +241,7 @@ impl DexInterface for GammaCpDex {
let result = swap_base_input(
&edge.pool,
&edge.config,
&edge.observation_state,
edge.pool.token_1_vault,
edge.vault_1_amount,
&edge.mint_1,
@ -308,13 +316,11 @@ impl DexInterface for GammaCpDex {
});
}
// let clock = chain_data.account(&Clock::id()).context("read clock")?;
// let now_ts = clock.account.deserialize_data::<Clock>()?.unix_timestamp as u64;
let quote = if id.is_a_to_b {
let result = swap_base_output(
&edge.pool,
&edge.config,
&edge.observation_state,
edge.pool.token_0_vault,
edge.vault_0_amount,
&edge.mint_0,
@ -335,6 +341,7 @@ impl DexInterface for GammaCpDex {
let result = swap_base_output(
&edge.pool,
&edge.config,
&edge.observation_state,
edge.pool.token_1_vault,
edge.vault_1_amount,
&edge.mint_1,

View File

@ -0,0 +1,43 @@
use std::collections::HashMap;
use std::env;
use solana_program_test::tokio;
use router_lib::dex::DexInterface;
use router_lib::test_tools::{generate_dex_rpc_dump, rpc};
#[tokio::test]
async fn test_dump_input_data_gamma_cp() -> anyhow::Result<()> {
let options = HashMap::from([]);
if router_test_lib::config_should_dump_mainnet_data() {
gamma_cp_step_1(&options).await?;
}
gamma_cp_step_2(&options).await?;
Ok(())
}
async fn gamma_cp_step_1(options: &HashMap<String, String>) -> anyhow::Result<()> {
let rpc_url = env::var("RPC_HTTP_URL")?;
let (mut rpc_client, chain_data) = rpc::rpc_dumper_client(rpc_url, "gamma_dump.lz4");
let dex = dex_gamma_cp::gammaCpDex::initialize(&mut rpc_client, options.clone()).await?;
generate_dex_rpc_dump::run_dump_mainnet_data(dex, rpc_client, chain_data).await?;
Ok(())
}
async fn gamma_cp_step_2(options: &HashMap<String, String>) -> anyhow::Result<()> {
// Replay
let (mut rpc_client, chain_data) = rpc::rpc_replayer_client("gamma_dump.lz4");
let dex = dex_gamma_cp::gammaCpDex::initialize(&mut rpc_client, options.clone()).await?;
generate_dex_rpc_dump::run_dump_swap_ix("gamma_swap.lz4", dex, chain_data).await?;
Ok(())
}

View File

@ -1,43 +0,0 @@
// use std::collections::HashMap;
// use std::env;
// use solana_program_test::tokio;
// use router_lib::dex::DexInterface;
// use router_lib::test_tools::{generate_dex_rpc_dump, rpc};
// #[tokio::test]
// async fn test_dump_input_data_raydium_cp() -> anyhow::Result<()> {
// let options = HashMap::from([]);
// if router_test_lib::config_should_dump_mainnet_data() {
// raydium_cp_step_1(&options).await?;
// }
// raydium_cp_step_2(&options).await?;
// Ok(())
// }
// async fn raydium_cp_step_1(options: &HashMap<String, String>) -> anyhow::Result<()> {
// let rpc_url = env::var("RPC_HTTP_URL")?;
// let (mut rpc_client, chain_data) = rpc::rpc_dumper_client(rpc_url, "raydium_cp_dump.lz4");
// let dex = dex_raydium_cp::RaydiumCpDex::initialize(&mut rpc_client, options.clone()).await?;
// generate_dex_rpc_dump::run_dump_mainnet_data(dex, rpc_client, chain_data).await?;
// Ok(())
// }
// async fn raydium_cp_step_2(options: &HashMap<String, String>) -> anyhow::Result<()> {
// // Replay
// let (mut rpc_client, chain_data) = rpc::rpc_replayer_client("raydium_cp_dump.lz4");
// let dex = dex_raydium_cp::RaydiumCpDex::initialize(&mut rpc_client, options.clone()).await?;
// generate_dex_rpc_dump::run_dump_swap_ix("raydium_cp_swap.lz4", dex, chain_data).await?;
// Ok(())
// }