Skip to content

Introduce a memory field to store arbitrary data #723

@michielmulders

Description

@michielmulders

Problem

A memory field in the global state of the CLI allows you to store data long-term.

When using the script blocks feature, you execute script blocks. However, the args field for script blocks get erased after each execution to keep them isolated and repeatable. But in some cases, you might want to store an account ID or contract ID for later reuse or reference. The same is true for data coming from Hardhat script execution.

Solution

Implementation:

  • stateController that can be accessed anywhere.
/**
 * Store data in memory
 * @param {string} key - The key to store the data under
 * @param {any} value - The value to store
 */
const saveToMemory = (key: string, value: any) => {
  const memory = state.get('memory');
  memory[key] = value;
  saveKey('memory', memory);
};

/**
 * Get data from memory
 * @param {string} key - The key to retrieve the data from
 * @returns {any} - The value stored under the key, or undefined if not found
 */
const getFromMemory = (key: string) => {
  const memory = state.get('memory');
  return memory[key];
};

Alternatives

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions