Socket
Book a DemoInstallSign in
Socket

@perp/v2-sdk

Package Overview
Dependencies
Maintainers
7
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@perp/v2-sdk

An SDK for Perpetual Protocol V2

latest
npmnpm
Version
1.0.0
Version published
Weekly downloads
1
-83.33%
Maintainers
7
Weekly downloads
 
Created
Source

Perpetual Protocol V2 SDK

An SDK for Perpetual Protocol V2

Setup

Install the lib

yarn add @perp/v2-sdk

Commit:

Use commitlint and commitizen to regulate commit message.

 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.
baseToken: ETH
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:
Use quoteToken to add liquidity.
baseToken: ETH
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)

Keywords

perpetual protocol

FAQs

Package last updated on 18 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts