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

@mux-network/mux.js

v2.9.12

Published

Mux Protocol JavaScript API

Downloads

921

Readme

mux.js - MUX Protocol JavaScript API

Install

npm install --save @mux-network/mux.js

Tutorial

MUX currently supports Arbitrum, Avalanche, BSC and Fantom networks. The following code all uses Arbitrum in the examples.

1. Connects to Arbitrum

import { JsonRpcProvider } from '@ethersproject/providers'

const provider = new JsonRpcProvider('https://arb1.arbitrum.io/rpc')

2. List asset IDs and symbols

import { getReaderContract, getChainStorage } from '@mux-network/mux.js'

const reader = await getReaderContract(provider)
const chainStorage = await getChainStorage(reader)
for (let asset of chainStorage.assets) {
  console.log(`assetId: ${asset.id} symbol: ${asset.symbol} tokenAddress: ${asset.tokenAddress}`)
}

This returns

assetId: 0 symbol: USDC tokenAddress: 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8
assetId: 1 symbol: USDT tokenAddress: 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9
assetId: 2 symbol: DAI tokenAddress: 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1
assetId: 3 symbol: ETH tokenAddress: 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1
assetId: 4 symbol: BTC tokenAddress: 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f
...

3. Calculate a subAccountId

A sub-account is a segregated account nested under an ETH address; each { address, collateral asset, underlying asset, long/short } pair is a sub-account. The following example calculates a sub-account for long ETH, with USDC as collateral.

import { encodeSubAccountId } from '@mux-network/mux.js'

const traderAddress = '' // paste your ETH address here
const subAccountId = encodeSubAccountId(traderAddress, 0 /* USDC */, 3 /* ETH */, true /* long */)
console.log(subAccountId)

4. Get the trader's position size

import { getSubAccounts } from '@mux-network/mux.js'

const subAccounts = await getSubAccounts(reader, [subAccountId])
const subAccount = subAccounts[subAccountId]
console.log(`position: ${subAccount.size.toFixed()}`)

5. Calculate margin balance

Calculating margin balance requires asset price (ex: ETH price). Reading ETH price is not contained in mux.js. However, you can read assets price and info from the Broker API.

import { computeSubAccount } from '@mux-network/mux.js'
import BigNumber from 'bignumber.js'

const computed = computeSubAccount(
  chainStorage.assets,
  subAccountId,
  subAccount,
  new BigNumber('1'), // collateral (USDC) price
  new BigNumber('1000') // asset (ETH) price
)
console.log(`marginBalance(usd): ${computed.computed.marginBalanceUsd}`)

6. Open a position (place a position order)

In this example, we deposit 10 USDC and long 0.01 ETH using the market price. Do not forget to approve USDC to OrderBook before opening the position (not included in this example). The OrderBook addresses are different between different chains. Please check CHAIN_ID_TO_ORDER_BOOK_ADDRESS in mux.js for the addresses.

import { CHAIN_ID_TO_ORDER_BOOK_ADDRESS, OrderBook__factory, PositionOrderFlags } from '@mux-network/mux.js'

const privateKey = '' // paste your private key here
const wallet = new ethers.Wallet(privateKey, provider)
const orderBookAddress = CHAIN_ID_TO_ORDER_BOOK_ADDRESS[42161 /* Arbitrum chain ID */]
const orderBook = OrderBook__factory.connect(orderBookAddress, wallet)
const tx = await orderBook.placePositionOrder2(
  subAccountId,
  new BigNumber('10').shiftedBy(6).toFixed(), // deposit $10 as collateral. USDC.decimals = 6
  new BigNumber('0.01').shiftedBy(18).toFixed(), // position 0.01 ETH. decimals is always 18
  '0', // limit price must be 0 for a market order
  0, // profitTokenId is only used when close a position
  PositionOrderFlags.OpenPosition + PositionOrderFlags.MarketOrder, // check PositionOrderFlags for details
  0, // a default deadline will be applied on a market order
  '0x0000000000000000000000000000000000000000000000000000000000000000' // an empty referral code
)
console.log(`placing order. tx: ${tx.hash}`)
await tx.wait()

7. Close the position (place another position order)

const tx = await orderBook.placePositionOrder2(
  subAccountId,
  '0', // we leave the collateral = 0 because we will withdraw all collaterals later in the flag
  new BigNumber('0.01').shiftedBy(18).toFixed(), // position 0.01 ETH. decimals is always 18
  '0', // limit price must be 0 for a market order
  3, // profitTokenId is the token id for you profit (the loss will always be charged from your collateral). should be 3 (ETH) for a long position. should be 0 (USDC) or other stable coins for a short position
  PositionOrderFlags.MarketOrder + PositionOrderFlags.WithdrawAllIfEmpty, // check PositionOrderFlags for details
  0, // a default deadline will be applied on a market order
  '0x0000000000000000000000000000000000000000000000000000000000000000' // an empty referral code
)
console.log(`placing order. tx: ${tx.hash}`)
await tx.wait()

Test

npm run test