multiversx-vault-pattern_skill

This skill helps you manage intermediate balances in multistep token operations with an in-memory ledger, reducing storage costs and improving efficiency.

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-vault-pattern

  • SKILL.md7.5 KB

Overview

This skill implements an in-memory token ledger pattern for tracking intermediate balances during multi-step operations inside a single transaction. It avoids storage writes by using a fast map for lookups and a vector for ordered iteration and settlement. Use it to safely aggregate, transform, and return token payments within atomic flows like swaps, batch processors, or multi-step DeFi logic.

How this skill works

The ledger pairs a ManagedMapEncoded for O(1) balance lookups with a ManagedVec that records insertion order for efficient settlement. You initialize the ledger from incoming payments, deposit and withdraw precise or percentage amounts, and remove zero balances to keep the set compact. At the end of the transaction the ledger converts non-zero balances into payment objects for transfer back to the caller or protocol.

When to use it

  • Building multi-step token flows executed in a single transaction (swaps, routing)
  • Aggregators or batch processors that transform tokens sequentially
  • Atomic swaps or cross-token operations where intermediate state must be tracked
  • When you need temporary balances only for the duration of one call (avoid storage)
  • When you want deterministic ordered settlement of remaining tokens

Best practices

  • Initialize ledger from call_value payments to capture all incoming tokens
  • Use ManagedMapEncoded for constant-time updates and ManagedVec for ordered settlement
  • Always remove zero balances when withdrawing to avoid gas waste during settle_all
  • Use NonZeroBigUint/Payment types for settlement to skip zero-amount transfers
  • Keep token sets small (typically < 10) to make O(N) removal acceptable

Example use cases

  • A DEX route executor: swap A→B→C in a single endpoint, tracking intermediate B without storage writes
  • A batch processor that collects many token payments, runs transformations, and returns leftovers atomically
  • An atomic swap contract that debits inputs, runs verification and conversion steps, then settles outputs to participants
  • A multi-step yield flow that stakes, harvests, and rebalances tokens inside one transaction

FAQ

The map gives O(1) balance reads/updates while the vector preserves insertion order for settlement. Together they provide efficient runtime behavior and ordered output.

Can this pattern handle partial withdrawals?

Yes. The ledger supports exact withdrawals, percentage (PPM) based withdrawals, and withdraw-all semantics. Percentage mode computes amounts safely using integer math.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
multiversx-vault-pattern skill by multiversx/mx-ai-skills | VeilStrat