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

@instadapp/utils

v0.7.4

Published

[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href] [![Github Actions][github-actions-src]][github-actions-href] [![Codecov][codecov-src]][codecov-href]

Downloads

664

Readme

@instadapp/utils

npm version npm downloads Github Actions Codecov

Instadapp Utils

Usage

Install package:

# npm
npm install @instadapp/utils

# yarn
yarn add @instadapp/utils

# pnpm
pnpm install @instadapp/utils

Import:

// ESM
import {} from "@instadapp/utils";

// CommonJS
const {} = require("@instadapp/utils");

Examples

AbiFetcher

basic setup :

import { AbiFetcher } from "@instadapp/utils";

const abiFetcher = new AbiFetcher();

await abiFetcher.get("0x0000000000000000000000000000000000001010", "polygon");

with caching:

import { AbiFetcher } from "@instadapp/utils";
import NodeCache from "node-cache";

const abiCache = new NodeCache();

const abiFetcher = new AbiFetcher({
  cache: {
    get(key) {
      return abiCache.get(key);
    },
    set(key, value) {
      return abiCache.set(key, value, 10);
    },
  },
});

await abiFetcher.get("0x0000000000000000000000000000000000001010", "polygon");

with etherscan api keys (recommended)

import { AbiFetcher } from "@instadapp/utils";

const abiFetcher = new AbiFetcher({
  etherscanApiKey: {
    mainnet: "9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB",
  },
});

await abiFetcher.get("0xB8c77482e45F1F44dE1745F52C74426C631bDD52", "mainnet");

CastDecoder

basic setup :

import { CastDecoder } from "@instadapp/utils";

const castDecoder = new CastDecoder();

// using cast data
await castDecoder.getSpells(
  "0x9304c934000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009414156452d56322d410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000084ce88b439000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000071afd498d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "mainnet"
);

