Skip to content

Commit 1be4dec

Browse files
authored
Token docs
* Token docs * Update with feedback
1 parent f4fffbf commit 1be4dec

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

docs/zkapps/advanced-snarkyjs.mdx

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,124 @@ A detailed explanation of the problem, and how actions can provide the solution,
296296

297297
We also have a [full code example](https://github.com/o1-labs/snarkyjs/blob/main/src/examples/zkapps/reducer/reducer.ts) which demonstrates this pattern. Leveraging `Reducer.reduce`, it takes only about 30 lines of code to build a zkApp which handles concurrent state updates.
298298

299+
## Custom Tokens
300+
301+
Mina Protocol supports the creation of custom fungible tokens. Custom fungible tokens play a significant role in decentralized applications and allow zkApps to create tokens that can be used in the zkApp ecosystem.
302+
303+
Custom tokens are typically governed by zkApp accounts, where zkApp accounts can mint/burn tokens and facilitate transfers between two separate accounts. You can interact with custom tokens using the SnarkyJS API. SnarkyJS contains built-in methods to send, burn, and authorize transactions between accounts.
304+
305+
:::info
306+
307+
The custom token API is currently under the `experimental` namespace. This means that the API is subject to change in future releases.
308+
309+
:::
310+
311+
### Custom Token API
312+
313+
This section shows you how to perform common token operations, such as minting, burning, and sending tokens, using SnarkyJS.
314+
315+
### Minting
316+
317+
Minting generates new tokens whereby the zkApp updates the balance of an account by adding the newly created tokens to it. Minted tokens can be sent to any existing account in the ledger.
318+
319+
To mint new tokens using a zkApp, we access the `this.experimental.token` property on our `SmartContract` class. See the example below of how a zkApp can mint tokens to another account:
320+
321+
```ts
322+
class MintExample extends SmartContract {
323+
...
324+
@method mintNewTokens(receiverAddress: PublicKey) {
325+
this.experimental.token.mint({
326+
address: receiverAddress,
327+
amount: 100_000,
328+
});
329+
}
330+
}
331+
```
332+
333+
In the snippet above, we define a smart contract called `MintExample` with a method called `mintNewTokens`. Using `this.experimental.token`, the smart contract specifies the address to mint new tokens for as well as the amount.
334+
335+
### Burning
336+
337+
Burning tokens is the opposite of minting. Burning tokens deducts the balance of a certain address by the specified amount. See the example below of how a zkApp can burn tokens of another account:
338+
339+
```ts
340+
class BurnExample extends SmartContract {
341+
...
342+
@method burnTokens(addressToDecrease: PublicKey) {
343+
this.experimental.token.burn({
344+
address: addressToDecrease,
345+
amount: 100_000,
346+
});
347+
}
348+
}
349+
```
350+
351+
In the snippet above, we define a smart contract called `BurnExample` with a method called `burnTokens`. Similar to minting, we use the `this.experimental.token` property to call the `burn` method. This specifies the amount of tokens to burn for the specified address.
352+
353+
:::note
354+
355+
A zkApp cannot burn more tokens than the specified account has. If there is such a case, an error is thrown and no such transaction is made.
356+
357+
:::
358+
359+
### Sending
360+
361+
To send a custom token, use the `send` method available on `this.experimental.token`. See the example below of how a zkApp can authorize sending tokens between two accounts:
362+
363+
```ts
364+
class SendExample extends SmartContract {
365+
...
366+
@method sendTokens(
367+
senderAddress: PublicKey,
368+
receiverAddress: PublicKey,
369+
amount: UInt64
370+
) {
371+
this.experimental.token.send({
372+
to: receiverAddress,
373+
from: senderAddress,
374+
amount,
375+
});
376+
}
377+
}
378+
```
379+
380+
In the snippet above, we define a smart contract called `SendExample` with a method called `sendTokens`. Then, in the same fashion, as minting and burning, we use the `this.experimental.token` property to call the `send` method.
381+
382+
For a more comprehensive example of how to use custom tokens with a zkApp, please see this [custom token example](https://github.com/o1-labs/snarkyjs/blob/main/src/lib/token.test.ts)
383+
384+
### Understanding Important Terms
385+
386+
If you are writing your zkApp to interact with custom tokens, you should understand the following essential terms:
387+
388+
#### Token Id
389+
390+
Token Ids are unique identifiers that are used to distinguish between different types of custom tokens. Custom token identifiers are globally unique across the entire network, meaning no two custom tokens can have the same token id.
391+
392+
Token Ids themeselves are derived from a zkApp. To check the token id of a zkApp, you can use the `this.experimental.token.id` property.
393+
394+
#### Token Accounts
395+
396+
Token accounts are like regular accounts, but they hold a balance of a specific custom token instead of MINA. A token account is created from an existing account but is instead specified by a public key _and_ token id.
397+
If an existing account receives a transaction that is specified by a custom token, a token account for that public key and token id will be created if it does not exist.
398+
399+
Token accounts are specific for each type of custom token, meaning that a single public key can have many different types of token accounts.
400+
401+
To create a token account for a specified custom token, a token account is automatically created for that public key whenever an existing account receives a transaction denoted with a custom token." to "A token account is automatically created for a public key whenever an existing account receives a transaction denoted with a custom token.
402+
403+
:::info
404+
405+
Suppose a token account is being created for the first time. In that case, an account creation fee must be paid similarly to creating a new standard account.
406+
407+
:::
408+
409+
Aside from sending custom tokens, custom tokens can be minted and burned by a **token owner account**. A token owner account is the governing zkApp account for a specific custom token.
410+
411+
#### Token Owner
412+
413+
A token owner is an account that creates, facilitates, and governs how a custom token is to be used. Concretely, the token owner is the account that created the custom token and is the only account that can mint and burn tokens.
414+
415+
In addition to being the only account that can mint and burn tokens, the token owner is also the only account that can authorize sending tokens between two accounts. If two accounts want to send tokens to each other, the token owner must authorize the transaction. The token owner can authorize the transaction either with a signature or proof.
416+
299417
## Merkle trees
300418

301419
:::info
@@ -333,12 +451,14 @@ A possible solution to that problem would be to utilize the power of Merkle tree
333451
Lets look at our data structure first. Imagine wanting to map a player's id to score points, so this:
334452

335453
```
454+
336455
0: 5 points
337456
1: 3 points
338457
2: 0 points
339458
3: 8 points
340459
... : ...
341460
7: 2 points
461+
342462
```
343463

344464
#### Implementing the smart contract

0 commit comments

Comments
 (0)