0.7.0 • Published 4 years ago

@mcdex/mai3.js v0.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

mai3.js - Mai Protocol v3 JavaScript API

CoverageStatus

Install

npm install @mcdex/mai3.js

Quick start

  1. Goto mcdex.io/trade to open some positions.

  2. Connect to Arbitrum Testnet:

import { JsonRpcProvider } from '@ethersproject/providers'

const provider = new JsonRpcProvider('https://rinkeby.arbitrum.io/rpc')
const chainId = (await provider.getNetwork()).chainId
  1. Enum Trader's positions (in all perpetual markets). A perpetual is identified by (liquidityPoolAddress, perpetualIndex):
import { CHAIN_ID_TO_POOL_CREATOR_ADDRESS, PoolCreatorFactory } from '@mcdex/mai3.js'
import { listActivatePerpetualsOfTrader } from '@mcdex/mai3.js'

const traderAddress = '' // Paste your ETH address here
const poolCreator = PoolCreatorFactory.connect(CHAIN_ID_TO_POOL_CREATOR_ADDRESS[chainId], provider)
const positions = await listActivatePerpetualsOfTrader(poolCreator, traderAddress)
positions.forEach(async ({liquidityPoolAddress, perpetualIndex}) => {
  console.log(liquidityPoolAddress, perpetualIndex)
})
  1. Get the symbol, underlying name and collateral name of a perpetual. The (ticker) symbol is a number assigned to each perpetual. The underlying name is a string. The collateral is a ERC20 token.
import { getReaderContract, getLiquidityPool } from '@mcdex/mai3.js'
import { IERC20Factory, erc20Symbol } from '@mcdex/mai3.js'

const reader = await getReaderContract(provider)
const pool = await getLiquidityPool(reader, liquidityPoolAddress)

const collateralTokenAddress = pool.collateral
const collateral = IERC20Factory.connect(collateralTokenAddress, provider)
const collateralSymbol = await erc20Symbol(collateral)

const perpetual = pool.perpetuals.get(perpetualIndex)
console.log(perpetual.symbol, perpetual.underlyingSymbol, collateralSymbol)
  1. Get the trader's position size and margin balance. A positive position means buy/long, a negative position means sell/short.
import { BigNumber } from 'bignumber.js'

const account = await reader.callStatic.getAccountStorage(liquidityPoolAddress, perpetualIndex, traderAddress)
console.log(
  'position',
  new BigNumber(account.accountStorage.position.toString()).shiftedBy(-18).toFixed(),
  'margin',
  new BigNumber(account.accountStorage.margin.toString()).shiftedBy(-18).toFixed()
)

Test

npm run test
0.7.0

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.90

4 years ago

0.5.88

4 years ago

0.5.87

4 years ago

0.5.85

4 years ago

0.5.86

4 years ago

0.5.84

4 years ago

0.5.81

4 years ago

0.5.80

4 years ago

0.5.79

4 years ago

0.5.78

4 years ago

0.5.77

4 years ago

0.4.8

4 years ago

0.5.76

4 years ago

0.5.74

4 years ago

0.5.75

4 years ago

0.5.73

4 years ago

0.5.72

4 years ago

0.5.71

4 years ago

0.5.70

4 years ago

0.5.66

4 years ago

0.5.69

4 years ago

0.5.67

4 years ago

0.5.68

4 years ago

0.5.65

4 years ago

0.5.64

4 years ago

0.5.62

4 years ago

0.5.55

4 years ago

0.5.58

4 years ago

0.5.59

4 years ago

0.5.56

4 years ago

0.5.57

4 years ago

0.5.60

4 years ago

0.5.54

4 years ago

0.5.53

4 years ago

0.5.52

4 years ago

0.5.50

4 years ago

0.5.51

4 years ago

0.5.49

4 years ago

0.5.47

4 years ago

0.5.48

4 years ago

0.5.46

4 years ago

0.5.45

4 years ago

0.5.44

4 years ago

0.5.43

4 years ago

0.5.42

4 years ago

0.5.41

4 years ago

0.5.39

4 years ago

0.5.38

4 years ago

0.5.36

4 years ago

0.5.37

4 years ago

0.5.35

4 years ago

0.5.33

4 years ago

0.5.34

4 years ago

0.5.31

4 years ago

0.5.28

4 years ago

0.5.25

4 years ago

0.5.24

4 years ago

0.4.6

4 years ago

0.3.34

4 years ago