# @pooltogether/v4-js

> PoolTogether V4 JS library for general calculations and computers of the protocol state.

Latest version **0.0.1-beta.0** (published 2021-11-16) · GPL-3.0-only license · 0 weekly downloads

## Install

```sh
npm install @pooltogether/v4-js
pnpm add @pooltogether/v4-js
yarn add @pooltogether/v4-js
bun add @pooltogether/v4-js
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1-beta.0 |
| Published | 2021-11-16 |
| First published | 2021-11-16 |
| Weekly downloads | 0 |
| License | GPL-3.0-only |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 15 |
| Unpacked size | 510.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Kames Geraghty |
| Maintainers | asselstine, chuckbergeron, dylandesrosier, aodhgan, pierrick, kamesdev |

## Links

- npm: https://www.npmjs.com/package/@pooltogether/v4-js
- npm.io page: https://npm.io/package/@pooltogether/v4-js

## Dependencies (15)

- [ethers](https://npm.io/package/ethers.md) ^5.5.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [@ethersproject/abi](https://npm.io/package/@ethersproject/abi.md) ^5.5.0
- [@ethersproject/bytes](https://npm.io/package/@ethersproject/bytes.md) ^5.5.0
- [@ethersproject/units](https://npm.io/package/@ethersproject/units.md) ^5.5.0
- [@pooltogether/v4-core](https://npm.io/package/@pooltogether/v4-core.md) ^1.1.1
- [@ethersproject/address](https://npm.io/package/@ethersproject/address.md) ^5.5.0
- [@ethersproject/strings](https://npm.io/package/@ethersproject/strings.md) ^5.5.0
- [@ethersproject/networks](https://npm.io/package/@ethersproject/networks.md) ^5.5.0
- [@ethersproject/bignumber](https://npm.io/package/@ethersproject/bignumber.md) ^5.5.0
- [@ethersproject/contracts](https://npm.io/package/@ethersproject/contracts.md) ^5.5.0
- [@ethersproject/keccak256](https://npm.io/package/@ethersproject/keccak256.md) ^5.5.0
- [@pooltogether/v4-periphery](https://npm.io/package/@pooltogether/v4-periphery.md) ^1.0.0
- [@pooltogether/v4-timelocks](https://npm.io/package/@pooltogether/v4-timelocks.md) ^1.0.0
- [@pooltogether/contract-list-schema](https://npm.io/package/@pooltogether/contract-list-schema.md) ^0.1.4

## Recent versions

- 0.0.1-beta.0 (latest) — 2021-11-16

## README

<p align="center">
  <a href="https://github.com/pooltogether/pooltogether--brand-assets">
    <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">
  </a>
</p>

<br />

# PoolTogether V4 JS
![Tests](https://github.com/pooltogether/v4-js/actions/workflows/main.yml/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/pooltogether/v4-js/badge.svg?branch=master)](https://coveralls.io/github/pooltogether/v4-js?branch=master)
[![GPLv3 license](https://img.shields.io/badge/License-GPLv3-blue.svg)](http://perso.crans.org/besson/LICENSE.html)

The `v4-js` module is a general purpose JS library for reading/writing to the Pooltogether V4 protocol.

# Installation

This project is available as an NPM package:

```sh
npm install @pooltogether/v4-js
```

```sh
yarn add @pooltogether/v4-js
```

The repo can be cloned from Github for contributions.

```sh
git clone https://github.com/pooltogether/v4-core
```

# Overview

## Setup
- [PoolTogetherV4]()
- [initialize]()

## Core
- [batchCalculateDrawResults](https://github.com/pooltogether/v4-js/blob/master/src/batchCalculateDrawResults.ts)
- [calculateDrawResults]()
- [calculatePicks]()
- [calculatePicksFromAverageTotalSuppliesBetween]()
- [computeCardinality]()
- [computeDrawResults]()
- [computePicks]()
- [computePrizeDistribution]()
- [generatePicks]()
- [prepareClaims]()
- [validatePrizeDistributionParameters]()

## Config
- [bridging]()
- [chains]()
- [networks]()
## Fetching
- [getMultiTicketAverageTotalSuppliesBetween]()

## Utils
- [createContract]()
- [createInterface]()
- [getJsonRpcProvider]()
- [getProviderFromChainId]()
- [getProviderFromNetwork]()
- [isContractConnectedToProvider]()
- [isContractConnectedToSigner]()
- [sumBigNumbers]()
- [validateContractListIsConnected]()

# Getting Started

## Initializing

The `v4-js` module must be initialized with providers and a contract list.

Current `mainnet` and `testnet` contract lists can be used via the `@pooltogether/v4-pool-data` NPM package

The `v4-js` module must be initialized before blockchain read requests be executed.

```js
import PoolTogetherV4, { config } from 'pooltogether/v4-js';

const ptv4 = new PoolTogetherV4(config.providersMainnet, mainnetContractList)
```

*or*

```js
import { initialize, config } from 'pooltogether/v4-js';

const ptv4 = initialize(config.providersMainnet, mainnetContractList)
```

## Providers
Providers being passed must inherit the Provider type interface from `@ethersproject/abstract-provider`. In other words, ethers provider interfaces like JsonRpcProvider, InfuraProvider, FallbackProvider, etc... are usable as Provider interfaces.

```js
const providerMainnet = ethers.getDefaultProvider('mainnet');
const providerRinkeby = new ethers.providers.InfuraProvider('rinkeby', key)
const providerMumbai = new ethers.providers.JsonRpcProvider("https://rpc-mumbai.maticvigil.com");
const providers = { 1: providerMainnet, 4: providerRinkeby, 80001: providerMumbai }
```

### `computePrizeDistribution`

To compute a PrizeDistribution for a specific PrizePool/Ticket supply a Draw (from the DrawBuffer) and contract addresses for L1 PrizeTierHistory and the target Ticket contract, plus secondary Ticket contract addresses in a list.

```js
import { computePrizeDistribution } from '../src';

const draw = {
  winningRandomNumber: BigNumber.from('21288413488180966377126236036201345909019919575750940621513526137694302720820'),
  drawId: 1,
  timestamp: 1634410924,
  beaconPeriodStartedAt: 1634324400,
  beaconPeriodSeconds: 86400,
}

const prizeTierHistory = '0xdD1cba915Be9c7a1e60c4B99DADE1FC49F67f80D'
const ticketL1 = '0xdd4d117723C257CEe402285D3aCF218E9A8236E1'
const ticketL2 = '0x6a304dFdb9f808741244b6bfEe65ca7B3b3A6076'

const results = await computePrizeDistribution(draw, prizeTierHistory, ticketL1, [ticketL2])
```

---
_Source: https://npm.io/package/@pooltogether/v4-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
