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.3.0

Published

Client for Reflector subscriptions service

Downloads

153

Readme

@reflector/subscription-client

Client for Reflector Subscriptions service

Installation

npm i @reflector/subscription-client

Usage

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('SC...OI')) //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: 'GA...6D',  //address of the account that will own subscriptions
    signTransaction: sign, //tx signing callback
    rpcUrl: 'https://soroban-testnet.stellar.org/' //RPC URL
})

//create new subscription
const created = await sc.createSubscription({
    webhook: 'https://your.site/endpoint-that-will-receive-notifications',
    base: {asset: 'USD', source: 'CAB5NCTKZEET4NFI35VEQBO4N52C74HUHLZYMTQAOW7W3VOZM6XV2NU4'}, //base symbol
    quote: {asset: 'BTC', source: 'CAB5NCTKZEET4NFI35VEQBO4N52C74HUHLZYMTQAOW7W3VOZM6XV2NU4'}, //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": "CCPYWKODBPDCHCYJTQ3S5OSLXB6LAXQWN6FEOYUYMWXCX2ESYLHPAYMW",
    "events": [
      "ryA05D06CloUpBAQ0svM7lmk4FJvqJD/PIpO02BMRKc="
    ],
    "event": {
      "subscription": "595",
      "base": {
        "source": "CC4RBGIT7TGCGSZXO3BV2LD3IWHPVA5ER5HUVHKC2CDAV7GHXRRXGSUV",
        "asset": "CA2E53VHFZ6YSWQIEIPBXJQGT6VW3VKWWZO555XKRQXYJ63GEBJJGHY7"
      },
      "quote": {
        "source": "CC4RBGIT7TGCGSZXO3BV2LD3IWHPVA5ER5HUVHKC2CDAV7GHXRRXGSUV",
        "asset": "CDJF2JQINO7WRFXB2AAHLONFDPPI4M3W2UM5THGQQ7JMJDIEJYC4CMPG"
      },
      "decimals": 14,
      "price": "76000159215",
      "prevPrice": "76457567715",
      "timestamp": 1721871780000
    },
    "root": "F7F29BCMKes8JBox8MUkwZBtDa5jx+SkpnTkZDkV9ss="
  },
  "signature": "j74r4hipZFiAXhz6JOXdIMverJWMkKGFKXTO8AZ/RaABgU8/wBdSUEIeLCh3BhhlT+a2Okxcb3UksrzDC+71Ag==",
  "verifier": "GBE2YEDQJUD3LL5CXKUTOHD5FXHRWLPGLIKAOAUQ7LURPCA77RSMLIAV"
}