Skip to content

Commit

Permalink
solana: address PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: bingyuyap <[email protected]>
  • Loading branch information
bingyuyap committed Oct 24, 2024
1 parent d0f7b5b commit 64e3659
Show file tree
Hide file tree
Showing 35 changed files with 1,803 additions and 424 deletions.
2 changes: 1 addition & 1 deletion svm/Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[toolchain]
anchor_version = "0.30.1"
solana_version = "1.18.23"
solana_version = "1.18.17"

[features]
resolution = true
Expand Down
2 changes: 1 addition & 1 deletion svm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ anchor deploy --provider.cluster mainnet --provider.wallet ~/.config/solana/your
### Upgrading

```
anchor upgrade --provider.cluster <network> --provider.wallet ~/.config/solana/your-key.json --program-id <PROGRAM_ID> target/deploy/routers.so
anchor upgrade --provider.cluster <network> --provider.wallet ~/.config/solana/your-key.json --program-id <PROGRAM_ID> target/deploy/router.so
```

If you get an error like this
Expand Down
65 changes: 65 additions & 0 deletions svm/programs/mock-integrator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Mock Integrator Program

This program serves as a mock integrator to demonstrate how to call the register function in the router program. It's designed to simulate the process of registering an integrator, which requires a Cross-Program Invocation (CPI) call with a Program Derived Address (PDA) signer.

## Overview

The mock integrator program is crucial for testing the entire router program functionality. It serves as a gateway to test all other features of the router program because:

1. It simulates the registration process, which is a prerequisite for all other router program operations.
2. By successfully calling the `register` function, it establishes the necessary account such as `IntegratorConfig` and permissions required for subsequent router program interactions.
3. It allows us to test the router program's handling of Cross-Program Invocations (CPIs) from an integrator.

This program contains a single instruction:

1. `invoke_register`: This instruction demonstrates how to properly set up the accounts and sign the transaction using a PDA, which is required for the registration process.

By first calling `invoke_register`, we can then proceed to test all other aspects of the router program, ensuring its complete functionality in a controlled testing environment.

## Requirements

Before testing, ensure you have the following installed:

- [Rust 1.75.0](https://www.rust-lang.org/tools/install)
- [Solana 1.18.17](https://solana.com/docs/intro/installation)
- [Anchor 0.30.1](https://www.anchor-lang.com/docs/installation)
- [Yarn](https://yarnpkg.com/getting-started/install)

## How to Test

To test this program, follow these steps:

1. Ensure you have Rust, Anchor, and the Solana tool suite installed on your system.

2. Navigate to the root directory of the SVM project:

```
cd svm
```

3. Build the Anchor project:

```
anchor build
```

4. Navigate to the mock-integrator program's directory:

```
cd programs/mock-integrator
```

5. Run the tests using Cargo:
```
cargo test-sbf
```

This process will first build all the programs in the workspace, including the router program that the mock-integrator depends on, and then run the tests for the mock-integrator program in a Solana BPF environment.

## Code Structure

The main components of the program are:

- `invoke_register`: The instruction that demonstrates the CPI call to the router program's register function.
- `InvokeRegisterArgs`: The struct that defines the arguments for the invoke_register instruction.
- `InvokeRegister`: The struct that defines the accounts required for the invoke_register instruction.
22 changes: 13 additions & 9 deletions svm/programs/mock-integrator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,40 @@ pub mod mock_integrator {
/// Invokes the register function in the router program via a CPI call.
/// This function demonstrates how to properly set up the accounts and sign the transaction
/// using a PDA, which is required for the registration process.
pub fn invoke_register(ctx: Context<InvokeRegister>, args: RegisterArgs) -> Result<()> {
let bump_seed = &[args.integrator_program_pda_bump][..];
pub fn invoke_register(ctx: Context<InvokeRegister>, args: InvokeRegisterArgs) -> Result<()> {
let bump_seed = &[ctx.bumps.integrator_program_pda][..];
let signer_seeds: &[&[&[u8]]] = &[&[b"router_integrator", bump_seed]];

router::cpi::register(
ctx.accounts.invoke_register().with_signer(signer_seeds),
args,
RegisterArgs {
integrator_program_pda_bump: ctx.bumps.integrator_program_pda,
integrator_program_id: crate::ID,
admin: args.admin,
},
)?;
Ok(())
}
}
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct InvokeRegisterArgs {
pub admin: Pubkey,
}

#[derive(Accounts)]
#[instruction(args: RegisterArgs)]
#[instruction(args: InvokeRegisterArgs)]
pub struct InvokeRegister<'info> {
#[account(mut)]
pub payer: Signer<'info>,

/// CHECK: This account is not checked for safety because it is assumed to be a trusted admin account.
pub admin: UncheckedAccount<'info>,

#[account(mut)]
/// CHECK: This account is to be checked and initialized by the router program
pub integrator_config: UncheckedAccount<'info>,

/// The integrator program's PDA
#[account(
seeds = [b"router_integrator"],
bump = args.integrator_program_pda_bump,
bump,
)]
pub integrator_program_pda: SystemAccount<'info>,

Expand All @@ -59,7 +64,6 @@ impl<'info> InvokeRegister<'info> {
let cpi_program = self.router_program.to_account_info();
let cpi_accounts = Register {
payer: self.payer.to_account_info(),
admin: self.admin.to_account_info(),
integrator_config: self.integrator_config.to_account_info(),
integrator_program_pda: self.integrator_program_pda.to_account_info(),
system_program: self.system_program.to_account_info(),
Expand Down
Loading

0 comments on commit 64e3659

Please sign in to comment.