Mint Fungible Asset

On Solana, we use the mint authority PDA to mint new token.


use anchor_spl::{
associated_token::AssociatedToken,
token::{mint_to, Mint, MintTo, Token, TokenAccount},
};
#[derive(Accounts)]
pub struct MintToken<'info> {
#[account(mut)]
pub payer: Signer<'info>,
pub recipient: SystemAccount<'info>,
// Mint account address is a PDA
#[account(
mut,
seeds = [b"mint"],
bump
)]
pub mint_account: Account<'info, Mint>,
#[account(
init_if_needed,
payer = payer,
associated_token::mint = mint_account,
associated_token::authority = recipient,
)]
pub associated_token_account: Account<'info, TokenAccount>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,
}
pub fn handle_mint_token(ctx: Context<MintToken>, amount: u64) -> Result<()> {
let signer_seeds: &[&[&[u8]]] = &[&[b"mint", &[ctx.bumps.mint_account]]];
mint_to(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
MintTo {
mint: ctx.accounts.mint_account.to_account_info(),
to: ctx.accounts.associated_token_account.to_account_info(),
authority: ctx.accounts.mint_account.to_account_info(),
},
signer_seeds,
),
// Mint tokens, adjust for decimals
amount * 10u64.pow(ctx.accounts.mint_account.decimals as u32),
)?;
Ok(())
}


On Aptos, we retrive the mint ref from the fungible asset object and mint new fungible asset. Note how we retrive the decimals from the fungible asset metadata, on Aptos, all structs are private to the module they are defined in, so we need to use getter function decimals provided by the fungible asset module to retrive the decimals.


use aptos_std::math64;
use aptos_framework::object;
use aptos_framework::fungible_asset;
public entry fun mint_fa(
sender: &signer,
fa: object::Object<fungible_asset::Metadata>,
amount: u64
) acquires FAController {
let sender_addr = signer::address_of(sender);
let fa_obj_addr = object::object_address(&fa);
let config = borrow_global<FAController>(fa_obj_addr);
let decimals = fungible_asset::decimals(fa);
primary_fungible_store::mint(&config.mint_ref, sender_addr, amount * math64::pow(10, (decimals as u64)));
}