-
Notifications
You must be signed in to change notification settings - Fork 8
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
update: fix issues in native sol transfer #407
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
rm -fr ./test-ledger | ||
|
||
solana-test-validator > /dev/null 2>&1 & | ||
VALIDATOR_PID=$! | ||
|
||
echo "Solana test validator started in the background with PID $VALIDATOR_PID." | ||
|
||
sleep 5 | ||
|
||
echo "Running Debug and Deployment" | ||
|
||
# Default behavior | ||
SKIP_DEPLOY=false | ||
|
||
# Parse command-line arguments | ||
while [[ "$#" -gt 0 ]]; do | ||
case $1 in | ||
--skip-deploy) SKIP_DEPLOY=true ;; | ||
*) echo "Unknown parameter passed: $1"; exit 1 ;; | ||
esac | ||
shift | ||
done | ||
|
||
|
||
solana config set --url http://127.0.0.1:8899 | ||
|
||
if [ "$SKIP_DEPLOY" = false ]; then | ||
anchor build -p bridge_escrow | ||
solana program deploy target/deploy/bridge_escrow.so --program-id target/deploy/bridge_escrow-keypair.json | ||
else | ||
echo "Skipping Deployment" | ||
fi | ||
|
||
cargo test -p bridge-escrow -- --nocapture | ||
|
||
echo "Stopping solana-test-validator..." | ||
kill $VALIDATOR_PID | ||
|
||
wait $VALIDATOR_PID | ||
echo "Solana test validator has been stopped." |
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 |
---|---|---|
|
@@ -23,7 +23,7 @@ pub mod events; | |
#[cfg(test)] | ||
mod tests; | ||
|
||
declare_id!("AhfoGVmS19tvkEG2hBuZJ1D6qYEjyFmXZ1qPoFD6H4Mj"); | ||
declare_id!("H3jfQXtiC3DW4bwMafKiiW2xhYjwZDkV21SMaYqEtMM8"); | ||
|
||
#[program] | ||
pub mod bridge_escrow { | ||
|
@@ -63,32 +63,57 @@ pub mod bridge_escrow { | |
); | ||
|
||
require!( | ||
new_intent.token_in == ctx.accounts.user_token_account.mint, | ||
new_intent.token_in == System::id() || new_intent.token_in == ctx.accounts.user_token_account.clone().unwrap().mint, | ||
ErrorCode::TokenInNotMint | ||
); | ||
|
||
require!( | ||
new_intent.user_in == ctx.accounts.user_token_account.owner, | ||
new_intent.user_in == ctx.accounts.user_token_account.clone().unwrap().owner, | ||
ErrorCode::SrcUserNotUserIn | ||
); | ||
|
||
// Step 2: Escrow the funds (same as before) | ||
// Handle Native SOL Transfer | ||
if new_intent.token_in == System::id() { | ||
// Perform SOL transfer from Solver to User | ||
let src_user = ctx.accounts.user.clone(); | ||
|
||
let ix = solana_program::system_instruction::transfer( | ||
src_user.key, | ||
&ctx.accounts.auctioneer_state.key(), | ||
new_intent.amount_in, | ||
); | ||
|
||
let cpi_accounts = SplTransfer { | ||
from: ctx.accounts.user_token_account.to_account_info(), | ||
to: ctx.accounts.escrow_token_account.to_account_info(), | ||
authority: ctx.accounts.user.to_account_info(), | ||
}; | ||
|
||
let cpi_program = ctx.accounts.token_program.to_account_info(); | ||
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); | ||
|
||
token::transfer(cpi_ctx, new_intent.amount_in)?; | ||
invoke( | ||
&ix, | ||
&[ | ||
src_user.to_account_info(), | ||
ctx.accounts.auctioneer_state.to_account_info(), | ||
ctx.accounts.system_program.to_account_info(), | ||
], | ||
)?; | ||
|
||
// **src_user.lamports.borrow_mut() -= new_intent.amount_in; | ||
// **ctx.accounts.auctioneer_state.to_account_info().lamports.borrow_mut() += new_intent.amount_in; | ||
|
||
} else { | ||
|
||
let cpi_accounts = SplTransfer { | ||
from: ctx.accounts.user_token_account.clone().unwrap().to_account_info(), | ||
to: ctx.accounts.escrow_token_account.clone().unwrap().to_account_info(), | ||
authority: ctx.accounts.user.to_account_info(), | ||
}; | ||
|
||
let cpi_program = ctx.accounts.token_program.to_account_info(); | ||
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); | ||
|
||
token::transfer(cpi_ctx, new_intent.amount_in)?; | ||
} | ||
|
||
events::emit(events::Event::EscrowFunds(events::EscrowFunds { | ||
amount: new_intent.amount_in, | ||
sender: ctx.accounts.user.key(), | ||
token_mint: ctx.accounts.token_mint.key(), | ||
token_mint: ctx.accounts.token_mint.clone().unwrap().key(), | ||
})).map_err(|err| { | ||
msg!("{}", err); | ||
ErrorCode::InvalidEventFormat | ||
|
@@ -318,20 +343,11 @@ pub mod bridge_escrow { | |
// Handle Native SOL Transfer | ||
if intent.token_out == System::id().to_string() { | ||
// Perform SOL transfer from Solver to User | ||
let ix = solana_program::system_instruction::transfer( | ||
solver.key, | ||
&intent.user_in, | ||
amount_out, | ||
); | ||
|
||
invoke( | ||
&ix, | ||
&[ | ||
solver.to_account_info(), | ||
accounts.user_token_out_account.to_account_info(), | ||
accounts.system_program.to_account_info(), | ||
], | ||
)?; | ||
let out_user_account = accounts.user_account.clone().unwrap(); | ||
|
||
**solver.lamports.borrow_mut() -= amount_out; | ||
**out_user_account.lamports.borrow_mut() += amount_out; | ||
|
||
} else { | ||
// Perform SPL transfer from Solver to User | ||
let cpi_accounts = SplTransfer { | ||
|
@@ -368,26 +384,31 @@ pub mod bridge_escrow { | |
.as_ref() | ||
.ok_or(ErrorCode::AccountsNotPresent)?; | ||
|
||
require!( | ||
intent.token_in == auctioneer_token_in_account.mint, | ||
ErrorCode::MismatchTokenIn | ||
); | ||
|
||
let cpi_accounts = SplTransfer { | ||
from: auctioneer_token_in_account.to_account_info(), | ||
to: solver_token_in_account.to_account_info(), | ||
authority: accounts.auctioneer_state.to_account_info(), | ||
}; | ||
let cpi_program = token_program.to_account_info(); | ||
|
||
token::transfer( | ||
CpiContext::new_with_signer( | ||
cpi_program, | ||
cpi_accounts, | ||
signer_seeds, | ||
), | ||
intent.amount_in, | ||
)?; | ||
if intent.token_in == System::id() { | ||
**accounts.auctioneer_state.to_account_info().lamports.borrow_mut() -= intent.amount_in; | ||
**solver.lamports.borrow_mut() += intent.amount_in; | ||
} else { | ||
require!( | ||
intent.token_in == auctioneer_token_in_account.mint, | ||
ErrorCode::MismatchTokenIn | ||
); | ||
|
||
let cpi_accounts = SplTransfer { | ||
from: auctioneer_token_in_account.to_account_info(), | ||
to: solver_token_in_account.to_account_info(), | ||
authority: accounts.auctioneer_state.to_account_info(), | ||
}; | ||
let cpi_program = token_program.to_account_info(); | ||
|
||
token::transfer( | ||
CpiContext::new_with_signer( | ||
cpi_program, | ||
cpi_accounts, | ||
signer_seeds, | ||
), | ||
intent.amount_in, | ||
)?; | ||
} | ||
|
||
events::emit(events::Event::SendFundsToUser( | ||
events::SendFundsToUser { | ||
|
@@ -430,20 +451,11 @@ pub mod bridge_escrow { | |
// Check if token_out is native SOL or SPL token | ||
if accounts.token_out.key() == System::id() { | ||
// Handle Native SOL Transfer | ||
let ix = solana_program::system_instruction::transfer( | ||
solver.key, | ||
&accounts.user_token_out_account.key(), | ||
amount_out, | ||
); | ||
|
||
invoke( | ||
&ix, | ||
&[ | ||
solver.to_account_info(), | ||
accounts.user_token_out_account.to_account_info(), | ||
accounts.system_program.to_account_info(), | ||
], | ||
)?; | ||
let out_user_account = accounts.user_account.clone().unwrap(); | ||
|
||
**solver.lamports.borrow_mut() -= amount_out; | ||
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. here as well. |
||
**out_user_account.lamports.borrow_mut() += amount_out; | ||
|
||
} else { | ||
// Perform SPL token transfer from Solver to User | ||
let cpi_accounts = SplTransfer { | ||
|
@@ -713,6 +725,10 @@ pub struct SplTokenTransferCrossChain<'info> { | |
#[account(mut, token::mint = token_out)] | ||
pub user_token_out_account: Box<Account<'info, TokenAccount>>, | ||
|
||
// Solver -> User Account in case of SOL transfer | ||
#[account(mut, address = user_token_out_account.owner)] | ||
pub user_account: Option<AccountInfo<'info>>, | ||
|
||
pub token_program: Program<'info, Token>, | ||
pub associated_token_program: Program<'info, AssociatedToken>, | ||
pub system_program: Program<'info, System>, | ||
|
@@ -767,7 +783,7 @@ pub struct SplTokenTransfer<'info> { | |
#[account(mut, address = intent.clone().unwrap().user_in)] | ||
/// CHECK: | ||
pub intent_owner: UncheckedAccount<'info>, | ||
#[account(seeds = [AUCTIONEER_SEED], bump)] | ||
#[account(mut, seeds = [AUCTIONEER_SEED], bump)] | ||
pub auctioneer_state: Box<Account<'info, Auctioneer>>, | ||
#[account(mut)] | ||
pub solver: Signer<'info>, | ||
|
@@ -791,6 +807,10 @@ pub struct SplTokenTransfer<'info> { | |
#[account(mut, token::mint = token_out)] | ||
pub user_token_out_account: Box<Account<'info, TokenAccount>>, | ||
|
||
// Solver -> User Account in case of SOL transfer | ||
#[account(mut, address = user_token_out_account.owner)] | ||
pub user_account: Option<AccountInfo<'info>>, | ||
|
||
pub token_program: Program<'info, Token>, | ||
pub associated_token_program: Program<'info, AssociatedToken>, | ||
pub system_program: Program<'info, System>, | ||
|
@@ -844,18 +864,18 @@ pub struct EscrowAndStoreIntent<'info> { | |
|
||
// Box this account to avoid copying large account data | ||
#[account(mut, token::authority = user, token::mint = token_mint)] | ||
pub user_token_account: Box<Account<'info, TokenAccount>>, | ||
pub user_token_account: Option<Box<Account<'info, TokenAccount>>>, | ||
|
||
// Box this account as it holds state that might be large | ||
#[account(seeds = [AUCTIONEER_SEED], bump)] | ||
#[account(mut, seeds = [AUCTIONEER_SEED], bump)] | ||
pub auctioneer_state: Box<Account<'info, Auctioneer>>, | ||
|
||
// Box the token mint account if it's large or for performance reasons | ||
pub token_mint: Box<Account<'info, Mint>>, | ||
pub token_mint: Option<Box<Account<'info, Mint>>>, | ||
|
||
// Box the escrow token account as it's mutable and holds token data | ||
#[account(init_if_needed, payer = user, associated_token::mint = token_mint, associated_token::authority = auctioneer_state)] | ||
pub escrow_token_account: Box<Account<'info, TokenAccount>>, | ||
pub escrow_token_account: Option<Box<Account<'info, TokenAccount>>>, | ||
|
||
// From StoreIntent | ||
// Box the intent account, as it's a new account with considerable space allocated | ||
|
Oops, something went wrong.
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.
since solver account is owned by system program, u need to call system program to transfer just like how you do it in
escrow_and_store_intent
method.