@blockworks-foundation/mango-client

中文文档
2.3.0发布于 2023/11/14MITTypeScriptCommonJS近 30 天下载 4079

Library for interacting with Mango Markets' solana smart contracts.

原始使用文档
文档目录5 节
  1. Versions
  2. Testing
  3. Requirements
  4. Testing liquidations
  5. Example

Mango Client

A client to engage with the mango program

Versions

2.0.0 released Jun 9, 2021

0.1.19

Testing

All tests are meant to be run on devnet The full test run, takes a long time to run and in some cases fail due to connectivity issues or someone else working on devnet. Therefore it is recommended to run isolated tests like so -

yarn test -g "<test description>"

For example the following test -

it('should log lotSizes', async() => {...});

would be run like this -

yarn test -g "should log lotSizes"

You can also specify a test group that can run multiple tests for example the following group -

describe('log stuff', async() => {...});

can be run like this -

yarn test -g "should log lotSizes"

If however you do want to run the full suite of tests simply run -

yarn test

Requirements

Some tests like logging and order matching will run without any requirements, however all liquidation tests expect to have an AGGREGATOR_PATH variable set in your .env.

The AGGREGATOR_PATH variable should be the solana-flux-aggregator path which you can get here.

Example .env file -

AGGREGATOR_PATH=~/solana-flux-aggregator

Testing liquidations

One of the most dynamic tests in the suite are the liquidation tests, which can be found in the stress testing partial liquidation group simply do a search for it in your IDE.

All of the tests there are variations of the stressTestLiquidation function.

Roughly this function works by -

  1. Creates and funds a liqee account to be liquidated
  2. Changes the oracle price to the order price the liqee is about submit
  3. Submits a long or a short order on behalf of the liqee
  4. Changes the oracle price to force liqee's collateral ratio to drop below maintenance threshold
  5. Creates and funds a liqor account that will perform the liquidation
  6. liqor performs the liquidation

Listed below are the parameters that can allow to create many different liquidation scenarios -

mangoGroupSpotMarket: Market,
orderQuantity?: number, // Default = 1
customLiqeeOwner?: Account, // Default = null
shouldPartialLiquidate?: boolean, // Default = false
shouldCreateNewLiqor?: boolean, // Default = true
shouldFinishLiquidationInTest?: boolean, // Default = true
customOrderPrice?: number, // Default = 0
customOrderSize?: number, // Default = 0
leverageCoefficient?: number, // Default = 15
matchLeveragedOrder?: boolean, // Default = false
side?: 'buy' | 'sell' // Default = 'buy'
  • mangoGroupSpotMarket is the spot market in which the liquidation opportunity should be created
  • orderQuantity specifies how many orders the liqee account should create, it doesn't affect the orderSize
  • customLiqeeOwner can be specified if you would like to reuse an existing account as liqee. This is useful if you want to chain liquidation scenarios. If left empty a new account is created
  • shouldPartialLiquidate specifies whether it should be a regular liquidation or partial
  • shouldCreateNewLiqor will create a new liqor account that will get funded to perform the liquidation. If you're chaining a liquidation scenario there's no need to create a liquidator on each, in that case you can set it to false
  • shouldFinishLiquidationInTest - As with above you should set this parameter to false if you want to chain liquidation scenarios, or would like to finish the liquidation manually.
  • customOrderPrice - if not set it will use the minPrice from the market
  • customOrderSize - if not set it will use the minSize from the market
  • leverageCoefficient - this parameter specifies force the level of leverage for the liqee, e.g. 15 coefficient will force liqee to be 15x
  • matchLeveragedOrder specifies if the long or short should be matched, otherwise the liquidation scenario will play out with the borrow to always stay in the same currency as an open order. If set to true it will double the leverageCoefficient.
  • side specifies whether it should be a long or a short that get's liquidated

Example

async function main() {
  const client = new MangoClient()
  const cluster = 'mainnet-beta'
  const clusterUrl = process.env.CLUSTER_URL || IDS.cluster_urls[cluster]
  const connection = new Connection(clusterUrl, 'singleGossip')
  const programId = new PublicKey(IDS[cluster].mango_program_id)
  const dexProgramId = new PublicKey(IDS[cluster].dex_program_id)
  const mangoGroupPk = new PublicKey(IDS[cluster].mango_groups['BTC_ETH_USDT'].mango_group_pk)

  const keyPairPath = process.env.KEYPAIR || homedir() + '/.config/solana/id.json'

  const payer = new Account(JSON.parse(fs.readFileSync(keyPairPath, 'utf-8')))
  const mangoGroup = await client.getMangoGroup(connection, mangoGroupPk)
  const prices = await mangoGroup.getPrices(connection)
  const marginAccounts = (await client.getMarginAccountsForOwner(connection, programId, mangoGroup, payer))
  marginAccounts.sort(
    (a, b) => (a.computeValue(mangoGroup, prices) > b.computeValue(mangoGroup, prices) ? -1 : 1)
  )
  let marginAccount = marginAccounts[0]

  const market = await Market.load(connection, mangoGroup.spotMarkets[0], { skipPreflight: true, commitment: 'singleGossip'}, mangoGroup.dexProgramId)
  console.log('placing order')
  const txid = await client.placeOrder(connection, programId, mangoGroup, marginAccount, market, payer, 'buy', 48000, 0.0001)
  console.log('order placed')

  await sleep(5000)
  marginAccount = await client.getMarginAccount(connection, marginAccount.publicKey, mangoGroup.dexProgramId)
  const bids = await market.loadBids(connection)
  const asks = await market.loadAsks(connection)
  console.log('canceling orders')
  await marginAccount.cancelAllOrdersByMarket(connection, client, programId, mangoGroup, market, bids, asks, payer)
  console.log('orders canceled')
}

版本与发布记录

@blockworks-foundation/mango-client 共发布 154 个版本,首次发布于 2021/02/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.0latest2023/11/142 年前
3.6.202022/10/044 年前
3.6.192022/09/164 年前
3.6.182022/09/134 年前
3.6.172022/09/074 年前
3.6.162022/09/064 年前

其他发布通道:beta → 3.0.2,old → 2.1.4,next → 3.1.0。

关于 @blockworks-foundation/mango-client 的常见问题

@blockworks-foundation/mango-client 是什么?

Library for interacting with Mango Markets' solana smart contracts.

如何安装 @blockworks-foundation/mango-client?

在项目目录运行 npm install @blockworks-foundation/mango-client,或使用 pnpm add / yarn add / bun add。

@blockworks-foundation/mango-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@blockworks-foundation/mango-client 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@blockworks-foundation/mango-client 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@blockworks-foundation/mango-client 可以商用吗?

@blockworks-foundation/mango-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@blockworks-foundation/mango-client 的下载量有多少?

根据 npm 官方下载统计,@blockworks-foundation/mango-client 在 2026/10/09 – 2026/11/07 这 30 天内被下载 4,079 次,日均约 136 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@blockworks-foundation/mango-client 还在维护吗?

最新版本 2.3.0 发布于 2023/11/14(2 年前);至今共发布 154 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/02/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。