Code Monkey home page Code Monkey logo

mem-sdk's Introduction

@decentldotland/mem-sdk

JS SDK for MEM Serverless Functions

Build Locally

git clone https://github.com/decentldotland/mem-sdk.git

cd mem-sdk

npm install && npm run build

Build size

  • dist/index.cjs.js 3.46 kB │ gzip: 1.07 kB
  • dist/index.umd.js 3.66 kB │ gzip: 1.17 kB
  • dist/index.es.js 4.74 kB │ gzip: 1.18 kB

Install

npm install mem-sdk

Usage Guide

Import MEM SDK

React, and other frameworks (also check out React Version):

import Mem from "mem-sdk";

const mem : Mem = new Mem({
  network: "mainnet" || "testnet"
});

Node (TS)

const { Mem } = require("mem-sdk");
const mem: Mem = new Mem({
  network: "mainnet" || "testnet"
});

Node (ES6)

import { Mem } from "mem-sdk"
const mem = new Mem({
  network: "mainnet" || "testnet"
});

Retrieve a function state

const FUNCTION_ID = "...";
const state = await mem.read(FUNCTION_ID);

Send an interaction (only works on server-side)

const FUNCTION_ID = "...";
const inputs = { function: "test", inputA: "valueA" };

const txid = await mem.write(FUNCTION_ID, inputs);

Deploy a function

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


if (input.function === "save") {
    const { username, bio } = input;

    ContractAssert(username.trim().length, "ERROR_INVALID_INPUT");
    ContractAssert(typeof username === "string" && typeof bio === "string");
    state.logs.push ({ username, bio });
    return { state }
  }
}`;

const INIT_STATE = '{"logs": []}';

const id = await mem.deploy(SRC, INIT_STATE);

Fork a function to mainnet or testnet

While the SDK should be initialized for mainnet using this method, the fork functionality allows forking mainnet deployed function to either mainnet or carbon testnet:

const MAINNET_FUNCTION_ID = "...";
const FORK_IT_TO = "mainnet" || "testnet";
const OVERWRITE_INIT_STATE = btoa('{"new": "state"}'); // optional

const id = await mem.fork(MAINNET_FUNCTION_ID, FORK_IT_TO, OVERWRITE_INIT_STATE?);

Get a function's KV object (only mainnet)

const MAINNET_FUNCTION_ID = "...";
const kv = await mem.kvGet(MAINNET_FUNCTION_ID);

Named functions resolving DX (only mainnet)

It is possible to assign a memorable name to a function ID using the function registry tool (e.g., ans.mem instead of Tih...I5M). These .mem names are resolvable with the MEM SDK the same way you would pass a function ID:

await mem.read("ans.mem");

License

This repository is licensed under the MIT License

mem-sdk's People

Contributors

charmful0x avatar maxknivets avatar xylophonez avatar

Forkers

maxknivets

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.