Query onchain data

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 Arweave GraphQL guide and playground 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"
    ]
  }
}

Last updated

mem.tech ยฉ 2023