-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rust-sdk position test #601
Merged
+247
−11
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
542ad5f
Add position test
pauldragonfly 6c64a13
Merge branch 'main' of github.com:orca-so/whirlpools into paul/rust-s…
pauldragonfly 7a8934d
Address comments
pauldragonfly a88ca69
Merge branch 'main' of github.com:orca-so/whirlpools into paul/rust-s…
pauldragonfly 3c095da
Merge branch 'main' into paul/rust-sdk-add-position-test
wjthieme 77947f6
Update test to check te and bundle
pauldragonfly 0cc91b2
Merge branch 'paul/rust-sdk-add-position-test' of github.com:orca-so/…
pauldragonfly 1440df3
Update dependabot
pauldragonfly 24abe07
Fix dependabot space
pauldragonfly a847a80
revert dependabot
pauldragonfly 57f9e6e
FMT
pauldragonfly cdefdca
Merge branch 'main' into paul/rust-sdk-add-position-test
pauldragonfly f329cdd
Update not to create mint, instead use instructions
pauldragonfly 880f397
Merge branch 'main' into paul/rust-sdk-add-position-test
pauldragonfly 7861e2e
Update to use actual address in positions
pauldragonfly 0e9be37
Merge branch 'main' into paul/rust-sdk-add-position-test
pauldragonfly 1d7f86a
Update position bundle
pauldragonfly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
use solana_sdk::{pubkey::Pubkey, signer::Signer, system_program}; | ||
use std::error::Error; | ||
|
||
use orca_whirlpools_client::{ | ||
get_fee_tier_address, get_token_badge_address, get_whirlpool_address, InitializePoolV2, | ||
InitializePoolV2InstructionArgs, | ||
get_bundled_position_address, get_fee_tier_address, get_position_address, | ||
get_position_bundle_address, get_token_badge_address, get_whirlpool_address, InitializePoolV2, | ||
InitializePoolV2InstructionArgs, InitializePositionBundle, OpenBundledPosition, | ||
OpenBundledPositionInstructionArgs, OpenPosition, OpenPositionInstructionArgs, | ||
}; | ||
use orca_whirlpools_core::tick_index_to_sqrt_price; | ||
use solana_program::program_pack::Pack; | ||
use solana_program::sysvar::rent::ID as RENT_PROGRAM_ID; | ||
use solana_sdk::{ | ||
pubkey::Pubkey, | ||
signer::{keypair::Keypair, Signer}, | ||
system_instruction, system_program, | ||
}; | ||
use spl_associated_token_account::{ | ||
get_associated_token_address, get_associated_token_address_with_program_id, | ||
instruction::create_associated_token_account, | ||
}; | ||
use spl_token::{state::Mint, ID as TOKEN_PROGRAM_ID}; | ||
use spl_token_2022::{state::Mint as Token2022Mint, ID as TOKEN_2022_PROGRAM_ID}; | ||
use std::error::Error; | ||
|
||
use crate::WHIRLPOOLS_CONFIG_ADDRESS; | ||
|
||
use super::rpc::RpcContext; | ||
|
||
use crate::tests::token::{setup_ata, setup_mint_with_decimals}; | ||
use crate::tests::token_extensions::setup_mint_te; | ||
|
||
pub async fn setup_whirlpool( | ||
ctx: &RpcContext, | ||
token_a: Pubkey, | ||
|
@@ -60,14 +75,124 @@ pub async fn setup_whirlpool( | |
Ok(whirlpool) | ||
} | ||
|
||
pub async fn setup_position(_whirlpool: Pubkey) -> Result<Pubkey, Box<dyn Error>> { | ||
todo!() | ||
pub async fn setup_position(whirlpool: Pubkey) -> Result<Pubkey, Box<dyn Error>> { | ||
let ctx = RpcContext::new().await; | ||
|
||
let position_mint = ctx.get_next_keypair(); | ||
|
||
let (position_pubkey, position_bump) = get_position_address(&position_mint.pubkey())?; | ||
|
||
let open_position_ix = OpenPosition { | ||
funder: ctx.signer.pubkey(), | ||
owner: ctx.signer.pubkey(), | ||
position: position_pubkey, | ||
position_mint: position_mint.pubkey(), | ||
position_token_account: Pubkey::default(), // instruction will create | ||
whirlpool, | ||
token_program: TOKEN_PROGRAM_ID, // or TOKEN_2022_PROGRAM_ID if needed | ||
system_program: system_program::id(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TOKEN_2022_PROGRAM_ID is only for TE positions so irrelevant here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
associated_token_program: spl_associated_token_account::id(), | ||
rent: RENT_PROGRAM_ID, | ||
} | ||
.instruction(OpenPositionInstructionArgs { | ||
tick_lower_index: -128, | ||
tick_upper_index: 128, | ||
position_bump, | ||
}); | ||
|
||
ctx.send_transaction_with_signers(vec![open_position_ix], vec![&position_mint]) | ||
.await?; | ||
|
||
Ok(position_pubkey) | ||
} | ||
|
||
pub async fn setup_te_position(_whirlpool: Pubkey) -> Result<Pubkey, Box<dyn Error>> { | ||
todo!() | ||
pub async fn setup_te_position(whirlpool: Pubkey) -> Result<Pubkey, Box<dyn Error>> { | ||
let ctx = RpcContext::new().await; | ||
|
||
// 1) Keypair for the TE position mint | ||
let te_position_mint = ctx.get_next_keypair(); | ||
|
||
// 2) Derive the position PDA | ||
let (position_pubkey, position_bump) = get_position_address(&te_position_mint.pubkey())?; | ||
|
||
// 3) Build an OpenPosition instruction with token_program = TOKEN_2022_PROGRAM_ID | ||
let open_position_ix = OpenPosition { | ||
funder: ctx.signer.pubkey(), | ||
owner: ctx.signer.pubkey(), | ||
position: position_pubkey, | ||
position_mint: te_position_mint.pubkey(), | ||
position_token_account: Pubkey::default(), | ||
whirlpool, | ||
token_program: TOKEN_2022_PROGRAM_ID, // TE uses token-2022 | ||
system_program: system_program::id(), | ||
associated_token_program: spl_associated_token_account::id(), | ||
rent: RENT_PROGRAM_ID, | ||
} | ||
.instruction(OpenPositionInstructionArgs { | ||
tick_lower_index: -128, | ||
tick_upper_index: 128, | ||
position_bump, | ||
}); | ||
|
||
// 4) The instruction itself will create the mint & ATA | ||
ctx.send_transaction_with_signers(vec![open_position_ix], vec![&te_position_mint]) | ||
.await?; | ||
|
||
// 5) Return the position PDA | ||
Ok(position_pubkey) | ||
} | ||
|
||
pub async fn setup_position_bundle(_whirlpool: Pubkey) -> Result<Pubkey, Box<dyn Error>> { | ||
todo!() | ||
pub async fn setup_position_bundle( | ||
whirlpool: Pubkey, | ||
bundle_positions: Option<Vec<()>>, | ||
) -> Result<Pubkey, Box<dyn Error>> { | ||
let ctx = RpcContext::new().await; | ||
|
||
let position_bundle_mint = ctx.get_next_keypair(); | ||
let (position_bundle_address, _bundle_bump) = | ||
get_position_bundle_address(&position_bundle_mint.pubkey())?; | ||
|
||
let open_bundle_ix = InitializePositionBundle { | ||
funder: ctx.signer.pubkey(), | ||
position_bundle: position_bundle_address, | ||
position_bundle_mint: position_bundle_mint.pubkey(), | ||
position_bundle_token_account: Pubkey::default(), | ||
position_bundle_owner: ctx.signer.pubkey(), | ||
token_program: TOKEN_PROGRAM_ID, | ||
system_program: system_program::id(), | ||
associated_token_program: spl_associated_token_account::id(), | ||
rent: RENT_PROGRAM_ID, | ||
} | ||
.instruction(); | ||
|
||
ctx.send_transaction_with_signers(vec![open_bundle_ix], vec![&position_bundle_mint]) | ||
.await?; | ||
|
||
if let Some(positions) = bundle_positions { | ||
for (i, _) in positions.iter().enumerate() { | ||
let bundle_index = i as u16; | ||
let (bundled_position_address, _) = | ||
get_bundled_position_address(&position_bundle_mint.pubkey(), bundle_index as u8)?; | ||
|
||
let open_bundled_ix = OpenBundledPosition { | ||
funder: ctx.signer.pubkey(), | ||
bundled_position: bundled_position_address, | ||
position_bundle: position_bundle_address, | ||
position_bundle_authority: ctx.signer.pubkey(), | ||
position_bundle_token_account: Pubkey::default(), | ||
whirlpool, | ||
system_program: system_program::id(), | ||
rent: RENT_PROGRAM_ID, | ||
} | ||
.instruction(OpenBundledPositionInstructionArgs { | ||
tick_lower_index: -128, | ||
tick_upper_index: 128, | ||
bundle_index, | ||
}); | ||
|
||
ctx.send_transaction(vec![open_bundled_ix]).await?; | ||
} | ||
} | ||
|
||
Ok(position_bundle_address) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The instruction will initiate the account but you still need to provide the correct address for position_token_account
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do this using the https://docs.rs/spl-associated-token-account/latest/spl_associated_token_account/fn.get_associated_token_address_with_program_id.html method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!