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

@waves/node-api-grpc

v0.0.4

Published

A client for Waves Node gRPC and Blockchain Updates

Downloads

34

Readme

About

npm version

A client for Waves Node gRPC and Blockchain Updates.

How to use

Npm package: @waves/node-api-grpc.

We use:

  • @grpc/proto-loader to load proto-files and the embedded app proto-loader-gen-types to generate definitions;
  • @grpc/grpc-js to request the data from Waves Node gRPC API;
  • long.js to represent 64-bit integers: int64, uint64, etc.

Examples

  1. npm install --save @waves/node-api-grpc bs58

    bs58 here for encoding and decoding addresses and ids.

  2. default usage with TypeScript looks like:

    import * as w from '@waves/node-api-grpc'
    import b58 from 'bs58'
       
    const grpcChannel = w.grpc.mkDefaultChannel('grpc.wavesnodes.com:6870')
       
    // Node gRPC API - a streaming example
    const transactionsApi = w.api.waves.node.grpc.mkTransactionsApi(grpcChannel)
    const txnId = '287XcMXPDY7pnw2tECbV86TZetPi2x9JBg9BVUsGaSJx';
    transactionsApi
        .getTransactions({transactionIds: [b58.decode(txnId)]}) // see TransactionsRequest
        .on("data", (item: w.api.waves.node.grpc.TransactionResponse) => console.log(`[getTransactions] The transaction '${txnId}' was on height of ${item.height}`))
        .on("end", () => console.log("[getTransactions] Stream ended"))
        .on("error", (e: Error) => console.error("[getTransactions] Failed", e))
       
    // Node gRPC API - an one-shot example
    const accountsApi = w.api.waves.node.grpc.mkAccountsApi(grpcChannel)
    const alias = 'likli'
    accountsApi.resolveAlias(
        {value: alias}, // Accepts google.protobuf.StringValue, that has "value" field
        (error, response) => {
            if (error === null) {
                const addressBytes = response?.value || new Buffer(0);
                console.log(`[resolveAlias] The address of '${alias}' is ${b58.encode(addressBytes)}`)
            } else console.error(`[resolveAlias] Can't determine address of '${alias}'`, error)
        }
    )
      
    // Blokchain updates gRPC API example
    // Note, we have to do another connection
    const blockchainUpdatesChannel = w.grpc.mkDefaultChannel('grpc.wavesnodes.com:6881') // 6881 instead of 6870
         
    const blockchainUpdatesApi = w.api.waves.events.grpc.mkBlockchainUpdatesApi(blockchainUpdatesChannel)
    blockchainUpdatesApi.getBlockUpdate(
        {height: 1},
        (error, response) => {
            if (error === null) {
                const txnIds = (response?.update?.append?.transactionIds || []).map(x => b58.encode(x));
                console.log(`[getBlockUpdate] Transactions of block 1: ${txnIds.join(", ")}`)
            } else console.error(`[getBlockUpdate] Can't get transactions of block 1`, error)
        }
    )

    With JavaScript looks similar:

    const w = require('@waves/node-api-grpc');
    const b58 = require('bs58');
       
    const channel = w.grpc.mkDefaultChannel('grpc.wavesnodes.com:6870')
       
    const transactionsApi = w.api.waves.node.grpc.mkTransactionsApi(channel)
    const txnId = '287XcMXPDY7pnw2tECbV86TZetPi2x9JBg9BVUsGaSJx';
    transactionsApi
        .getTransactions({transactionIds: [b58.decode(txnId)]})
        .on("data", (item) => console.log(`[getTransactions] The transaction '${txnId}' was on height of ${item.height}`))
        .on("end", () => console.log("[getTransactions] Stream ended"))
        .on("error", (e) => console.error("[getTransactions] Failed", e))

Types and API clients correlates with a structure of proto-files. For example:

  • waves/node/grpc/transactions_api.proto relates to waves.node.grpc:
    package waves.node.grpc;
  • It has the TransactionResponse message:
    // waves/node/grpc/transactions_api.proto
    message TransactionResponse {
  • So we have waves.node.grpc.TransactionResponse;
  • We used w.api.waves.node.grpc.TransactionResponse in the example.

If you want to create a client of API that isn't listed in the example, you need:

  1. Find it among proto-files
  2. Write w.api.{here.is.a.namespace.of.your.api}mk{Api name}
  3. Then look at a method you are interested in: a. If you see stream in the response like:
    rpc GetTransactions (TransactionsRequest) returns (stream TransactionResponse)
    Then you need to register an event handler for "data" event (see the "Streaming" example) b. Otherwise, you need to provide only a callback (see the "One-shot" example)

How to build and test locally

$ npm run build && npm test