0.7.0 • Published 2 years ago

@mcdex/mai3.js v0.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 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

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.90

3 years ago

0.5.88

3 years ago

0.5.87

3 years ago

0.5.85

3 years ago

0.5.86

3 years ago

0.5.84

3 years ago

0.5.81

3 years ago

0.5.80

3 years ago

0.5.79

3 years ago

0.5.78

3 years ago

0.5.77

3 years ago

0.4.8

3 years ago

0.5.76

3 years ago

0.5.74

3 years ago

0.5.75

3 years ago

0.5.73

3 years ago

0.5.72

3 years ago

0.5.71

3 years ago

0.5.70

3 years ago

0.5.66

3 years ago

0.5.69

3 years ago

0.5.67

3 years ago

0.5.68

3 years ago

0.5.65

3 years ago

0.5.64

3 years ago

0.5.62

3 years ago

0.5.55

3 years ago

0.5.58

3 years ago

0.5.59

3 years ago

0.5.56

3 years ago

0.5.57

3 years ago

0.5.60

3 years ago

0.5.54

3 years ago

0.5.53

3 years ago

0.5.52

3 years ago

0.5.50

3 years ago

0.5.51

3 years ago

0.5.49

3 years ago

0.5.47

3 years ago

0.5.48

3 years ago

0.5.46

3 years ago

0.5.45

3 years ago

0.5.44

3 years ago

0.5.43

3 years ago

0.5.42

3 years ago

0.5.41

3 years ago

0.5.39

3 years ago

0.5.38

3 years ago

0.5.36

3 years ago

0.5.37

3 years ago

0.5.35

3 years ago

0.5.33

3 years ago

0.5.34

3 years ago

0.5.31

3 years ago

0.5.28

3 years ago

0.5.25

3 years ago

0.5.24

3 years ago

0.4.6

3 years ago

0.3.34

3 years ago