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

stellar-resolve-claimant-predicates

v2.0.10

Published

Evaluate stellar claimant predicates

Downloads

309

Readme

Resolve stellar ClaimPredicates

Tests GitHub package.json version (develop)

Publish to npm NPM version

On the stellar network ClaimableBalances can used to send funds to another wallet that does not have a trust-line established to a given asset, yet. In addition to just sending funds, the validity can be limited using predicates.

These predicates are described by a time-constraint and can be nested using boolean operators (see https://stellar.github.io/js-stellar-base/Claimant.html for details).

The js-stellar-sdk lacks methods to resolve any given predicate to a specific timestamp.

This module aims at resolving claimable balance predicates to a specific timestamp so that stellar-clients can check if a ClaimableBalance can be claimed at a given time (i.e. when displaying). This can be used to filter out ClaimableBalances that are currently not claimable (e.g. expired or not yet claimable).

Usage

Installation

npm install -S stellar-resolve-claimant-predicates

Convert horizon response to xdr.ClaimPredicate

When querying horizon for claimable balances, the response is less easy to parse and process than the xdr.ClaimPredicate objects. For further processing just convert the response back to xdr.ClaimPredicate objects:

const { predicateFromHorizonResponse } = require('stellar-resolve-claimant-predicates');
server.claimableBalances
  .claimant('GB7U....NMRQ')
  .call()
  .then(({records}) => records.map(r => ({
    ...r,
    claimants: r.claimants
      // here the predicate is mapped from horizon-response format to xdr.ClaimPredicate
      .map(c => ({...c, predicate: predicateFromHorizonResponse(c.predicate)})
  })))
  .then(records => {
      // have the xdr.ClaimPredicates available inside the response here 
  });

The code above will replace the Horizon.Predicate data with xdr.ClaimPredicate data inside the response.

Getting information on a certain predicate

The PredicateInformation holds the information necessary to determine if a predicate can be claimed. The status field makes determination easy. validFrom & validTo only hold values if the original predicate defined 'before' (i.e. predicateBeforeAbsoluteTime(...)) or 'after' (i.e. predicateNot(predicateBeforeAbsoluteTime(...))) times.

{
    status: 'claimable' | 'expired' | 'upcoming';
    predicate: xdr.ClaimPredicate,
    validFrom?: number,
    validTo?: number,
}

To retrieve the information object simply call getPredicateInformation() with the predicate and optionally a Date object as of when the claimable balance is supposed to be claimed:

const info = getPredicateInformation(xdrClaimPredicate, new Date());

Resolve a predicate to a timestamp

Predicates can potentially cover different time-slots when a claimable balance is claimable. flattenPredicate() resolves the nested structure of predicates to a given timestamp (e.g. now) for easy inspection by leaving out irrelevant predicates. For example a predicate defining a claimable window of one hour can be resolved to three different predicates:

const beginOfClaimWindowPredicate = Claimant.predicateNot(Claimant.predicateBeforeAbsoluteTime("1637017200"));
const endOfClaimWindowPredicate = Claimant.predicateBeforeAbsoluteTime("1637020800");

// this defines a claimable time window of one hour
const claimPredicate = Claimant.predicateAnd(beginOfClaimWindowPredicate, endOfClaimWindowPredicate);

// a second before the claimable time window the predicate will be resolved only to the starting time 
flattenPredicate(claimPredicate, new Date(1637017199)) === beginOfClaimWindowPredicate;

// within the claimable time window the predicate will be resoved as is
flattenPredicate(claimPredicate, new Date(1637019000)) === claimPredicate;

// a second after the claimable time window the predicate will be resolved to the predicate describing the end
// of the claimable window only, as the start-time is irrelevant for an expired predicate
flattenPredicate(claimPredicate, new Date(1637020801)) === endOfClaimWindowPredicate;

Tests

To run the tests, simply execute them via npm:

npm test