Skip to content

Commit 6550e95

Browse files
committed
feat: completed spl-token minter
1 parent a98aa3b commit 6550e95

File tree

7 files changed

+302
-30
lines changed

7 files changed

+302
-30
lines changed

tokens/spl-token-minter/poseidon/spl-token-minter/package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
66
},
77
"dependencies": {
8-
"@coral-xyz/anchor": "^0.30.1"
8+
"@coral-xyz/anchor": "^0.30.0",
9+
"@solana/web3.js": "^1.95.2"
910
},
1011
"devDependencies": {
11-
"chai": "^4.3.4",
12-
"mocha": "^9.0.3",
13-
"ts-mocha": "^10.0.0",
12+
"anchor-bankrun": "^0.4.0",
13+
"solana-bankrun": "^0.3.0",
1414
"@types/bn.js": "^5.1.0",
1515
"@types/chai": "^4.3.0",
1616
"@types/mocha": "^9.0.0",
17-
"typescript": "^4.3.5",
18-
"prettier": "^2.6.2"
17+
"chai": "^4.4.1",
18+
"mocha": "^9.0.3",
19+
"prettier": "^2.6.2",
20+
"ts-mocha": "^10.0.0",
21+
"typescript": "^4.3.5"
1922
}
2023
}

tokens/spl-token-minter/poseidon/spl-token-minter/pnpm-lock.yaml

Lines changed: 90 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tokens/spl-token-minter/poseidon/spl-token-minter/programs/spl-token-minter/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ cpi = ["no-entrypoint"]
1414
no-entrypoint = []
1515
no-idl = []
1616
no-log-ix-name = []
17-
idl-build = ["anchor-lang/idl-build"]
17+
idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]
1818

