Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social operations context helper functions #366

Open
gabehamilton opened this issue Nov 7, 2023 · 2 comments
Open

Social operations context helper functions #366

gabehamilton opened this issue Nov 7, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@gabehamilton
Copy link
Collaborator

gabehamilton commented Nov 7, 2023

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:

  • base64Decode
    Every indexer currently defines it (though with a lower case d).
  • getFunctionCalls(contract, method)
  • getSocialOperations(operation)

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
Copy link
Collaborator

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

@gabehamilton
Copy link
Collaborator Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants