MEM IDE: A Toolkit for Web3 Serverless Functions

MEM IDE: A Toolkit for Web3 Serverless Functions

August 30, 2023

It’s only been 30 days since we launched Molecular Execution Machine, the web3-ready serverless functions platform, and we’ve been working to ship developer tooling to make it as easy as possible to write and test MEM contracts.

The MEM developer toolkit consists of a CLI, API and local testnet. Instead of having developers jump through hoops to get started, we’ve rolled these all into a single web app that saves setup time and just drops you into a playground environment that you can use instantly.

Meet the MEM IDE!

MEM IDE features

The MEM IDE features a helpful code editor, terminal output, and prefilled fields for sending interactions to your testnet contracts.

It’s super easy to get started. Just open the IDE, start editing the default example contract, and interact using the fields in the bottom pane. When creating a new project, you can choose from templates to scaffold contracts that use special features like deterministicFetch or signature authentication.

Anatomy of a MEM project

A MEM project consists of two files:

  • contract.js - the main body of source code
  • state.json - the object which stores contract state, global variables and the ABI

MEM contracts use familiar, easy-to-learn syntax. We think it’s best to learn from examples, like this pastebin clone or this guestbook contract in the MEM IDE. The tiny contract below is fully-formed:

The example above references its respective state.json, which contains a logs array and the ABI, denoted by the publicFunctions key:

Saving a new log to the contract state involves sending an interaction object to MEM like this:

{"username":"memfan88","data":"Hello, MEM!"}

In the IDE, this is done via the interact pane which reads functions and their arguments from the ABI (the publicFunctions object), providing easy JSON templates to fill with your test variables.

MEM example contracts

We’ve put together a set of examples you can paste straight into the IDE and play around with:

  1. Guestbook
  2. Counter
  3. Ethereum authentication
  4. Haiku generator
  5. CRUD

For more, including full production backends, check the MEM contract megathread!

MEM contract syntax

Go beyond examples and start writing your own. MEM uses syntax based on the SmartWeave standard.

  • The contract source must include a function named handle which can be either synchronous or asynchronous depending on your code logic. Also, it must have two arguments: state and action.

    • The state argument represents the current state of the contract.
    • The action argument must include the input field and it holds the arguments passed along with the contract interaction
  • The “input” field may contain the function property and any additional data required for the operation (depending on your contract logic).

The handle function must exit by either returning a result or state or by throwing an error.

  • Returning { state: new_state } to update the contract state.
  • Returning { result: new_result } to return a result.
  • Throwing an ContractError("ERROR_TEXT") exception.

When an error is thrown in the MEM contract, it immediately terminates the execution of the current function and any subsequent code within that function.

  • throw new ContractError("ERROR_TEXT") which is equivalent to throw new Error("") in JavaScript
  • ContractAssert(condition) which is equivalent to the assert statement in node:assert

LFG

If you’re not already registered for the MEM beta, head here to join and let’s talk about what you’re building. Resources: