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

@lyrafinance/lyra-js

v0.0.29

Published

A JavaScript SDK for the Lyra Protocol.

Downloads

831

Readme

Lyra.js

A JavaScript SDK for the Lyra Protocol. Wraps around Ethers.js. Works in the web browser and Node.js.

Documentation

Guides

⚠️ This SDK is in open alpha and is constantly under development. USE AT YOUR OWN RISK.

Install

yarn add @lyrafinance/lyra-js

Quickstart

Read Lyra's market data.

import Lyra, { Chain } from '@lyrafinance/lyra-js'

const lyra = new Lyra(Chain.Optimism)

// Fetch all markets
const markets = await lyra.markets()

console.log(
  markets.map(market => ({
    address: market.address,
    name: market.name,
    // List all live boards (expiries)
    expiries: market.liveBoards().map(board => ({
      id: board.id,
      expiryTimestamp: board.expiryTimestamp,
      // List all strikes
      strikes: board.strikes().map(strike => ({
        id: strike.id,
        strikePrice: strike.strikePrice,
      })),
    })),
  }))
)

Executing trades

Prepare and execute trades with a simple interface.

import Lyra, { Chain, TradeEvent } from '@lyrafinance/lyra-js'

const lyra = new Lyra(Chain.Arbitrum)

// Initialize account
const signer = new ethers.Wallet(process.env.PRIVATE_KEY, lyra.provider)
const account = lyra.account(signer.address)

const market = await lyra.market('eth')

// Select most recent expiry
const board = market.liveBoards()[0]

// Select first strike in delta range
const strike = board.strikes().find(strike => strike.isDeltaInRange)
if (!strike) {
  throw new Error('No strike in delta range')
}

// Prepare trade (Open 1.0 Long ETH Call with 0.1% slippage)
const trade = await lyra.trade(account.address, 'eth', strike.id, true, true, ONE_BN, 0.1 / 100)

// Approve USDC
const approveTx = await trade.approveQuote(signer.address, MAX_BN)
const approveResponse = await signer.sendTransaction(approveTx)
await approveResponse.wait()
console.log('Approved USDC:', approveResponse.hash)

// Execute trade
const tradeResponse = await signer.sendTransaction(trade.tx)
console.log('Executed trade:', tradeResponse.hash)
const tradeReceipt = await tradeResponse.wait()

// Get trade result
const tradeEvent = (await TradeEvent.getByHash(lyra, tradeReceipt.transactionHash))[0]

printObject('Trade Result', {
  blockNumber: tradeEvent.blockNumber,
  positionId: tradeEvent.positionId,
  premium: tradeEvent.premium,
  fee: tradeEvent.fee,
})

Listeners

Create trade feeds across all markets with a simple listener

import Lyra, { Chain } from '@lyrafinance/lyra-js'

const lyra = new Lyra(Chain.Arbitrum)

lyra.onTrade(trade => {
  console.log({
    trader: trade.trader,
    positionId: trade.positionId,
    market: trade.marketName,
    size: trade.size,
    isBuy: trade.isBuy,
    isLong: trade.isLong,
    premium: trade.premium,
    setCollateralTo: trade.setCollateralTo,
    isLiquidation: trade.isLiquidation,
  })
})

Examples

See the src/scripts directory for more examples of SDK interactions.

Run Script

To run a script, first clone the lyra-js repository

git clone https://github.com/lyra-finance/lyra-js.git
cd lyra-js

Install dependencies locally

yarn install

Choose a script and run

yarn script <script>
yarn script markets
yarn script simple-trade