# @mcdex/mai3.js

> Mai Protocol v3 JavaScript API

Latest version **0.7.0** (published 2021-12-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mcdex/mai3.js
pnpm add @mcdex/mai3.js
yarn add @mcdex/mai3.js
bun add @mcdex/mai3.js
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2021-12-06 |
| First published | 2021-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | * |
| Dependencies | 2 |
| Unpacked size | 1 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Zhu Tianchi |
| Maintainers | jie85, zhutianchi, huhan-mcarlo |
| Keywords | mai, protocol, mcdex |

## Links

- npm: https://www.npmjs.com/package/@mcdex/mai3.js
- Repository: https://github.com/mcdexio/mai3.js
- Homepage: http://github.com/mcdexio/mai3.js
- Issues: https://github.com/mcdexio/mai3.js/issues
- npm.io page: https://npm.io/package/@mcdex/mai3.js

## Dependencies (2)

- [ethers](https://npm.io/package/ethers.md) ^5.5.2
- [bignumber.js](https://npm.io/package/bignumber.js.md) ~9.0.0

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 0.7.0 (latest) — 2021-12-06
- 0.6.3 — 2021-11-18
- 0.6.2 — 2021-11-10
- 0.6.1 — 2021-11-10
- 0.6.0 — 2021-11-08
- 0.5.90 — 2021-10-15
- 0.5.88 — 2021-09-22
- 0.5.87 — 2021-09-16
- 0.5.86 — 2021-09-16
- 0.5.85 — 2021-09-16
- 0.5.84 — 2021-09-16
- 0.5.81 — 2021-08-24
- 0.5.80 — 2021-08-18
- 0.5.79 — 2021-08-17
- 0.5.78 — 2021-08-11
- … 48 more at https://npm.io/package/@mcdex/mai3.js/versions

## README

# mai3.js - Mai Protocol v3 JavaScript API

![CoverageStatus](https://github.com/mcdexio/mai3.js/workflows/Coverage/badge.svg)

## Install
```
npm install @mcdex/mai3.js
```

## Quick start

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

2. Connect to Arbitrum Testnet:

```js
import { JsonRpcProvider } from '@ethersproject/providers'

const provider = new JsonRpcProvider('https://rinkeby.arbitrum.io/rpc')
const chainId = (await provider.getNetwork()).chainId
```

3. Enum Trader's positions (in all perpetual markets). A perpetual is identified by (liquidityPoolAddress, perpetualIndex):

```js
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)
})
```

4. 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.
```js
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)
```

5. Get the trader's position size and margin balance. A positive position means buy/long, a negative position means sell/short.
```js
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
```

---
_Source: https://npm.io/package/@mcdex/mai3.js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
