# @mercurial-finance/dynamic-amm-sdk

> Mercurial Vaults SDK is a typescript library that allows you to interact with Mercurial v2's AMM.

Latest version **1.1.23** (published 2024-12-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mercurial-finance/dynamic-amm-sdk
pnpm add @mercurial-finance/dynamic-amm-sdk
yarn add @mercurial-finance/dynamic-amm-sdk
bun add @mercurial-finance/dynamic-amm-sdk
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.1.23 |
| Published | 2024-12-17 |
| First published | 2022-06-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 16 |
| Unpacked size | 1.7 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 125 |
| Author | mercurial-finance |
| Maintainers | siong, zhengyutay, dicksonp, mcsam94, dongnguyen91861, raccoons-bot, thimerosa |
| Keywords | mercurial, finance, dynamic, amm, sdk, solana |

## Links

- npm: https://www.npmjs.com/package/@mercurial-finance/dynamic-amm-sdk
- Repository: https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk
- Homepage: https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk#readme
- Issues: https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/issues
- npm.io page: https://npm.io/package/@mercurial-finance/dynamic-amm-sdk

## Dependencies (16)

- [bn.js](https://npm.io/package/bn.js.md) 5.2.1
- [dotenv](https://npm.io/package/dotenv.md) ^16.0.1
- [bn-sqrt](https://npm.io/package/bn-sqrt.md) ^1.0.0
- [invariant](https://npm.io/package/invariant.md) ^2.2.4
- [decimal.js](https://npm.io/package/decimal.js.md) ^10.4.1
- [@solana/web3.js](https://npm.io/package/@solana/web3.js.md) ^1.95.8
- [@coral-xyz/borsh](https://npm.io/package/@coral-xyz/borsh.md) ^0.28.0
- [@meteora-ag/m3m3](https://npm.io/package/@meteora-ag/m3m3.md) 1.0.4
- [@coral-xyz/anchor](https://npm.io/package/@coral-xyz/anchor.md) ^0.28.0
- [@solana/spl-token](https://npm.io/package/@solana/spl-token.md) ^0.4.6
- [@project-serum/anchor](https://npm.io/package/@project-serum/anchor.md) ^0.24.2
- [@solana/buffer-layout](https://npm.io/package/@solana/buffer-layout.md) ^3 || ^4
- [@solana/spl-token-registry](https://npm.io/package/@solana/spl-token-registry.md) ^0.2.4574
- [@mercurial-finance/vault-sdk](https://npm.io/package/@mercurial-finance/vault-sdk.md) 2.2.0
- [@mercurial-finance/token-math](https://npm.io/package/@mercurial-finance/token-math.md) 6.0.0
- [@metaplex-foundation/mpl-token-metadata](https://npm.io/package/@metaplex-foundation/mpl-token-metadata.md) ~2.13.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.1.23 (latest) — 2024-12-17
- 1.1.23-rc.1 (next) — 2024-12-16
- 0.0.1-7f58e282.0 (develop) — 2022-08-11
- 1.1.23-rc.0 — 2024-12-16
- 1.1.22 — 2024-12-15
- 1.1.21 — 2024-12-15
- 1.1.19 — 2024-12-03
- 1.1.18 — 2024-11-29
- 1.1.17 — 2024-11-28
- 1.1.16-rc.0 — 2024-11-27
- 1.1.16 — 2024-11-27
- 1.1.16-sam.8 — 2024-11-26
- 1.1.16-sam.7 — 2024-11-26
- 1.1.16-sam.6 — 2024-11-26
- 1.1.16-sam.5 — 2024-11-26
- … 615 more at https://npm.io/package/@mercurial-finance/dynamic-amm-sdk/versions

## README

# Mercurial Amm Dynamic SDK

<p align="center">
<img align="center" src="https://vaults.mercurial.finance/icons/logo.svg" width="180" height="180" />
</p>
<br>

## Getting started

NPM: https://www.npmjs.com/package/@mercurial-finance/dynamic-amm-sdk

SDK: https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk

<!-- Docs: https://docs.mercurial.finance/mercurial-dynamic-yield-infra/ -->

Discord: https://discord.com/channels/841152225564950528/864859354335412224

<hr>

## Install

1. Install deps

```
npm i @mercurial-finance/dynamic-amm-sdk @project-serum/anchor @solana/web3.js @solana/spl-token @solana/spl-token-registry
```

2. Initialize AmmImpl instance

```ts
import AmmImpl, { MAINNET_POOL } from '@mercurial-finance/dynamic-amm-sdk';
import { PublicKey } from '@solana/web3.js';
import { Wallet, AnchorProvider } from '@project-serum/anchor';

// Connection, Wallet, and AnchorProvider to interact with the network
const mainnetConnection = new Connection('https://api.mainnet-beta.solana.com');
const mockWallet = new Wallet(new Keypair());
const provider = new AnchorProvider(mainnetConnection, mockWallet, {
  commitment: 'confirmed',
});
// Alternatively, to use Solana Wallet Adapter

const constantProductPool = await AmmImpl.create(connection, MAINNET_POOL.USDC_SOL);
const stablePool = await AmmImpl.create(connection, MAINNET_POOL.USDT_USDC);

// If you need to create multiple, can consider using `createMultiple`
const pools = [MAINNET_POOL.USDC_SOL, MAINNET_POOL.USDT_USDC];
const [constantProductPool, stablePool] = await AmmImpl.createMultiple(connection, pools);
```

3. To interact with the AmmImpl

- Get Lp Supply

```ts
// To refetch the pool's latest supply
// Alternatively, use `AmmImpl.poolState.lpSupply`
const lpSupply = await constantProductPool.getLpSupply();
```

- Check pool balance

```ts
// Get the user's ATA LP balance
const userLpBalance = await constantProductPool.getUserBalance(mockWallet.publicKey);
```

- Update pool state (It's recommended to update the deposit before perform any quotation)

```ts
await constantProduct.updateState();
```

- Deposit to constant product

```ts
const inAmountALamport = new BN(1 * 10 ** constantProductPool.tokenA.decimals); // 1.0 SOL

// Get deposit quote for constant product
const { poolTokenAmountOut, tokenAInAmount, tokenBInAmount } = constantProductPool.getDepositQuote(
  inAmountALamport,
  new BN(0),
);

const depositTx = await constantProductPool.deposit(
  mockWallet.publicKey,
  tokenAInAmount,
  tokenBInAmount,
  poolTokenAmountOut,
); // Web3 Transaction Object
const depositResult = await provider.sendAndConfirm(depositTx); // Transaction hash
```

- Balance deposit to stable pool

```ts
const inAmountALamport = new BN(0.1 * 10 ** stablePool.tokenA.decimals);

const { poolTokenAmountOut, tokenAInAmount, tokenBInAmount } = stablePool.getDepositQuote(inAmountALamport, new BN(0));

const depositTx = await stablePool.deposit(mockWallet.publicKey, tokenAInAmount, tokenBInAmount, poolTokenAmountOut); // Web3 Transaction Object
const depositResult = await provider.sendAndConfirm(depositTx); // Transaction hash
```

- Double side imbalance deposit to stable pool

```ts
const inAmountALamport = new BN(0.1 * 10 ** stablePool.tokenA.decimals);
const inAmountBLamport = new BN(0.1 * 10 ** stablePool.tokenB.decimals);

const { poolTokenAmountOut, tokenAInAmount, tokenBInAmount } = stablePool.getDepositQuote(
  inAmountALamport,
  inAmountBLamport,
  false, // pass in false for imbalance deposit quote
); // Web3 Transaction Object
const depositTx = await stablePool.deposit(mockWallet.publicKey, tokenAInAmount, tokenBInAmount, poolTokenAmountOut);
const depositResult = await provider.sendAndConfirm(depositTx); // Transaction hash
```

- Single side imbalance deposit to stable pool

```ts
const inAmountALamport = new BN(0.1 * 10 ** stablePool.tokenA.decimals);

const { poolTokenAmountOut, tokenAInAmount, tokenBInAmount } = stablePool.getDepositQuote(
  inAmountALamport,
  new BN(0),
  false, // pass in false for imbalance deposit quote
); // Web3 Transaction Object
const depositTx = await stablePool.deposit(mockWallet.publicKey, tokenAInAmount, tokenBInAmount, poolTokenAmountOut);
const depositResult = await provider.sendAndConfirm(depositTx); // Transaction hash
```

- Balance withdraw from constant product

```ts
const outTokenAmountLamport = new BN(0.1 * 10 ** cpPool.decimals);

const { poolTokenAmountIn, tokenAOutAmount, tokenBOutAmount } = cpPool.getWithdrawQuote(outTokenAmountLamport); // use lp balance for full withdrawal
const withdrawTx = await cpPool.withdraw(mockWallet.publicKey, poolTokenAmountIn, tokenAOutAmount, tokenBOutAmount); // Web3 Transaction Object
const withdrawResult = await provider.sendAndConfirm(withdrawTx); // Transaction hash
```

- Balance withdraw from stable pool

```ts
const outTokenAmountLamport = new BN(0.1 * 10 ** stablePool.decimals);

const { poolTokenAmountIn, tokenAOutAmount, tokenBOutAmount } = stablePool.getWithdrawQuote(outTokenAmountLamport); // use lp balance for full withdrawal
const withdrawTx = await stablePool.withdraw(mockWallet.publicKey, poolTokenAmountIn, tokenAOutAmount, tokenBOutAmount); // Web3 Transaction Object
const withdrawResult = await provider.sendAndConfirm(withdrawTx);
```

- Imbalance withdraw from stable pool

```ts
const outTokenAmountLamport = new BN(0.1 * 10 ** stablePool.decimals);

const { poolTokenAmountIn, tokenAOutAmount, tokenBOutAmount } = stablePool.getWithdrawQuote(
  outTokenAmountLamport,
  new PublicKey(stablePool.tokenA.address), // Pass in token A/B mint to perform imbalance withdraw
);
const withdrawTx = await stablePool.withdraw(mockWallet.publicKey, poolTokenAmountIn, tokenAOutAmount, tokenBOutAmount); // Web3 Transaction Object
const withdrawResult = await provider.sendAndConfirm(withdrawTx);
```

- Swap

```ts
const inAmountLamport = new BN(0.1 * 10 ** constantProductPool.tokenB.decimals);

// Swap SOL → USDT
const { minSwapOutAmount } = constantProductPool.getSwapQuote(
  new PublicKey(constantProductPool.tokenB.address),
  inAmountLamport,
);

const swapTx = await constantProductPool.swap(
  mockWallet.publicKey,
  new PublicKey(constantProductPool.tokenB.address),
  inAmountLamport,
  minSwapOutAmount,
);
const swapResult = await provider.sendAndConfirm(swapTx);
```

- Update pool state

```ts
constantProductPool.updateState();
```

---
_Source: https://npm.io/package/@mercurial-finance/dynamic-amm-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
