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 🙏

© 2025 – Pkg Stats / Ryan Hefner

range-bet-math-core

v1.0.5

Published

Mathematical core library for Signals Breakout Programs protocol

Readme

range-bet-math-core

npm version License: ISC

A WebAssembly-powered library that implements the mathematical functions for the Signals Protocol. This package provides cost calculation functions for prediction market betting.

Installation

npm install range-bet-math-core
# or
yarn add range-bet-math-core

API Reference

| Function | Description | Parameters | Return Type | | ---------------------------- | ----------------------------------------- | ----------------------------------------------- | ----------- | | calculateBinBuyCost | Calculate cost to buy tokens in a bin | x: bigint, q: bigint, t: bigint | bigint | | calculateBinSellCost | Calculate revenue from selling tokens | x: bigint, q: bigint, t: bigint | bigint | | calculateMultiBinsBuyCost | Calculate cost for multiple bins | x: bigint, qs: BigUint64Array, t: bigint | bigint | | calculateMultiBinsSellCost | Calculate revenue for multiple bins | x: bigint, qs: BigUint64Array, t: bigint | bigint | | calculateXForMultiBins | Find max tokens purchasable within budget | budget: bigint, qs: BigUint64Array, t: bigint | bigint |

Where:

  • x: Amount of tokens to purchase/sell
  • q: Current token quantity in the bin
  • qs: Array of token quantities in multiple bins
  • t: Total token quantity in the market
  • budget: Available budget for purchasing tokens

Usage Examples

Basic Cost Calculation

import { calculateBinBuyCost } from "range-bet-math-core";

// Calculate purchase cost
const cost = calculateBinBuyCost(100n, 500n, 1000n);
console.log(`Purchase cost: ${cost}`);

Multiple Bins Calculation

import { calculateMultiBinsBuyCost } from "range-bet-math-core";

// Create bin array using BigUint64Array (required)
const bins = new BigUint64Array([300n, 400n, 500n]);

// Calculate purchase cost across multiple bins
const cost = calculateMultiBinsBuyCost(100n, bins, 1000n);
console.log(`Multiple bins purchase cost: ${cost}`);

Finding Maximum Purchasable Amount

import { calculateXForMultiBins } from "range-bet-math-core";

const budget = 10000n;
const bins = new BigUint64Array([300n, 400n, 500n]);

// Calculate maximum tokens purchasable with budget
const x = calculateXForMultiBins(budget, bins, 1000n);
console.log(`Maximum purchasable: ${x} tokens`);

React Integration Example

import React, { useState, useEffect } from "react";
import { calculateBinBuyCost } from "range-bet-math-core";

function BettingCalculator() {
  const [amount, setAmount] = useState(100);
  const [cost, setCost] = useState<string | null>(null);

  useEffect(() => {
    try {
      const calculatedCost = calculateBinBuyCost(BigInt(amount), 500n, 1000n);
      setCost(calculatedCost.toString());
    } catch (error) {
      console.error("Calculation error:", error);
    }
  }, [amount]);

  return (
    <div>
      <h2>Betting Cost Calculator</h2>
      <input
        type="number"
        value={amount}
        onChange={(e) => setAmount(Number(e.target.value))}
        min="1"
      />
      <div>
        <p>Purchase cost: {cost}</p>
      </div>
    </div>
  );
}

Next.js Integration Example

"use client"; // Important: only use in client components

import { useState, useEffect } from "react";
import { calculateBinBuyCost } from "range-bet-math-core";

export default function BettingComponent() {
  const [cost, setCost] = useState<string | null>(null);

  useEffect(() => {
    try {
      const calculatedCost = calculateBinBuyCost(100n, 500n, 1000n);
      setCost(calculatedCost.toString());
    } catch (error) {
      console.error("Calculation error:", error);
    }
  }, []);

  return (
    <div>
      <h1>Betting Cost</h1>
      {cost ? <p>Purchase cost: {cost}</p> : <p>Calculating...</p>}
    </div>
  );
}

Important Notes

  • All functions require BigInt inputs and return BigInt values
  • Arrays must be passed as BigUint64Array instances
  • This package is for client-side use (browsers); server-side requires additional configuration
  • Always implement error handling as invalid inputs will throw errors
  • Common error cases: q > t, division by zero, overflow in calculations

Mathematical Background

This library implements the $(q+t)/(T+t)$ integral formula:

$$\int_{t=0}^{x} \frac{q+t}{T+t} , dt = x + (q-T) \ln\left(\frac{T+x}{T}\right)$$

Where:

  • $q$: Current token quantity in the bin
  • $T$: Total token supply in the market
  • $x$: Token quantity to purchase

Note: While the API uses integer values (BigInt), the underlying WASM module performs floating-point calculations internally for accurate logarithmic operations.

For a detailed explanation of the mathematical model:

Additional Documentation

License

ISC