MEM
WebsiteLabsGuides
  • โš—๏ธMolecular Execution Machine
  • ๐Ÿ‘‹Introduction
    • What is MEM?
    • MEM features
    • MEM vs alternatives
    • Technical architecture
    • Use-case examples
  • โšกQuickstart
    • Quickstart guide
  • ๐Ÿ“–MEM Specifications
    • Function syntax
    • Function specs
    • Transaction specs
    • Supported languages
    • About MIPs
  • โš›๏ธMolecules
    • Overview
    • Multichain authentication
    • Using APIs
    • API reference
  • ๐ŸงชMEM IDE
    • Overview
    • Function ABI
  • โš’๏ธMEM Carbon Testnet
    • Overview
    • Deploy to testnet
    • Interact with testnet functions
  • โš™๏ธMEM SDK
    • SDK Usage
  • ๐Ÿ–ฅ๏ธMEM CLI
    • Function deployment
    • Function interaction
  • ๐Ÿ—๏ธMEM API
    • Overview
    • Write operations
    • Read operations
  • โš›๏ธ3EM-MEM
    • Overview
    • EXM API
    • Key-Value Storage (KVS)
  • ๐ŸงฉExamples
    • Pastebin clone
    • Query onchain data
Powered by GitBook

mem.tech ยฉ 2023

On this page
  • Arweave
  • NEAR
  1. Examples

Query onchain data

PreviousPastebin clone

Last updated 1 year ago

deterministicFetch() can be used to query data from any blockchain RPC. This is often used to verify that an onchain action has taken place in order to continue MEM function logic. This way, MEM can be deployed alongside smart contracts from any chain as a way to compute with data that would be otherwise impossible or uneconomical to do inside the onchain contract itself.

Arweave

Retrieve tag names and values for a given transaction ID. Refer to the and for documentation on other types of supported queries.

function.js

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

  if (input.function === "getArweaveTxData") {
    const res = await EXM.deterministicFetch("https://arweave.net/graphql", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },

      body: JSON.stringify({
        query: `
        {
          transactions(ids: ["G-1t0Lqysin897HC3IV8xu_Mr884B-Mo5YEnlhUH54k"]) {
            edges {
              node {
                id
                tags {
                  name
                  value
                }
              }
            }
          }
        }
        `,
      }),
    });
    state.data.push(res.asJSON());
  }
  return { state };
}

state.json

{
  "data": [],
  "publicFunctions": {
    "getArweaveTxData": []
  }
}

NEAR

Pull all transactions from a given block.

function.js

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

  if (input.function === "getblock") {
    const id = input.id;
    const txs  = [];
    const chunksRes = await EXM.deterministicFetch(
      "https://rpc.mainnet.near.org",
      {
        method: "POST",
        headers: {"Content-Type": "application/json"},

        body: JSON.stringify({
          jsonrpc: "2.0",
          id: "dontcare",
          method: "block",
          params: { block_id: Number(id)},
        }),
      },
    );

    const chunks = chunksRes.asJSON().result.chunks.map((chunk) => chunk.chunk_hash);
    
    for (const chunk of chunks) {
    const txsRes = await EXM.deterministicFetch(
      "https://rpc.mainnet.near.org",
      {
        method: "POST",
        headers: {"Content-Type": "application/json"},

        body: JSON.stringify({
          jsonrpc: "2.0",
          id: "dontcare",
          method: "chunk",
          params: { chunk_id: chunk},
        }),
      },
    );

    txs.push(txsRes.asJSON().result.transactions)
    }

    state.data.push({id: id, txs: txs.flat()})

    return { state };
  }
}

state.json

{
  "data": [],
  "publicFunctions": {
    "getblock": [
      "id"
    ]
  }
}
๐Ÿงฉ
Arweave GraphQL guide
playground