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

@reflector/subscription-client

v0.4.0

Published

Client for Reflector subscriptions service

Downloads

17

Readme

@reflector/subscription-client

Client for Reflector Subscriptions service

Installation

npm i @reflector/subscription-client

Usage

List tickers available for quotation

import {getAvailableReflectorTickers} from '@reflector/subscription-client'

const pubnetTickers = await getAvailableReflectorTickers('pubnet')
const externalTickers = await getAvailableReflectorTickers('exchanges')

console.log(pubnetTickers)
//['BTC', 'ETH', 'USDT', 'BNB', 'SOL', 'XRP', ...] 

Create subscription

import SubscriptionClient from '@reflector/subscription-client'
import {Keypair, Networks, TransactionBuilder} from '@stellar/stellar-sdk'

//callback for signing generated trasnactions
function sign(xdr, opts) {
    const tx = TransactionBuilder.fromXDR(xdr, opts.networkPassphrase) //parse a transaction from raw XDR
    tx.sign(Keypair.fromSecret('SC7L...8LOI')) //sign it
    return tx.toEnvelope().toXDR('base64') //return signed transaction
    //alternatively, you can call wallet here to sign the transaction
}

//create a new instance of the client
const sc = new SubscriptionClient({
    publicKey: 'GAD4...F06D',  //address of the account that will own subscriptions
    signTransaction: sign, //tx signing callback
    rpcUrl: 'https://rpc.url/' //RPC URL
})

//create new subscription
const created = await sc.createSubscription({
    webhook: 'https://your.site/endpoint-that-will-receive-notifications',
    base: {asset: 'AQUA:GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA', source: 'pubnet'}, //base symbol
    quote: {asset: 'SOL', source: 'exchanges'}, //quote symbol
    threshold: 3,  //trigger subscription if the price changed more than 0.3%
    heartbeat: 20, //send updates to the server once every 20 minutes regardless of the price changes
    initialBalance: '300' //deposit 300 tokens
})
console.log(created)
const subscriptionId = created.id

//retrieve subscription from the ledger
const fetched = await sc.getSubscription(subscriptionId)
console.log(fetched) //sholuld be identical to the previosuly created subscription

//estimate daily retention fee
console.log(await sc.getRetentionFee(subscriptionId))

//deposit additional tokens to the subscription balance
await sc.deposit(subscriptionId, '100')

//cancel the subscription and get back the remaining deposit balance
await sc.cancel(subscriptionId)

Notifications format

Once a subscription trigger conditions met, reflector nodes will send notifications to the address specified in the webhookUrl via HTTP POST request in JSON format. Each node signs and transmits the data independently, so the endpoint will receive several similar notifications with identical event descriptor, but different signatures.

Example of the POST notification received by the webhook endpoint:

{
  "update": {
    "contract": "CBNGTWIVRCD4FOJ24FGAKI6I5SDAXI7A4GWKSQS7E6UYSR4E4OHRI2JX",
    "events": [
      "B873JM7PV3/z+vZ4+qlCuiRI5WiSyJExLD9GcKUmtks=",
      "Wkc9HnBcMAGxrcAi2nqhst69F/vO31IhmVO7j41UyFU=",
      "ivg5kPjWFOt8Rwm1dT9VHc6FH00DI6zVbgGlgeao8fk="
    ],
    "event": {
      "subscription": "16",
      "base": {
        "source": "pubnet",
        "asset": "AQUA:GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA"
      },
      "quote": {
        "source": "exchanges",
        "asset": "SOL"
      },
      "decimals": 14,
      "price": "21749494669965161500",
      "prevPrice": "21688544256328711209",
      "timestamp": 1725578340000
    },
    "root": "O1hOLCVWIxaPWMvyfBvh+MOYh0TTMb8ekj8E1Ao4PfQ="
  },
  "signature": "FLS6e1auSAdPjDTlb/EKfrv2KvZ1juMRX4zaXCNQWyyjciN4h3kV5hkbfEdjVYTKZmOHaXBod+QFjZPIBEEuCA==",
  "verifier": "GCQTHGZAYIJB3SEMFJZABA7V7QIAJVRD72GHQAYNHHRBIMOMUXMWBPG3"
}