|
| 1 | +--- |
| 2 | +title: Mint Firelight Vault Shares |
| 3 | +tags: [intermediate, fassets] |
| 4 | +slug: firelight-vault-mint |
| 5 | +description: Learn how to mint Firelight vault shares |
| 6 | +keywords: [fassets, flare-network, fxrp, firelight, vault] |
| 7 | +sidebar_position: 3 |
| 8 | +--- |
| 9 | + |
| 10 | +import CodeBlock from "@theme/CodeBlock"; |
| 11 | +import FirelightMint from "!!raw-loader!/examples/developer-hub-javascript/firelight-mint.ts"; |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +This guide demonstrates how to mint vault shares in a Firelight vault by depositing assets. |
| 16 | +The Firelight vault implements the [ERC-4626](https://eips.ethereum.org/EIPS/eip-4626) standard, which allows users to deposit assets (e.g., FXRP) and receive vault shares in return. |
| 17 | + |
| 18 | +Minting shares is the process of depositing assets into the vault and receiving vault shares, which represent your proportional ownership of the vault's assets. |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit). |
| 23 | +- [Flare Network Periphery Contracts](https://www.npmjs.com/package/@flarenetwork/flare-periphery-contracts). |
| 24 | +- Understanding of [FAssets](/fassets/overview). |
| 25 | +- Sufficient asset balance (e.g., FXRP) to deposit into the vault. |
| 26 | + |
| 27 | +## Firelight Vault Mint Script |
| 28 | + |
| 29 | +The following script demonstrates how to mint vault shares by depositing assets into the Firelight vault: |
| 30 | + |
| 31 | +<CodeBlock language="typescript" title="scripts/firelight/mint.ts"> |
| 32 | + {FirelightMint} |
| 33 | +</CodeBlock> |
| 34 | + |
| 35 | +## Script Breakdown |
| 36 | + |
| 37 | +### 1. Script Setup |
| 38 | + |
| 39 | +The script starts by defining constants and importing the required contract interfaces: |
| 40 | + |
| 41 | +- `FIRELIGHT_VAULT_ADDRESS`: The address of the Firelight vault contract on the [Flare Testnet Coston2 network](/network/overview). |
| 42 | +- `SHARES_TO_MINT`: The number of shares to mint (default: 1 share). |
| 43 | +- `IFirelightVault`: The interface for interacting with the Firelight vault contract. |
| 44 | +Taken from the Firelight GitHub [repository](https://github.com/firelight-protocol/firelight-core/blob/main/contracts/FirelightVault.sol). |
| 45 | +- `IERC20`: Standard [ERC-20](https://eips.ethereum.org/EIPS/eip-20) interface for token interactions. |
| 46 | + |
| 47 | +### 2. Get Asset Information |
| 48 | + |
| 49 | +The script retrieves the underlying asset information to format amounts and calculate the required asset amount for minting shares from the vault: |
| 50 | + |
| 51 | +- **Asset address**: The token address that the vault accepts (e.g., FXRP). |
| 52 | +- **Asset symbol**: The token symbol for display purposes. |
| 53 | +- **Asset decimals**: The number of decimals used by the token. |
| 54 | + |
| 55 | +### 3. Calculate Shares to Mint |
| 56 | + |
| 57 | +The script converts the desired number of shares to mint into the correct units: |
| 58 | + |
| 59 | +```typescript |
| 60 | +const sharesToMint = SHARES_TO_MINT * (10 ** assetDecimalsNum); |
| 61 | +``` |
| 62 | + |
| 63 | +### 4. Check Maximum Mint Capacity |
| 64 | + |
| 65 | +Before minting, the script checks if the requested amount exceeds the maximum allowed: |
| 66 | + |
| 67 | +```typescript |
| 68 | +const maxMint = await vault.maxMint(account); |
| 69 | +``` |
| 70 | + |
| 71 | +The `maxMint` function returns the maximum number of shares that the specified account can mint. |
| 72 | + |
| 73 | +If the requested amount exceeds the maximum, the script exits with an error to prevent a failed transaction. |
| 74 | + |
| 75 | +### 5. Calculate Required Assets |
| 76 | + |
| 77 | +The script uses the `previewMint` function to calculate how many assets are needed to mint the desired number of shares: |
| 78 | + |
| 79 | +```typescript |
| 80 | +const assetsNeeded = await vault.previewMint(sharesToMint); |
| 81 | +``` |
| 82 | + |
| 83 | +The `previewMint` function is part of the [ERC-4626](https://eips.ethereum.org/EIPS/eip-4626) standard and returns the amount of assets required to mint a specific number of shares, accounting for the current exchange rate between assets and shares. |
| 84 | + |
| 85 | +### 6. Approve Token Transfer |
| 86 | + |
| 87 | +Before minting, the script must approve the vault to spend the required amount of assets: |
| 88 | + |
| 89 | +```typescript |
| 90 | +const approveTx = await assetToken.approve(vault.address, assetsNeeded, { from: account }); |
| 91 | +``` |
| 92 | + |
| 93 | +This approval is required because the vault needs to transfer assets from the user's account. |
| 94 | +The approval amount should match or exceed the `assetsNeeded` value calculated in the previous step. |
| 95 | + |
| 96 | +### 7. Mint Vault Shares |
| 97 | + |
| 98 | +Finally, the script mints the vault shares: |
| 99 | + |
| 100 | +```typescript |
| 101 | +const mintTx = await vault.mint(sharesToMint, account, { from: account }); |
| 102 | +``` |
| 103 | + |
| 104 | +The `mint` function: |
| 105 | +- Takes the number of shares to mint (`sharesToMint`). |
| 106 | +- Specifies the recipient address (`account`). |
| 107 | +- Transfers the required assets from the user to the vault. |
| 108 | +- Mints the corresponding number of shares to the recipient. |
| 109 | + |
| 110 | +## Running the Script |
| 111 | + |
| 112 | +To run the Firelight vault mint script: |
| 113 | + |
| 114 | +1. Ensure you have the [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit) set up. |
| 115 | +2. Update the `FIRELIGHT_VAULT_ADDRESS` constant with the correct vault address for your network. |
| 116 | +3. Adjust the `SHARES_TO_MINT` constant to the desired number of shares. |
| 117 | +4. Ensure your account has sufficient asset balance (e.g., FXRP) to cover the minting cost. |
| 118 | +5. Run the script using Hardhat: |
| 119 | + |
| 120 | +```bash |
| 121 | +npx hardhat run scripts/firelight/mint.ts --network coston2 |
| 122 | +``` |
| 123 | + |
| 124 | +## Output |
| 125 | + |
| 126 | +The script outputs the following information: |
| 127 | + |
| 128 | +```bash |
| 129 | +=== Mint vault shares (ERC-4626) === |
| 130 | +Sender: 0x0d09ff7630588E05E2449aBD3dDD1D8d146bc5c2 |
| 131 | +Vault: 0x91Bfe6A68aB035DFebb6A770FFfB748C03C0E40B |
| 132 | +Asset: 0x0b6A3645c240605887a5532109323A3E12273dc7 (FTestXRP, decimals=6) |
| 133 | +Shares to mint: 1000000 (= 1 share) |
| 134 | +Max mint: 115792089237316195423570985008687907853269984665640564039457584007913129639935 |
| 135 | +Assets needed (from previewMint): 1000000 |
| 136 | +Approve tx: 0xfe5683b82a4997df7d7b7c22c8c4dc416cdec8d1380dceeb996279c64c525460 |
| 137 | +Mint tx: 0x14d1c7ffe4f3b6a9fa04315eb8592ff9c64f2ae80c7e6e3a6e1b9cf9478106c3 |
| 138 | +``` |
| 139 | + |
| 140 | +## Difference Between Mint and Deposit |
| 141 | + |
| 142 | +The Firelight vault provides two ways to add assets: |
| 143 | + |
| 144 | +- **`mint`**: You specify the number of shares you want, and the vault calculates how many assets you need to deposit. |
| 145 | +- **`deposit`**: You specify the amount of assets to deposit, and the vault calculates how many shares you'll receive based on the current exchange rate. |
| 146 | + |
| 147 | +Both functions follow the [ERC-4626](https://eips.ethereum.org/EIPS/eip-4626) standard and result in the same outcome: you deposit assets and receive vault shares. |
| 148 | + |
| 149 | +## Summary |
| 150 | + |
| 151 | +In this guide, you learned how to mint vault shares in a Firelight vault by specifying the number of shares you want. |
| 152 | + |
| 153 | +:::tip[What's next] |
| 154 | + |
| 155 | +To continue your Firelight development journey, you can: |
| 156 | + |
| 157 | +- Learn how to [get Firelight vault status](/fassets/firelight/status) to monitor your position. |
| 158 | +- Learn how to [deposit assets into a Firelight vault](/fassets/firelight/deposit) by specifying the amount of assets to deposit. |
| 159 | +- Explore the [FAssets system overview](/fassets/overview) to understand the broader ecosystem. |
| 160 | + |
| 161 | +::: |
0 commit comments