@mmasood/v4-utils-js
v0.1.9
Published
Utility library for PoolTogether V4 off-chain calculations, computations and core logic.
Downloads
5
Readme
🧰 Javascript Utilility Library - PoolTogether V4
Application | Contracts | Documentation | Draw Calculator | Frontend Client | Static Cache
Calculations, Computations and Core Logic
The @mmasood/v4-utils-js
node module package provides calculations, computations and core logic for the PoolTogether V4 protocol.
Assisting with low-level tasks like hashing addresses to generate picks and calculating the total number of prizes for a prize tier. The calculations
namespaced functions are modular: consuming low-level inputs/types to parity smart contract EVM operations.
High-order operations like filtering for a users winning picks and processing chain state (draws, prizeDistributions, etc..) to analyze/predict short and long term outcomes are included in the computations
namespaced functions.
🧮 Caclulations:
Arithmetic and operations to match v4-core
smart contract operations:
- calculateCardinality
- calculateFractionOfPrize
- calculateNormalizedBalancePicksFromTotalPicks
- calculateNumberOfMatches
- calculateNumberOfPrizesForTierIndex
- calculatePick
- calculatePicks
- calculatePicksFromAverageTotalSuppliesBetween
- calculatePrizeForTierPercentage
- calculateTierIndexFromMatches
🖥️ Computations:
Consume protocol chain state and return computed outcomes:
- computeDrawResults
- computePickPrize
- computePicksPrizes
- computePrizeAmount
- computePrizeDistributionFromTicketAverageTotalSupplies
- computeUserPicks
- computeUserWinningPicksForRandomNumber
- computeWinningPicks
🏆 Core:
Gain insights into the protocol state by analyzing and encoding historical and potential chain state.
🙋 Potential Additions:
- poolAverageYieldBetweenTimestamps
- userAverageYieldBetweenTimestamps
- oddsPerPrizeTierUsingNormalizedBalance
- encodeDrawPercentageRateAtomicUpdatesAcrossChains
Create Issue to request new features.Open Pull Request adhering to Contribution guidelines.
💾 Installation
This project is available as an NPM package:
npm install @mmasood/v4-utils-js
yarn add @mmasood/v4-utils-js
The repo can be cloned from Github for contributions.
git clone https://github.com/pooltogether/v4-utils-js
🏆 Quickstart (Claim Winning Picks)
Core utility functions like winningPicks(user, draw, prizeDistribution)
calculate, compute and encode any EVM chain compatible transaction with the maximum number of winning picks using the input arguments.
In short, if you just need to calulcate winning picks and claim prizes the winningPicks
function is for you 👋
import { Wallet } from '@ethersproject/wallet';
import { providers } from '@ethersproject/provider';
import { winningPicks, computeWinningPicks, encodeWinningPicks } from '@mmasood/v4-utils-js';
// Compute and Encode Winning Picks Seperately
const computedPicks = computeWinningPicks(user, [draw], [prizeDistribution]);
const encodePicks = encodeWinningPicks(user, computedWinningPicks);
// Compute and Encode Winning Picks Together
const computedAndEncodedWinningPicks = winningPicks(user, [draw], [prizeDistribution]);
// Send Encoded Transaction to Mainnet
const wallet = Wallet.createRandom().connect(providers.getDefaultProvider())
wallet.send(computedAndEncodedWinningPicks.encodedWinningPickIndices)
🪜 Examples
import { BigNumber } from '@ethersproject/bignumber';
import { parseEther } from '@ethersproject/units';
import { winningPicks, utils } from '@mmasood/v4-utils-js';
const user = {
address: '0x0000000000000000000000000000000000000001',
normalizedBalances: [
parseEther('0.1'), // 10% of totalSupply
],
};
const draw = {
drawId: 1,
winningRandomNumber: BigNumber.from(
'0x0000000000000000000000000000000000000000000000000000000000000001'
),
};
const prizeDistribution = {
bitRangeSize: 4,
matchCardinality: 10,
numberOfPicks: 1000,
prize: parseEther('1000'),
maxPicksPerUser: 30,
tiers: [
// Tier prizeAmount is 100% split between 10 tiers.
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
utils.formatTierPercentage('0.1'),
0,
0,
0,
0,
0,
0,
],
};
const generatedPicks = winningPicks(user, [draw], [prizeDistribution]);
/**
userAddress: string;
drawIds: number[];
winningPickIndices: BigNumber[][];
encodedWinningPickIndices: string;
* -------------------
userAddress: '0x0000000000000000000000000000000000000001',
drawIds: [1],
winningPickIndices: [[1]],
encodedWinningPickIndices: '0x000...2000...0001'
* /
💻 Developer Experience
The package is setup using the TSDX zero-config CLI which includes:
- Typescript
- Rollup
- Jest
- Prettier
- ESLint
Minor changes have been made to extend the default configuration.
ESLint
The TSDX linting configuration is overwritten to include override(s)* for:
- Import/Order (used to enforce consistent module import ordering)
*The ESLint overrides may incorrectly be interpreted by VSCode since the nested config file is ignored in the IDE
📖 Documentation
Namespaces
References
- calculateCardinality
- calculateFractionOfPrize
- calculateNormalizedBalancePicksFromTotalPicks
- calculateNumberOfMatches
- calculateNumberOfPrizesForTierIndex
- calculatePick
- calculatePicks
- calculatePicksFromAverageTotalSuppliesBetween
- calculatePrizeForTierPercentage
- calculateTierIndexFromMatches
- computeDrawResults
- computePickPrize
- computePicksPrizes
- computePrizeAmount
- computePrizeDistributionFromTicketAverageTotalSupplies
- computeUserPicks
- computeUserWinningPicksForRandomNumber
- computeWinningPicks
- encodeWinningPicks
- winningPicks