npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hexxagon/log-finder

v1.1.6

Published

Generic pattern matcher for Terra transaction logs

Downloads

9

Readme

@hexxagon/log-finder

Generic pattern matcher for Terra transaction logs

Installation

npm i @hexxagon/log-finder

Usage

Defining log rules

Any log pattern is defined like below:

export const createCW20TransferRule = (tokenAddress: string) => ({
  // type: must match with the log type
  type: 'from_contract',

  // attributes: list of attributes to be matched against log's key and value.
  // - index 0 and 1 are matched against key, value respectively.
  // - you can omit index 1 to match only using key; useful when the value is not static.
  // - you can also use callback as value `fn(string) => boolean`.
  attributes: [
    ['contract_address', tokenAddress],
    ['action', 'transfer'],
    ['from', (address) => address.startsWith('terra1')],
    ['to'],
    ['amount'],
  ],

  // (optional) forces log matcher to find an exact match; rule is no longer partial.
  strict: false,

  // (optional) matchUntil extends the matched chunk until another attribute (with the key being matchUntil) is found
  matchUntil: '...',
});

By default, log finder will match all logs that contain the log rule. This allows your log rule to be partial to the logs you are looking for. This behaviour is useful in case of inter-contract execution; a contract A calling contract B, but you only know the specific log pattern emitted from contract B, and still want to match the logs.

You can also define an optional strict flag, forcing the log matcher to find an exact occurrence of the rule in the log.

Callback pattern

Callback pattern is the default usage. Callback pattern is useful when you need to some transformation of the found logs as soon as they are found.

Note that the callback function is not async-able.

// create a callback log finder
const logFinder = createLogFinder(
  // provide your rule
  createCW20TransferRule('terra1....'),

  // callback function to be called every time there is an occurrence
  // found: the entire log fragment that triggered callback fn
  // match: attributes that matched with the pattern you provided
  (found, match) => {
    // ... do something
  },
);

logFinder({
  type: 'from_contract',
  attriutes: [
    { key: '...', value: '...' },
    { key: '...', value: '...' },
  ],
});

Return pattern

Returning pattern is a thin wrapper around callback pattern log finder. It allows you to treat log finder result as array of occurrences.

// use createReturningLogFinder for this usage
const logFinder = createReturningLogFinder(
  // provide your rule
  createCW20TransferRule('terra1...'),

  // optional transformer function; whatever is returned from this function will be
  // included as `transformed` in the result
  (found, match) => {
    // return something
  },
);

const result = logFinder({
  type: 'from_contract',
  attriutes: [
    { key: '...', value: '...' },
    { key: '...', value: '...' },
  ],
});

License

This software is licensed under the MIT license. See LICENSE for full disclosure.

© 2021 Terraform Labs, PTE.