Skip to content

Instantly share code, notes, and snippets.

@thlorenz
Last active October 4, 2022 16:34
Show Gist options
  • Save thlorenz/0a4fd8afe81dc1ba81e63619ed5a77bf to your computer and use it in GitHub Desktop.
Save thlorenz/0a4fd8afe81dc1ba81e63619ed5a77bf to your computer and use it in GitHub Desktop.
Walk through the "Solanas Token Program Explained" article using _amman_ to label, inspect, etc.
#!/usr/bin/env bash
## Prepare Wallets
solana-keygen new --silent --no-bip39-passphrase --outfile solpair1.json
solana-keygen new --silent --no-bip39-passphrase --outfile solpair2.json
### Airdrop and label them
amman airdrop ./solpair1.json -l soladdr1
amman airdrop ./solpair2.json -l soladdr2
### Use first one as default wallet
solana config set --keypair `pwd`/solpair1.json
amman account soladdr1
## Token Creation
amman run -l token1 -l tx-create-token1 -- \
spl-token create-token --mint-authority ./solpair1.json
## Token Account Creation
amman run -l ata-tok1-addr1 -l tx-create-ata-tok1-addr1 -- \
spl-token create-account +token1 --owner +soladdr1
## Token Minting
amman run -l tx-mint-token1-to-ata-tok1-addr1 -t -- \
spl-token mint +token1 10 +ata-tok1-addr1 --mint-authority ./solpair1.json
## Token Transferring
### Create ATA for `soladdr2`
amman run -l ata-tok1-addr2 -l tx-ata-tok1-addr2 -- \
spl-token create-account +token1 --owner +soladdr2
### Transfer the token Soladdr2
amman run -l tx-transfer-token1-addr2 -t -- \
spl-token transfer +token1 1 +soladdr2 --owner ./solpair1.json

Solanas Token Program Explained

Walk through this article using amman to label, inspect, etc.

Prepare tools

npm install -g @metaplex-foundation/amman

Prepare Wallets

solana-keygen new --silent --no-bip39-passphrase --outfile solpair1.json 
solana-keygen new --silent --no-bip39-passphrase --outfile solpair2.json 

Airdrop and label them

amman airdrop ./solpair1.json -l soladdr1
amman airdrop ./solpair2.json -l soladdr2

Optionally label the "From Address" faucet

amman label faucet:AsK6qsN4HCi3e8GxZiCmPgvASyJU56kNSgNzNLXXXcvq

Use first one as default wallet

solana config set --keypair `pwd`/solpair1.json
solana config get

Check our Accounts

amman account soladdr1
amman run -- solana balance +soladdr1

Simple example of amman run and how it resolves labels marked with +

Token Creation

spl-token create-token --mint-authority ./solpair1.json
  • amman explorer now shows that transaction
  • let's label the created token and the transaction (note that we can copy the addresses from the command line or from the explorer)
amman label token1:B4Bexr8YcAfypaRjope8kZyWjnpp48WAA7bEakXsSdoD \ 
amman label tx-create-token1:3ka9HdVKF8MJLcirUa5szaYE1abnQjRtcPBFtxTc1pKs6g2qAdeVNkDHniYmqZG1VCsbPiPWR9Apqs6r8oqzKdRZ

Inspect Token

  • in explorer select it first inspect Token Mint and then Resolved Info
    • they overlap a bit, but Resolved Info will track states as they change
  • note that addresses have labels added when they exist
  • do the same from the command line
amman account token1
  • similar to solana account, but supports passing labels + resolves account info like the explorer does
  • token is owned (internal Solana relation) by the Token Program
  • mintAuthority is our soladdr1 (user-space relation)

Token Account Creation

amman run -l ata-tok1-addr1 -l tx-create-ata-tok1-addr1 -- \
  spl-token create-account +token1 --owner +soladdr1
  • prefixing the command with amman run allows us to pass labels indicated by + (think expand to address)
  • labeling ata-tok1-addr1 and the transaction as part of the command

Inspect Transaction

  • labels help a lot to see how sol were transfered, then the token account allocated and assigned to the Token Program
  • finally the account was initialized byt the Token Program

Inspect Token Account

  • we can inspect the created account via the amman CLI as well as in the explorer
amman account ata-tok1-addr1
  • token1 is the Mint (user-space)
  • Token Program is Owner (internal Solana relation)
  • soladdr1 i owner (user-space relation)

Token Minting

amman run -- spl-token balance +token1 --owner +soladdr1
  • shows 0 currently
amman run -l tx-mint-token1-to-ata-tok1-addr1 -t -- \
  spl-token mint +token1 10 +ata-tok1-addr1 --mint-authority ./solpair1.json
  • labeling transaction as part of command

Inspect Token Account and Token

  • inspect Token Balances and the only one instruction
  • ata-tok1-addr1 now shows 2 Resolved Info (mainly the amount changed)
  • confirm on CLI
amman account ata-tok1-addr1
  • same for token shows that supply now is 10.0000000 (9 decimals)
  • not sure why Current Supply above is 0
amman account token1

Inspect Owner Account

amman run -- spl-token accounts --owner +soladdr1
Token                                         Balance
---------------------------------------------------------------
7L2BuYfwZUqHQ2MjRRH2X9vqc8kHoiNjBZfaCp2kiMF6  10

Trying to mint with invalid mint authority

amman run -- spl-token mint +token1 10 +ata-tok1-addr1 --mint-authority ./solpair2.json
  • fails

Token Transferring

amman run -- spl-token transfer +token1 1 +soladdr2 --owner ./solpair1.json
  • fails since soladdr2 does not have the needed ATA yet
  • could create ATA or use --fund-recipient

Create ATA for soladdr2

amman run -l ata-tok1-addr2 -l tx-create-ata-tok1-addr2 -- \
  spl-token create-account +token1 --owner +soladdr2
  • inspect ata-tok1-addr2 (has no tokens yet)

Transfer the token Soladdr2

amman run -l tx-transfer-token1-addr2 -t -- \
  spl-token transfer +token1 1 +soladdr2 --owner ./solpair1.json
  • labeling transaction only as part of command
  • looking at accounts for that transaction we see that soladdr2 was not sent along, but instead the derived ATA
  • inspect ata-tok1-addr2 (which now has token)
  • find soladdr2 in explorer and inspect Tokens which now shows 1 token1
  • soladdr1 has 9 now
  • token1 Distribution confirms this and also shows Owners (internal Solana relation)
  • (not sure why token1 Transfers shows nothing)
  • amman account shows a summary of all accounts created
  • amman run -- spl-token accounts --owner +soladdr2 shows token balance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment