- Home
- Skills
- Multiversx
- Mx Ai Skills
- Multiversx Flash Loan Patterns
multiversx-flash-loan-patterns_skill
10
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill multiversx/mx-ai-skills --skill multiversx-flash-loan-patterns- SKILL.md7.2 KB
Overview
This skill implements the MultiversX atomic lend-execute-verify pattern for smart contracts. It provides a concise, secure template to lend assets, execute a callback on a target contract, and verify repayment (plus fee) within a single transaction. Use it to build flash loans, atomic swaps, and temporary grants that must be fully repaid before completion.
How this skill works
The pattern enforces a guarded flow: set a reentrancy guard, validate the target is on the same shard, validate the callback endpoint, record the contract balance, send assets and call the target, then re-check the balance to ensure repayment plus fee. If verification fails, the transaction reverts and no state persists. The template includes storage mappers, shard checks, endpoint validation, fee calculation, and guard lifecycle management.
When to use it
- Flash loans that must lend and be repaid atomically within one transaction
- Atomic swaps where you must verify the counterparty returned assets before finalizing
- Temporary grants where assets are lent for a callback and must be returned immediately
- Any operation that requires on-chain, same-shard atomicity for asset lending
- Avoid for simple transfers or cross-shard flows that cannot be atomic
Best practices
- Always set a reentrancy guard before sending assets and clear it only after verification
- Require the target contract to be on the same shard to guarantee atomicity
- Validate callback endpoints and disallow built-in functions that could bypass logic
- Check actual on-chain contract balance (get_sc_balance) rather than internal storage counters
- Compute and require fee repayment explicitly (e.g., fee basis points) to prevent loss
Example use cases
- A flash-loan provider lending ESDT tokens for arbitrage and verifying repayment plus fee in the same call
- An atomic swap contract sending tokens to a counterparty, invoking their execute endpoint, and ensuring tokens returned before completing
- A compute grant that lends tokens to run a gas-heavy callback and enforces immediate return
- A liquidation helper that temporarily supplies collateral to trigger a repay-and-return flow in one transaction
FAQ
Cross-shard calls execute asynchronously across blocks, breaking single-transaction atomicity and allowing state changes between send and verify.
What if the callback re-enters the operation?
A reentrancy guard prevents nested calls. Always set the guard before sending assets to avoid bypassing verification.