Skip to content

Commit

Permalink
token: Transition perf-monitor to solana-program-test (solana-labs#2586)
Browse files Browse the repository at this point in the history
* token: Transition perf-monitor to solana-program-test

* fmt

* Refactor for clarity
  • Loading branch information
joncinque authored Nov 16, 2021
1 parent 688ee42 commit 7764a9d
Show file tree
Hide file tree
Showing 10 changed files with 854 additions and 184 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ members = [
exclude = [
"themis/client_ristretto",
"themis/program_ristretto",
"token/perf-monitor", # TODO: Rework perf-monitor to use solana-program-test, avoiding the need to link directly with the BPF VM
]

[profile.dev]
Expand Down
18 changes: 0 additions & 18 deletions token/perf-monitor/Cargo.toml

This file was deleted.

165 changes: 0 additions & 165 deletions token/perf-monitor/tests/assert_instruction_count.rs

This file was deleted.

2 changes: 2 additions & 0 deletions token/program-2022/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exclude = ["js/**"]

[features]
no-entrypoint = []
test-bpf = []

[dependencies]
arrayref = "0.3.6"
Expand All @@ -20,6 +21,7 @@ solana-program = "1.8.1"
thiserror = "1.0"

[dev-dependencies]
solana-program-test = "1.8.1"
solana-sdk = "1.8.1"

[lib]
Expand Down
140 changes: 140 additions & 0 deletions token/program-2022/tests/action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use {
solana_program_test::BanksClient,
solana_sdk::{
hash::Hash,
program_pack::Pack,
pubkey::Pubkey,
signature::{Keypair, Signer},
system_instruction,
transaction::Transaction,
transport::TransportError,
},
spl_token_2022::{
id, instruction,
state::{Account, Mint},
},
};

pub async fn create_mint(
banks_client: &mut BanksClient,
payer: &Keypair,
recent_blockhash: Hash,
pool_mint: &Keypair,
manager: &Pubkey,
decimals: u8,
) -> Result<(), TransportError> {
let rent = banks_client.get_rent().await.unwrap();
let mint_rent = rent.minimum_balance(Mint::LEN);

let transaction = Transaction::new_signed_with_payer(
&[
system_instruction::create_account(
&payer.pubkey(),
&pool_mint.pubkey(),
mint_rent,
Mint::LEN as u64,
&id(),
),
instruction::initialize_mint(&id(), &pool_mint.pubkey(), manager, None, decimals)
.unwrap(),
],
Some(&payer.pubkey()),
&[payer, pool_mint],
recent_blockhash,
);
banks_client.process_transaction(transaction).await?;
Ok(())
}

pub async fn create_account(
banks_client: &mut BanksClient,
payer: &Keypair,
recent_blockhash: Hash,
account: &Keypair,
pool_mint: &Pubkey,
owner: &Pubkey,
) -> Result<(), TransportError> {
let rent = banks_client.get_rent().await.unwrap();
let account_rent = rent.minimum_balance(Account::LEN);

let transaction = Transaction::new_signed_with_payer(
&[
system_instruction::create_account(
&payer.pubkey(),
&account.pubkey(),
account_rent,
Account::LEN as u64,
&id(),
),
instruction::initialize_account(&id(), &account.pubkey(), pool_mint, owner).unwrap(),
],
Some(&payer.pubkey()),
&[payer, account],
recent_blockhash,
);
banks_client.process_transaction(transaction).await?;
Ok(())
}

pub async fn mint_to(
banks_client: &mut BanksClient,
payer: &Keypair,
recent_blockhash: Hash,
mint: &Pubkey,
account: &Pubkey,
mint_authority: &Keypair,
amount: u64,
) -> Result<(), TransportError> {
let transaction = Transaction::new_signed_with_payer(
&[
instruction::mint_to(&id(), mint, account, &mint_authority.pubkey(), &[], amount)
.unwrap(),
],
Some(&payer.pubkey()),
&[payer, mint_authority],
recent_blockhash,
);
banks_client.process_transaction(transaction).await?;
Ok(())
}

pub async fn transfer(
banks_client: &mut BanksClient,
payer: &Keypair,
recent_blockhash: Hash,
source: &Pubkey,
destination: &Pubkey,
authority: &Keypair,
amount: u64,
) -> Result<(), TransportError> {
let transaction = Transaction::new_signed_with_payer(
&[
instruction::transfer(&id(), source, destination, &authority.pubkey(), &[], amount)
.unwrap(),
],
Some(&payer.pubkey()),
&[payer, authority],
recent_blockhash,
);
banks_client.process_transaction(transaction).await?;
Ok(())
}

pub async fn burn(
banks_client: &mut BanksClient,
payer: &Keypair,
recent_blockhash: Hash,
mint: &Pubkey,
account: &Pubkey,
authority: &Keypair,
amount: u64,
) -> Result<(), TransportError> {
let transaction = Transaction::new_signed_with_payer(
&[instruction::burn(&id(), account, mint, &authority.pubkey(), &[], amount).unwrap()],
Some(&payer.pubkey()),
&[payer, authority],
recent_blockhash,
);
banks_client.process_transaction(transaction).await?;
Ok(())
}
Loading

0 comments on commit 7764a9d

Please sign in to comment.