npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@beanstalk/contract-storage

v1.1.0

Published

Retrieves data from arbitrary contract storage variables/mappings etc.

Downloads

32

Readme

EVM Contract Storage Tool

A lightweight tool for quickly retrieving arbitrary storage variables. View functions are no longer necessary to read every field.

✅ fields ✅ structs ✅ mappings ✅ arrays ✅ dynamic arrays

Sample Mapping

In Solidity, you might write something like:

uint256 stalk = s.s.stalk;
uint256 userPlotAmount = s.a[account].plots[index].amount;

To achieve this in JavaScript, now we can write:

const beanstalk = new ContractStorage(provider, contractAddress, storageLayout);
const stalk = await beanstalk.s.s.stalk;
const userPlotAmount = await beanstalk.s.a[account].plots[index].amount;

ContractStorage object

constructor(provider, contractAddress, storageLayout, defaultBlock = 'latest')

  • provider: an ethersjs provider, or anything having a getStorageAt(address, slot) function.
  • contractAddress: the address of the contract that you desire to retrieve storage for.
  • storageLayout: the storage layout mapping for your contract.
  • blockNumber (optional): the default block number to use for storage lookup. Uses the latest block if not provided.

__setDefaultBlock(block)

  • Changes the default block to be block.

Where to get the storageLayout mapping

After compiling the Solidity contract you want to analyze, included among the compilation artifacts is a JSON file containing a storageLayout property. The location of this file may vary depending on which compiler you are using. Once you have located the output file, find the storageLayout property for the desired contract. Copy the value into a JSON file. This file will be read into the program and supplied to the ContractStorage constructor. Some sample storageLayout mappings (for Beanstalk) can be found in this repository, but are not included in the published package.

Other features

If you want to read a different block, rather than changing the default block, the below syntax is also supported. This is extremely useful if you want to retrieve multiple slots at once using Promise.all(), where changing the default block could lead to a race condition.

const stalkAtBlock19m = await beanstalk[19000000].s.s.stalk;
const stalkAtBlock20m = await beanstalk[20000000].s.s.stalk;

If trying to read an entire array, rather than enumerating by length, we can write the below. This is significantly more performant than enumeration if the underlying data type can fit multiple entries in the same slot.

const allCases = await beanstalk.s.cases;

If you just want the slot number associated with a field, and not to retrieve its contents, you can instead write:

const userPlotSlot = beanstalk.s.a[account].plots[index].amount.slot;
// Compared to
const userPlotAmount = await beanstalk.s.a[account].plots[index].amount;

Future Work

For Diamond/Proxy contracts, the option to provide multiple storageLayout files corresponding to different block ranges would allow for more seamless analysis of contracts in the midst of protocol upgrades. Currently, a separate ContractStorage object would need to be constructed and this orchestration managed externally.