mvx_cache_patterns_skill

This skill helps optimize MultiversX contracts by using drop-based write-back caches to minimize gas through selective reads and cached state.

10

GitHub Stars

1

Bundled Files

3 weeks ago

Catalog Refreshed

2 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 veilstart where the catalogue uses aiagentskills.

npx veilstart add skill multiversx/mx-ai-skills --skill mvx_cache_patterns

  • SKILL.md2.8 KB

Overview

This skill documents drop-based write-back cache patterns for MultiversX smart contracts to reduce gas costs when endpoints read multiple storage values. It provides a minimal, safe pattern that loads storage into an in-memory struct, mutates it during execution, and commits only on scope exit. The patterns include computed helper methods and selective write-back to avoid unnecessary storage writes.

How this skill works

On entry, the cache struct reads multiple storage entries into memory. Mutations happen on the in-memory fields. A Drop implementation writes changed fields back to storage when the cache goes out of scope, making intermediate reads free and reducing round-trip storage operations. Read-only variants skip Drop, and manual scoping is used to avoid crossing async boundaries.

When to use it

  • Endpoint reads 3 or more storage values in a single transaction
  • Complex mutative flows where many intermediate reads occur
  • View functions that read multiple values (use read-only cache without Drop)
  • When you want to minimize storage writes by committing only at the end
  • Before making async calls: ensure cache is dropped manually

Best practices

  • Only include fields that will be mutated in Drop; skip immutable config to save gas
  • Wrap cache usage in a limited scope so Drop always runs before async exits
  • Provide computed helper methods on the cache to derive values without extra storage reads
  • Explicitly document which fields are committed and which are read-only
  • Write tests verifying Drop commits expected fields and that no fields are forgotten

Example use cases

  • A deposit endpoint that reads total_shares, total_deposited, and user_balance and updates all three
  • A liquidation flow that needs multiple storage lookups and heavy intermediate math across entries
  • A view endpoint that computes exchange_rate from cached totals without committing anything
  • A batch update handler that aggregates many changes then writes back a small set of storage keys
  • Pre-async transfer logic: update balances, drop cache, then perform async external call

FAQ

Drop will not run when execution terminates via async_call_and_exit, so writes will never happen. Always scope the cache so it goes out of scope and drops before initiating async termination.

Can I cache immutable configuration fields?

You can read immutable config into the cache for convenience, but do not write them back in Drop. Excluding them from Drop saves gas and avoids accidental writes.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational