diff --git a/.github/workflows/test-protocol.yml b/.github/workflows/test-protocol.yml index c19c7c5..46d2ace 100644 --- a/.github/workflows/test-protocol.yml +++ b/.github/workflows/test-protocol.yml @@ -86,8 +86,7 @@ jobs: if: steps.cache-anchor.outputs.cache-hit != 'true' run: | export PATH="/home/runner/.local/share/solana/install/active_release/bin:$PATH" - cargo install --git https://github.com/coral-xyz/anchor avm --locked --force - avm install ${{ env.anchor_version }} + cargo install --git https://github.com/project-serum/anchor --tag v${{ env.anchor_version }} anchor-cli --locked - name: build programs working-directory: ${{ env.protocol_path }} run: | diff --git a/protocol/Anchor.toml b/protocol/Anchor.toml index eea9518..49e0576 100644 --- a/protocol/Anchor.toml +++ b/protocol/Anchor.toml @@ -6,7 +6,6 @@ skip-lint = false [programs.localnet] protocol = "HTBzkQCWc2sbkn5WmLkPmQKKotaeeWgZ3RSD4Eg3f1MS" -puppet = "8KQzCc22ZqGLPoipqRhYvkQtHJw6nY1NxrrGy8JLz1jC" invariant = "CsT21LCRqBfh4SCcNZXtWjRZ6xvYKvdpEBaytCVmWnVJ" [registry] @@ -17,7 +16,7 @@ cluster = "localnet" wallet = "~/.config/solana/id.json" [workspace] -members = ["programs/protocol", "programs/puppet", "programs/invariant"] +members = ["programs/protocol", "programs/invariant"] [scripts] test = "ts-mocha -p ./tsconfig.json -t 1000000" diff --git a/protocol/Cargo.lock b/protocol/Cargo.lock index 2945ff5..8e08533 100644 --- a/protocol/Cargo.lock +++ b/protocol/Cargo.lock @@ -1503,14 +1503,6 @@ dependencies = [ "bytemuck", "decimal", "invariant", - "puppet", -] - -[[package]] -name = "puppet" -version = "0.1.0" -dependencies = [ - "anchor-lang", ] [[package]] diff --git a/protocol/build.sh b/protocol/build.sh index 32594a9..af1c321 100755 --- a/protocol/build.sh +++ b/protocol/build.sh @@ -2,13 +2,11 @@ set -e # Define the keypair JSON content protocol='[125,25,103,8,46,252,74,11,10,231,221,13,113,82,123,17,118,205,218,140,247,37,159,150,140,109,50,158,185,90,57,107,244,112,134,82,19,192,92,70,188,247,227,156,239,127,119,76,193,85,143,146,12,43,48,189,79,193,48,21,49,108,226,209]' -puppet='[51,155,154,107,110,131,244,50,197,81,99,152,71,175,210,224,80,66,26,119,45,91,107,252,7,64,94,239,6,209,249,114,30,90,202,122,203,133,109,126,97,16,179,156,79,250,190,92,149,86,98,253,100,145,117,244,93,225,240,192,20,135,163,104]' invarinat='[100,94,104,217,253,66,112,71,155,206,9,70,30,6,93,185,82,49,169,250,96,24,120,234,25,145,148,106,134,60,55,123,176,93,3,185,31,73,255,255,107,72,82,123,112,9,31,16,214,15,171,107,164,37,143,208,35,195,133,157,224,137,246,197]' # Write the keypair JSON to file mkdir -p target/deploy echo "$protocol" > target/deploy/protocol-keypair.json -echo "$puppet" > target/deploy/puppet-keypair.json echo "$invarinat" > target/deploy/invariant-keypair.json # Build the program using Anchor diff --git a/protocol/package.json b/protocol/package.json index ee5700a..9d556b8 100644 --- a/protocol/package.json +++ b/protocol/package.json @@ -9,8 +9,6 @@ "build:all": "npm run build:program && npm run build:sdk", "test": "npm run build:all && npx ts-mocha -p tsconfig.json tests/**/*.ts", "test:init": "anchor test --skip-build tests/init.test.ts", - "test:token": "anchor test --skip-build tests/token.test.ts", - "test:invoke": "anchor test --skip-build tests/invoke.test.ts", "test:init-lp-pool": "anchor test --skip-build tests/init-lp-pool.test.ts", "test:mint": "anchor test --skip-build tests/mint.test.ts", "test:burn": "anchor test --skip-build tests/burn.test.ts", diff --git a/protocol/programs/protocol/Cargo.toml b/protocol/programs/protocol/Cargo.toml index e8840a1..b9bd40e 100644 --- a/protocol/programs/protocol/Cargo.toml +++ b/protocol/programs/protocol/Cargo.toml @@ -21,5 +21,4 @@ decimal = { path = "../invariant/decimal" } bytemuck = "1.16.0" anchor-lang = {version = "0.29.0", features = ["init-if-needed"]} anchor-spl = "0.29.0" -puppet = { path = "../puppet", features = ["cpi"]} invariant = { path = "../invariant", features = ["cpi"]} diff --git a/protocol/programs/protocol/src/contexts/invoke_close_position.rs b/protocol/programs/protocol/src/contexts/invoke_close_position.rs deleted file mode 100644 index ddfcecb..0000000 --- a/protocol/programs/protocol/src/contexts/invoke_close_position.rs +++ /dev/null @@ -1,104 +0,0 @@ -use anchor_lang::prelude::*; -use anchor_spl::token::Token; -use invariant::{cpi::accounts::RemovePosition, program::Invariant}; - -#[derive(Accounts)] -pub struct InvokeClosePositionCtx<'info> { - #[account(mut)] - /// CHECK: checked in Invariant - pub invariant_state: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - pub invariant_program_authority: UncheckedAccount<'info>, - #[account(mut)] - pub owner: Signer<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub removed_position: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub position_list: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub last_position: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub pool: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub tickmap: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub lower_tick: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub upper_tick: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - pub token_x: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - pub token_y: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub account_x: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub account_y: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub reserve_x: UncheckedAccount<'info>, - /// CHECK: checked in Invariant - #[account(mut)] - pub reserve_y: UncheckedAccount<'info>, - pub token_x_program: Program<'info, Token>, - pub token_y_program: Program<'info, Token>, - pub invariant_program: Program<'info, Invariant>, -} - -impl InvokeClosePositionCtx<'_> { - pub fn process(&self, index: u32, lower_tick_index: i32, upper_tick_index: i32) -> Result<()> { - let InvokeClosePositionCtx { - invariant_state, - invariant_program_authority, - owner, - removed_position, - position_list, - last_position, - pool, - tickmap, - lower_tick, - upper_tick, - token_x, - token_y, - account_x, - account_y, - reserve_x, - reserve_y, - token_x_program, - token_y_program, - invariant_program, - } = self; - - let program = invariant_program.to_account_info(); - let accounts = RemovePosition { - state: invariant_state.to_account_info(), - program_authority: invariant_program_authority.to_account_info(), - owner: owner.to_account_info(), - removed_position: removed_position.to_account_info(), - position_list: position_list.to_account_info(), - last_position: last_position.to_account_info(), - pool: pool.to_account_info(), - tickmap: tickmap.to_account_info(), - lower_tick: lower_tick.to_account_info(), - upper_tick: upper_tick.to_account_info(), - token_x: token_x.to_account_info(), - token_y: token_y.to_account_info(), - account_x: account_x.to_account_info(), - account_y: account_y.to_account_info(), - reserve_x: reserve_x.to_account_info(), - reserve_y: reserve_y.to_account_info(), - token_x_program: token_x_program.to_account_info(), - token_y_program: token_y_program.to_account_info(), - }; - let ctx = CpiContext::new(program, accounts); - invariant::cpi::remove_position(ctx, index, lower_tick_index, upper_tick_index) - } -} diff --git a/protocol/programs/protocol/src/contexts/invoke_create_position.rs b/protocol/programs/protocol/src/contexts/invoke_create_position.rs deleted file mode 100644 index e4d1ad3..0000000 --- a/protocol/programs/protocol/src/contexts/invoke_create_position.rs +++ /dev/null @@ -1,126 +0,0 @@ -use anchor_lang::prelude::*; -use invariant::{cpi::accounts::CreatePosition, program::Invariant}; - -#[derive(Accounts)] -pub struct InvokeCreatePositionCtx<'info> { - pub invariant_program: Program<'info, Invariant>, - #[account(mut)] - pub owner: Signer<'info>, - /// CHECK: - pub state: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub position: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub pool: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub position_list: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub lower_tick: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub upper_tick: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub tickmap: UncheckedAccount<'info>, - /// CHECK: - pub token_x: UncheckedAccount<'info>, - /// CHECK: - pub token_y: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub account_x: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub account_y: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub reserve_x: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub reserve_y: UncheckedAccount<'info>, - /// CHECK: - pub program_authority: UncheckedAccount<'info>, - /// CHECK: - pub token_x_program: UncheckedAccount<'info>, - /// CHECK: - pub token_y_program: UncheckedAccount<'info>, - pub rent: Sysvar<'info, Rent>, - pub system_program: Program<'info, System>, -} - -impl InvokeCreatePositionCtx<'_> { - pub fn process( - &mut self, - lower_tick_index: i32, - upper_tick_index: i32, - liquidity_delta: u128, - slippage_limit_lower: u128, - slippage_limit_upper: u128, - ) -> Result<()> { - let InvokeCreatePositionCtx { - invariant_program, - state, - position, - pool, - position_list, - owner, - lower_tick, - upper_tick, - tickmap, - token_x, - token_y, - account_x, - account_y, - reserve_x, - reserve_y, - program_authority, - token_x_program, - token_y_program, - rent, - system_program, - .. - } = self; - - let program = invariant_program.to_account_info(); - let accounts = CreatePosition { - state: state.to_account_info(), - position: position.to_account_info(), - pool: pool.to_account_info(), - position_list: position_list.to_account_info(), - payer: owner.to_account_info(), - owner: owner.to_account_info(), - lower_tick: lower_tick.to_account_info(), - upper_tick: upper_tick.to_account_info(), - tickmap: tickmap.to_account_info(), - token_x: token_x.to_account_info(), - token_y: token_y.to_account_info(), - account_x: account_x.to_account_info(), - account_y: account_y.to_account_info(), - reserve_x: reserve_x.to_account_info(), - reserve_y: reserve_y.to_account_info(), - program_authority: program_authority.to_account_info(), - token_x_program: token_x_program.to_account_info(), - token_y_program: token_y_program.to_account_info(), - rent: rent.to_account_info(), - system_program: system_program.to_account_info(), - }; - let ctx = CpiContext::new(program, accounts); - - invariant::cpi::create_position( - ctx, - lower_tick_index, - upper_tick_index, - invariant::decimals::Liquidity { v: liquidity_delta }, - invariant::decimals::Price { - v: slippage_limit_lower, - }, - invariant::decimals::Price { - v: slippage_limit_upper, - }, - ) - } -} diff --git a/protocol/programs/protocol/src/contexts/invoke_update_seconds_per_liquidity.rs b/protocol/programs/protocol/src/contexts/invoke_update_seconds_per_liquidity.rs deleted file mode 100644 index c6296e8..0000000 --- a/protocol/programs/protocol/src/contexts/invoke_update_seconds_per_liquidity.rs +++ /dev/null @@ -1,64 +0,0 @@ -use anchor_lang::prelude::*; -use invariant::{cpi::accounts::UpdateSecondsPerLiquidity, program::Invariant}; - -#[derive(Accounts)] -pub struct InvokeUpdateSecondsPerLiquidityCtx<'info> { - pub invariant_program: Program<'info, Invariant>, - /// CHECK: - #[account(mut)] - pub pool: UncheckedAccount<'info>, - /// CHECK: - pub lower_tick: UncheckedAccount<'info>, - /// CHECK: - pub upper_tick: UncheckedAccount<'info>, - /// CHECK: - #[account(mut)] - pub position: UncheckedAccount<'info>, - /// CHECK: - pub token_x: UncheckedAccount<'info>, - /// CHECK: - pub token_y: UncheckedAccount<'info>, - #[account(mut)] - pub owner: Signer<'info>, - pub rent: Sysvar<'info, Rent>, - pub system_program: Program<'info, System>, -} - -impl InvokeUpdateSecondsPerLiquidityCtx<'_> { - pub fn process( - &mut self, - lower_tick_index: i32, - upper_tick_index: i32, - index: i32, - ) -> Result<()> { - let InvokeUpdateSecondsPerLiquidityCtx { - invariant_program, - pool, - lower_tick, - upper_tick, - position, - token_x, - token_y, - owner, - rent, - system_program, - } = self; - - let program = invariant_program.to_account_info(); - let accounts = UpdateSecondsPerLiquidity { - pool: pool.to_account_info(), - lower_tick: lower_tick.to_account_info(), - upper_tick: upper_tick.to_account_info(), - position: position.to_account_info(), - token_x: token_x.to_account_info(), - token_y: token_y.to_account_info(), - owner: owner.to_account_info(), - signer: owner.to_account_info(), - rent: rent.to_account_info(), - system_program: system_program.to_account_info(), - }; - let ctx = CpiContext::new(program, accounts); - - invariant::cpi::update_seconds_per_liquidity(ctx, lower_tick_index, upper_tick_index, index) - } -} diff --git a/protocol/programs/protocol/src/contexts/mod.rs b/protocol/programs/protocol/src/contexts/mod.rs index e25b648..ad7a40d 100644 --- a/protocol/programs/protocol/src/contexts/mod.rs +++ b/protocol/programs/protocol/src/contexts/mod.rs @@ -1,21 +1,9 @@ mod burn; mod init; mod init_pool; -mod invoke_close_position; -mod invoke_create_position; -mod invoke_update_seconds_per_liquidity; mod mint; -mod reopen_position; -mod test; -mod token; pub use burn::*; pub use init::*; pub use init_pool::*; -pub use invoke_close_position::*; -pub use invoke_create_position::*; -pub use invoke_update_seconds_per_liquidity::*; pub use mint::*; -pub use reopen_position::*; -pub use test::*; -pub use token::*; diff --git a/protocol/programs/protocol/src/contexts/reopen_position.rs b/protocol/programs/protocol/src/contexts/reopen_position.rs deleted file mode 100644 index ca76e7c..0000000 --- a/protocol/programs/protocol/src/contexts/reopen_position.rs +++ /dev/null @@ -1,142 +0,0 @@ -use anchor_lang::prelude::*; -use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface}; -use invariant::{ - cpi::accounts::{CreatePosition, RemovePosition}, - program::Invariant, - structs::{Pool, Position, PositionList, State as InvariantState, Tick, Tickmap}, -}; - -#[derive(Accounts)] -pub struct ReopenPositionCtx<'info> { - pub invariant_program: Program<'info, Invariant>, - #[account(mut)] - pub owner: Signer<'info>, - pub invariant_state: AccountLoader<'info, InvariantState>, - /// CHECK: invariant_program_authority is the authority of the Invariant program - pub invariant_program_authority: AccountInfo<'info>, - #[account(mut)] - pub position: AccountLoader<'info, Position>, - #[account(mut)] - pub last_position: AccountLoader<'info, Position>, - #[account(mut)] - pub pool: AccountLoader<'info, Pool>, - #[account(mut)] - pub position_list: AccountLoader<'info, PositionList>, - #[account(mut)] - pub lower_tick: AccountLoader<'info, Tick>, - #[account(mut)] - pub upper_tick: AccountLoader<'info, Tick>, - #[account(mut)] - pub tickmap: AccountLoader<'info, Tickmap>, - pub token_x: InterfaceAccount<'info, Mint>, - pub token_y: InterfaceAccount<'info, Mint>, - #[account(mut)] - pub account_x: InterfaceAccount<'info, TokenAccount>, - #[account(mut)] - pub account_y: InterfaceAccount<'info, TokenAccount>, - #[account(mut)] - pub reserve_x: InterfaceAccount<'info, TokenAccount>, - #[account(mut)] - pub reserve_y: InterfaceAccount<'info, TokenAccount>, - pub token_x_program: Interface<'info, TokenInterface>, - pub token_y_program: Interface<'info, TokenInterface>, - pub rent: Sysvar<'info, Rent>, - pub system_program: Program<'info, System>, -} - -impl ReopenPositionCtx<'_> { - pub fn process(&mut self, index: u32) -> Result<()> { - let ReopenPositionCtx { - invariant_program, - invariant_state, - position, - last_position, - pool, - position_list, - owner, - lower_tick, - upper_tick, - tickmap, - token_x, - token_y, - account_x, - account_y, - reserve_x, - reserve_y, - invariant_program_authority, - token_x_program, - token_y_program, - rent, - system_program, - .. - } = self; - let program = invariant_program.to_account_info(); - - let Position { - lower_tick_index, - upper_tick_index, - liquidity, - .. - } = *position.load()?; - let Pool { sqrt_price, .. } = *pool.load()?; - { - let accounts = RemovePosition { - state: invariant_state.to_account_info(), - program_authority: invariant_program_authority.to_account_info(), - owner: owner.to_account_info(), - removed_position: position.to_account_info(), - position_list: position_list.to_account_info(), - last_position: last_position.to_account_info(), - pool: pool.to_account_info(), - tickmap: tickmap.to_account_info(), - lower_tick: lower_tick.to_account_info(), - upper_tick: upper_tick.to_account_info(), - token_x: token_x.to_account_info(), - token_y: token_y.to_account_info(), - account_x: account_x.to_account_info(), - account_y: account_y.to_account_info(), - reserve_x: reserve_x.to_account_info(), - reserve_y: reserve_y.to_account_info(), - token_x_program: token_x_program.to_account_info(), - token_y_program: token_y_program.to_account_info(), - }; - let ctx = CpiContext::new(program.clone(), accounts); - invariant::cpi::remove_position(ctx, index, lower_tick_index, upper_tick_index)?; - } - { - let accounts = CreatePosition { - state: invariant_state.to_account_info(), - // the previous last position was moved to the previous' address - position: last_position.to_account_info(), - pool: pool.to_account_info(), - position_list: position_list.to_account_info(), - payer: owner.to_account_info(), - owner: owner.to_account_info(), - lower_tick: lower_tick.to_account_info(), - upper_tick: upper_tick.to_account_info(), - tickmap: tickmap.to_account_info(), - token_x: token_x.to_account_info(), - token_y: token_y.to_account_info(), - account_x: account_x.to_account_info(), - account_y: account_y.to_account_info(), - reserve_x: reserve_x.to_account_info(), - reserve_y: reserve_y.to_account_info(), - program_authority: invariant_program_authority.to_account_info(), - token_x_program: token_x_program.to_account_info(), - token_y_program: token_y_program.to_account_info(), - rent: rent.to_account_info(), - system_program: system_program.to_account_info(), - }; - let ctx = CpiContext::new(program, accounts); - - invariant::cpi::create_position( - ctx, - lower_tick_index, - upper_tick_index, - liquidity, - sqrt_price, - sqrt_price, - ) - } - } -} diff --git a/protocol/programs/protocol/src/contexts/test.rs b/protocol/programs/protocol/src/contexts/test.rs deleted file mode 100644 index 86a9072..0000000 --- a/protocol/programs/protocol/src/contexts/test.rs +++ /dev/null @@ -1,37 +0,0 @@ -use anchor_lang::prelude::*; -use puppet::{cpi::accounts::CreateCounter, program::Puppet}; - -#[derive(Accounts)] -pub struct Test<'info> { - #[account(mut)] - pub payer: Signer<'info>, - - pub puppet_program: Program<'info, Puppet>, - - /// CHECK: This is the account that will be created by the Puppet program. - #[account(mut)] - pub counter: UncheckedAccount<'info>, - - pub system_program: Program<'info, System>, -} - -impl Test<'_> { - pub fn process(&mut self, state_bump: u8) -> Result<()> { - let Test { - payer, - puppet_program, - counter, - system_program, - } = self; - - let program = puppet_program.to_account_info(); - let accounts = CreateCounter { - counter: counter.to_account_info(), - admin: payer.to_account_info(), - system_program: system_program.to_account_info(), - }; - let ctx = CpiContext::new(program, accounts); - - puppet::cpi::create_counter(ctx, state_bump) - } -} diff --git a/protocol/programs/protocol/src/contexts/token/deposit.rs b/protocol/programs/protocol/src/contexts/token/deposit.rs deleted file mode 100644 index 38b1d1f..0000000 --- a/protocol/programs/protocol/src/contexts/token/deposit.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::ErrorCode::*; -use anchor_lang::prelude::*; -use anchor_spl::token::{self, Token, TokenAccount, Transfer}; - -use crate::states::{DerivedAccountIdentifier, State}; - -#[derive(Accounts)] -pub struct DepositCtx<'info> { - #[account( - seeds = [State::IDENT], - bump = state.load()?.bump - )] - pub state: AccountLoader<'info, State>, - - /// CHECK: pretty much only cached from the state account - #[account(constraint = &state.load()?.program_authority == program_authority.key @ InvalidAuthority)] - pub program_authority: AccountInfo<'info>, - #[account(mut)] - pub token_mint: Account<'info, anchor_spl::token::Mint>, - - #[account(mut, - constraint = reserve.mint == token_mint.key() @ InvalidMint, - constraint = reserve.owner == program_authority.key() @ InvalidAuthority - )] - pub reserve: Account<'info, TokenAccount>, - - #[account(mut, - constraint = user_balance.mint == token_mint.key() @ InvalidMint, - constraint = user_balance.owner == owner.key() @ InvalidOwner - )] - pub user_balance: Account<'info, TokenAccount>, - - pub owner: Signer<'info>, - pub token_program: Program<'info, Token>, -} - -impl<'info> DepositCtx<'info> { - pub fn deposit_ctx(&self) -> CpiContext<'_, '_, '_, 'info, Transfer<'info>> { - CpiContext::new( - self.token_program.to_account_info(), - Transfer { - from: self.user_balance.to_account_info(), - to: self.reserve.to_account_info(), - authority: self.owner.to_account_info(), - }, - ) - } -} - -impl DepositCtx<'_> { - pub fn process(&self, amount: u64) -> Result<()> { - token::transfer(self.deposit_ctx(), amount) - } -} diff --git a/protocol/programs/protocol/src/contexts/token/mint.rs b/protocol/programs/protocol/src/contexts/token/mint.rs deleted file mode 100644 index ae5effb..0000000 --- a/protocol/programs/protocol/src/contexts/token/mint.rs +++ /dev/null @@ -1,53 +0,0 @@ -use crate::{get_signer, ErrorCode::*}; -use anchor_lang::prelude::*; -use anchor_spl::{ - token::{self}, - token_2022::{self}, - token_interface::{mint_to, Mint, MintTo, TokenAccount, TokenInterface}, -}; - -use crate::states::{DerivedAccountIdentifier, State}; - -#[derive(Accounts)] -pub struct MintCtx<'info> { - #[account( - seeds = [State::IDENT], - bump = state.load()?.bump - )] - pub state: AccountLoader<'info, State>, - - /// CHECK: cached from the state account - #[account(constraint = &state.load()?.program_authority == program_authority.key @ InvalidAuthority)] - pub program_authority: AccountInfo<'info>, - #[account(mut)] - pub token_mint: InterfaceAccount<'info, Mint>, - #[account(mut, - constraint = to.mint == token_mint.key() - )] - pub to: InterfaceAccount<'info, TokenAccount>, - #[account(constraint = token_program.key() == token::ID || token_program.key() == token_2022::ID)] - pub token_program: Interface<'info, TokenInterface>, -} - -// WARNING: In our actual entrypoint we are going to use Token2022::MintTo -impl<'info> MintCtx<'info> { - pub fn mint_ctx(&self) -> CpiContext<'_, '_, '_, 'info, MintTo<'info>> { - CpiContext::new( - self.token_program.to_account_info(), - MintTo { - mint: self.token_mint.to_account_info(), - to: self.to.to_account_info(), - authority: self.program_authority.to_account_info(), - }, - ) - } -} - -impl MintCtx<'_> { - pub fn process(&self, amount: u64) -> Result<()> { - let state = &self.state.load()?; - - let signer: &[&[&[u8]]] = get_signer!(state.bump_authority); - mint_to(self.mint_ctx().with_signer(signer), amount) - } -} diff --git a/protocol/programs/protocol/src/contexts/token/mod.rs b/protocol/programs/protocol/src/contexts/token/mod.rs deleted file mode 100644 index ef55c27..0000000 --- a/protocol/programs/protocol/src/contexts/token/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub mod deposit; -// pub mod mint; -pub mod withdraw; - -pub use self::deposit::*; -// pub use self::mint::*; -pub use self::withdraw::*; diff --git a/protocol/programs/protocol/src/contexts/token/withdraw.rs b/protocol/programs/protocol/src/contexts/token/withdraw.rs deleted file mode 100644 index fe5fd4f..0000000 --- a/protocol/programs/protocol/src/contexts/token/withdraw.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::{get_signer, ErrorCode::*}; -use anchor_lang::prelude::*; -use anchor_spl::token::{self, Token, TokenAccount, Transfer}; - -use crate::states::{DerivedAccountIdentifier, State}; - -#[derive(Accounts)] -pub struct WithdrawCtx<'info> { - #[account( - seeds = [State::IDENT], - bump = state.load()?.bump - )] - pub state: AccountLoader<'info, State>, - - /// CHECK: pretty much only cached from the state account - #[account(constraint = &state.load()?.program_authority == program_authority.key @ InvalidAuthority)] - pub program_authority: AccountInfo<'info>, - #[account(mut)] - pub token_mint: Account<'info, anchor_spl::token::Mint>, - - #[account(mut, - constraint = reserve.mint == token_mint.key() @ InvalidMint, - constraint = reserve.owner == program_authority.key() @ InvalidAuthority - )] - pub reserve: Account<'info, TokenAccount>, - - #[account(mut, - constraint = user_balance.mint == token_mint.key() @ InvalidMint, - constraint = user_balance.owner == owner.key() @ InvalidOwner - )] - pub user_balance: Account<'info, TokenAccount>, - - pub owner: Signer<'info>, - pub token_program: Program<'info, Token>, -} - -impl<'info> WithdrawCtx<'info> { - pub fn withdraw_ctx(&self) -> CpiContext<'_, '_, '_, 'info, Transfer<'info>> { - CpiContext::new( - self.token_program.to_account_info(), - Transfer { - from: self.reserve.to_account_info(), - to: self.user_balance.to_account_info(), - authority: self.program_authority.clone(), - }, - ) - } -} - -impl WithdrawCtx<'_> { - pub fn process(&self, amount: u64) -> Result<()> { - let state = &self.state.load()?; - let signer: &[&[&[u8]]] = get_signer!(state.bump_authority); - - token::transfer(self.withdraw_ctx().with_signer(signer), amount) - } -} diff --git a/protocol/programs/protocol/src/lib.rs b/protocol/programs/protocol/src/lib.rs index 6953a31..9be4910 100644 --- a/protocol/programs/protocol/src/lib.rs +++ b/protocol/programs/protocol/src/lib.rs @@ -35,63 +35,6 @@ pub mod protocol { ctx.accounts.process(bump, bump_authority) } - pub fn test(ctx: Context, state_bump: u8) -> Result<()> { - ctx.accounts.process(state_bump) - } - - // pub fn mint(ctx: Context, amount: u64) -> Result<()> { - // ctx.accounts.process(amount) - // } - - pub fn deposit(ctx: Context, amount: u64) -> Result<()> { - ctx.accounts.process(amount) - } - - pub fn withdraw(ctx: Context, amount: u64) -> Result<()> { - ctx.accounts.process(amount) - } - - pub fn invoke_update_seconds_per_liquidity( - ctx: Context, - lower_tick_index: i32, - upper_tick_index: i32, - index: i32, - ) -> Result<()> { - ctx.accounts - .process(lower_tick_index, upper_tick_index, index) - } - - pub fn invoke_create_position( - ctx: Context, - lower_tick_index: i32, - upper_tick_index: i32, - liquidity_delta: u128, - slippage_limit_lower: u128, - slippage_limit_upper: u128, - ) -> Result<()> { - ctx.accounts.process( - lower_tick_index, - upper_tick_index, - liquidity_delta, - slippage_limit_lower, - slippage_limit_upper, - ) - } - - pub fn invoke_close_position( - ctx: Context, - index: u32, - lower_tick_index: i32, - upper_tick_index: i32, - ) -> Result<()> { - ctx.accounts - .process(index, lower_tick_index, upper_tick_index) - } - - pub fn reopen_position(ctx: Context, index: u32) -> Result<()> { - ctx.accounts.process(index) - } - pub fn init_lp_pool(ctx: Context) -> Result<()> { let token_bump = ctx.bumps.token_lp; let bump = ctx.bumps.lp_pool; diff --git a/protocol/programs/puppet/Cargo.toml b/protocol/programs/puppet/Cargo.toml deleted file mode 100644 index d129b65..0000000 --- a/protocol/programs/puppet/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "puppet" -version = "0.1.0" -description = "Created with Anchor" -edition = "2018" - -[lib] -crate-type = ["cdylib", "lib"] -name = "puppet" - -[features] -no-entrypoint = [] -no-idl = [] -no-log-ix-name = [] -cpi = ["no-entrypoint"] -default = [] - -[dependencies] -anchor-lang = "0.29.0" diff --git a/protocol/programs/puppet/Xargo.toml b/protocol/programs/puppet/Xargo.toml deleted file mode 100644 index 475fb71..0000000 --- a/protocol/programs/puppet/Xargo.toml +++ /dev/null @@ -1,2 +0,0 @@ -[target.bpfel-unknown-unknown.dependencies.std] -features = [] diff --git a/protocol/programs/puppet/src/contexts/create_counter.rs b/protocol/programs/puppet/src/contexts/create_counter.rs deleted file mode 100644 index 0505c01..0000000 --- a/protocol/programs/puppet/src/contexts/create_counter.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::states::{Counter, DerivedAccountIdentifier}; -use anchor_lang::prelude::*; - -#[derive(Accounts)] -pub struct CreateCounter<'info> { - #[account(init, space = Counter::LEN, seeds = [Counter::IDENT], bump, payer = admin)] - pub counter: Account<'info, Counter>, - - #[account(mut)] - pub admin: Signer<'info>, - - pub system_program: Program<'info, System>, -} - -impl<'info> CreateCounter<'info> { - pub fn process(&mut self, state_bump: u8) -> Result<()> { - let CreateCounter { counter, admin, .. } = self; - - **counter = Counter { - owner: admin.key(), - counter: 0, - bump: state_bump, - }; - - Ok(()) - } -} diff --git a/protocol/programs/puppet/src/contexts/mod.rs b/protocol/programs/puppet/src/contexts/mod.rs deleted file mode 100644 index e87cdc0..0000000 --- a/protocol/programs/puppet/src/contexts/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod create_counter; - -pub use create_counter::*; diff --git a/protocol/programs/puppet/src/errors.rs b/protocol/programs/puppet/src/errors.rs deleted file mode 100644 index 65e41d0..0000000 --- a/protocol/programs/puppet/src/errors.rs +++ /dev/null @@ -1,21 +0,0 @@ -use anchor_lang::prelude::*; -use std::convert::TryInto; - -// Define errors, custom error code: 300 + idx => 0x12C + 0x${idx} -#[error_code(offset = 300)] -pub enum ErrorCode { - #[msg("[S001] ErrorExample")] //0x12C (300) - ErrorExample, -} - -impl TryInto for u32 { - type Error = (); // Error if u32 is out of range - - fn try_into(self) -> std::result::Result { - if (300..=320).contains(&self) { - Ok(unsafe { std::mem::transmute(self - 300) }) - } else { - Err(()) - } - } -} diff --git a/protocol/programs/puppet/src/lib.rs b/protocol/programs/puppet/src/lib.rs deleted file mode 100644 index 2ee9ebb..0000000 --- a/protocol/programs/puppet/src/lib.rs +++ /dev/null @@ -1,24 +0,0 @@ -mod contexts; -mod errors; -pub mod states; - -use anchor_lang::prelude::*; -pub use contexts::*; -pub use errors::ErrorCode; -pub use program_id::*; - -mod program_id { - use anchor_lang::prelude::*; - declare_id!("8KQzCc22ZqGLPoipqRhYvkQtHJw6nY1NxrrGy8JLz1jC"); -} - -#[program] -pub mod puppet { - - use super::*; - - pub fn create_counter(ctx: Context, state_bump: u8) -> Result<()> { - ctx.accounts.process(state_bump)?; - Ok(()) - } -} diff --git a/protocol/programs/puppet/src/states/counter.rs b/protocol/programs/puppet/src/states/counter.rs deleted file mode 100644 index e3f9994..0000000 --- a/protocol/programs/puppet/src/states/counter.rs +++ /dev/null @@ -1,18 +0,0 @@ -use crate::size; - -use super::DerivedAccountIdentifier; -use anchor_lang::prelude::*; - -impl DerivedAccountIdentifier for Counter { - const IDENT: &'static [u8] = b"PUPPETCounter"; -} - -#[account] -#[derive(Default, Debug, InitSpace)] -pub struct Counter { - pub owner: Pubkey, - pub counter: u8, - pub bump: u8, -} - -size!(Counter); diff --git a/protocol/programs/puppet/src/states/mod.rs b/protocol/programs/puppet/src/states/mod.rs deleted file mode 100644 index 441a1a7..0000000 --- a/protocol/programs/puppet/src/states/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -mod counter; - -pub use counter::Counter; - -pub trait DerivedAccountIdentifier { - const IDENT: &'static [u8]; -} - -#[macro_export] -macro_rules! size { - ($name: ident) => { - impl $name { - pub const LEN: usize = $name::INIT_SPACE + 8; - } - }; -} diff --git a/protocol/sdk/package.json b/protocol/sdk/package.json index 4bc97f6..843bf75 100644 --- a/protocol/sdk/package.json +++ b/protocol/sdk/package.json @@ -5,8 +5,7 @@ "types": "dist/index.d.ts", "scripts": { "prebuild:protocol": "rm -rf ./src/idl/protocol.ts && cp -r ../target/types/protocol.ts ./src/idl", - "prebuild:puppet": "rm -rf ./src/idl/puppet.ts && cp -r ../target/types/puppet.ts ./src/idl", - "prebuild:all": "npm run prebuild:protocol && npm run prebuild:puppet", + "prebuild:all": "npm run prebuild:protocol", "build": "npm run prebuild:all && npx tsc" }, "keywords": [], diff --git a/protocol/sdk/src/consts.ts b/protocol/sdk/src/consts.ts index 65dec30..4c71399 100644 --- a/protocol/sdk/src/consts.ts +++ b/protocol/sdk/src/consts.ts @@ -1,6 +1,5 @@ export const PROTOCOL_STATE_SEED = "PROTOCOLState"; export const PROTOCOL_AUTHORITY_SEED = "PROTOCOLAuthority"; -export const PUPPET_COUNTER_SEED = "PUPPETCounter"; export const LP_POOL_SEED = "poolv1"; export const LP_TOKEN_SEED = "lp_tokenv1"; diff --git a/protocol/sdk/src/idl/protocol.ts b/protocol/sdk/src/idl/protocol.ts index 5acfa38..d3b523b 100644 --- a/protocol/sdk/src/idl/protocol.ts +++ b/protocol/sdk/src/idl/protocol.ts @@ -39,38 +39,7 @@ export type Protocol = { ] }, { - "name": "test", - "accounts": [ - { - "name": "payer", - "isMut": true, - "isSigner": true - }, - { - "name": "puppetProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "counter", - "isMut": true, - "isSigner": false - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "stateBump", - "type": "u8" - } - ] - }, - { - "name": "deposit", + "name": "initLpPool", "accounts": [ { "name": "state", @@ -83,168 +52,141 @@ export type Protocol = { "isSigner": false }, { - "name": "tokenMint", + "name": "lpPool", "isMut": true, "isSigner": false }, { - "name": "reserve", + "name": "tokenLp", "isMut": true, "isSigner": false }, { - "name": "userBalance", + "name": "payer", "isMut": true, - "isSigner": false - }, - { - "name": "owner", - "isMut": false, "isSigner": true }, { - "name": "tokenProgram", + "name": "pool", "isMut": false, "isSigner": false - } - ], - "args": [ - { - "name": "amount", - "type": "u64" - } - ] - }, - { - "name": "withdraw", - "accounts": [ + }, { - "name": "state", + "name": "tokenX", "isMut": false, "isSigner": false }, { - "name": "programAuthority", + "name": "tokenY", "isMut": false, "isSigner": false }, { - "name": "tokenMint", + "name": "reserveX", "isMut": true, "isSigner": false }, { - "name": "reserve", + "name": "reserveY", "isMut": true, "isSigner": false }, { - "name": "userBalance", - "isMut": true, + "name": "tokenXProgram", + "isMut": false, "isSigner": false }, { - "name": "owner", + "name": "tokenYProgram", "isMut": false, - "isSigner": true + "isSigner": false }, { "name": "tokenProgram", "isMut": false, "isSigner": false - } - ], - "args": [ + }, + { + "name": "associatedTokenProgram", + "isMut": false, + "isSigner": false + }, { - "name": "amount", - "type": "u64" + "name": "systemProgram", + "isMut": false, + "isSigner": false } - ] + ], + "args": [] }, { - "name": "invokeUpdateSecondsPerLiquidity", + "name": "mintLpToken", "accounts": [ { - "name": "invariantProgram", - "isMut": false, - "isSigner": false + "name": "owner", + "isMut": true, + "isSigner": true }, { - "name": "pool", - "isMut": true, + "name": "state", + "isMut": false, "isSigner": false }, { - "name": "lowerTick", - "isMut": false, + "name": "programAuthority", + "isMut": true, "isSigner": false }, { - "name": "upperTick", - "isMut": false, + "name": "lpPool", + "isMut": true, "isSigner": false }, { - "name": "position", + "name": "tokenLp", "isMut": true, "isSigner": false }, { - "name": "tokenX", - "isMut": false, + "name": "reserveX", + "isMut": true, "isSigner": false }, { - "name": "tokenY", - "isMut": false, + "name": "reserveY", + "isMut": true, "isSigner": false }, { - "name": "owner", + "name": "accountLp", "isMut": true, - "isSigner": true + "isSigner": false }, { - "name": "rent", + "name": "tokenProgram", "isMut": false, "isSigner": false }, { - "name": "systemProgram", + "name": "associatedTokenProgram", "isMut": false, "isSigner": false - } - ], - "args": [ - { - "name": "lowerTickIndex", - "type": "i32" }, { - "name": "upperTickIndex", - "type": "i32" + "name": "invProgram", + "isMut": false, + "isSigner": false, + "docs": [ + "INVARIANT" + ] }, { - "name": "index", - "type": "i32" - } - ] - }, - { - "name": "invokeCreatePosition", - "accounts": [ - { - "name": "invariantProgram", + "name": "invState", "isMut": false, "isSigner": false }, { - "name": "owner", - "isMut": true, - "isSigner": true - }, - { - "name": "state", + "name": "invProgramAuthority", "isMut": false, "isSigner": false }, @@ -253,6 +195,11 @@ export type Protocol = { "isMut": true, "isSigner": false }, + { + "name": "lastPosition", + "isMut": true, + "isSigner": false + }, { "name": "pool", "isMut": true, @@ -299,20 +246,15 @@ export type Protocol = { "isSigner": false }, { - "name": "reserveX", + "name": "invReserveX", "isMut": true, "isSigner": false }, { - "name": "reserveY", + "name": "invReserveY", "isMut": true, "isSigner": false }, - { - "name": "programAuthority", - "isMut": false, - "isSigner": false - }, { "name": "tokenXProgram", "isMut": false, @@ -336,97 +278,40 @@ export type Protocol = { ], "args": [ { - "name": "lowerTickIndex", - "type": "i32" - }, - { - "name": "upperTickIndex", - "type": "i32" - }, - { - "name": "liquidityDelta", - "type": "u128" - }, - { - "name": "slippageLimitLower", + "name": "liquidity", "type": "u128" }, { - "name": "slippageLimitUpper", - "type": "u128" + "name": "index", + "type": "u32" } ] }, { - "name": "invokeClosePosition", + "name": "burnLpToken", "accounts": [ - { - "name": "invariantState", - "isMut": true, - "isSigner": false - }, - { - "name": "invariantProgramAuthority", - "isMut": false, - "isSigner": false - }, { "name": "owner", "isMut": true, "isSigner": true }, { - "name": "removedPosition", - "isMut": true, - "isSigner": false - }, - { - "name": "positionList", - "isMut": true, - "isSigner": false - }, - { - "name": "lastPosition", - "isMut": true, - "isSigner": false - }, - { - "name": "pool", - "isMut": true, - "isSigner": false - }, - { - "name": "tickmap", - "isMut": true, - "isSigner": false - }, - { - "name": "lowerTick", - "isMut": true, - "isSigner": false - }, - { - "name": "upperTick", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenX", + "name": "state", "isMut": false, "isSigner": false }, { - "name": "tokenY", - "isMut": false, + "name": "programAuthority", + "isMut": true, "isSigner": false }, { - "name": "accountX", + "name": "lpPool", "isMut": true, "isSigner": false }, { - "name": "accountY", + "name": "tokenLp", "isMut": true, "isSigner": false }, @@ -441,56 +326,35 @@ export type Protocol = { "isSigner": false }, { - "name": "tokenXProgram", - "isMut": false, + "name": "accountLp", + "isMut": true, "isSigner": false }, { - "name": "tokenYProgram", + "name": "tokenProgram", "isMut": false, "isSigner": false }, { - "name": "invariantProgram", + "name": "associatedTokenProgram", "isMut": false, "isSigner": false - } - ], - "args": [ - { - "name": "index", - "type": "u32" }, { - "name": "lowerTickIndex", - "type": "i32" - }, - { - "name": "upperTickIndex", - "type": "i32" - } - ] - }, - { - "name": "reopenPosition", - "accounts": [ - { - "name": "invariantProgram", + "name": "invProgram", "isMut": false, - "isSigner": false - }, - { - "name": "owner", - "isMut": true, - "isSigner": true + "isSigner": false, + "docs": [ + "INVARIANT" + ] }, { - "name": "invariantState", + "name": "invState", "isMut": false, "isSigner": false }, { - "name": "invariantProgramAuthority", + "name": "invProgramAuthority", "isMut": false, "isSigner": false }, @@ -550,257 +414,12 @@ export type Protocol = { "isSigner": false }, { - "name": "reserveX", + "name": "invReserveX", "isMut": true, "isSigner": false }, { - "name": "reserveY", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenXProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenYProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "rent", - "isMut": false, - "isSigner": false - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "index", - "type": "u32" - } - ] - }, - { - "name": "initLpPool", - "accounts": [ - { - "name": "state", - "isMut": false, - "isSigner": false - }, - { - "name": "programAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "lpPool", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenLp", - "isMut": true, - "isSigner": false - }, - { - "name": "payer", - "isMut": true, - "isSigner": true - }, - { - "name": "pool", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenX", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenY", - "isMut": false, - "isSigner": false - }, - { - "name": "reserveX", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveY", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenXProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenYProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "associatedTokenProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [] - }, - { - "name": "mintLpToken", - "accounts": [ - { - "name": "owner", - "isMut": true, - "isSigner": true - }, - { - "name": "state", - "isMut": false, - "isSigner": false - }, - { - "name": "programAuthority", - "isMut": true, - "isSigner": false - }, - { - "name": "lpPool", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenLp", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveX", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveY", - "isMut": true, - "isSigner": false - }, - { - "name": "accountLp", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "associatedTokenProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "invProgram", - "isMut": false, - "isSigner": false, - "docs": [ - "INVARIANT" - ] - }, - { - "name": "invState", - "isMut": false, - "isSigner": false - }, - { - "name": "invProgramAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "position", - "isMut": true, - "isSigner": false - }, - { - "name": "lastPosition", - "isMut": true, - "isSigner": false - }, - { - "name": "pool", - "isMut": true, - "isSigner": false - }, - { - "name": "positionList", - "isMut": true, - "isSigner": false - }, - { - "name": "lowerTick", - "isMut": true, - "isSigner": false - }, - { - "name": "upperTick", - "isMut": true, - "isSigner": false - }, - { - "name": "tickmap", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenX", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenY", - "isMut": false, - "isSigner": false - }, - { - "name": "accountX", - "isMut": true, - "isSigner": false - }, - { - "name": "accountY", - "isMut": true, - "isSigner": false - }, - { - "name": "invReserveX", - "isMut": true, - "isSigner": false - }, - { - "name": "invReserveY", + "name": "invReserveY", "isMut": true, "isSigner": false }, @@ -835,177 +454,9 @@ export type Protocol = { "type": "u32" } ] - }, - { - "name": "burnLpToken", - "accounts": [ - { - "name": "owner", - "isMut": true, - "isSigner": true - }, - { - "name": "state", - "isMut": false, - "isSigner": false - }, - { - "name": "programAuthority", - "isMut": true, - "isSigner": false - }, - { - "name": "lpPool", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenLp", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveX", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveY", - "isMut": true, - "isSigner": false - }, - { - "name": "accountLp", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "associatedTokenProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "invProgram", - "isMut": false, - "isSigner": false, - "docs": [ - "INVARIANT" - ] - }, - { - "name": "invState", - "isMut": false, - "isSigner": false - }, - { - "name": "invProgramAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "position", - "isMut": true, - "isSigner": false - }, - { - "name": "lastPosition", - "isMut": true, - "isSigner": false - }, - { - "name": "pool", - "isMut": true, - "isSigner": false - }, - { - "name": "positionList", - "isMut": true, - "isSigner": false - }, - { - "name": "lowerTick", - "isMut": true, - "isSigner": false - }, - { - "name": "upperTick", - "isMut": true, - "isSigner": false - }, - { - "name": "tickmap", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenX", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenY", - "isMut": false, - "isSigner": false - }, - { - "name": "accountX", - "isMut": true, - "isSigner": false - }, - { - "name": "accountY", - "isMut": true, - "isSigner": false - }, - { - "name": "invReserveX", - "isMut": true, - "isSigner": false - }, - { - "name": "invReserveY", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenXProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenYProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "rent", - "isMut": false, - "isSigner": false - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "liquidity", - "type": "u128" - }, - { - "name": "index", - "type": "u32" - } - ] - } - ], - "accounts": [ + } + ], + "accounts": [ { "name": "lpPool", "type": { @@ -1112,638 +563,89 @@ export type Protocol = { }, { "name": "FeeGrowth", - "type": { - "kind": "struct", - "fields": [ - { - "name": "v", - "type": "u128" - } - ] - } - }, - { - "name": "FixedPoint", - "type": { - "kind": "struct", - "fields": [ - { - "name": "v", - "type": "u128" - } - ] - } - } - ], - "errors": [ - { - "code": 6000, - "name": "ErrorExample", - "msg": "[S001] ErrorExample" - }, - { - "code": 6001, - "name": "InvalidAuthority", - "msg": "Provided authority is different than expected" - }, - { - "code": 6002, - "name": "InvalidMint", - "msg": "Provided mint account is different than expected" - }, - { - "code": 6003, - "name": "InvalidOwner", - "msg": "Provided owner account is different than expected" - }, - { - "code": 6004, - "name": "InvalidInvariantAuthority", - "msg": "Provided Invariant authority is different than expected" - }, - { - "code": 6005, - "name": "InvalidTokenProgram", - "msg": "Provided Token Program for Token is different than expected" - }, - { - "code": 6006, - "name": "InvalidShares", - "msg": "Error originated from compute_lp_share_change" - } - ] -}; - -export const IDL: Protocol = { - "version": "0.1.0", - "name": "protocol", - "instructions": [ - { - "name": "init", - "accounts": [ - { - "name": "state", - "isMut": true, - "isSigner": false - }, - { - "name": "programAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "admin", - "isMut": true, - "isSigner": true - }, - { - "name": "rent", - "isMut": false, - "isSigner": false - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "bumpAuthority", - "type": "u8" - } - ] - }, - { - "name": "test", - "accounts": [ - { - "name": "payer", - "isMut": true, - "isSigner": true - }, - { - "name": "puppetProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "counter", - "isMut": true, - "isSigner": false - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "stateBump", - "type": "u8" - } - ] - }, - { - "name": "deposit", - "accounts": [ - { - "name": "state", - "isMut": false, - "isSigner": false - }, - { - "name": "programAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenMint", - "isMut": true, - "isSigner": false - }, - { - "name": "reserve", - "isMut": true, - "isSigner": false - }, - { - "name": "userBalance", - "isMut": true, - "isSigner": false - }, - { - "name": "owner", - "isMut": false, - "isSigner": true - }, - { - "name": "tokenProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "amount", - "type": "u64" - } - ] - }, - { - "name": "withdraw", - "accounts": [ - { - "name": "state", - "isMut": false, - "isSigner": false - }, - { - "name": "programAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenMint", - "isMut": true, - "isSigner": false - }, - { - "name": "reserve", - "isMut": true, - "isSigner": false - }, - { - "name": "userBalance", - "isMut": true, - "isSigner": false - }, - { - "name": "owner", - "isMut": false, - "isSigner": true - }, - { - "name": "tokenProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "amount", - "type": "u64" - } - ] - }, - { - "name": "invokeUpdateSecondsPerLiquidity", - "accounts": [ - { - "name": "invariantProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "pool", - "isMut": true, - "isSigner": false - }, - { - "name": "lowerTick", - "isMut": false, - "isSigner": false - }, - { - "name": "upperTick", - "isMut": false, - "isSigner": false - }, - { - "name": "position", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenX", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenY", - "isMut": false, - "isSigner": false - }, - { - "name": "owner", - "isMut": true, - "isSigner": true - }, - { - "name": "rent", - "isMut": false, - "isSigner": false - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "lowerTickIndex", - "type": "i32" - }, - { - "name": "upperTickIndex", - "type": "i32" - }, - { - "name": "index", - "type": "i32" - } - ] - }, - { - "name": "invokeCreatePosition", - "accounts": [ - { - "name": "invariantProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "owner", - "isMut": true, - "isSigner": true - }, - { - "name": "state", - "isMut": false, - "isSigner": false - }, - { - "name": "position", - "isMut": true, - "isSigner": false - }, - { - "name": "pool", - "isMut": true, - "isSigner": false - }, - { - "name": "positionList", - "isMut": true, - "isSigner": false - }, - { - "name": "lowerTick", - "isMut": true, - "isSigner": false - }, - { - "name": "upperTick", - "isMut": true, - "isSigner": false - }, - { - "name": "tickmap", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenX", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenY", - "isMut": false, - "isSigner": false - }, - { - "name": "accountX", - "isMut": true, - "isSigner": false - }, - { - "name": "accountY", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveX", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveY", - "isMut": true, - "isSigner": false - }, - { - "name": "programAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenXProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenYProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "rent", - "isMut": false, - "isSigner": false - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "lowerTickIndex", - "type": "i32" - }, - { - "name": "upperTickIndex", - "type": "i32" - }, - { - "name": "liquidityDelta", - "type": "u128" - }, - { - "name": "slippageLimitLower", - "type": "u128" - }, - { - "name": "slippageLimitUpper", - "type": "u128" - } - ] + "type": { + "kind": "struct", + "fields": [ + { + "name": "v", + "type": "u128" + } + ] + } }, { - "name": "invokeClosePosition", - "accounts": [ - { - "name": "invariantState", - "isMut": true, - "isSigner": false - }, - { - "name": "invariantProgramAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "owner", - "isMut": true, - "isSigner": true - }, - { - "name": "removedPosition", - "isMut": true, - "isSigner": false - }, - { - "name": "positionList", - "isMut": true, - "isSigner": false - }, - { - "name": "lastPosition", - "isMut": true, - "isSigner": false - }, - { - "name": "pool", - "isMut": true, - "isSigner": false - }, - { - "name": "tickmap", - "isMut": true, - "isSigner": false - }, - { - "name": "lowerTick", - "isMut": true, - "isSigner": false - }, - { - "name": "upperTick", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenX", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenY", - "isMut": false, - "isSigner": false - }, - { - "name": "accountX", - "isMut": true, - "isSigner": false - }, - { - "name": "accountY", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveX", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveY", - "isMut": true, - "isSigner": false - }, - { - "name": "tokenXProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenYProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "invariantProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "index", - "type": "u32" - }, - { - "name": "lowerTickIndex", - "type": "i32" - }, - { - "name": "upperTickIndex", - "type": "i32" - } - ] + "name": "FixedPoint", + "type": { + "kind": "struct", + "fields": [ + { + "name": "v", + "type": "u128" + } + ] + } + } + ], + "errors": [ + { + "code": 6000, + "name": "ErrorExample", + "msg": "[S001] ErrorExample" + }, + { + "code": 6001, + "name": "InvalidAuthority", + "msg": "Provided authority is different than expected" + }, + { + "code": 6002, + "name": "InvalidMint", + "msg": "Provided mint account is different than expected" + }, + { + "code": 6003, + "name": "InvalidOwner", + "msg": "Provided owner account is different than expected" }, { - "name": "reopenPosition", + "code": 6004, + "name": "InvalidInvariantAuthority", + "msg": "Provided Invariant authority is different than expected" + }, + { + "code": 6005, + "name": "InvalidTokenProgram", + "msg": "Provided Token Program for Token is different than expected" + }, + { + "code": 6006, + "name": "InvalidShares", + "msg": "Error originated from compute_lp_share_change" + } + ] +}; + +export const IDL: Protocol = { + "version": "0.1.0", + "name": "protocol", + "instructions": [ + { + "name": "init", "accounts": [ { - "name": "invariantProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "owner", - "isMut": true, - "isSigner": true - }, - { - "name": "invariantState", - "isMut": false, - "isSigner": false - }, - { - "name": "invariantProgramAuthority", - "isMut": false, - "isSigner": false - }, - { - "name": "position", - "isMut": true, - "isSigner": false - }, - { - "name": "lastPosition", - "isMut": true, - "isSigner": false - }, - { - "name": "pool", - "isMut": true, - "isSigner": false - }, - { - "name": "positionList", - "isMut": true, - "isSigner": false - }, - { - "name": "lowerTick", - "isMut": true, - "isSigner": false - }, - { - "name": "upperTick", - "isMut": true, - "isSigner": false - }, - { - "name": "tickmap", + "name": "state", "isMut": true, "isSigner": false }, { - "name": "tokenX", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenY", + "name": "programAuthority", "isMut": false, "isSigner": false }, { - "name": "accountX", - "isMut": true, - "isSigner": false - }, - { - "name": "accountY", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveX", - "isMut": true, - "isSigner": false - }, - { - "name": "reserveY", + "name": "admin", "isMut": true, - "isSigner": false - }, - { - "name": "tokenXProgram", - "isMut": false, - "isSigner": false - }, - { - "name": "tokenYProgram", - "isMut": false, - "isSigner": false + "isSigner": true }, { "name": "rent", @@ -1758,8 +660,8 @@ export const IDL: Protocol = { ], "args": [ { - "name": "index", - "type": "u32" + "name": "bumpAuthority", + "type": "u8" } ] }, diff --git a/protocol/sdk/src/idl/puppet.ts b/protocol/sdk/src/idl/puppet.ts deleted file mode 100644 index 1cbe016..0000000 --- a/protocol/sdk/src/idl/puppet.ts +++ /dev/null @@ -1,123 +0,0 @@ -export type Puppet = { - "version": "0.1.0", - "name": "puppet", - "instructions": [ - { - "name": "createCounter", - "accounts": [ - { - "name": "counter", - "isMut": true, - "isSigner": false - }, - { - "name": "admin", - "isMut": true, - "isSigner": true - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "stateBump", - "type": "u8" - } - ] - } - ], - "accounts": [ - { - "name": "counter", - "type": { - "kind": "struct", - "fields": [ - { - "name": "owner", - "type": "publicKey" - }, - { - "name": "counter", - "type": "u8" - }, - { - "name": "bump", - "type": "u8" - } - ] - } - } - ], - "errors": [ - { - "code": 6000, - "name": "ErrorExample", - "msg": "[S001] ErrorExample" - } - ] -}; - -export const IDL: Puppet = { - "version": "0.1.0", - "name": "puppet", - "instructions": [ - { - "name": "createCounter", - "accounts": [ - { - "name": "counter", - "isMut": true, - "isSigner": false - }, - { - "name": "admin", - "isMut": true, - "isSigner": true - }, - { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } - ], - "args": [ - { - "name": "stateBump", - "type": "u8" - } - ] - } - ], - "accounts": [ - { - "name": "counter", - "type": { - "kind": "struct", - "fields": [ - { - "name": "owner", - "type": "publicKey" - }, - { - "name": "counter", - "type": "u8" - }, - { - "name": "bump", - "type": "u8" - } - ] - } - } - ], - "errors": [ - { - "code": 6000, - "name": "ErrorExample", - "msg": "[S001] ErrorExample" - } - ] -}; diff --git a/protocol/sdk/src/network.ts b/protocol/sdk/src/network.ts index 34a36a8..c1a3a80 100644 --- a/protocol/sdk/src/network.ts +++ b/protocol/sdk/src/network.ts @@ -20,15 +20,3 @@ export const getProtocolProgramAddress = (network: Network): string => { } }; -export const getPuppetProgramAddress = (network: Network): string => { - switch (network) { - case Network.LOCAL: - return "8KQzCc22ZqGLPoipqRhYvkQtHJw6nY1NxrrGy8JLz1jC"; - case Network.DEV: - return "8KQzCc22ZqGLPoipqRhYvkQtHJw6nY1NxrrGy8JLz1jC"; - case Network.MAIN: - return "8KQzCc22ZqGLPoipqRhYvkQtHJw6nY1NxrrGy8JLz1jC"; - default: - throw new Error("Unknown network"); - } -}; diff --git a/protocol/sdk/src/protocol.ts b/protocol/sdk/src/protocol.ts index cdb59be..f3a447c 100644 --- a/protocol/sdk/src/protocol.ts +++ b/protocol/sdk/src/protocol.ts @@ -31,16 +31,8 @@ import { } from "@solana/spl-token"; import { IBurnLpToken, - IDeposit, IInitLpPool, - IInvokeClosePosition, - IInvokeCreatePosition, - IInvokeUpdateSecondsPerLiquidity, - IMint, IMintLpToken, - IReopenPosition, - ITest, - IWithdraw, LpPoolStructure, } from "./types"; import { @@ -184,210 +176,6 @@ export class Protocol { .instruction(); } - async test(params: ITest, signer: Keypair): Promise { - const ix = await this.testIx({ ...params }, signer); - return await this.sendTx([ix], [signer]); - } - - async testIx( - { stateBump, ...accounts }: ITest, - signer?: Keypair - ): Promise { - const payer = signer?.publicKey ?? this.wallet.publicKey; - return await this.program.methods - .test(stateBump) - .accounts({ - systemProgram: SystemProgram.programId, - payer, - ...accounts, - }) - .instruction(); - } - - async mint(params: IMint, signer: Keypair): Promise { - const ix = await this.mintIx(params); - return await this.sendTx([ix], [signer]); - } - - async mintIx({ amount, tokenMint, ...accounts }: IMint): Promise { - const tokenProgram = await getTokenProgramAddress( - this.connection, - tokenMint - ); - // return await this.program.methods - // .mint(amount) - // .accounts({ - // state: this.stateAddress, - // programAuthority: this.programAuthority, - // tokenMint, - // tokenProgram, - // ...accounts, - // }) - // .instruction(); - } - - async deposit( - params: IDeposit, - signer: Keypair - ): Promise { - const ix = await this.depositIx(params, signer); - return await this.sendTx([ix], [signer]); - } - - async depositIx( - { amount, ...accounts }: IDeposit, - signer?: Keypair - ): Promise { - const owner = signer?.publicKey ?? this.wallet.publicKey; - return await this.program.methods - .deposit(amount) - .accounts({ - state: this.stateAddress, - programAuthority: this.programAuthority, - owner, - tokenProgram: TOKEN_PROGRAM_ID, - ...accounts, - }) - .instruction(); - } - - async withdraw( - params: IWithdraw, - signer: Keypair - ): Promise { - const ix = await this.withdrawIx(params, signer); - return await this.sendTx([ix], [signer]); - } - - async withdrawIx( - { amount, ...accounts }: IWithdraw, - signer?: Keypair - ): Promise { - const owner = signer?.publicKey ?? this.wallet.publicKey; - return await this.program.methods - .withdraw(amount) - .accounts({ - state: this.stateAddress, - programAuthority: this.programAuthority, - tokenProgram: TOKEN_PROGRAM_ID, - owner, - ...accounts, - }) - .instruction(); - } - - async invokeUpdateSecondsPerLiquidity( - params: IInvokeUpdateSecondsPerLiquidity, - signer: Keypair - ): Promise { - const ix = await this.invokeUpdateSecondsPerLiquidityIx(params, signer); - return await this.sendTx([ix], [signer]); - } - - async invokeUpdateSecondsPerLiquidityIx( - { - lowerTickIndex, - upperTickIndex, - index, - ...accounts - }: IInvokeUpdateSecondsPerLiquidity, - signer?: Keypair - ): Promise { - const owner = signer?.publicKey ?? this.wallet.publicKey; - return await this.program.methods - .invokeUpdateSecondsPerLiquidity(lowerTickIndex, upperTickIndex, index) - .accounts({ - systemProgram: SystemProgram.programId, - owner, - ...accounts, - }) - .instruction(); - } - - async invokeCreatePosition( - params: IInvokeCreatePosition, - signer: Keypair - ): Promise { - const ix = await this.invokeCreatePositionIx(params, signer); - return await this.sendTx([ix], [signer]); - } - - async invokeCreatePositionIx( - { - lowerTickIndex, - upperTickIndex, - liquidityDelta, - slippageLimitLower, - slippageLimitUpper, - ...accounts - }: IInvokeCreatePosition, - signer?: Keypair - ): Promise { - const owner = signer?.publicKey ?? this.wallet.publicKey; - return await this.program.methods - .invokeCreatePosition( - lowerTickIndex, - upperTickIndex, - liquidityDelta, - slippageLimitLower, - slippageLimitUpper - ) - .accounts({ - systemProgram: SystemProgram.programId, - owner, - ...accounts, - }) - .instruction(); - } - - async invokeClosePosition( - params: IInvokeClosePosition, - signer: Keypair - ): Promise { - const ix = await this.invokeClosePositionIx(params, signer); - return await this.sendTx([ix], [signer]); - } - - async invokeClosePositionIx( - { - index, - lowerTickIndex, - upperTickIndex, - ...accounts - }: IInvokeClosePosition, - signer?: Keypair - ): Promise { - const owner = signer?.publicKey ?? this.wallet.publicKey; - return await this.program.methods - .invokeClosePosition(index, lowerTickIndex, upperTickIndex) - .accounts({ owner, ...accounts }) - .instruction(); - } - - async reopenPosition( - params: IReopenPosition, - signer: Keypair - ): Promise { - const setCuIx = computeUnitsInstruction(1_400_000); - const ix = await this.reopenPositionIx(params, signer); - return await this.sendTx([setCuIx, ix], [signer]); - } - - async reopenPositionIx( - { index, ...accounts }: IReopenPosition, - signer?: Keypair - ): Promise { - const owner = signer?.publicKey ?? this.wallet.publicKey; - return await this.program.methods - .reopenPosition(index) - .accounts({ - systemProgram: SystemProgram.programId, - owner, - ...accounts, - }) - .instruction(); - } - async initLpPool(params: IInitLpPool, signer: Keypair) { const ix = await this.initLpPoolIx(params, signer); return await this.sendTx([ix], [signer]); diff --git a/protocol/sdk/src/puppet.ts b/protocol/sdk/src/puppet.ts deleted file mode 100644 index 20aef9c..0000000 --- a/protocol/sdk/src/puppet.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { - Connection, - Keypair, - PublicKey, - Transaction, - TransactionInstruction, -} from "@solana/web3.js"; -import { getPuppetProgramAddress, Network } from "./network"; -import { IWallet } from "./wallet"; -import { Program, utils } from "@coral-xyz/anchor"; -import { IDL, Puppet as PuppetProgram } from "./idl/puppet"; -import { PUPPET_COUNTER_SEED } from "./consts"; -import { signAndSend } from "./utils"; -import { ITransaction } from "./types"; - -export class Puppet { - public connection: Connection; - public wallet: IWallet; - public network: Network; - public program: Program; - public counterAddressAndBump: [PublicKey, number]; - - private constructor( - network: Network, - wallet: IWallet, - connection: Connection - ) { - this.connection = connection; - this.wallet = wallet; - this.network = network; - const programAddress = getPuppetProgramAddress(network); - this.program = new Program(IDL, programAddress); - this.counterAddressAndBump = PublicKey.findProgramAddressSync( - [Buffer.from(utils.bytes.utf8.encode(PUPPET_COUNTER_SEED))], - this.program.programId - ); - } - - getCounterAddressAndBump() { - return this.counterAddressAndBump; - } - - getCounterAddress() { - return this.counterAddressAndBump[0]; - } - - public static async build( - network: Network, - wallet: IWallet, - connection: Connection - ): Promise { - const instance = new Puppet(network, wallet, connection); - - return instance; - } -} diff --git a/protocol/sdk/src/types.ts b/protocol/sdk/src/types.ts index 9f75a08..251a25b 100644 --- a/protocol/sdk/src/types.ts +++ b/protocol/sdk/src/types.ts @@ -19,118 +19,6 @@ export interface ITransaction { signers?: Keypair[]; } -export interface ITest { - puppetProgram: PublicKey; - counter: PublicKey; - stateBump: number; -} - -export interface IMint { - amount: BN; - tokenMint: PublicKey; - to: PublicKey; -} - -export interface IDeposit { - tokenMint: PublicKey; - reserve: PublicKey; - userBalance: PublicKey; - amount: BN; -} - -export interface IWithdraw { - tokenMint: PublicKey; - reserve: PublicKey; - userBalance: PublicKey; - amount: BN; -} - -export interface IInvokeUpdateSecondsPerLiquidity { - lowerTickIndex: number; - upperTickIndex: number; - index: number; - invariantProgram: PublicKey; - pool: PublicKey; - lowerTick: PublicKey; - upperTick: PublicKey; - position: PublicKey; - tokenX: PublicKey; - tokenY: PublicKey; -} - -export interface IInvokeCreatePosition { - lowerTickIndex: number; - upperTickIndex: number; - liquidityDelta: BN; - slippageLimitLower: BN; - slippageLimitUpper: BN; - invariantProgram: PublicKey; - state: PublicKey; - position: PublicKey; - pool: PublicKey; - positionList: PublicKey; - lowerTick: PublicKey; - upperTick: PublicKey; - tickmap: PublicKey; - tokenX: PublicKey; - tokenY: PublicKey; - accountX: PublicKey; - accountY: PublicKey; - reserveX: PublicKey; - reserveY: PublicKey; - programAuthority: PublicKey; - tokenXProgram: PublicKey; - tokenYProgram: PublicKey; -} - -export interface IInvokeClosePosition { - index: number; - lowerTickIndex: number; - upperTickIndex: number; - invariantState: PublicKey; - invariantProgramAuthority: PublicKey; - removedPosition: PublicKey; - positionList: PublicKey; - lastPosition: PublicKey; - pool: PublicKey; - tickmap: PublicKey; - lowerTick: PublicKey; - upperTick: PublicKey; - tokenX: PublicKey; - tokenY: PublicKey; - accountX: PublicKey; - accountY: PublicKey; - reserveX: PublicKey; - reserveY: PublicKey; - tokenXProgram: PublicKey; - tokenYProgram: PublicKey; - invariantProgram: PublicKey; -} - -export interface IReopenPosition { - // params - index: number; - // accounts - invariantProgram: PublicKey; - invariantState: PublicKey; - position: PublicKey; - lastPosition: PublicKey; - pool: PublicKey; - positionList: PublicKey; - lowerTick: PublicKey; - upperTick: PublicKey; - tickmap: PublicKey; - tokenX: PublicKey; - tokenY: PublicKey; - accountX: PublicKey; - accountY: PublicKey; - reserveX: PublicKey; - reserveY: PublicKey; - invariantProgramAuthority: PublicKey; - tokenXProgram: PublicKey; - tokenYProgram: PublicKey; -} - export interface IInitLpPool { pair: Pair; pool?: PublicKey; diff --git a/protocol/sdk/src/utils.ts b/protocol/sdk/src/utils.ts index 592dc6b..5a188c9 100644 --- a/protocol/sdk/src/utils.ts +++ b/protocol/sdk/src/utils.ts @@ -1,16 +1,14 @@ -import { AnchorProvider, BN, utils } from "@coral-xyz/anchor"; +import { AnchorProvider, BN } from "@coral-xyz/anchor"; import { BlockheightBasedTransactionConfirmationStrategy, ComputeBudgetProgram, ConfirmOptions, Connection, Keypair, - PublicKey, Transaction, TransactionInstruction, TransactionSignature, } from "@solana/web3.js"; -import { PUPPET_COUNTER_SEED } from "./consts"; export const signAndSend = async ( tx: Transaction, @@ -45,19 +43,6 @@ export const computeUnitsInstruction = ( return ComputeBudgetProgram.setComputeUnitLimit({ units }); }; -export const getPuppetCounterAddressAndBump = ( - programId: PublicKey -): [PublicKey, number] => { - return PublicKey.findProgramAddressSync( - [Buffer.from(utils.bytes.utf8.encode(PUPPET_COUNTER_SEED))], - programId - ); -}; - -export const getPuppetCounterAddress = (programId: PublicKey): PublicKey => { - return getPuppetCounterAddressAndBump(programId)[0]; -}; - export const bigNumberToBuffer = (n: BN, size: 16 | 32 | 64 | 128 | 256) => { const chunk = new BN(2).pow(new BN(16)); diff --git a/protocol/tests.sh b/protocol/tests.sh index 2fa2c38..54d14a3 100755 --- a/protocol/tests.sh +++ b/protocol/tests.sh @@ -3,8 +3,6 @@ set -e e2e_tests=( "init" - # "token" - "invoke" "init-lp-pool" "mint" "burn" diff --git a/protocol/tests/init.test.ts b/protocol/tests/init.test.ts index 4c49fc5..1d05d82 100644 --- a/protocol/tests/init.test.ts +++ b/protocol/tests/init.test.ts @@ -3,9 +3,6 @@ import { Network } from "../sdk/src/network"; import { Protocol } from "../sdk/src/protocol"; import { Keypair } from "@solana/web3.js"; import { requestAirdrop } from "./test-utils"; -import { Puppet } from "../sdk/dist/puppet"; -import { getPuppetCounterAddressAndBump } from "../sdk/src/utils"; -import { assert } from "chai"; describe("init", () => { const { wallet: walletAnchor, connection } = AnchorProvider.local(); @@ -28,33 +25,4 @@ describe("init", () => { await protocol.init(owner); }); - - it("cpi works", async () => { - const protocol = await Protocol.build( - Network.LOCAL, - walletAnchor, - connection - ); - const puppet = await Puppet.build(Network.LOCAL, walletAnchor, connection); - - const [puppetCounterAddress, bump] = getPuppetCounterAddressAndBump( - puppet.program.programId - ); - - await protocol.test( - { - puppetProgram: puppet.program.programId, - counter: puppetCounterAddress, - stateBump: bump, - }, - owner - ); - - const stateAccount = await puppet.program.account.counter.fetch( - puppetCounterAddress - ); - assert.equal(stateAccount.owner?.toString(), owner.publicKey.toString()); - assert.equal(stateAccount.counter, 0); - assert.equal(stateAccount.bump, bump); - }); }); diff --git a/protocol/tests/invoke.test.ts b/protocol/tests/invoke.test.ts deleted file mode 100644 index c6a6f27..0000000 --- a/protocol/tests/invoke.test.ts +++ /dev/null @@ -1,401 +0,0 @@ -import { AnchorProvider, BN } from "@coral-xyz/anchor"; -import { Network } from "../sdk/src/network"; -import { Protocol } from "../sdk/src/protocol"; -import { Keypair } from "@solana/web3.js"; -import { - createTokenMint, - initMarket, - INVARIANT_ADDRESS, - requestAirdrop, - sleep, -} from "./test-utils"; -import { assert } from "chai"; -import { getOrCreateAssociatedTokenAccount, mintTo } from "@solana/spl-token"; -import { calculatePriceSqrt, Pair } from "@invariant-labs/sdk-eclipse"; -import { - fromFee, - getTokenProgramAddress, - LIQUIDITY_DENOMINATOR, -} from "@invariant-labs/sdk-eclipse/lib/utils"; -import { - CreateTick, - FeeTier, - InitPosition, - Market, -} from "@invariant-labs/sdk-eclipse/lib/market"; - -describe("invariant cpi", () => { - const { wallet: walletAnchor, connection } = AnchorProvider.local(); - const owner = Keypair.generate(); - const wallet = Keypair.generate(); - const mintAuthority = Keypair.generate(); - - let protocol: Protocol; - let market: Market; - const feeTier: FeeTier = { - fee: fromFee(new BN(600)), - tickSpacing: 10, - }; - let pair: Pair; - const lowerTick = -10; - const upperTick = 10; - const initTick = 0; - - before(async () => { - let giveSOL = [owner.publicKey, mintAuthority.publicKey, wallet.publicKey]; - await Promise.all( - giveSOL.map((account) => requestAirdrop(connection, account, 1e14)) - ); - - protocol = await Protocol.build(Network.LOCAL, walletAnchor, connection); - await protocol.init(owner); - - market = await Market.build( - Network.LOCAL, - walletAnchor, - connection, - INVARIANT_ADDRESS - ); - - const [token0, token1] = await Promise.all([ - createTokenMint(connection, owner, mintAuthority.publicKey, 6), - createTokenMint(connection, owner, mintAuthority.publicKey, 6), - ]); - pair = new Pair(token0, token1, feeTier); - - await initMarket(market, [pair], owner, initTick); - await market.createPositionList(owner.publicKey, owner); - - const lowerTickVars: CreateTick = { - pair, - index: lowerTick, - payer: owner.publicKey, - }; - const upperTickVars: CreateTick = { - pair, - index: upperTick, - payer: owner.publicKey, - }; - await market.createTick(lowerTickVars, owner); - await market.createTick(upperTickVars, owner); - - const xOwnerAmount = 1e10; - const yOwnerAmount = 1e10; - - const userTokenXAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenX, - owner.publicKey - ); - const userTokenYAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenY, - owner.publicKey - ); - - await mintTo( - connection, - owner, - pair.tokenX, - userTokenXAccount.address, - mintAuthority, - xOwnerAmount - ); - await mintTo( - connection, - owner, - pair.tokenY, - userTokenYAccount.address, - mintAuthority, - yOwnerAmount - ); - }); - - it("update seconds per liquidity", async () => { - const userTokenXAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenX, - owner.publicKey - ); - const userTokenYAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenY, - owner.publicKey - ); - - const liquidityDelta = { v: LIQUIDITY_DENOMINATOR.muln(10_000) }; - const position: InitPosition = { - pair, - owner: owner.publicKey, - userTokenX: userTokenXAccount.address, - userTokenY: userTokenYAccount.address, - lowerTick, - upperTick, - liquidityDelta, - knownPrice: calculatePriceSqrt(initTick), - slippage: { v: new BN(0) }, - }; - await market.initPosition(position, owner); - - const poolAddress = await pair.getAddress(INVARIANT_ADDRESS); - const { positionAddress } = await market.getPositionAddress( - owner.publicKey, - 0 - ); - const { tickAddress: lowerTickAddress } = await market.getTickAddress( - pair, - -10 - ); - const { tickAddress: upperTickAddress } = await market.getTickAddress( - pair, - 10 - ); - - const positionBefore = await market.getPosition(owner.publicKey, 0); - - await sleep(1000); - - await protocol.invokeUpdateSecondsPerLiquidity( - { - lowerTickIndex: lowerTick, - upperTickIndex: upperTick, - index: initTick, - invariantProgram: INVARIANT_ADDRESS, - pool: poolAddress, - lowerTick: lowerTickAddress, - upperTick: upperTickAddress, - position: positionAddress, - tokenX: pair.tokenX, - tokenY: pair.tokenY, - }, - owner - ); - - const positionAfter = await market.getPosition(owner.publicKey, 0); - assert.ok( - positionAfter.secondsPerLiquidityInside.v.gt( - positionBefore.secondsPerLiquidityInside.v - ) - ); - }); - - it("create position", async () => { - const userTokenXAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenX, - owner.publicKey - ); - const userTokenYAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenY, - owner.publicKey - ); - - const positionId = 1; - - const { address: stateAddress } = await market.getStateAddress(); - const poolAddress = await pair.getAddress(INVARIANT_ADDRESS); - const { positionListAddress: positionListAddress } = - await market.getPositionListAddress(owner.publicKey); - const { positionAddress } = await market.getPositionAddress( - owner.publicKey, - positionId - ); - const { tickAddress: lowerTickAddress } = await market.getTickAddress( - pair, - lowerTick - ); - const { tickAddress: upperTickAddress } = await market.getTickAddress( - pair, - upperTick - ); - const { tokenXReserve, tokenYReserve, tickmap } = await market.getPool( - pair - ); - const tokenXProgram = await getTokenProgramAddress(connection, pair.tokenX); - const tokenYProgram = await getTokenProgramAddress(connection, pair.tokenY); - - const liquidityDelta = LIQUIDITY_DENOMINATOR.muln(10_000); - const slippageLimitLower = new BN(0); - const slippageLimitUpper = new BN(2n ** 128n - 1n); - - await protocol.invokeCreatePosition( - { - lowerTickIndex: lowerTick, - upperTickIndex: upperTick, - liquidityDelta, - slippageLimitLower, - slippageLimitUpper, - invariantProgram: INVARIANT_ADDRESS, - state: stateAddress, - position: positionAddress, - pool: poolAddress, - positionList: positionListAddress, - lowerTick: lowerTickAddress, - upperTick: upperTickAddress, - tickmap, - tokenX: pair.tokenX, - tokenY: pair.tokenY, - accountX: userTokenXAccount.address, - accountY: userTokenYAccount.address, - reserveX: tokenXReserve, - reserveY: tokenYReserve, - programAuthority: market.programAuthority, - tokenXProgram, - tokenYProgram, - }, - owner - ); - - const position = await market.getPosition(owner.publicKey, positionId); - assert.ok(position); - }); - - it("close and re-open position in one Ix", async () => { - const userTokenXAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenX, - owner.publicKey - ); - const userTokenYAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenY, - owner.publicKey - ); - - const positionId = 0; - const lastPositionId = 1; - - const { address: stateAddress } = await market.getStateAddress(); - const poolAddress = await pair.getAddress(INVARIANT_ADDRESS); - const { positionListAddress: positionListAddress } = - await market.getPositionListAddress(owner.publicKey); - const { positionAddress } = await market.getPositionAddress( - owner.publicKey, - positionId - ); - const { positionAddress: lastPositionAddress } = - await market.getPositionAddress(owner.publicKey, lastPositionId); - const { tickAddress: lowerTickAddress } = await market.getTickAddress( - pair, - lowerTick - ); - const { tickAddress: upperTickAddress } = await market.getTickAddress( - pair, - upperTick - ); - const { tokenXReserve, tokenYReserve, tickmap } = await market.getPool( - pair - ); - const tokenXProgram = await getTokenProgramAddress(connection, pair.tokenX); - const tokenYProgram = await getTokenProgramAddress(connection, pair.tokenY); - - await protocol.reopenPosition( - { - index: positionId, - invariantProgram: INVARIANT_ADDRESS, - invariantState: stateAddress, - position: positionAddress, - lastPosition: lastPositionAddress, - pool: poolAddress, - positionList: positionListAddress, - lowerTick: lowerTickAddress, - upperTick: upperTickAddress, - tickmap, - tokenX: pair.tokenX, - tokenY: pair.tokenY, - accountX: userTokenXAccount.address, - accountY: userTokenYAccount.address, - reserveX: tokenXReserve, - reserveY: tokenYReserve, - invariantProgramAuthority: market.programAuthority, - tokenXProgram, - tokenYProgram, - }, - owner - ); - - const position = await market.getPosition(owner.publicKey, positionId); - assert.ok(position); - }); - - it("close position", async () => { - const userTokenXAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenX, - owner.publicKey - ); - const userTokenYAccount = await getOrCreateAssociatedTokenAccount( - connection, - owner, - pair.tokenY, - owner.publicKey - ); - - const positionId = 0; - const lastPositionId = 1; - - const { address: stateAddress } = await market.getStateAddress(); - const poolAddress = await pair.getAddress(INVARIANT_ADDRESS); - const { positionListAddress: positionListAddress } = - await market.getPositionListAddress(owner.publicKey); - const { positionAddress } = await market.getPositionAddress( - owner.publicKey, - positionId - ); - const { positionAddress: lastPositionAddress } = - await market.getPositionAddress(owner.publicKey, lastPositionId); - const { tickAddress: lowerTickAddress } = await market.getTickAddress( - pair, - lowerTick - ); - const { tickAddress: upperTickAddress } = await market.getTickAddress( - pair, - upperTick - ); - const { tokenXReserve, tokenYReserve, tickmap } = await market.getPool( - pair - ); - const tokenXProgram = await getTokenProgramAddress(connection, pair.tokenX); - const tokenYProgram = await getTokenProgramAddress(connection, pair.tokenY); - - await protocol.invokeClosePosition( - { - index: positionId, - lowerTickIndex: lowerTick, - upperTickIndex: upperTick, - invariantProgram: INVARIANT_ADDRESS, - invariantState: stateAddress, - removedPosition: positionAddress, - lastPosition: lastPositionAddress, - pool: poolAddress, - positionList: positionListAddress, - lowerTick: lowerTickAddress, - upperTick: upperTickAddress, - tickmap, - tokenX: pair.tokenX, - tokenY: pair.tokenY, - accountX: userTokenXAccount.address, - accountY: userTokenYAccount.address, - reserveX: tokenXReserve, - reserveY: tokenYReserve, - invariantProgramAuthority: market.programAuthority, - tokenXProgram, - tokenYProgram, - }, - owner - ); - - const position = await market.getPosition(owner.publicKey, positionId); - assert.ok(position); - }); -}); diff --git a/protocol/tests/token.test.ts b/protocol/tests/token.test.ts deleted file mode 100644 index 7b776fd..0000000 --- a/protocol/tests/token.test.ts +++ /dev/null @@ -1,182 +0,0 @@ -import { AnchorProvider, BN } from "@coral-xyz/anchor"; -import { Network } from "../sdk/src/network"; -import { Protocol } from "../sdk/src/protocol"; -import { Keypair, PublicKey } from "@solana/web3.js"; -import { createTokenMint, requestAirdrop } from "./test-utils"; -import { assert } from "chai"; -import { - getAccount, - getOrCreateAssociatedTokenAccount, -} from "@solana/spl-token"; - -describe("token", () => { - const { wallet, connection } = AnchorProvider.local(); - const owner: Keypair = Keypair.generate(); - const payer = Keypair.generate(); - - let protocol: Protocol; - let lpTokenMinter: PublicKey; - - const tokenAmount = 100n; - const tokenDecimals = 6; - const mintAmount = new BN(tokenAmount * 10n ** BigInt(tokenDecimals)); - const depositAmount = mintAmount.div(new BN(4)); - const withdrawAmount = depositAmount.div(new BN(5)); - - before(async () => { - await Promise.all([ - requestAirdrop(connection, owner.publicKey, 1e14), - requestAirdrop(connection, payer.publicKey, 1e9), - ]); - - protocol = await Protocol.build(Network.LOCAL, wallet, connection); - await protocol.init(owner); - - const mintAuthority = protocol.programAuthority; - - lpTokenMinter = await createTokenMint( - connection, - payer, - mintAuthority, - tokenDecimals - ); - }); - - it("mint", async () => { - const lpTokenAccount = await getOrCreateAssociatedTokenAccount( - connection, - payer, - lpTokenMinter, - payer.publicKey - ); - - { - const lpTokenAccountInfo = await getAccount( - connection, - lpTokenAccount.address - ); - assert.equal(lpTokenAccountInfo.amount, 0n); - } - - await protocol.mint( - { - tokenMint: lpTokenMinter, - to: lpTokenAccount.address, - amount: mintAmount, - }, - payer - ); - - { - const lpTokenAccountInfo = await getAccount( - connection, - lpTokenAccount.address - ); - assert.equal(lpTokenAccountInfo.amount, mintAmount); - } - }); - it("deposit", async () => { - const programAuthority = protocol.programAuthority; - - const lpTokenAccount = await getOrCreateAssociatedTokenAccount( - connection, - payer, - lpTokenMinter, - payer.publicKey - ); - - { - const lpTokenAccountInfo = await getAccount( - connection, - lpTokenAccount.address - ); - assert.equal(lpTokenAccountInfo.amount, mintAmount); - } - - const lpTokenReserve = await getOrCreateAssociatedTokenAccount( - connection, - payer, - lpTokenMinter, - programAuthority, - true - ); - - await protocol.deposit( - { - tokenMint: lpTokenMinter, - reserve: lpTokenReserve.address, - userBalance: lpTokenAccount.address, - amount: depositAmount, - }, - payer - ); - { - const lpTokenAccountInfo = await getAccount( - connection, - lpTokenAccount.address - ); - const lpTokenReserveInfo = await getAccount( - connection, - lpTokenReserve.address - ); - - assert.equal(lpTokenAccountInfo.amount, mintAmount.sub(depositAmount)); - assert.equal(lpTokenReserveInfo.amount, depositAmount); - } - }); - it("withdraw", async () => { - const programAuthority = protocol.programAuthority; - - const lpTokenAccount = await getOrCreateAssociatedTokenAccount( - connection, - payer, - lpTokenMinter, - payer.publicKey - ); - - { - const lpTokenAccountInfo = await getAccount( - connection, - lpTokenAccount.address - ); - assert.equal(lpTokenAccountInfo.amount, mintAmount.sub(depositAmount)); - } - - const lpTokenReserve = await getOrCreateAssociatedTokenAccount( - connection, - payer, - lpTokenMinter, - programAuthority, - true - ); - - await protocol.withdraw( - { - tokenMint: lpTokenMinter, - reserve: lpTokenReserve.address, - userBalance: lpTokenAccount.address, - amount: withdrawAmount, - }, - payer - ); - { - const lpTokenAccountInfo = await getAccount( - connection, - lpTokenAccount.address - ); - const lpTokenReserveInfo = await getAccount( - connection, - lpTokenReserve.address - ); - - assert.equal( - lpTokenAccountInfo.amount, - mintAmount.sub(depositAmount).add(withdrawAmount) - ); - assert.equal( - lpTokenReserveInfo.amount, - depositAmount.sub(withdrawAmount) - ); - } - }); -});