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

@decentraweb/dweb-live-cache

v0.1.4

Published

Ethereum address cache library for Decentraweb

Downloads

4

Readme

Live Cache for Decentraweb

One of the main features of Decentraweb is resolving domain names to Ethereum addresses and addresses to names. Normally, this is done by querying the Ethereum blockchain. In some cases, this can be slow, especially with batch operations. This package allows you to create Redis cache that will be updated automatically by listening on conract events.

Installation

npm install --save @decentraweb/dweb-live-cache
#OR
yarn add @decentraweb/dweb-live-cache

Instantiating the cache

To start using the cache, you need to instantiate it. The cache is a class that takes 3 parameters:

  1. provider - an instance of ethers.js provider
  2. redisUrl - a URL to the Redis server
  3. keyPrefix - refix that will be added to all Redis keys
import { DWEBIndex } from '@decentraweb/dweb-live-cache';
const provider = new providers.WebSocketProvider('wss://example.com/0000000000000000000000000000000', 'mainnet');
const dwebIndex = new DWEBIndex(provider, 'redis://localhost:6379', 'dweb-cache');

Seeding the cache

When starting new cache instance, you need to pre-populate it with initial data. This can be done by calling seed method:

import { DWEBIndex } from '@decentraweb/dweb-live-cache';
const provider = new providers.WebSocketProvider(WS_URL, ETH_NETWORK);
const dwebIndex = new DWEBIndex(provider, REDIS_URL, REDIS_PREFIX);
await dwebIndex.seedCache();

Listening for events

For each cache instance you need to have one process that will listen for events and update the cache. You can run live-cache-service app from our repo or write your own script that will instantiate the cache and call start method:

import { DWEBIndex } from '@decentraweb/dweb-live-cache';
const provider = new providers.WebSocketProvider(WS_URL, ETH_NETWORK);
const dwebIndex = new DWEBIndex(provider, REDIS_URL, REDIS_PREFIX);

dwebIndex.start().then(() => {
  console.log('Started processing eth blocks');
});

Notes:

  1. Make sure to have only one listener per cache instance.
  2. Receiving events is crucial for cache reliability. So if no block events received for 30 seconds, cache will throw an error as it means that connection to the blockchain is lost.

After calling start, cache will check which block was last processed and will start processing from that block until the latest one. Also cache class will subscribe to block event on contract. On each new block it will query DWEB PublicResolver and DefaultReverseResolver contract events to detect changes in Ethereum address assignment.

Using the cache

To query data, you can create multiple instances of DWEBIndex class. There are 2 main methods:

  1. resolveAddress(address: string, forceRefresh?: boolean): Promise<AddressResolution> - to resolve Ethereum address to name
  2. resolveName(name: string, forceRefresh?: boolean): Promise<string | null> - to resolve name to Ethereum address forceRefresh parameter is optional and can be used to force class to query the blockchain instead of using cached data.
import { DWEBIndex } from '@decentraweb/dweb-live-cache';
const provider = new providers.WebSocketProvider(WS_URL, ETH_NETWORK);
const dwebIndex = new DWEBIndex(provider, REDIS_URL, REDIS_PREFIX);

const address = await dwebIndex.resolveName('notyourbro');
// returns '0xcB3E45F337Dd3Beeba98F5F9F9A16e9cD152cC86'
const nameResult = await dwebIndex.resolveAddress('0xcB3E45F337Dd3Beeba98F5F9F9A16e9cD152cC86');
// returns { name: 'notyourbro', confirmed: true }

Note: Address to name resolution includes so-called "forward check". When user is creating reverse resolution record for their wallet, they can assign any random name. To confirm that the name is actually owned by the user, we resolve name to Ethereum address and check that it matches the address we are resolving. confirmed flag in result is indicating if forward check was successful.