Flash Swaps
Flash Swaps in the Neptune Protocol allow users to atomically swap their collateral or debt positions without needing to manually unwind and rebuild their margin accounts. By leveraging Neptune's flash loan infrastructure, Flash Swaps execute complex multi-step operations in a single transaction, ensuring that users can efficiently restructure their positions with minimal risk and no intermediate exposure.
How Flash Swaps Work
Flash Swaps utilise Neptune flash loans to temporarily access the liquidity needed to restructure a user's margin account. The entire process is atomic, meaning all steps either succeed together or the transaction is fully reverted, protecting both the user and the protocol.
Neptune supports two types of Flash Swaps:
Collateral Swaps: Change the collateral asset in a margin account from one asset to another
Debt Swaps: Change the debt asset in a margin account from one asset to another
Both operations require the user to have an active margin account on Neptune and to have granted the Flash Swap contract authorization to execute operations on their behalf.
Collateral Swaps
A collateral swap allows a user to replace the collateral backing their margin account with a different asset, all within a single transaction. This is useful when a user wants to rotate into a more favourable collateral type, reduce exposure to a specific asset, or take advantage of yield opportunities without closing their borrowing position.
Collateral Swap Flow
Initiate Flash Loan: The user initiates a flash loan from Neptune Market for the target collateral asset
Deposit New Collateral: The flash-loaned target asset is deposited as collateral into the user's margin account
Withdraw Old Collateral: The original collateral is withdrawn from the user's margin account
Swap Assets: The withdrawn collateral is swapped on the DEX for the target asset
Repay Flash Loan: The flash loan is repaid from the swap proceeds, including all applicable fees
Verify Position: The protocol verifies the margin account remains healthy with the new collateral
Supported Collateral Paths
Flash Swaps support multiple collateral types and automatically handle the conversion paths between them:
Token to Token: A direct swap on the DEX (e.g., INJ to USDT)
Token to nToken: A DEX swap followed by lending the target asset to receive the nToken (e.g., INJ to nUSDT)
nToken to Token: Redeeming the nToken for the underlying asset, then swapping on the DEX (e.g., nINJ to USDT)
nToken to nToken: Redeeming the source nToken, swapping the underlying assets, and lending to receive the target nToken (e.g., nINJ to nUSDT)
Same Underlying Conversions: When the source and target share the same underlying asset, no DEX swap is required. The contract converts directly by redeeming or lending (e.g., INJ to nINJ or nINJ to INJ)
Debt Swaps
A debt swap allows a user to change the asset they have borrowed, without needing to source the repayment asset externally. This is useful when a user wants to switch their debt to a lower interest rate asset, reduce exposure to a volatile borrowed asset, or restructure their position to optimize account health.
Debt Swap Flow
Initiate Flash Loan: The user initiates a flash loan for the target debt asset
Swap to Source Debt: The flash-loaned target asset is swapped on the DEX for the current debt asset
Repay Original Debt: The original debt is repaid in the user's margin account
Borrow Target Asset: The target asset is borrowed against the user's existing collateral
Repay Flash Loan: The newly borrowed target asset is used to repay the flash loan, including all applicable fees
Debt swaps are currently supported for native tokens only.
Accessing Flash Swaps
NEPT Staking Requirement
Flash Swaps are built on top of Neptune's flash loan infrastructure and therefore require the same access. Users must have NEPT tokens staked in Staking Pool 3 to access flash loans. The maximum value a user can utilize in a Flash Swap is determined by the value of their staked NEPT and the Flashloan Multiplier. See NEPT Token for details on staking requirements.
Authz Grants
Users must grant the Flash Swap contract authorization to execute Neptune Market operations on their behalf. This is done through Injective's authz module, which allows the contract to deposit, withdraw, borrow, and repay assets in the user's margin account during the atomic swap process. The grant provided to the contract is used exclusively to execute flash swap transactions.
Slippage Protection
When estimating flash swap execution, output amounts are determined by the available prices and liquidity of Injective CLOB exchange. Large spreads and inefficient markets can contribute to losses when executing swaps. Users can specify a slippage tolerance parameter when initiating a flash swap, which sets the minimum acceptable output from the DEX swap. If the swap output falls below this threshold, the entire transaction reverts, protecting the user from unfavourable price movements.
Minimum Swap Quantities
Due to the functionality and parameter design of the Injective CLOB exchange, every swappable asset is limited by;
minimum trade quantity (the amount which is being swapped)
minimum notional value of a swap (value of tokens being swapped, typically $1 minimum)
The Flash Swap contract accounts for these minimums and rounds tokens values where needed to meet the minimum execution parameters for spot market swaps. This rounding can cause dust to accumulate in a users margin account and ensures swaps can execute without failure.
Limitation on minimum execution are usually only an issue when attempting to swap very small amounts of tokens.
Fees
Flash Swaps incur the following fees during execution:
Flash Loan Fee: A fee charged by Neptune Market for accessing the flash loan (typically 0.1%). See Flash Loans for details.
Protocol Fee: A fee charged by the Flash Swap contract on the swap output amount. This fee is sent to the protocol fee recipient.
DEX Swap Fee: Standard trading fees incurred when swapping assets on the decentralized exchange.
The protocol fee is calculated as:
The total cost to the user is the sum of all three fees, which are deducted from the swap proceeds before the final position is settled.
Safety Mechanisms
Flash Swaps incorporate several layers of protection to ensure the security of user funds and protocol solvency:
Atomic Execution
All steps in a Flash Swap execute within a single transaction
If any step fails, the entire operation reverts with no partial state changes
User positions are never left in an intermediate or vulnerable state
Reentrancy Protection
The contract enforces that only one swap operation can be in progress at a time
Concurrent swap attempts are rejected to prevent reentrancy attacks
Caller Validation
Flash loan callbacks are restricted to the Neptune Market contract
Internal processing messages can only be invoked by the Flash Swap contract itself
All callback data is validated before execution
Token Whitelisting
Only whitelisted tokens with verified configurations can be used in Flash Swaps
Each whitelisted token has a minimum swap amount configured to comply with DEX tick sizes
Pause Functionality
Protocol administrators can pause Flash Swap operations in the event of an emergency
Pausing prevents all new swap operations while existing state remains intact
Common Use Cases
Flash Swaps enable several strategies for managing positions on Neptune:
Collateral Rotation: Swap volatile collateral for stable assets to protect a margin account from liquidation, or swap into a higher-yield collateral to earn more on lending positions
Debt Optimization: Switch borrowed assets to take advantage of lower interest rates on a different asset, reducing ongoing borrowing costs
Risk Reduction: Replace concentrated collateral exposure with a more diversified or less volatile asset without closing the borrowing position
Yield Maximization: Convert standard token collateral into nToken collateral to earn lending yield while maintaining the same borrowing position
Last updated
