Quickstart guide

0 to 1 as fast as possible

The recommended path to developing an application with MEM is:

  1. Write a MEM function in JavaScript, leaning on examples and the syntax spec for guidance

  2. Test in the IDE or locally

  3. Integrate MEM with your UI using API calls

  4. Get your function whitelisted for mainnet

Writing MEM functions

The fastest and best-documented way to write MEM functions is in JavaScript. The IDE and examples repo has plenty you can test and fork for common backend patterns.

Here's the function.js for a counter:

export async function handle(state, action) {
  const input = action.input;

  if (input.function === "increment") {
    state.count += 1;
    return { state };
  }
  if (input.function === "decrement") {
    state.count -= 1;
    return { state };
  }
}

Here's its respective state.json, which holds global variables and the ABI:

{
  "count": 0,
  "publicFunctions": {
    "increment": [],
    "decrement": []
  }
}

Testing in the IDE

The MEM IDE is a web playground for you to write and test functions without needing to deploy them live.

Use the + button for a new blank slate or to choose one of the examples.

Each function consists of a function.js and a state.json -- the state holds global variables and the function ABI, which populates the interaction panel below the function code editor.

The IDE is powered by the mem-testnet package, which you can also run locally.

Optional: adding authentication methods

By default, functions in MEM are fully public and anonymous.

Developers can add web3 authentication via in-function calls to authentication molecules. These molecules accept a wallet address and a signature and return true/false based on whether the signature can be verifiably attributed to the caller.

Click here for an example of EVM authentication.

Deploying with the CLI

When you're happy with how the function runs in the IDE, you can use the CLI to deploy it to the MEM Carbon testnet.

Install the CLI:

npm i -g mem-cli-js

Deploy a function:

mem deploy --testnet --src function.js --init-state state.json

Copy the function ID from the CLI once deployed.

Mainnet functions must be whitelisted before they will accept interactions. This is a temporary safeguard while MEM is in beta. Use this form to submit a whitelist request. We recommend developers use the MEM Carbon Testnet by passing the --testnet flag to the CLI.

Integrating MEM with your front end

MEM functions can be read and written to from a front end using the MEM API.

async function writeFunction() {
  try {
    const inputs = [
      {
        input: {
          function: "save",
          username: "anon",
          data: "hello world",
        },
      },
    ];
    const functionId = "jBA874p2FtnLhDONzcphPgOp9Nzf5ly620BWQWf9rUI";

    const req = await axios.post(
      "https://api.mem.tech/api/transactions",
      {
        functionId: functionId,
        inputs: inputs,
      },
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    console.log(req?.data);
    return req?.data;
  } catch (error) {
    console.log(error);
  }
}

Get extra help

Reach out to us on Telegram and Discord, or book a call to discuss.

Last updated

mem.tech ยฉ 2023