1919
[dependencies]
20-
anchor-lang = "0.30.1"
20+
anchor-lang = { version = "0.30.1", features = ["init-if-needed"] }
21+
anchor-spl = "0.30.1"
Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,63 @@
11
use anchor_lang::prelude::*;
2-
2+
use anchor_spl::{
3+
token::{mint_to, Mint, MintTo, Token, TokenAccount},
4+
associated_token::AssociatedToken,
5+
};
36
declare_id!("DmrXSUGWYaqtWg8sbi9JQN48yVZ1y2m7HvWXbND52Mcw");
4-
57
#[program]
68
pub mod spl_token_minter {
79
use super::*;
8-
9-
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
10-
msg!("Greetings from: {:?}", ctx.program_id);
10+
pub fn create_token_mint(
11+
ctx: Context<CreateTokenMintContext>,
12+
decimals: u8,
13+
freeze_authority: Pubkey,
14+
) -> Result<()> {
15+
Ok(())
16+
}
17+
pub fn mint(ctx: Context<MintContext>, amount: u64) -> Result<()> {
18+
let cpi_ctx = CpiContext::new(
19+
ctx.accounts.token_program.to_account_info(),
20+
MintTo {
21+
mint: ctx.accounts.mint_account.to_account_info(),
22+
to: ctx.accounts.associated_token_account.to_account_info(),
23+
authority: ctx.accounts.mint_authority.to_account_info(),
24+
},
25+
);
26+
mint_to(cpi_ctx, amount)?;
1127
Ok(())
1228
}
1329
}
14-
1530
#[derive(Accounts)]
16-
pub struct Initialize {}
31+
#[instruction(decimals: u8)]
32+
pub struct CreateTokenMintContext<'info> {
33+
#[account(mut)]
34+
pub mint_authority: Signer<'info>,
35+
#[account(
36+
init,
37+
payer = mint_authority,
38+
mint::decimals = decimals,
39+
mint::authority = mint_authority.key(),
40+
)]
41+
pub mint_account: Account<'info, Mint>,
42+
pub token_program: Program<'info, Token>,
43+
pub system_program: Program<'info, System>,
44+
}
45+
#[derive(Accounts)]
46+
pub struct MintContext<'info> {
47+
#[account(mut)]
48+
pub mint_account: Account<'info, Mint>,
49+
#[account(mut)]
50+
pub mint_authority: Signer<'info>,
51+
#[account(mut)]
52+
pub recipient: SystemAccount<'info>,
53+
#[account(
54+
init_if_needed,
55+
payer = mint_authority,
56+
associated_token::mint = mint_account,
57+
associated_token::authority = recipient,
58+
)]
59+
pub associated_token_account: Account<'info, TokenAccount>,
60+
pub associated_token_program: Program<'info, AssociatedToken>,
61+
pub token_program: Program<'info, Token>,
62+
pub system_program: Program<'info, System>,
63+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { describe, it } from 'node:test';
2+
import * as anchor from '@coral-xyz/anchor';
3+
import { getAccount, getMint } from '@solana/spl-token';
4+
import { Keypair, PublicKey, SystemProgram } from '@solana/web3.js';
5+
import { BankrunProvider } from 'anchor-bankrun';
6+
import { assert } from 'chai';
7+
import { startAnchor } from 'solana-bankrun';
8+
import type { SplTokenMinter } from '../target/types/spl_token_minter';
9+
10+
const IDL = require('../target/idl/spl_token_minter.json');
11+
const PROGRAM_ID = new PublicKey(IDL.address);
12+
13+
describe('Bankrun - SPL Token Minter', async () => {
14+
// Start the Bankrun context
15+
const context = await startAnchor('', [{ name: 'spl_token_minter', programId: PROGRAM_ID }], []);
16+
17+
const provider = new BankrunProvider(context);
18+
anchor.setProvider(provider);
19+
const program = new anchor.Program<SplTokenMinter>(IDL, provider);
20+
21+
const payer = provider.wallet as anchor.Wallet;
22+
let mintAccount: PublicKey;
23+
let recipientTokenAccount: PublicKey;
24+
const recipient = Keypair.generate();
25+
26+
it('Creates a new token mint', async () => {
27+
const mintAccountKeypair = Keypair.generate();
28+
mintAccount = mintAccountKeypair.publicKey;
29+
30+
const decimals = 9;
31+
const freezeAuthority = payer.publicKey;
32+
33+
const tx = await program.methods
34+
.createTokenMint(decimals, freezeAuthority)
35+
.accounts({
36+
mintAuthority: payer.publicKey,
37+
mintAccount: mintAccount,
38+
})
39+
.signers([payer.payer, mintAccountKeypair])
40+
.rpc();
41+
42+
console.log('Transaction signature for createTokenMint:', tx);
43+
44+
const mintInfo = await getMint(provider.connection, mintAccount);
45+
46+
assert.equal(mintInfo.decimals, decimals, 'Mint account decimals should match specified value');
47+
assert.equal(mintInfo.mintAuthority?.toBase58(), payer.publicKey.toBase58(), 'Mint authority should be the payer');
48+
console.log('Mint created successfully:', mintAccount.toBase58());
49+
});
50+
51+
it('Mints tokens to the associated token account', async () => {
52+
const amount = new anchor.BN(1000 * 10 ** 9);
53+
54+
// Create an associated token account for the recipient
55+
recipientTokenAccount = await anchor.utils.token.associatedAddress({
56+
mint: mintAccount,
57+
owner: recipient.publicKey,
58+
});
59+
60+
const tx = await program.methods
61+
.mint(amount)
62+
.accounts({
63+
mintAccount: mintAccount,
64+
mintAuthority: payer.publicKey,
65+
recipient: recipient.publicKey,
66+
})
67+
.signers([payer.payer])
68+
.rpc();
69+
70+
console.log('Transaction signature for mint:', tx);
71+
72+
const recipientTokenAccountInfo = await getAccount(provider.connection, recipientTokenAccount);
73+
const mintedAmount = Number(recipientTokenAccountInfo.amount) / 10 ** 9;
74+
75+
assert.equal(mintedAmount, 1000, 'Minted token amount does not match');
76+
console.log('Recipient Token Account Balance:', mintedAmount);
77+
});
78+
});

0 commit comments

Comments
 (0)