简体   繁体   中英

Conceptual difference between Token, Account and Associated account in Solana

I'm coming from an Ethereum environment and the Solana concepts are a bit confusing. I found various tutorials that guide thru the code steps, but don't really explain the concept behind the logic.

I understand that in Solana we've got Programs, that contain no data and are just logic - the executable entity in the network. And then we've got Accounts, which actually contain the data.

I've read thru the Solana docs and the Program library (eg. the Token Program ), but I've still got some blank spots of understanding.

I gotta set up an automated NFT minting pipeline and I'm a bit lost between the concepts of tokens, accounts and associated accounts.

Now, if I follow the Token Program tutorial (linked above), it will guide me thru:

  1. creating a mint
  2. creating (or getting) an associated account
  3. minting a token to that account

So, I'm wondering

  • once I create the mint, and search for it in Solscan, it opens an Account entity. So, the mint is actually not a token, but just an Account that holds data? (eg. here ). On the other hand I can actually find Token entities in Solscan. What's the difference?
  • this token (or account) doesn't have any metadata... literally nothing. It's like a data placeholder. Is it an actually minted token already or something half baked? If the latter, then do I have to attach the metadata afterwards?
  • what is the Associated Account for?
  • the SPL library has a mintTo function that is used in the above linked tutorial from the Solana docs. Is that the action of transferring a token to another wallet? The example actually transfers (mints) it to an Associated Account. What's the logic here?
  • if I use the public Metaplex Program to mint the tokens, and I deploy my own Anchor Program - is this program for adding the metadata to the minted tokens? What's the concept here, I don't get it.
  • When I've got an NFT ready (minted + metadata) and I want to transfer it to someone else's wallet - do I actually have to create an Associated Account for that wallet and then transfer the token to that Account? What's the underlying concept?

There's a lot of questions to cover here, so let's go through bit by bit.

The most important bit is the overloading of the term "account", which means many different things. An account in Solana is like a file in a normal operating system, and it can contain any data as defined by a program. The token program defines:

  • mint accounts, which handle the creation of new tokens
  • holding accounts (also sometimes called token accounts), which actually hold quantities of tokens, and are owned by someone

The crucial bit is that these are all accounts. So to get to your questions:

once I create the mint, and search for it in Solscan, it opens an Account entity. So, the mint is actually not a token, but just an Account that holds data? (eg. here). On the other hand I can actually find Token entities in Solscan. What's the difference?

In this example, JAf858mSrDuQuHQCVqfA3KN8PNaVxZokMVNAJiC3zMpr is the mint address (or the token type), and 4FLiMhW2Weagy8LjtMCVkFQkLrB3zmF2VUcJDq2NQcJN is the address of a user's token holdings for that token type. Solscan calls this a "token account".

this token (or account) doesn't have any metadata... literally nothing. It's like a data placeholder. Is it an actually minted token already or something half baked? If the latter, then do I have to attach the metadata afterwards?

Correct, the account at 4FLiMhW2Weagy8LjtMCVkFQkLrB3zmF2VUcJDq2NQcJN has no metadata, and neither does the mint JAf858mSrDuQuHQCVqfA3KN8PNaVxZokMVNAJiC3zMpr . The metadata is attached in a separate account, like in the Metaplex token metadata standard. In that model, you can attach it to a mint if you have the minting authority.

what is the Associated Account for?

The Associated Token Account is a standard defining a "canonical" account for holding tokens of a particular type for a given wallet, so that it's easier to find where to send tokens of a certain type for a user. That way, just with your wallet address of E5GvXygLz1AbCFqtcp14feEo8hy6YAv8wr74Xnpo1qBH , I know where to send tokens of any type / mint by deriving the associated token account. 4FLiMhW2Weagy8LjtMCVkFQkLrB3zmF2VUcJDq2NQcJN is that derived address for token mint JAf858mSrDuQuHQCVqfA3KN8PNaVxZokMVNAJiC3zMpr .

the SPL library has a mintTo function that is used in the above linked tutorial from the Solana docs. Is that the action of transferring a token to another wallet? The example actually transfers (mints) it to an Associated Account. What's the logic here?

mintTo creates totally new tokens, and puts them in a holding account, no transferring done at all.

if I use the public Metaplex Program to mint the tokens, and I deploy my own Anchor Program - is this program for adding the metadata to the minted tokens? What's the concept here, I don't get it.

You don't need a new program for the metadata -- the Metaplex Token Metadata program does that for you. It's a stateless program, like any other, that defines logic for how to write data to an account. In this case, that data is metadata for a token mint.

When I've got an NFT ready (minted + metadata) and I want to transfer it to someone else's wallet - do I actually have to create an Associated Account for that wallet and then transfer the token to that Account? What's the underlying concept?

That's the best practice. You transfer from one associated token account to another, and if the destination user's associated token account doesn't exist yet, you can create it using the associated token account program. It will create a new token account at the address, eg 4FLiMhW2Weagy8LjtMCVkFQkLrB3zmF2VUcJDq2NQcJN , and assign it to the correct owner, eg E5GvXygLz1AbCFqtcp14feEo8hy6YAv8wr74Xnpo1qBH .

You can find all of the token program logic for minting at https://github.com/solana-labs/solana-program-library/blob/7caf27cca6a9f58055f93517774318eb2b2f97bf/token/program/src/processor.rs#L516 and for transferring at https://github.com/solana-labs/solana-program-library/blob/7caf27cca6a9f58055f93517774318eb2b2f97bf/token/program/src/processor.rs#L222 and for the associated token account program at https://github.com/solana-labs/solana-program-library/blob/7caf27cca6a9f58055f93517774318eb2b2f97bf/associated-token-account/program/src/processor.rs#L66

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM