Pastebin clone

Build a basic CRUD dApp

A simple pastebin clone should allow anonymous users to easily publish and share plain text publicly, and browse text uploaded by others.

Building this on MEM has several advantages:

  • No need to deploy and manage API servers

  • Application state and all content is stored for free on Arweave forever

  • Web3-ready - plug in authentication, encryption, token-gating and more

View this example on GitHub

Pastebin MEM function

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

  if (input.function === "save") {
    const { username, data } = input;
    ContractAssert(
      username.trim().length && data.trim().length,
      "ERROR_INVALID_INPUT",
    );
    ContractAssert(typeof username === "string" && typeof data === "string");
    state.logs.push({ username, data });
    return { state };
  }
}
state.json
{ "logs": [] }

UI: writing a paste

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);
  }
}

UI: reading pastes

https://api.mem.tech/api/state/jBA874p2FtnLhDONzcphPgOp9Nzf5ly620BWQWf9rUI

Optional features supported by MEM

  • Add the option to authenticate using a wallet

  • Private pastes using encryption (coming soon)

  • Self-destruct using encryption (coming soon)

Last updated

mem.tech ยฉ 2023