// using encodedSpells
await castDecoder.getSpells(
  {
    targets: ["AAVE-V2-A"],
    spells: [
      "0xce88b439000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000071afd498d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    ],
  },
  "mainnet"
);

// decode one encoded spell
await castDecoder.getSpell(
  "AAVE-V2-A",
  "0xce88b439000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000071afd498d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "mainnet"
);

retry

import { retry } from "@instadapp/utils";

retry(() => asyncCall(), {
  timeouts: [5_000, 10_000, 15_000], // timeouts for each retry attempt in ms
  delay: 300, // delay between retries in ms
});

wait

import { wait } from "@instadapp/utils";

await wait(300);

promiseTimeout

import { promiseTimeout } from "@instadapp/utils";

await promiseTimeout(promiseFn, 10_000);

JsonRpcRetryProvider

import { JsonRpcRetryProvider } from "@instadapp/utils";

const provider = new JsonRpcRetryProvider("https://rpc.ankr.io/eth");

const providerWithCustomOptions = new JsonRpcRetryProvider(
  "https://rpc.ankr.io/eth",
  {
    timeouts: [5_000, 10_000, 15_000], // timeouts for each retry attempt in ms
    delay: 300, // delay between retries in ms
  }
);
import { JsonRpcRetryProvider } from "@instadapp/utils";

const provider = new JsonRpcRetryProvider([
  "https://rpc.ankr.com/invalid",
  "https://rpc.ankr.com/invalid-2",
  "https://rpc.ankr.com/eth",
  "https://eth.llamarpc.com",
]);

JsonRpcRetryBatchProvider

import { JsonRpcRetryBatchProvider } from "@instadapp/utils";

const provider = new JsonRpcRetryBatchProvider("https://rpc.ankr.io/eth");

const providerWithCustomOptions = new JsonRpcRetryBatchProvider(
  "https://rpc.ankr.io/eth",
  {
    timeouts: [5_000, 10_000, 15_000], // timeouts for each retry attempt in ms
    delay: 300, // delay between retries in ms
  }
);

StaticJsonRpcRetryProvider

import { StaticJsonRpcRetryProvider } from "@instadapp/utils";

const provider = new StaticJsonRpcRetryProvider("https://rpc.ankr.io/eth");

const providerWithCustomOptions = new StaticJsonRpcRetryProvider(
  "https://rpc.ankr.io/eth",
  {
    timeouts: [5_000, 10_000, 15_000], // timeouts for each retry attempt in ms
    delay: 300, // delay between retries in ms
  }
);

Cache

import { Cache, ICacheDriver } from "@instadapp/utils";

Cache.extend("mongodb", {
  async get(key: string) {},
  async set(key: string, value: any, seconds?: number) {},
  async forget(key: string) {},
  async flush() {},
  // optional, and experimental
  lock(key: string, seconds: number) {
    return {
      async acquire() {
        return true;
      },
      async release() {},
    };
  },
});

// Note: you should set this once per life cycle
Cache.setDefault("mongodb"); // default is `memory` https://github.com/Instadapp/utils/tree/master/src/cache/drivers/index.ts

await Cache.get("key1");
await Cache.get("key1", "default");
await Cache.get("key1", async () => "default");

await Cache.put("key2");
await Cache.put("key2", "default");
await Cache.put("key2", async () => "default");

const seconds = 42;

await Cache.put("key2", "default");
await Cache.put("key2", "default", seconds);
await Cache.put("key2", async () => "default", seconds);

await Cache.pull("key3"); // get and forget
await Cache.forget("key1");
await Cache.flush();

const users = await Cache.remember("users", seconds, async () => {
  return await User.find();
});

await Cache.store("memory").get("users:1"); // safe way to switch driver for a moment

// experimental
const isLocked = await Cache.lock("key").get(async () => {
  console.log("do something");
});

// or
const isAcquired = await Cache.getLock("key", async () => {
  console.log("do something");
});

// or
const lock = Cache.lock("key");

if (await lock.get()) {
  console.log("do something");

  await lock.release();
}

// or
const lock = Cache.lock("key");

try {
  await lock.block(5);
  // Lock acquired after waiting a maximum of 5 seconds...
} catch {
  // Unable to acquire lock...
} finally {
  await lock.release();
}

// or
await Cache.block("key", 5, async () => {
  // Lock acquired after waiting a maximum of 5 seconds...
});

toJsonRpcProvider

import { toJsonRpcProvider } from "@instadapp/utils";
import { ethers } from "ethers";

let provider = toJsonRpcProvider(window.ethereum); // Metamask, Rabby, ...
let provider = toJsonRpcProvider(web3); // Web3.js instance, `web3.currentProvider` will be used internally
let provider = toJsonRpcProvider(web3.currentProvider); // Web3.js provider, ex:  Metamask, Rabby, WalletConnect, ...
let provider = toJsonRpcProvider("https://rpc.ankr.com/eth"); // Http RPC URL
let provider = toJsonRpcProvider(
  new ethers.providers.JsonRpcProvider("https://rpc.ankr.com/eth")
); // ethers JsonRpcProvider instance

Blockscan

import { Blockscan, Chain } from "@instadapp/utils";

const etherscan = new Blockscan(Chain.Mainnet);

await etherscan.getTransactions("0x6975be450864c02b4613023c2152ee0743572325");
await etherscan.getInternalTransactions(
  "0x6975be450864c02b4613023c2152ee0743572325"
);
await etherscan.getErc20TokenTransferEvents(
  "0x6975be450864c02b4613023c2152ee0743572325"
);
await etherscan.getErc721TokenTransferEvents(
  "0x6975be450864c02b4613023c2152ee0743572325"
);
await etherscan.getErc1155TokenTransferEvents(
  "0x6975be450864c02b4613023c2152ee0743572325"
);

const basescan = Blockscan.custom(
  "https://basescan.org",
  "https://api.basescan.org/api"
);
await basescan.contractSourceCode("0x833589fcd6edb6e08f4c7c32d4f71b54bda02913");

💻 Development

  • Clone this repository
  • Install dependencies using yarn install
  • Run interactive tests using yarn dev

License

Made with 💛

Published under MIT License.