near / queryapi

Near Indexing as a Service
17 stars 3 forks source link

Social operations context helper functions #366

Open gabehamilton opened 11 months ago

gabehamilton commented 11 months ago

The problem

Indexer functions would be easier to read if there were helper functions to extract common data.

Near-primitives solution

Full Example Indexer

block.socialOperations('index')
  .map(({accountId,data}) => {
    const like = data.like ? JSON.parse(data.like) : null;
    if(like) {
      const likedAccount = like.path.split('/')[0];  
      context.db.LikesByAccount.insert({
          account_id: accountId,
          liked_account: likedAccount,
      });
    }
  });

Original

Proposed Solution

Add the following context methods:

Decide whether to pass block in method signature.

Full Example Indexer

context.getSocialOperations('index').map(({accountId,data}) => {
  const like = data.like ? JSON.parse(data.like) : null;
  if(like) {
    const likedAccount = like.path.split('/')[0];  
    context.db.LikesByAccount.insert({
        account_id: accountId,
        liked_account: likedAccount,
    });
  }
});

Proposed code blocks

base64Decode(encodedValue)

    let buff = Buffer.from(encodedValue, "base64");
    return JSON.parse(buff.toString("utf-8"));

getFunctionCalls(contract, method)

block
    .actions()
    .filter((action) => action.receiverId === contract)
    .flatMap((action) =>
      action.operations
        .map(({FunctionCall}) => FunctionCall)
        .filter((operation) => operation?.methodName === method)
 .map((functionCallOperation) => ({
          ...functionCallOperation,
          args: base64Decode(functionCallOperation.args),
        }))

getSocialOperations(operation)

const contract = "social.near";
const method = "set";
getFunctionCalls(contract, method)
.filter((functionCall) => {
        const accountId = Object.keys(functionCall.args.data)[0];
        return functionCall.args.data[accountId][operation];
})
.map((functionCall) => {
  const accountId = Object.keys(functionCall.args.data)[0];
  return {
              accountId,
              data: functionCall.args.data[accountId][operation],
  }
);

Test Indexer

pkudinov commented 10 months ago

This does look like a bunch of methods to add to near lake primitives, correct?

gabehamilton commented 10 months ago

Yes, that would make the most sense. Maybe we can add them there and since they won't make the 0.1.1 version we're upgrading to soon, also patch block with them in this repo?