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

@pooltogether/draw-calculator-js

v1.0.3-beta.4

Published

PoolTogether Draw Calculator

Downloads

272

Readme

PoolTogether Draw Calculator JS

Coveralls npm version TypeScript definitions on DefinitelyTyped

This library includes a stateless Typescript model of the Solidity DrawCalculator. It is intended to be uses as a tool to easily check if a User has won a prize for a particular draw. This could also be calculated on-chain through the DrawCalculator::calculate() view function but this library is much faster.

Setup

This project is available as an NPM package:

$ yarn add @pooltogether/draw-calculator-js

How to use

To create a claim or calculate winnings for an address:

  1. Run yarn add @pooltogether/draw-calculator-js in your project to install the package.
  2. Import the desired functions and types: import {drawCalculator, Draw, PrizeDistribution, DrawResults, filterResultsByValue, generatePicks, prepareClaims } from "@pooltogether/draw-calculator-js"

Starting with a particular drawId and userAddress, fetch the Draw information from the DrawBuffer contract:

const drawBuffer: Contract = new ethers.Contract(address, drawBufferAbi, signerOrProvider);
const drawId: number = await drawBuffer.getNewestDraw(); // can go back cardinality in time (8 draws)
const draw: Draw = await drawBuffer.functions.getDraw(drawId); // read-only rpc call

Next fetch the PrizeDistribution for the drawId from the PrizeDistributionBuffer contract:

// get PrizeDistribution from the  DrawCalculatorHistory contract for a particular drawId
const PrizeDistributionBufferContract: Contract = new ethers.Contract(
    address,
    prizeDistributionAbi,
    signerOrProvider,
);
const prizeDistribution = await PrizeDistributionBufferContract.functions.getPrizeDistribution(
    drawId,
); // read-only rpc call

Next, get the users balance using the convenient getNormalizedBalancesForDrawIds(address _user, uint32[] calldata _drawIds) view method on the DrawCalculator contract which returns an array of balances for drawIds: W

const drawCalculator: Contract = new ethers.Contract(address, drawCalculatorAbi, signerOrProvider);
const balances = await drawCalculator.functions.getNormalizedBalancesForDrawIds(userAddress, [
    drawId,
]); // read-only rpc call

Run this draw-calculator-js library locally to see the user has any prizes to claim:

const exampleUser: User = {
    address: userAddress // user address we want to calculate for
    normalizedBalances: balances
}

let results: DrawResults = batchCalculateDrawResults([prizeDistribution], [draw], exampleUser)

The results.totalValue field should indicate the total amount of prize available for userAddress for the drawId.

These results may then need to be filtered by value, since the user can only claim prizeDistribution.maxPicksPerUser number of prizes per draw.

results = filterResultsByValue(results, prizeDistribution.maxPicksPerUser);

Finally, to claim a prize, forward these DrawResults to prepareClaims(user: User, drawResult: DrawResults[]) to generate the data for the on-chain PrizeDistributor claim() call:

const claim: Claim = prepareClaims(user, [results]);

The on-chain call to PrizeDistributor::claim(address _user, uint32[] calldata _drawIds, bytes calldata _data) can then be populated and called with this data:

const PrizeDistributorContract = new ethers.Contract(
    address,
    PrizeDistributorAbi,
    signerOrProvider,
);
await PrizeDistributorContract.functions.claim(
    claim.userAddress,
    claim.drawIds,
    claim.encodedWinningPickIndices,
); //write rpc call

Congratulations you have now claimed a prize!

API Guide

todo.

Types

A full breakdown of the types can be found here

Testing

Unit tests can be run using:

$ yarn test

Development

Fork/clone this repo. Create a pull request with the changes you would like to make. Unit tests must be passing.