Odak modu
The humble token - the original promise of the blockchain, and probably the main reason you installed a wallet. From synthetic stocks to the hundreds of dog coins, tokens are the purest representation of assets on the blockchain.
This lesson is all about how tokens work on Solana. If you’re familiar with other chains, there are likely some differences here, so try not to draw too many connections to how you currently think about tokens.
Talking about how tokens work in Solana is also a great opportunity to see how different programs use accounts. The deeper and deeper you get into Solana, the more important you see accounts have. They’re abstract and flexible like files in a file system, but that also means the accounts on any given program can get complex! It may be confusing starting out, but give it time and it’ll start to make more sense.
Tokens on Solana are made and managed using the Solana Token Program, one of the few programs in the Solana Program Library (SPL). Both regular tokens and NFTs are Solana program library tokens. We won't be working with NFTs today, but don't worry we'll get there soon.
We'll start by getting a lay of the land. There's three essential accounts necessary for the token program:
Let's dive into each account and see what it looks like on the inside.
The mint account stores metadata about the token itself, so not your ownership of the token, but the token more broadly. It has these properties:
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.
You've probably seen TONS of different tokens floating around the ecosysystem. Your wallet could be filled with tons of different ones right now. So, how does the network know you hold certain tokens? An account stores that data! The preferred way to do this is via an associated token account. Check it --
Here's what the data relationships and account properties look like.
The token account has to be associated with a user or a wallet. An easy way to do that is to create a PDA whose address links the mint account and the wallet. The seeds for the token account PDA are the address of the mint account and the wallet address (the token program ID is there by default).
There’s a lot of different information included, but for now it’s just important to know that your wallet doesn't actually hold the specific tokens. It's associated with a different account that you have that stores the number of tokens. Separately, there is a mint account that stores information about all of the tokens and the mint more broadly.
Spend some time staring at the graph and google the bits that don't make sense (like wtf is the associated token program?). After all the borsh stuff, this is easy!
Enough looking at graphs, let's look at some code to find out how this all happens.
To create a new SPL-Token you first have to create a Token Mint (the account that holds data about that specific token).
Think of it like baking a pizza. You need a recipe (the data about the token), the ingredients (the mint account and the address of the wallet), and someone to put it all together (derive a new PDA). Just like with a pizza, if you have all of the right ingredients and follow the recipe, you'll end up with a delicious new token!
Since the token program is part of the SPL, you can create the transaction pretty easily with the @solana/spl-token
Typescript SDK.
Here's what a createMint
call would look like:
You'd need these arguments:
connection
- the JSON-RPC connection to the clusterpayer
- the public key of the payer for the transactionmintAuthority
- the account which is authorized to mint new tokensfreezeAuthority
- 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 tokenOnce this is done, you'll be able to take the next steps:
Everything you need to make this happen is in the @solana/spl-token
SDK. Check out the docs here if you're curious about a specific part.
Most of the time, you won't need to create the raw transactions yourself, the SDK will be enough.
A cool sidenote on this -- if for some reason you wanna create the mint along with another instruction, you would want to create the instructions yourself and package them into one transaction to ensure that it all happens atomically. Maybe you're building a super secret token program where you want all tokens to be locked up right after mint without anyone being able to tranfer theirs.
Needless to say, there is a crazy amount of stuff thats happening around these tokens. You can check out what's happening with each of these functions under the hood here and even checkout some of the instructions around things like burning tokens :).
Now we go one step further and start using libraries. In the Introduction to Solana II section, how to rapidly adapt to the web3 ecosystem and start producing Tokens and NFTs by using Solana tools and libraries.
If you’re ready, we will cover the following in this course with hands-on tasks:
The written contents in this section are explained in detail on the basis of "SPL Tokens". As an additional resource, you can also watch Solandy's videos in here: The SPL Token Program
Yazılım Kariyerinde İlerlemeni Hızlandıracak Programlar
Patika+ programlarımız ile 4-8 aylık yoğun yazılım kamplarına katıl, temel bilgilerden başlayarak kapsamlı bilgiler edin, yazılım kariyerine başla!
Yorum yapabilmek için derse kayıt olmalısın!