6.0.0 • Published 5 months ago

@uxd-protocol/uxd-client v6.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

npm

JavaScript Client for the UXD Solana Program

Install

npm install --save @uxd-protocol/uxd-client

Usage

About UXD on Solana

Mainnet-Beta

  • UXD Program: UXD8m9cvwk4RcSxnX2HZ9VudQCEeDH6fRnB4CAP57Dr
  • UXD Mint: 7kbnvuGBxxj8AG9qp8Scn56muWGaRaFqxg1FsRp3PaFT

Devnet

  • UXD Program: BA67esrWE7cPzQWtAftaTbrVWtmHZJ1PbbBBpZgpjH4p
  • UXD Mint: B8fvu55oqVmPzRFjo99wvnPXkuA2AcFBou5tstpeHFaR

Instantiate the Controller Object

import { Controller } from '@uxd-protocol/uxd-client';

const controller = new Controller(
  'UXD',
  6
  'UXD8m9cvwk4RcSxnX2HZ9VudQCEeDH6fRnB4CAP57Dr'
);

Instantiate a Solana Connection

import { Connection } from '@solana/web3.js';

const connection = new Connection('https://api.mainnet-beta.solana.com');

Create and initialize the Mango object

import { createAndInitializeMango } from '@uxd-protocol/uxd-client';

const mango = createAndInitializeMango(
  connection,
  'mainnet' // 'devnet'
);

Instantiate a MangoDepository Object for a given UXD collateral, ie WSOL

import {
  WSOL,
  SOL_DECIMALS,
  USDC,
  USDC_DECIMALS,
  MangoDepository,
} from '@uxd-protocol/uxd-client';

return new MangoDepository(
  WSOL,
  'SOL',
  SOL_DECIMALS,
  USDC, // Use mainnet mint, must be matching the program used (see USDC_DEVNET)
  'USDC',
  USDC_DECIMALS,
  'UXD8m9cvwk4RcSxnX2HZ9VudQCEeDH6fRnB4CAP57Dr' // Mainnet program
);

Instantiate the UXD Client

import { UXDClient } from '@uxd-protocol/uxd-client';

const client = new UXDClient('UXD8m9cvwk4RcSxnX2HZ9VudQCEeDH6fRnB4CAP57Dr');

Mint 1 SOL worth of UXD on the SOL Mango Depository

import { Transaction } from '@solana/web3.js';

const transaction = new Transaction();
const mintWithMangoDepositoryIx =
  client.createMintWithMangoDepositoryInstruction(
    1, // UI amount of collateral to use to mint UXD
    5, // 0.5% slippage in points
    controller,
    depository, // matching Mango Depository for the collateral
    mango,
    user // PublicKey of recipient
    {}, // ConfirmOptions, leave empty for defaults
    payer, // PublicKey of payer, optional, defaults to user
  );

transaction.add(mintWithMangoDepositoryIx);

// sign, send & confirm transaction

Redeem UXD on the SOL Mango Depository

import { Transaction } from '@solana/web3.js';

const transaction = new Transaction();
const redeemFromMangoDepositoryIx =
  client.createRedeemFromMangoDepositoryInstruction(
    10, // UI amount of UXD to redeem (returned as the provided depository Collateral, here SOL))
    5, // 0.5% slippage in points
    controller,
    depository, // matching Mango Depository for the collateral
    mango,
    user // PublicKey of recipient
    {}, // ConfirmOptions, leave empty for defaults
    payer, // PublicKey of payer, optional, defaults to user
  );

transaction.add(redeemFromMangoDepositoryIx)

// sign, send & confirm transaction

Rebalance an UXD Mango Depository

This can be used as a Swap. Depending of the polarity of the PnL of the depository, this instruction will switch Quote->Collateral or Collateral->Quote.

For instance, when the PnL is Positive (Collateral price went up since minting), user can provide USDC. By doing so, UXDProgram will offset the PnL of an equal amount, close equivalent short position, and return the backing collateral amount.

