# @mercurial-finance/vault-sdk

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

Latest version **2.2.1** (published 2024-10-09) · MIT license · 18.7K weekly downloads

## Install

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

## Health

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

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.2.1 |
| Published | 2024-10-09 |
| First published | 2022-06-08 |
| Weekly downloads | 18.7K |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 9 |
| Unpacked size | 485.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 35 |
| Author | mercurial-finance |
| Maintainers | thimerosa, raccoons-bot, mcsam94, dicksonp, siong, zhengyutay |

## Links

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

## Dependencies (9)

- [jsbi](https://npm.io/package/jsbi.md) 4.3.0
- [bn.js](https://npm.io/package/bn.js.md) 5.2.1
- [decimal.js](https://npm.io/package/decimal.js.md) 10.3.1
- [cross-fetch](https://npm.io/package/cross-fetch.md) ^3.1.5
- [@solana/web3.js](https://npm.io/package/@solana/web3.js.md) ^1.91.6
- [@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
- [@solana/buffer-layout](https://npm.io/package/@solana/buffer-layout.md) ^4.0.1
- [@solana/spl-token-registry](https://npm.io/package/@solana/spl-token-registry.md) ^0.2.4574

## Recent versions

- 2.2.1 (latest) — 2024-10-09
- 2.1.2-rc.0 (next) — 2024-08-23
- 0.4.5 (alpha.0.4.5.123) — 2023-05-18
- 0.4.4 (alpha) — 2023-05-18
- 0.4.3 (alpha.0.4.4) — 2023-05-18
- 0.2.18-develop (develop) — 2022-07-05
- 2.2.0 — 2024-08-23
- 2.1.1 — 2024-08-23
- 2.1.1-rc.0 — 2024-08-23
- 2.1.0 — 2024-08-19
- 2.1.1-test.0 — 2024-08-19
- 2.0.2-test.0 — 2024-08-19
- 2.0.1 — 2024-08-14
- 2.0.1-rc.1 — 2024-08-13
- 2.0.1-rc.0 — 2024-08-13
- … 300 more at https://npm.io/package/@mercurial-finance/vault-sdk/versions

## README

# Mercurial Vault 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/vault-sdk

SDK: https://github.com/mercurial-finance/vault-sdk

Demo: https://vault-sdk-demo.vercel.app/

Demo repo: https://github.com/mercurial-finance/vault-sdk-demo
- Easiest way to get started with our Typescript SDK, the example demo includes all functionality and information we display on our own site.

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/vault-sdk @project-serum/anchor @solana/web3.js @solana/spl-token @solana/spl-token-registry
```

2. Initialize VaultImpl instance
- Affiliate or partner? refer to the [Vault Affiliate Program]()
```ts
import VaultImpl from '@mercurial-finance/vault-sdk';
import { PublicKey } from '@solana/web3.js';
import { StaticTokenListResolutionStrategy, TokenInfo } from "@solana/spl-token-registry";
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, refer to `Demo Repo`

const tokenMap = new StaticTokenListResolutionStrategy().resolve();
// Find the token info you want to use.
const SOL_TOKEN_INFO = tokenMap.find(token => token.symbol === 'SOL') as TokenInfo;
const vaultImpl = await VaultImpl.create(connection, SOL_TOKEN_INFO);
```

3. To interact with the VaultImpl
```ts
// To refetch the vault's latest supply
// Alternatively, use `vaultImpl.lpSupply`
const lpSupply = await vaultImpl.getVaultSupply();

// Rewards are not instantly redeemable, and are subject to a lock.
// This function returns the amount of LP that are redeemable.
const unlockedAmount = await vaultImpl.getWithdrawableAmount()

// To deposit into the vault
const amountInLamports = 1 * 10 ** SOL_TOKEN_INFO.decimals; // 1.0 SOL
const depositTx = await vaultImpl.deposit(mockWallet.publicKey, new BN(amountInLamports)); // Web3 Transaction Object
const depositResult = await provider.sendAndConfirm(depositTx); // Transaction hash

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

// To withdraw from the vault
const withdrawTx = await vaultImpl.withdraw(mockWallet.publicKey, new BN(userLpBalance)); // Web3 Transaction Object
const withdrawResult = await provider.sendAndConfirm(withdrawTx); // Transaction hash
```

4. Helper function
```ts
import { helper } from '@mercurial-finance/vault-sdk';

const userShare = await vaultImpl.getUserBalance(mockWallet.publicKey);
const unlockedAmount = await vaultImpl.getWithdrawableAmount()
const lpSupply = await vaultImpl.getVaultSupply();

// To convert user's LP balance into underlying token amount
const underlyingShare = helper.getAmountByShare(userShare, unlockedAmount, lpSupply)

// To convert underlying token amount into user's LP balance
const amountInLamports = 1 * 10 ** SOL_TOKEN_INFO.decimals; // 1.0 SOL
const lpToUnmint = helper.getUnmintAmount(new BN(amountInLamports), unlockedAmount, lpSupply) // To withdraw 1.0 SOL
```

<hr>

## Vault Affiliate
To be a part of the Mercurial Finance's Vault Affiliate Program, visit our Discord above!

<br>

#### To initialize vault with affiliate
Affiliates only need to initialize the vault instance with the third paratemer `opt.affiliate`, subsequently, all interaction with the vault are the same as the usage guide above, no further configuration required.

```ts
const vaultImpl = await VaultImpl.create(
    connection, 
    SOL_TOKEN_INFO,
    {
        affiliateId: new PublicKey('YOUR_PARTNER_PUBLIC_KEY');
    }
);
```

#### To check Partner info
```ts
// Affiliate / Partner info
const partnerInfo = await vaultImpl.getAffiliateInfo();
```

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