# @perp/v2-sdk

> An SDK for Perpetual Protocol V2

Latest version **1.0.0** (published 2022-03-18) · BSD-3-Clause license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @perp/v2-sdk
pnpm add @perp/v2-sdk
yarn add @perp/v2-sdk
bun add @perp/v2-sdk
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-03-18 |
| First published | 2022-02-17 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 12.22 |
| Dependencies | 8 |
| Unpacked size | 9.6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | miyachen, perp_dev, yurenju, wraecca, kimiwu, detoo, vinta |
| Keywords | perpetual protocol, DeFi, sdk |

## Links

- npm: https://www.npmjs.com/package/@perp/v2-sdk
- npm.io page: https://npm.io/package/@perp/v2-sdk

## Dependencies (8)

- [big.js](https://npm.io/package/big.js.md) 6.1.1
- [ethers](https://npm.io/package/ethers.md) 5.4.2
- [cross-fetch](https://npm.io/package/cross-fetch.md) 3.1.5
- [dotenv-flow](https://npm.io/package/dotenv-flow.md) 3.2.0
- [@uniswap/v3-core](https://npm.io/package/@uniswap/v3-core.md) 1.0.0
- [@babel/preset-modules](https://npm.io/package/@babel/preset-modules.md) 0.1.5
- [@perp/curie-periphery](https://npm.io/package/@perp/curie-periphery.md) 1.3.1
- [@perp/curie-deployments](https://npm.io/package/@perp/curie-deployments.md) 2022.3.10-1646893072651

## Recent versions

- 1.0.0 (latest) — 2022-03-18
- 0.1.9 — 2022-03-17
- 0.1.8 — 2022-03-17
- 0.1.7 — 2022-03-16
- 0.1.6 — 2022-03-16
- 0.1.6-alpha — 2022-03-15
- 0.1.6-multicollateral — 2022-03-15
- 0.1.5-alpha — 2022-03-15
- 0.1.5-multicollateral — 2022-03-15
- 0.1.4-multicollateral — 2022-03-15
- 0.1.4 — 2022-03-11
- 0.1.3 — 2022-03-10
- 0.1.2 — 2022-03-10
- 0.1.1 — 2022-02-17
- 0.1.0 — 2022-02-17

## README

# Perpetual Protocol V2 SDK

 An SDK for  Perpetual Protocol V2

# Setup
Install the lib

```bash
yarn add @perp/v2-sdk
```

Commit:

Use commitlint and commitizen to regulate commit message.
 ```bash
  git ci
```
Test:

```
 yarn test
```

# Layers
- **Market:** Tradable pairs for positions.
    - `Market`
        - values:
            - `indexPrice`, `markPrice`, `tradingVolume24h`, `fundingRate`
- **Pool** (*extends Market*)**:** Addable pairs for liquidity.
    - `Pool`
        - values:
            - `TVL`, `APR`
- **Wallet:** Manages user’s assets in his/her connected web3 wallet. (e.g. MetaMask)
    - `Wallet`
    - `DepositHistory`
    - `WithdrawHistory`
- **Vault:** Manages user’s assets stored inside Perpetual Protocol.
    - `Vault`
        - methods:
            - `deposit`, `withdraw`
        - values:
            - `accountBalance`, `totalPnl`
- **ClearingHouse:** Manage transactions.
    - `ClearingHouse`
        - methods:
            - openPosition, closePosition
            - addLiquidity, removeLiquidity
- **Position:**
    - `Position`
    - `PositionDraft`
    - `PositionHistory`
    - `FundingPaymentHistory`




# Usage

## Create a perpetualProtocol instance.

* Now we only support **optimism**


```

const perp = new PerpetualProtocol({
  chainId: 10,
  providerConfigs: [ { rpcUrl: "https://mainnet.optimism.io"}] })
await perp.init()

```

## Open a position

* Remember to provide your singer when connecting.

For example:

Open a `Long` position using `quoteToken`. <br>
baseToken: ETH <br>
quoteToken: USD

```
const perp = new PerpetualProtocol({
  chainId: 10,
  providerConfigs: [ { rpcUrl: "https://mainnet.optimism.io"}] })
await perp.init()
await perp.connect({ signer })

```

  ```
  const tickerSymbol =  "ETHUSD"
  const slippage = new Big(0.02) // remember to transformed to Big type
  const amountInput = new Big(100) // remember to transformed to Big type
  const side = PositionSide.LONG
  const isAmountInputBase = false // we are not using base token to open a long position here.

  const newPositionDraft = perp.clearingHouse.createPositionDraft({
                tickerSymbol,
                side,
                amountInput,
                isAmountInputBase,
            })
  perp.clearingHouse.openPosition(positionDraft, slippage)
  ```

## Close a position

```
const tickerSymbol = "ETHUSD"
const position = await perp.positions.getTakerPositionByTickerSymbol(tickerSymbol)
perp.clearingHouse.closePosition(position, slippage)
```
## Add liquidity
* Remember to provide your singer when connecting.

 For example:<br />
  Use `quoteToken` to add liquidity. <br />
  baseToken: ETH <br />
  quoteToken: USD


  ```
  const perpParam = {
    chainId: 10,
    providerConfigs: [ { rpcUrl: "https://mainnet.optimism.io"}]
  }
 const perp = new PerpetualProtocol(perpParam)
 await perp.init()
 await perp.connect({ signer })
 ```
 ```
 const tickerSymbol = "ETHUSD"
 const market = perp.markets.getMarket({ tickerSymbol })
 const lowerTick = perp.market.getPriceToTick(lowerTickPrice)
 const upperTick = perp.market.getPriceToTick(upperTickPrice)

 const slippage = new Big(0.02) // remember to transformed to Big type

const rawBaseAmount = undefined
const rawQuoteAmount = new Big(100) // remember to transformed to Big type

  const liquidityDraft = perp.clearingHouse.createLiquidityDraft({
      tickerSymbol,
      rawBaseAmount,
      rawQuoteAmount,
      upperTick,
      lowerTick,
  })

  perp.clearingHouse.addLiquidity(liquidityDraft, slippage)
  ```

## Close liquidity

- `ratio` means how much ratio you would like to remove. 1 means 100%
- Use `filterFn` to filter out liquidity you would like to remove.
```

const ratio = new Big(1) // remember to transformed to Big type
const slippage = new Big(0.02) // remember to transformed to Big type
const liquidity = perp.liquidities.getTotalLiquidities().filter(filterFn)
perp.clearingHouse.removeLiquidity(liquidity, ratio, slippage)

```

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