@mcdex/mai3.js
v0.7.0
Published
Mai Protocol v3 JavaScript API
Downloads
177
Readme
mai3.js - Mai Protocol v3 JavaScript API
Install
npm install @mcdex/mai3.js
Quick start
Goto mcdex.io/trade to open some positions.
Connect to Arbitrum Testnet:
import { JsonRpcProvider } from '@ethersproject/providers'
const provider = new JsonRpcProvider('https://rinkeby.arbitrum.io/rpc')
const chainId = (await provider.getNetwork()).chainId
- 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)
})
- 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)
- 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