SPL Tokens [Solana]

Tokens with gage

  • solana programs are composable i.e most them sort use this thing call Factory Patten
  • so, this means - The Program will allow users to create Accounts and do special things with those accounts.

SPL Token program

  • if will look how the factory pattern works in context of SPL token program

  • SPL Token Program is the factory for Mint Accounts

  • From SPL Token Program users can create Mint Accounts(which will be set as authority of that account but still owned by SPL token program).

  • And from MInt Accounts users can create Token Accounts. Also, Mint Accounts gonna have the Accounts, Decimals, Meta Data

  • Mint Account is a factory for Token Accounts

  • With Token Accounts they can do things like Mint tokens , Transfer tokens and Burn Tokens

  • more on SPL Token Program factory pattern here
    SPL Token Program factory pattern

Tansfer Tokens

  • to transfer tokens users can ask SPL tokens to send their tokens(which they got authority on), to other users.

Tokens in context of NFTs

  • we have SPL tokens that are now tied to another program's account most commonly MetaPlex the Metadata program.

  • Non-Fungible tokens - An NFT is simply a token type where only a single token has been minted.

The Token Program

  • Tokens on solana are made and managed using Solana token program - one of few programs in Solana Program Library(SPL)
  • both regular tokens and NFTs are Solana program library tokens(SPL) tokens

Account relationships

  • 3 essential accounts for token program

1. wallet account: ur wallet 2. mint account : stores metadata about token mint. 3 .token account: tied to wallet and stores how many tokens the wallet has.

  • more here with the image
     3 essential accounts for token program

  • lets see all 3 one by one

1. Mint Account

  • owner of mint account is Token Program
  • the metadata it contains about the token are here as follows:
    • mint authority: the only account that can sign and mint tokens
    • freeze authority: the one person or program authority to freeze or mint.(to mint? is it kinda the same thing with mint authority?)
    • decimal: Decimals is how many decimal places do we allow tokens to be broken up into - the precision with our tokens. I guess what decimal with 2 mean is like you can send 32.03 , 1.23 its about decimals after the decimal point?
    • supply: how many tokens exist?
    • is initialized: it has to do with account not with token. like is this account ready to go?

_It’s pretty standard to set the mint authority to your wallet, mint everything you want, and then remove the mint authority to basically say no more supply will be issued in the future. Alternatively, if you have some kind of dynamic issuance of tokens, it would be common to put them into authority as a program that program manages the minting of tokens.

The freeze authority works the same way._

  • more in the image here:
    mint account

2. Token Accounts (Associated Token Accounts(ATAs))

  • should be associated with user or wallet. In order to achieve this you will need to create PDA(Program driven Account) which gonna link user/wallet and mint account to the token minted. And that’s gonna get as ATAs

  • the seeds for PDA obviously user wallet address and mint account, token program id is there by default.

  • more with picture here:
     Token Accounts (Associated Token Accounts(ATAs))

The token minting process

  • step one create a mint account

  • step two put up mint account and wallet address to derive PDA to get Associated Token Account.

  • you can easily do this with the sdk @solana/spl-token

  • here is what mint looks like

1
2
3
4
5
6
7
const mint = await createMint(
connection,
payer,
mintAuthority,
freezeAuthority,
decimal
);
  • connection - the JSON-RPCconnection to the cluster

  • payer - the keypair of the payer for the transaction

  • mintAuthority - the account which is authorized to mint new tokens

  • freezeAuthority - an account authorized to freeze the tokens in a token account. If you don’t wanna be able to freeze, set it to null!

  • decimals - specifies the desired decimal precision of the token

  • Once a mint account is created

  • create Associated Token Accounts

  • mint tokens to the Associated Token Account

  • then if u want with transfer instruction you can airdrop tokens to different accounts.

  • note: if u wanna mint a token with a different transaction instruction you can do that as well.

Mint tokens on Solana

  • Remember these steps:
    1. create a mint token account
    2. create associated account for the wallet
    3. you mint tokens to that wallet

Metaplex

  • Metaplex is a metadata store for solana
  • metadata could be image for your token, where that image is stored…
  • Metaplex is gonna handle all of the associated data for the Associated token account

Metaplex

  • So, from the image above we can see that:
    Metaplex has:
    1. Metadata Account: that’s gonna store metadata about token. This account is usually used for fungible tokens like USDC or an NFT(non fungible token) that has the same artwork. Contains the following metadataURI, Symbol , Price, Royalities, Creators
    2. Master Edition Account: is used for an NFT that has one type of artwork. Different piece of artwork. Also, it has a Collection token account which is to say hey this NFT belongs to this specific collection. Also, a Collection token account has Authority record account which lets a different account(other than a creator) to add NFTs to that specific Collection token account.

Token Metadata

  • metadata is set of data that describes about another data

  • so, token metadata is metadata about the token. like name, symbol , logo

  • On Solana, NFTs are just like other tokens but metadata defines them as NFTs via attributes such as decimals.

  • attaching metadata to token is done using Token Metadata Program. It does this by using PDAs that are derived from Mint accounts

Token Metadata Account

  • stores different info about the Mint token account

  • Notice the URI property on Metadata account? - it points to off-chain JSON file, mainly used for NFT. Since the JSON is off-chain you can store high quality medias.

  • Metadata Account

Token standard

  • the off-chain part follows Metaplex token standard.

  • we tell what Token Standard we’re using using Token Standard on-chain field on Token Metadata Account

  • Token standard options:

    1. NonFungible: a non-fungile token with Master Edition (NFTs)
    2. FungibleAsset: a fungible asset with metadata, that also have attributes. AKA, Semi-Fungible ex: game items
    3. Fungible: a fungible token with metadata. ex: (USDC, SOL, your own token)
    4. NonFungibleEdition: a non-fungible token with Edition Account.(printed from Master Edition. Like 1 out of 100)
  • Metaplex Token Standard is widely accepted and various exchanges needs you to conform to it.

  • Here is how it knows which Token Standard is being used:

    1. if token has Master Edition account it is NonFungible
    2. if token has Edition Account it is NonFungibleEdition
    3. if token has NO Master or Edition Account, ensuring its supply can be > 1, and uses 0 decimal places - it is FungibleAsset. Because if you think about it a good example could be game collectibles and you cannot divide those more than a whole number. You cannot have 2.31 game collectibles. They are represented by whole numbers only.
    4. if token NO Master or Edition Account, ensuring its supply can be > 1 and uses atleast 1 decimal places, - it is Fungible
  • Here is an example of Fungible token standard

  • fungible token standard

Metaplex SDK

  • One of the most used SDKs on Solana.

  • We’ll be using @metaplex-foundation/js and @metaplex-foundation/mpl-token-metadata to create metadata account associated with our token mint.

  • general flow for attaching metadata for FungibleToken could be sth like this:

  1. setup Metaplex SDK
  2. Upload image for the logo
  3. Upload the off-chain metadata
  4. Derive the Metadata Account PDA
  5. Create on-chain Metadata Token Account