Skip to content

feat(solana-swap): Implement basic maker functionality for SOL swaps#1

Merged
shamardy merged 16 commits intodevfrom
solana-swap-v1
Jun 14, 2024
Merged

feat(solana-swap): Implement basic maker functionality for SOL swaps#1
shamardy merged 16 commits intodevfrom
solana-swap-v1

Conversation

@r2st
Copy link
Copy Markdown
Contributor

@r2st r2st commented Mar 28, 2024

No description provided.

@r2st r2st changed the base branch from main to dev March 28, 2024 06:59
@shamardy shamardy requested review from laruh and shamardy March 28, 2024 20:52
Copy link
Copy Markdown

@laruh laruh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this feature!

I have some notes and questions.

PS: please commit Cargo.lock file, so security team (and devs actually) can see the whole deps tree. and it helps to be sure that all contributors have same deps.

Copy link
Copy Markdown
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! First review iteration where the purpose is to make the code more readable to better review it in subsequent iterations.

Copy link
Copy Markdown

@laruh laruh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes, next iteration

Comment on lines +90 to +114
{
let create_instruction = system_instruction::create_account(
sender_account.key,
vault_pda_data.key,
rent_exemption_lamports,
41,
program_id,
);

let account_infos = vec![
sender_account.clone(),
vault_pda_data.clone(),
system_program_account.clone(),
];

invoke_signed(&create_instruction, &account_infos, &[vault_seeds_data])?;

let data = &mut vault_pda_data.try_borrow_mut_data()?;
if data.len() < payment_bytes.len() {
msg!("Error: Account data buffer too small");
return Err(ProgramError::AccountDataTooSmall);
}

data[..payment_bytes.len()].copy_from_slice(&payment_bytes);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this inner scope? I dont have compilation errors moving this inner scope operations to the outer.

To improve readability and maintainability please break the code into smaller well named functions, like Omer suggested.

mj-blockydevs and others added 4 commits June 5, 2024 16:39
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
…e transfer operation. Only sender and receiver are involved.

Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
* feat: Solana Swap with all of previous PR suggestions applied.
* feat: Providing system_program_account should not be necessary for the transfer operation. Only sender and receiver are involved.
* feat: Max line width fixed with cargo fmt
@shamardy
Copy link
Copy Markdown
Collaborator

shamardy commented Jun 6, 2024

The latest commit does the following as stated by @mj-blockydevs

List of applied suggestions:

  • Removed msg calls.
  • Removed unused variables. IMPORTANT: For now, I've left unused struct properties to avoid changing the communication interface with the program. My main goal was to apply all suggestions without touching the "tests.rs" file, to ensure the business logic behind the program remained unchanged.
  • Broke the code into smaller functions. I've not only moved them to separate functions but also placed the functions in a dedicated module for clarity. Let me know if you are okay with this decision.
  • Using invoke_signed without the system program account. Let me know if I understood this particular requirement correctly.
  • Replaced panicking asserts with supported Solana Errors. Let me know if the ones I used are okay for you.
  • Removed inner scopes where they were not necessary.

Do you want me to prepare unit tests for the functions moved to the /utils directory, or are the already prepared tests sufficient for us at this stage?

@mj-blockydevs @laruh let's continue the review here since @mj-blockydevs now has access to the repo.

@shamardy
Copy link
Copy Markdown
Collaborator

shamardy commented Jun 6, 2024

Removed unused variables. IMPORTANT: For now, I've left unused struct properties to avoid changing the communication interface with the program. My main goal was to apply all suggestions without touching the "tests.rs" file, to ensure the business logic behind the program remained unchanged.

Will cover it in next review if there are any problems.

I've not only moved them to separate functions but also placed the functions in a dedicated module for clarity. Let me know if you are okay with this decision.

Code looks much better this way, thanks for this refactor.

Using invoke_signed without the system program account. Let me know if I understood this particular requirement correctly.

Looks good on first look, will check it extensively in the next review iteration.

Replaced panicking asserts with supported Solana Errors. Let me know if the ones I used are okay for you.

Will check them case by case in the review.

Do you want me to prepare unit tests for the functions moved to the /utils directory, or are the already prepared tests sufficient for us at this stage?

Unit tests for each module would be great to have but not necessary at this stage, it's up to you if you want to add them if there are no other higher priority items to finish first.

Copy link
Copy Markdown
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks you for the fixes! Next review iteration!

}

impl AtomicSwapInstruction {
pub fn unpack(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should look into using borsh crate for serialization and deserialization instead of pack/unpack, maybe it will provide better performance but if not and the performance difference is negligible, using borsh will lead to less errors. This is not required in this PR but we should look into it in the future when completing the swap program for v1 and v2 swaps.

vault_seeds_data,
payment_bytes,
)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to transfer the tokens here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll leave comments in the code to remind us about it. However, it is out of scope for now. We will address this issue in a separate PR.

if token_program != Pubkey::new_from_array([0; 32]) {
return Err(ProgramError::Custom(NOT_SUPPORTED));
}
transfer_lamports(vault_pda, receiver_account, vault_seeds, amount)?;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need also to add the case of tokens transfer here

if token_program != Pubkey::new_from_array([0; 32]) {
return Err(ProgramError::Custom(NOT_SUPPORTED));
}
transfer_lamports(vault_pda, sender_account, vault_seeds, amount)?;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@shamardy shamardy requested a review from laruh June 6, 2024 13:55
Copy link
Copy Markdown

@laruh laruh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, here are notes from my side

Comment on lines +1 to +6
use crate::error_code::{INVALID_PAYMENT_HASH, INVALID_PAYMENT_STATE, SWAP_ACCOUNT_NOT_FOUND};
use crate::payment::{Payment, PaymentState};
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program::invoke_signed,
program_error::ProgramError, system_instruction,
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose there is a lack of spl_token crate usage to provide transfer_spl_tokens implementation

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will be necessary to support token transfers. This will be implemented later.

Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
Copy link
Copy Markdown
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the fixes! A few non-blocker comments that should be fixed here before merge.
@laruh can you please check that all your comments are fixed and that you have no more comments.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AtomicSwapInstruction/pack/unpack and the error codes are duplicated/used here GLEECBTC/komodo-defi-framework#2091, I suggest removing them from #2091 once this PR is merged and import them from this repo/crate. Then we would need to edit them only in one place in the future if the need arises.

Copy link
Copy Markdown

@laruh laruh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes, init v1 LGTM!

Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas+instead@blockydevs.com>
@shamardy shamardy changed the title feat(solana-swap): initial version v1 feat(solana-swap): Implement basic maker functionality for SOL swaps Jun 14, 2024
@shamardy shamardy merged commit acad67f into dev Jun 14, 2024
@shamardy shamardy deleted the solana-swap-v1 branch June 14, 2024 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants