|
| 1 | +--- |
| 2 | +title: Instant Redeem from Upshift Vault |
| 3 | +tags: [intermediate, upshift, fassets] |
| 4 | +slug: instant-redeem |
| 5 | +description: Learn how to instantly redeem LP tokens from an Upshift vault |
| 6 | +keywords: [fassets, flare-network, fxrp, upshift, vault, redeem] |
| 7 | +sidebar_position: 3 |
| 8 | +--- |
| 9 | + |
| 10 | +import CodeBlock from "@theme/CodeBlock"; |
| 11 | +import UpshiftInstantRedeem from "!!raw-loader!/examples/developer-hub-javascript/upshift-instant-redeem.ts"; |
| 12 | + |
| 13 | +This guide demonstrates how to perform an instant redemption from an Upshift vault. |
| 14 | +The Upshift vault implements an [ERC-4626](https://eips.ethereum.org/EIPS/eip-4626) style tokenized vault that supports instant redemptions for immediate liquidity when available. |
| 15 | + |
| 16 | +Instant redemption burns your LP tokens (vault shares) and immediately returns the underlying assets to your wallet, subject to an instant redemption fee. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit) |
| 21 | +- [Flare Network Periphery Contracts](https://www.npmjs.com/package/@flarenetwork/flare-periphery-contracts) |
| 22 | +- Understanding of [FAssets](/fassets/overview) |
| 23 | +- LP tokens (vault shares) in the Upshift vault to redeem. |
| 24 | + |
| 25 | +## Upshift Vault Instant Redeem Script |
| 26 | + |
| 27 | +The following script demonstrates how to perform an instant redemption from the Upshift vault: |
| 28 | + |
| 29 | +<CodeBlock language="typescript" title="scripts/upshift/instantRedeem.ts"> |
| 30 | + {UpshiftInstantRedeem} |
| 31 | +</CodeBlock> |
| 32 | + |
| 33 | +## Script Breakdown |
| 34 | + |
| 35 | +The `main()` function executes the following steps: |
| 36 | + |
| 37 | +1. **Initialize:** Gets the user account and logs the vault and user addresses. |
| 38 | +2. **Connect to vault:** Creates an instance of the Upshift vault contract. |
| 39 | +3. **Get reference asset info:** Retrieves the vault's reference asset (FXRP) address, symbol, and decimals. |
| 40 | +4. **Get LP token info:** Retrieves the LP token address and the user's current LP balance. |
| 41 | +5. **Validate balance:** Converts the shares amount and checks if the user has sufficient LP tokens. |
| 42 | +6. **Preview redemption:** Calls [`previewRedemption()`](https://docs.upshift.finance/developer-docs/vault-contract-interface#id-8.-previewredeem-uint256-shares) to see the expected assets before and after the instant redemption fee. |
| 43 | +7. **Get asset balance:** Records the user's asset balance before redemption. |
| 44 | +8. **Execute instant redemption:** Calls [`instantRedeem()`](https://docs.upshift.finance/developer-docs/vault-contract-interface#id-4.-instantredeem-uint256-shares-address-receiveraddr-address-holderaddr) to burn LP tokens and receive assets immediately. |
| 45 | +9. **Verify redemption:** Confirms the redemption by checking the updated LP and asset balances. |
| 46 | + |
| 47 | +## Understanding Instant Redemption |
| 48 | + |
| 49 | +The Upshift vault supports two types of redemptions: |
| 50 | + |
| 51 | +- **Instant Redemption**: Immediately burns LP tokens and returns assets, but incurs an instant redemption fee. |
| 52 | +- **Requested Redemption**: Creates a withdrawal request that is processed after a lag period, with a lower fee. |
| 53 | + |
| 54 | +The instant redemption fee compensates the vault for providing immediate liquidity. |
| 55 | +The [`previewRedemption()`](https://docs.upshift.finance/developer-docs/vault-contract-interface#id-8.-previewredeem-uint256-shares) function shows both the gross and net amounts you'll receive. |
| 56 | + |
| 57 | +## Running the Script |
| 58 | + |
| 59 | +To run the Upshift vault instant redeem script: |
| 60 | + |
| 61 | +1. Ensure you have the [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit) set up. |
| 62 | +2. Update the `VAULT_ADDRESS` constant with the correct vault address for your network. |
| 63 | +3. Adjust the `SHARES_TO_REDEEM` constant to the desired number of shares. |
| 64 | +4. Ensure your account has sufficient LP tokens (vault shares) to cover the redemption. |
| 65 | +5. Run the script using Hardhat: |
| 66 | + |
| 67 | +```bash |
| 68 | +npx hardhat run scripts/upshift/instantRedeem.ts --network coston2 |
| 69 | +``` |
| 70 | + |
| 71 | +## Output |
| 72 | + |
| 73 | +The script outputs the following information about the instant redemption: |
| 74 | + |
| 75 | +```bash |
| 76 | +INSTANT REDEEM FROM VAULT |
| 77 | + |
| 78 | +Vault Address: 0x24c1a47cD5e8473b64EAB2a94515a196E10C7C81 |
| 79 | +User Address: 0x0d09ff7630588E05E2449aBD3dDD1D8d146bc5c2 |
| 80 | +Reference Asset (asset receiving): 0x0b6A3645c240605887a5532109323A3E12273dc7 |
| 81 | + |
| 82 | +LP Balance: 10.0 |
| 83 | +Shares to Redeem: 1 (1000000) |
| 84 | + |
| 85 | +Instant Redemption Fee: 1.0% |
| 86 | +Expected Assets (before fee): 1.0 FTestXRP |
| 87 | +Expected Assets (after fee): 0.99 FTestXRP |
| 88 | + |
| 89 | +Asset Balance Before: 50.0 FTestXRP |
| 90 | + |
| 91 | +Instant Redeem: (tx: 0x3f1bd8c766852d3b835bcde79f6d8e20afeeb227d737e0ed28d057dc0e6b2ba9 , block: 12345678 ) |
| 92 | + |
| 93 | +Verifying redemption... |
| 94 | +LP Balance After: 9.0 |
| 95 | +Shares Redeemed: 1.0 |
| 96 | +Assets Received: 0.99 FTestXRP |
| 97 | +``` |
| 98 | + |
| 99 | +## Summary |
| 100 | + |
| 101 | +In this guide, you learned how to perform an instant redemption from an Upshift vault by specifying the number of LP tokens (shares) to redeem. |
| 102 | +The instant redemption provides immediate liquidity but incurs a fee. |
| 103 | + |
| 104 | +:::tip[What's next] |
| 105 | + |
| 106 | +To continue your Upshift development journey, you can: |
| 107 | + |
| 108 | +- Learn more about [FAssets](/fassets/overview) and how the system works. |
| 109 | +- Learn how to [deposit assets into an Upshift vault](/fxrp/upshift/deposit). |
| 110 | +- Learn how to [request a redemption from an Upshift vault](/fxrp/upshift/request-redeem) for delayed liquidity. |
| 111 | +- Learn how to [claim assets from an Upshift vault](/fxrp/upshift/claim) after your requested redemption is claimable. |
| 112 | + |
| 113 | +::: |
0 commit comments