{"_id":"@chanceprotocol/draw-calculator-js","name":"@chanceprotocol/draw-calculator-js","dist-tags":{"latest":"1.0.3-beta.4"},"versions":{"1.0.3-beta.4":{"name":"@chanceprotocol/draw-calculator-js","version":"1.0.3-beta.4","description":"PoolTogether Draw Calculator","main":"dist/index.js","types":"dist/index.d.ts","license":"MIT","dependencies":{"chalk":"^4.1.2","debug":"^4.3.2"},"devDependencies":{"@ethersproject/address":"^5.1.0","@ethersproject/constants":"^5.1.0","@ethersproject/providers":"^5.1.0","@ethersproject/abstract-signer":"^5.1.0","@ethersproject/abstract-provider":"^5.1.0","@ethersproject/contracts":"^5.1.0","@ethersproject/units":"^5.1.0","@ethersproject/strings":"^5.1.0","@ethersproject/keccak256":"^5.1.0","@ethersproject/bignumber":"^5.1.0","@ethersproject/abi":"^5.1.0","@types/node":"^15.14.9","@types/chai":"^4.2.21","@types/expect":"^24.3.0","@types/mocha":"^9.0.0","chai":"^4.3.4","chalk":"^4.1.2","commander":"^8.2.0","debug":"^4.3.2","ethers":"5.4.4","immutable":"^4.0.0-rc.14","mocha":"^9.0.3","prettier":"2.2.1","ts-generator":"0.1.1","ts-mocha":"^8.0.0","ts-node":"9.1.1","tsdx":"^0.14.1","typedoc":"^0.22.4","typedoc-plugin-markdown":"^3.11.0","typescript":"4.2.3","yalc":"^1.0.0-pre.53"},"scripts":{"sim":"ts-node ./src/sim/runSim.ts","test":"tsdx test","start":"tsdx watch --onSuccess \"yalc push\"","reinstall":"rm -rf node_modules && yarn install","build":"tsdx build","clean":"rm -rf dist/","docs":"typedoc --out docs src/index.ts","cli":"ts-node ./src/cli.ts"},"_id":"@chanceprotocol/draw-calculator-js@1.0.3-beta.4","_integrity":"sha512-waAjWFmfdFecu8g41/UVc1uxGxu0PL3ec70w4uRffRTG4dS3021N2QjgK5c7Gsvr6UPM6ffRMqLc+sRY2W5/Iw==","_resolved":"/tmp/c9c06bc55aa2108bf327c4a4b50f3589/chanceprotocol-draw-calculator-js-1.0.3-beta.4.tgz","_from":"file:chanceprotocol-draw-calculator-js-1.0.3-beta.4.tgz","_nodeVersion":"16.15.1","_npmVersion":"9.6.5","dist":{"integrity":"sha512-waAjWFmfdFecu8g41/UVc1uxGxu0PL3ec70w4uRffRTG4dS3021N2QjgK5c7Gsvr6UPM6ffRMqLc+sRY2W5/Iw==","shasum":"c9b58a1f9e36bb8def891654ef86773f808de6f3","tarball":"https://registry.npmjs.org/@chanceprotocol/draw-calculator-js/-/draw-calculator-js-1.0.3-beta.4.tgz","fileCount":31,"unpackedSize":134767,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBRV0sweaJnABSUJx+w5gbteOlpIs2EDZxllaKCwUBsuAiADslIpCWjhEkWc1W5G3AauoHBljQQZlOJDynL/JKgJCg=="}]},"_npmUser":{"name":"chanceprotocol","email":"chance@netixsol.com"},"directories":{},"maintainers":[{"name":"chanceprotocol","email":"chance@netixsol.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/draw-calculator-js_1.0.3-beta.4_1692257245447_0.8528566816950274"},"_hasShrinkwrap":false}},"time":{"created":"2023-08-17T07:27:25.391Z","1.0.3-beta.4":"2023-08-17T07:27:25.684Z","modified":"2023-08-17T07:27:25.945Z"},"maintainers":[{"name":"chanceprotocol","email":"chance@netixsol.com"}],"description":"PoolTogether Draw Calculator","license":"MIT","readme":"<p align=\"center\">\n  <a href=\"https://github.com/pooltogether/pooltogether--brand-assets\">\n    <img 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\">\n  </a>\n</p>\n\n<br />\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/@chanceprotocol%2Fdraw-calculator-js.svg)](https://badge.fury.io/js/@chanceprotocol%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 @chanceprotocol/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 @chanceprotocol/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 \"@chanceprotocol/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","readmeFilename":"README.md"}