It's a 4bps + slippage + spread swap (as we use the Perp price for all operations).

You can find detailed information about the current state of depositories on the UXD Backoffice.

import { Transaction } from '@solana/web3.js';

const unrealizedPnl = await depository.getUnrealizedPnl(mango, TXN_OPTS);
const polarity = unrealizedPnl > 0 ? PnLPolarity.Positive : PnLPolarity.Negative;

const transaction = new Transaction();
const rebalanceLiteMangoDepositoryIx =
  client.createRebalanceMangoDepositoryLiteInstruction(
    10, // UI amount in Quote to rebalance (Quote on mango is USDC)
    5, // 0.5% slippage in points
    PnLPolarity.positive, // The polarity of the PnL, the expected direction of the swap (can be enforce program side but here to be explicit)
    controller,
    depository, // matching Mango Depository for the collateral
    mango,
    user // PublicKey of recipient
    {}, // ConfirmOptions, leave empty for defaults
    payer, // PublicKey of payer, optional, defaults to user
  );

transaction.add(redeemFromMangoDepositoryIx)

// sign, send & confirm transaction
9.0.0-rc6

5 months ago

9.0.0-rc5

5 months ago

8.1.0-rc1

10 months ago

8.1.0-rc2

9 months ago

8.1.4-rc7

9 months ago

8.1.4-rc5

9 months ago

8.1.4-rc6

9 months ago

8.1.4-rc3

9 months ago

8.1.4-rc4

9 months ago

8.1.4-rc1

9 months ago

8.3.0-rc1

6 months ago

8.1.3-rc1

9 months ago

7.3.1-rc2

10 months ago

8.1.1-rc1

9 months ago

8.1.1-rc2

9 months ago

8.2.0-rc2

8 months ago

9.0.0-rc1

6 months ago

9.0.0-rc2

6 months ago

9.0.0-rc3

5 months ago

9.0.0-rc4

5 months ago

8.0.0-rc1

11 months ago

7.1.0

12 months ago

7.0.8

1 year ago

7.0.9

1 year ago

7.2.0-rc2

11 months ago

7.2.0-rc1

11 months ago

7.1.0-rc1

12 months ago

7.3.1-rc1

11 months ago

5.1.4

1 year ago

5.1.3

2 years ago

5.1.2

2 years ago

5.1.1

2 years ago

6.1.0

1 year ago

6.1.1

1 year ago

4.0.0-alpha.4

2 years ago

6.0.0-maple-rc1

2 years ago

4.3.0-alpha.1

2 years ago

4.0.0-maple-3

2 years ago

4.0.0-maple-2

2 years ago

4.0.0-maple-1

2 years ago

4.2.0-alpha.2

2 years ago

4.2.0-alpha.1

2 years ago

5.2.0-alpha.1

1 year ago

5.0.0

2 years ago

6.0.0

1 year ago

5.2.0-alpha.2

1 year ago

5.1.0-alpha.1

2 years ago

4.1.0-alpha.5

2 years ago

5.1.0-alpha.2

2 years ago

4.1.0-alpha.4

2 years ago

4.1.0-alpha.6

2 years ago

4.1.0

2 years ago

4.1.1

2 years ago

4.1.0-alpha.3

2 years ago

4.1.0-alpha.2

2 years ago

4.0.0-10

2 years ago

3.0.0-beta.1

2 years ago

4.0.0-11

2 years ago

4.0.0-dev.vb.1

2 years ago

4.0.0-alpha.3

2 years ago

4.0.0-0

2 years ago

4.0.0-alpha.1

2 years ago

4.0.0-3

2 years ago

4.0.0-4

2 years ago

4.0.0-1

2 years ago

4.0.0-2

2 years ago

4.0.0-5

2 years ago

4.0.0-9

2 years ago

3.0.0

2 years ago

4.1.0-alpha.1

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0-rc.1

2 years ago

2.0.0

2 years ago

2.0.0-beta.3

2 years ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

1.0.0

2 years ago

1.0.0-alpha.1

2 years ago