perp-v2-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

Perpetual Protocol V2 SDK

🛠 An JavaScript SDK for interacting with the Perpetual Protocol V2

Perpetual Protocol is an on-chain perpetual futures DEX with deep liquidity and builder-ready composability.

Features

  • 🔥Interacting with Perpetual Protocol V2 contracts
  • Subgraph Queries

💁Getting Started

To use Perpetual V2 SDK :

Install the lib using npm or yarn

yarn add perp-v2-sdk

Interacting with contracts

ES6 or TypeScript

import { ethers } from 'ethers'
import { ClearingHouse, MarketRegistry, Vault } from 'perp-v2-sdk'

const provider = new ethers.providers.JsonRpcProvider(process.env.ALCHEMY_API_KEY)
const clearingHouse = new ClearingHouse({ provider, privateKey: process.env.PRIVATE_KEY })
const marketRegistry = new MarketRegistry({ provider })
const valut = new Vault({ provider })

To get account value of a trader

const trader = '0x8B4e846c90a2521F0D2733EaCb56760209EAd51A'
const value = await clearingHouse.getAccountValue(trader)

To open a position

const reciept = await clearingHouse.openPosition(params)

To get total position size

const trader = '0x8B4e846c90a2521F0D2733EaCb56760209EAd51A'
const vETH = '0xccc170bdcdaA3c026302B19baef84Ea05C8aFB17'

const accountBalance = new AccountBalance({
  provider
  privateKey: process.env.PRIVATE_KEY
})
const size = await accountBalance.getTotalPositionSize(trader, vETH)

To approve vault to use USDC and deposit to vault

import { ChainId, USDC } from 'perp-v2-sdk'

const vaultAddress = vault.contract.address
const amount = '1000000000'
const usdc = new USDC({ provider, privateKey: process.env.PRIVATE_KEY, chainId: ChainId.Optimism })
await usdc.approve(vaultAddress, amount)
const reciept = await vault.deposit(usdc.address, amount)

Subgraph Queries

import { createSubgraphClient } from 'perp-v2-sdk'

const client = createSubgraphClient(process.env.SUBGRAPH_URL)

To list markets

import { Market_OrderBy, OrderDirection } from 'perp-v2-sdk'

const { markets } = await client.Markets({
  first: 3,
  where: { tradingVolume_gt: '10000000' },
  orderBy: Market_OrderBy.TradingVolume,
  orderDirection: OrderDirection.Desc,
})

To list positions

import { Position_OrderBy, OrderDirection } from 'perp-v2-sdk'

const trader = '0x8B4e846c90a2521F0D2733EaCb56760209EAd51A'
const { positions } = await client.Positions({
  first: 5,
  where: { trader, positionSize_gt: '100' },
  orderBy: Position_OrderBy.PositionSize,
  orderDirection: OrderDirection.Desc,
})

More examples are here


🔧Contributing

Created an .env file with a env variable: You can get one from providers such as Infura or Alchemy.

PROVIDER_URL=YOUR_PROVIDRER_URL

For local developement you can run

yarn dev

For production build:

yarn build

Which will generate a production build on "dist" folder.

To run tests type:

yarn test

Disclaimer

This is experimental software and is provided on an "as is" and "as available" basis.

We do not give any warranties and will not be liable for any loss incurred through any use of this codebase.

This is 3rd party code, use at your YOUR OWN RISK ⚠️

Package Sidebar

Install

npm i perp-v2-sdk

Weekly Downloads

1

Version

1.1.2

License

MIT

Unpacked Size

13.2 MB

Total Files

125

Last publish

Collaborators

  • massun-onibakuchi