Skip to content

Commit b5c348c

Browse files
committed
add steel test
1 parent ba80990 commit b5c348c

File tree

10 files changed

+99
-71
lines changed

10 files changed

+99
-71
lines changed

tokens/spl-token-minter/steel/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
license = "Apache-2.0"
99
homepage = ""
1010
documentation = ""
11-
respository = ""
11+
repository = ""
1212
readme = "./README.md"
1313
keywords = ["solana"]
1414

@@ -21,3 +21,7 @@ steel = { version = "2.0", features = ["spl"] }
2121
thiserror = "1.0"
2222
spl-token = "^4"
2323
mpl-token-metadata = { version = "4.1.2" }
24+
const-crypto = "0.1.0"
25+
spl-associated-token-account = { version = "^2.3", features = [
26+
"no-entrypoint",
27+
] }

tokens/spl-token-minter/steel/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ steel.workspace = true
1111
thiserror.workspace = true
1212
spl-token.workspace = true
1313
mpl-token-metadata.workspace = true
14+
const-crypto.workspace = true

tokens/spl-token-minter/steel/api/src/consts.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use const_crypto::ed25519;
2+
use solana_program::pubkey::Pubkey;
3+
14
/// The seed of the mint account PDA.
25
pub const MINT: &[u8] = b"mint";
36

@@ -8,3 +11,9 @@ pub const MINT_NOISE: [u8; 16] = [
811

912
/// The seed of the metadata account PDA.
1013
pub const METADATA: &[u8] = b"metadata";
14+
15+
/// The bump of the mint account.
16+
pub const MINT_BUMP: u8 = ed25519::derive_program_address(&[MINT, &MINT_NOISE], &PROGRAM_ID).1;
17+
18+
/// Program id for const pda derivations
19+
const PROGRAM_ID: [u8; 32] = unsafe { *(&crate::id() as *const Pubkey as *const [u8; 32]) };

tokens/spl-token-minter/steel/api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ pub mod consts;
22
pub mod error;
33
pub mod instruction;
44
pub mod sdk;
5+
pub mod utils;
56

67
pub mod prelude {
78
pub use crate::consts::*;
89
pub use crate::error::*;
910
pub use crate::instruction::*;
1011
pub use crate::sdk::*;
12+
pub use crate::utils::*;
1113
}
1214

1315
use steel::*;

tokens/spl-token-minter/steel/api/src/sdk.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,10 @@ use crate::prelude::*;
55
pub fn create(
66
payer: Pubkey,
77
mint_authority: Pubkey,
8-
token_name: String,
9-
token_symbol: String,
10-
token_uri: String,
8+
token_name: [u8; 32],
9+
token_symbol: [u8; 8],
10+
token_uri: [u8; 64],
1111
) -> Instruction {
12-
let token_name_bytes: [u8; 32] = token_name
13-
.as_bytes()
14-
.try_into()
15-
.expect("String wrong length, expected 32 bytes");
16-
let token_symbol_bytes: [u8; 8] = token_symbol
17-
.as_bytes()
18-
.try_into()
19-
.expect("String wrong length, expected 32 bytes");
20-
let token_uri_bytes: [u8; 64] = token_uri
21-
.as_bytes()
22-
.try_into()
23-
.expect("String wrong length, expected 32 bytes");
24-
2512
let mint_pda = Pubkey::find_program_address(&[MINT, MINT_NOISE.as_slice()], &crate::ID);
2613
let metadata_pda = Pubkey::find_program_address(
2714
&[
@@ -45,25 +32,20 @@ pub fn create(
4532
AccountMeta::new_readonly(sysvar::rent::ID, false),
4633
],
4734
data: Create {
48-
token_name: token_name_bytes,
49-
token_symbol: token_symbol_bytes,
50-
token_uri: token_uri_bytes,
35+
token_name,
36+
token_symbol,
37+
token_uri,
5138
}
5239
.to_bytes(),
5340
}
5441
}
55-
pub fn mint(
56-
signer: Pubkey,
57-
mint: Pubkey,
58-
to: Pubkey,
59-
authority: Pubkey,
60-
amount: u64,
61-
) -> Instruction {
42+
pub fn mint(signer: Pubkey, to: Pubkey, authority: Pubkey, amount: u64) -> Instruction {
43+
let mint_pda = Pubkey::find_program_address(&[MINT, MINT_NOISE.as_slice()], &crate::ID);
6244
Instruction {
6345
program_id: crate::ID,
6446
accounts: vec![
6547
AccountMeta::new(signer, true),
66-
AccountMeta::new(mint, false),
48+
AccountMeta::new(mint_pda.0, false),
6749
AccountMeta::new(to, false),
6850
AccountMeta::new(authority, false),
6951
AccountMeta::new_readonly(spl_token::ID, false),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub fn str_to_bytes<const N: usize>(str: &str) -> [u8; N] {
2+
let mut str_bytes = [0u8; N];
3+
let copy_len = str.len().min(N);
4+
str_bytes[..copy_len].copy_from_slice(&str.as_bytes()[..copy_len]);
5+
str_bytes
6+
}

tokens/spl-token-minter/steel/program/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ solana-program.workspace = true
1212
steel.workspace = true
1313
spl-token.workspace = true
1414
mpl-token-metadata.workspace = true
15+
spl-associated-token-account.workspace = true
1516

1617

1718
[dev-dependencies]
18-
bs64 = "0.1.2"
19+
base64 = "0.21"
1920
rand = "0.8.5"
2021
solana-program-test = "1.18"
2122
solana-sdk = "1.18"

tokens/spl-token-minter/steel/program/src/create.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,63 +12,64 @@ pub fn process_create(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
1212
let token_uri = String::from_utf8(args.token_uri.to_vec()).expect("Invalid UTF-8");
1313

1414
// Load accounts.
15-
let [payer_info, mint_info, mint_authority_info, metadata_info, token_program, system_program, rent_sysvar, token_metadata_program] =
15+
let [payer_info, mint_info, mint_authority_info, metadata_info, system_program, token_program, metadata_program, rent_sysvar] =
1616
accounts
1717
else {
1818
return Err(ProgramError::NotEnoughAccountKeys);
1919
};
2020

2121
// validation
2222
payer_info.is_signer()?;
23-
mint_info.as_mint()?;
24-
token_program.is_program(&spl_token::ID)?;
25-
rent_sysvar.is_sysvar(&sysvar::rent::ID)?;
23+
mint_info.is_empty()?.is_writable()?;
2624
system_program.is_program(&system_program::ID)?;
2725
token_program.is_program(&spl_token::ID)?;
28-
token_metadata_program.is_program(&mpl_token_metadata::ID)?;
2926
rent_sysvar.is_sysvar(&sysvar::rent::ID)?;
27+
msg!("metadata program key {}", metadata_program.key);
28+
msg!("metadata program ID {}", mpl_token_metadata::ID);
29+
// metadata_program.is_program(&mpl_token_metadata::ID)?;
30+
31+
if metadata_program.key.ne(&mpl_token_metadata::ID) {
32+
return Err(ProgramError::InvalidAccountData);
33+
}
3034

3135
// First create the account for the Mint
3236
//
3337
msg!("Creating mint account...");
3438
msg!("Mint: {}", mint_info.key);
35-
allocate_account(
39+
allocate_account_with_bump(
3640
mint_info,
3741
system_program,
3842
payer_info,
3943
Mint::LEN,
4044
&spl_token::ID,
4145
&[MINT, MINT_NOISE.as_slice()],
46+
MINT_BUMP,
4247
)?;
4348

4449
// Now initialize that account as a Mint (standard Mint)
4550
//
4651
msg!("Initializing mint account...");
4752
msg!("Mint: {}", mint_info.key);
48-
solana_program::program::invoke(
49-
&spl_token::instruction::initialize_mint(
50-
&spl_token::ID,
51-
mint_info.key,
52-
mint_authority_info.key,
53-
Some(mint_authority_info.key),
54-
9, // 9 Decimals for the default SPL Token standard,
55-
)?,
56-
&[
57-
token_program.clone(),
58-
mint_info.clone(),
59-
mint_authority_info.clone(),
60-
rent_sysvar.clone(),
61-
],
53+
initialize_mint_signed_with_bump(
54+
mint_info,
55+
mint_authority_info,
56+
None,
57+
token_program,
58+
rent_sysvar,
59+
9, // 9 Decimals for the default SPL Token standard
60+
&[MINT, MINT_NOISE.as_slice()],
61+
MINT_BUMP,
6262
)?;
6363

6464
// Now create the account for that Mint's metadata
6565
//
6666
msg!("Creating metadata account...");
6767
msg!("Metadata account address: {}", metadata_info.key);
6868
mpl_token_metadata::instructions::CreateMetadataAccountV3Cpi {
69-
__program: token_metadata_program,
69+
__program: metadata_program,
7070
metadata: metadata_info,
7171
mint: mint_info,
72+
// mint_authority: mint_authority_info,
7273
mint_authority: mint_authority_info,
7374
payer: payer_info,
7475
update_authority: (payer_info, true),

tokens/spl-token-minter/steel/program/src/mint.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ pub fn process_mint(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
2323

2424
to_info.as_associated_token_account(signer_info.key, mint_info.key)?;
2525

26-
token_program.is_program(&spl_token::ID)?;
27-
2826
solana_program::program::invoke(
2927
&spl_token::instruction::mint_to(
3028
&spl_token::id(),
Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
use steel_api::prelude::*;
21
use solana_program::hash::Hash;
32
use solana_program_test::{processor, BanksClient, ProgramTest};
43
use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction};
4+
use spl_token_minter_api::prelude::*;
55
use steel::*;
66

77
async fn setup() -> (BanksClient, Keypair, Hash) {
88
let mut program_test = ProgramTest::new(
9-
"steel",
10-
steel_api::ID,
11-
processor!(steel_program::process_instruction),
9+
"spl_token_minter_program",
10+
spl_token_minter_api::ID,
11+
processor!(spl_token_minter_program::process_instruction),
1212
);
13+
14+
program_test.add_program("token_metadata", mpl_token_metadata::ID, None);
15+
1316
program_test.prefer_bpf(true);
1417
program_test.start().await
1518
}
@@ -19,28 +22,49 @@ async fn run_test() {
1922
// Setup test
2023
let (mut banks, payer, blockhash) = setup().await;
2124

22-
// Submit initialize transaction.
23-
let ix = initialize(payer.pubkey());
25+
let name = str_to_bytes::<32>("Solana Gold");
26+
let symbol = str_to_bytes::<8>("GOLDSOL");
27+
let uri = str_to_bytes::<64>("https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json");
28+
29+
// Submit create transaction.
30+
let ix = create(payer.pubkey(), payer.pubkey(), name, symbol, uri);
2431
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
2532
let res = banks.process_transaction(tx).await;
2633
assert!(res.is_ok());
2734

28-
// Verify counter was initialized.
29-
let counter_address = counter_pda().0;
30-
let counter_account = banks.get_account(counter_address).await.unwrap().unwrap();
31-
let counter = Counter::try_from_bytes(&counter_account.data).unwrap();
32-
assert_eq!(counter_account.owner, steel_api::ID);
33-
assert_eq!(counter.value, 0);
35+
let mint_pda =
36+
Pubkey::find_program_address(&[MINT, MINT_NOISE.as_slice()], &spl_token_minter_api::ID);
37+
// dbg!(mint_pda);
38+
// let to_ata =
39+
// spl_associated_token_account::get_associated_token_address(&payer.pubkey(), &mint_pda.0);
40+
let to_ata = spl_associated_token_account::get_associated_token_address_with_program_id(
41+
&payer.pubkey(),
42+
&mint_pda.0,
43+
&spl_token::ID,
44+
);
45+
// dbg!(to_ata);
3446

35-
// Submit add transaction.
36-
let ix = add(payer.pubkey(), 42);
47+
// Submit mint transaction.
48+
let ix = mint(payer.pubkey(), to_ata, payer.pubkey(), 100);
3749
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
3850
let res = banks.process_transaction(tx).await;
3951
assert!(res.is_ok());
4052

41-
// Verify counter was incremented.
42-
let counter_account = banks.get_account(counter_address).await.unwrap().unwrap();
43-
let counter = Counter::try_from_bytes(&counter_account.data).unwrap();
44-
assert_eq!(counter.value, 42);
45-
}
53+
// // Verify counter was initialized.
54+
// let counter_address = counter_pda().0;
55+
// let counter_account = banks.get_account(counter_address).await.unwrap().unwrap();
56+
// let counter = Counter::try_from_bytes(&counter_account.data).unwrap();
57+
// assert_eq!(counter_account.owner, steel_api::ID);
58+
// assert_eq!(counter.value, 0);
59+
60+
// // Submit add transaction.
61+
// let ix = add(payer.pubkey(), 42);
62+
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
63+
// let res = banks.process_transaction(tx).await;
64+
// assert!(res.is_ok());
4665

66+
// // Verify counter was incremented.
67+
// let counter_account = banks.get_account(counter_address).await.unwrap().unwrap();
68+
// let counter = Counter::try_from_bytes(&counter_account.data).unwrap();
69+
// assert_eq!(counter.value, 42);
70+
}

0 commit comments

Comments
 (0)