Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Latest commit

 

History

History
223 lines (133 loc) · 3.86 KB

04_spl_tokens.md

File metadata and controls

223 lines (133 loc) · 3.86 KB

🛹 SPL Tokens


tl; dr


  • SPL tokens are all non-native tokens on the Solana blockchain (fungible or non-fungible).

  • The Token Program has instructions for creating and interacting with SPL tokens:

    • Token Mints are accounts holding data about a specific token (but not the tokens).
    • Token Accounts are used to hold tokens of a particular token mint.
    • They both require allocating rent in SOL.



Getting started with spl-token-cli


  • Install with:

cargo install spl-token-cli



Creating a SPL Token


spl-token create-token

  • The address of the token will be printed out.


Getting the Token's Supply


spl-token supply <TOKEN ADDRESS>

  • To interact with any created token, we must create an account to store the token amount in our wallet.

  • If we create a token without an account, the above command will return 0.



Creating an Account


  • A Token Account holds tokens of a specific mint and owner:

spl-token create-account <TOKEN ADDRESS>

  • This command will print the address of the account created for the token, representing the wallet (account) holding the tokens in your wallet.

  • If using JavaScript's @solana/spl-token:

const tokenAccount = await createAccount(
  connection,
  payer,
  mint,
  owner,
  keypair
);

Checking the Balance of an Account


spl-token balance <TOKEN ADDRESS>

  • This command will print 0 if you haven't minted any tokens to that wallet.


Minting Tokens


  • Minting tokens is the process of issuing new tokens into circulation (by increasing the supply of the token and depositing the newly minted tokens into a token account).

  • A Token Mint is the account that holds data about a specific token:


spl-token mint <TOKEN ADDRESS> <NUMBER OF TOKENS TO BE MINTED>

  • If using JavaScript's @solana/spl-token, this would create a new account and initialize a new mint together:

const tokenMint = await createMint(
  connection,
  payer,
  mintAuthority,
  freezeAuthority,
  decimal
);


Transferring Tokens


  • Token transfers require both the sender and receiver to have token accounts for the mint of the tokens being transferred:

spl-token transfer <TOKEN ADDRESS> <NUMBER OF TOKENS TO BE MINTED> <RECIPIENT ADDRESS>

  • If using JavaScript's @solana/spl-token:

const transactionSignature = await transfer(
  connection,
  payer,
  source,
  destination,
  owner,
  amount
)

  • If the recipient is new (has no account), you can add --fund-recipient, as it's a requirement to deposit enough SOL for rent exemption when initializing a new account (getMinimumBalanceForRentExemptMint on @solana/spl-token)


Approving or Revoking Delegate


  • The process of authorizing another account to transfer or burn tokens from a token account.
    • The authority over the token account remains with the original owner.
    • The maximum amount of tokens a delegate may transfer or burn is specified at the time the owner of the token account approves the delegate.


Resources