npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

perp-v2-sdk

v1.1.2

Published

🛠 An JavaScript SDK for interacting with the Perpetual V2

Downloads

5

Readme

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 ⚠️