-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode for Solana AI Coin
76 lines (65 loc) · 2.31 KB
/
Code for Solana AI Coin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint,
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
pubkey::Pubkey,
};
// Define the token's metadata
const TOKEN_NAME: &str = "Solana AI Coin";
const TOKEN_SYMBOL: &str = "SAIC";
const DECIMALS: u8 = 9;
// Define the token's accounts
struct TokenAccounts {
mint: Pubkey,
alice: Pubkey,
bob: Pubkey,
}
// Initialize the token's accounts
fn init_token_accounts(accounts: &[AccountInfo]) -> Result<TokenAccounts, ProgramError> {
let mint_account_info = next_account_info(accounts)?;
let alice_account_info = next_account_info(accounts)?;
let bob_account_info = next_account_info(accounts)?;
Ok(TokenAccounts {
mint: *mint_account_info.key,
alice: *alice_account_info.key,
bob: *bob_account_info.key,
})
}
// Create a new token mint
fn create_mint(accounts: &[AccountInfo], token_accounts: &TokenAccounts) -> Result<(), ProgramError> {
let mint_account_info = next_account_info(accounts)?;
let rent_sysvar_account_info = next_account_info(accounts)?;
let token_program_account_info = next_account_info(accounts)?;
// Create a new token mint
**mint_account_info.try_borrow_mut_lamports()? += 1;
msg!("Create mint account");
Ok(())
}
// Mint tokens to Alice's account
fn mint_to_alice(accounts: &[AccountInfo], token_accounts: &TokenAccounts) -> Result<(), ProgramError> {
let alice_account_info = next_account_info(accounts)?;
let mint_account_info = next_account_info(accounts)?;
let token_program_account_info = next_account_info(accounts)?;
// Mint 100 tokens to Alice's account
**alice_account_info.try_borrow_mut_lamports()? += 100 * 10_u64.pow(DECIMALS as u32);
msg!("Mint 100 tokens to Alice");
Ok(())
}
entrypoint!(process_instruction);
fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
if instruction_data.len() != 1 {
return Err(ProgramError::InvalidInstructionData);
}
let token_accounts = init_token_accounts(accounts)?;
match instruction_data[0] {
0 => create_mint(accounts, &token_accounts),
1 => mint_to_alice(accounts, &token_accounts),
_ => Err(ProgramError::InvalidInstruction),
}
}