{"id":20002060,"url":"https://github.com/pooltogether/draw-calculators-js","last_synced_at":"2025-03-01T23:54:47.173Z","repository":{"id":46063559,"uuid":"394406822","full_name":"pooltogether/draw-calculators-js","owner":"pooltogether","description":"Stateless Typescript library for the v4 Draw Calculator","archived":false,"fork":false,"pushed_at":"2021-12-02T01:21:24.000Z","size":27326,"stargazers_count":5,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-12T13:14:30.896Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pooltogether.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-09T18:59:01.000Z","updated_at":"2023-03-04T16:35:26.000Z","dependencies_parsed_at":"2022-07-21T22:48:17.762Z","dependency_job_id":null,"html_url":"https://github.com/pooltogether/draw-calculators-js","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fdraw-calculators-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fdraw-calculators-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fdraw-calculators-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fdraw-calculators-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pooltogether","download_url":"https://codeload.github.com/pooltogether/draw-calculators-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241439791,"owners_count":19963098,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T05:19:38.669Z","updated_at":"2025-03-01T23:54:47.149Z","avatar_url":"https://github.com/pooltogether.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/pooltogether/pooltogether--brand-assets\"\u003e\n    \u003cimg src=\"https://github.com/pooltogether/pooltogether--brand-assets/blob/977e03604c49c63314450b5d432fe57d34747c66/logo/pooltogether-logo--purple-gradient.png?raw=true\" alt=\"PoolTogether Brand\" style=\"max-width:100%;\" width=\"200\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cbr /\u003e\n\n# PoolTogether Draw Calculator JS\n\n[![Coveralls](https://github.com/pooltogether/draw-calculators-js/actions/workflows/main.yml/badge.svg)](https://github.com/pooltogether/draw-calculators-js/actions/workflows/main.yml)\n[![npm version](https://badge.fury.io/js/@pooltogether%2Fdraw-calculator-js.svg)](https://badge.fury.io/js/@pooltogether%2Fdraw-calculator-js)\n[![TypeScript definitions on DefinitelyTyped](https://definitelytyped.org/badges/standard.svg)](https://definitelytyped.org)\n\nThis 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.\n\n# Setup\n\nThis project is available as an NPM package:\n\n```bash\n$ yarn add @pooltogether/draw-calculator-js\n```\n\n# How to use\n\nTo create a claim or calculate winnings for an address:\n\n1. Run `yarn add @pooltogether/draw-calculator-js` in your project to install the package.\n1. Import the desired functions and types: `import {drawCalculator, Draw, PrizeDistribution, DrawResults, filterResultsByValue, generatePicks, prepareClaims } from \"@pooltogether/draw-calculator-js\"`\n\nStarting with a particular `drawId` and `userAddress`, fetch the Draw information from the DrawBuffer contract:\n\n```js\nconst drawBuffer: Contract = new ethers.Contract(address, drawBufferAbi, signerOrProvider);\nconst drawId: number = await drawBuffer.getNewestDraw(); // can go back cardinality in time (8 draws)\nconst draw: Draw = await drawBuffer.functions.getDraw(drawId); // read-only rpc call\n```\n\nNext fetch the PrizeDistribution for the `drawId` from the PrizeDistributionBuffer contract:\n\n```javascript\n// get PrizeDistribution from the  DrawCalculatorHistory contract for a particular drawId\nconst PrizeDistributionBufferContract: Contract = new ethers.Contract(\n    address,\n    prizeDistributionAbi,\n    signerOrProvider,\n);\nconst prizeDistribution = await PrizeDistributionBufferContract.functions.getPrizeDistribution(\n    drawId,\n); // read-only rpc call\n```\n\nNext, get the users balance using the convenient `getNormalizedBalancesForDrawIds(address _user, uint32[] calldata _drawIds)` view method\non the DrawCalculator contract which returns an array of balances for drawIds:\nW\n\n```js\nconst drawCalculator: Contract = new ethers.Contract(address, drawCalculatorAbi, signerOrProvider);\nconst balances = await drawCalculator.functions.getNormalizedBalancesForDrawIds(userAddress, [\n    drawId,\n]); // read-only rpc call\n```\n\nRun this `draw-calculator-js` library locally to see the user has any prizes to claim:\n\n```js\nconst exampleUser: User = {\n    address: userAddress // user address we want to calculate for\n    normalizedBalances: balances\n}\n\nlet results: DrawResults = batchCalculateDrawResults([prizeDistribution], [draw], exampleUser)\n```\n\nThe `results.totalValue` field should indicate the total amount of prize available for `userAddress` for the `drawId`.\n\nThese results may then need to be filtered by value, since the user can only claim `prizeDistribution.maxPicksPerUser` number of prizes per draw.\n\n```js\nresults = filterResultsByValue(results, prizeDistribution.maxPicksPerUser);\n```\n\nFinally, to claim a prize, forward these `DrawResults` to `prepareClaims(user: User, drawResult: DrawResults[])` to generate the data for the on-chain PrizeDistributor `claim()` call:\n\n```js\nconst claim: Claim = prepareClaims(user, [results]);\n```\n\nThe on-chain call to `PrizeDistributor::claim(address _user, uint32[] calldata _drawIds, bytes calldata _data)` can then be populated and called with this data:\n\n```js\nconst PrizeDistributorContract = new ethers.Contract(\n    address,\n    PrizeDistributorAbi,\n    signerOrProvider,\n);\nawait PrizeDistributorContract.functions.claim(\n    claim.userAddress,\n    claim.drawIds,\n    claim.encodedWinningPickIndices,\n); //write rpc call\n```\n\nCongratulations you have now claimed a prize!\n\n# API Guide\n\ntodo.\n\n# Types\n\nA full breakdown of the types can be found [here](./src/types.ts)\n\n# Testing\n\nUnit tests can be run using:\n\n```bash\n$ yarn test\n```\n\n# Development\n\nFork/clone this repo. Create a pull request with the changes you would like to make. Unit tests must be passing.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpooltogether%2Fdraw-calculators-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpooltogether%2Fdraw-calculators-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpooltogether%2Fdraw-calculators-js/lists"}