3.0.0-beta.0 • Published 3 months ago

@jup-ag/dca-sdk v3.0.0-beta.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 months ago

Jupiter DCA SDK

This SDK is an abstraction to interact with Jup's DCA Program.

Install

npm i @jup-ag/dca-sdk

Example Usage

import { CloseDCAParams, CreateDCAParamsV2, DCA, DepositParams, WithdrawParams, Network } from 'jup-dca-sdk';
import { Connection, Keypair, PublicKey, sendAndConfirmTransaction } from '@solana/web3.js';
import dotenv from 'dotenv';
dotenv.config();

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

const dca = new DCA(connection, Network.DEVNET);

const user = Keypair.fromSecretKey(new Uint8Array(JSON.parse(process.env.USER_PRIVATE_KEY)));

const USDC_DEVNET = new PublicKey('Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr');

const RANDOM_TOKEN = new PublicKey('EJwZgeZrdC8TXTQbQBoL6bfuAnFUUy1PVCMB4DYPzVaS');

async function createDCA() {
  const params: CreateDCAParamsV2 = {
    payer: payer.publicKey, // this is the payer of the tx aka opening on behalf. this can be user as well ie user.publicKey
    user: user.publicKey,
    inAmount: BigInt(5_000_000),
    inAmountPerCycle: BigInt(1_000_000),
    cycleSecondsApart: BigInt(86400),
    inputMint: USDC_DEVNET,
    outputMint: RANDOM_TOKEN,
    minOutAmountPerCycle: null,
    maxOutAmountPerCycle: null,
    startAt: null,
    userInTokenAccount, // optional: Public key of user's input_mint token account. if the user's input_mint token account is an associated token account, feel free to leave this out. if it's not an ATA, then make sure to pass it in.
  };

  const { tx, dcaPubKey } = await dca.createDcaV2(params);
  const txid = await sendAndConfirmTransaction(connection, tx, [user, payer]);

  console.log('Create DCA: ', { txid });

  return dcaPubKey;
}

/*
This is for withdrawing from your DCA account.
By default, output tokens are sent to you account automatically.
Withdraw is useful if you want to partially withdraw the input token but would still like to continue the DCA.
Or if for some reason, the output token was not sent to you automatically, you can use this instruction to withdraw them at anytime.
If you want to stop the DCA completely, use `closeDCA` instead (see below).
*/
async function withdraw(dcaPubKey) {
  // it's possible to withdraw in-tokens only or out-tokens only or both in and out tokens together. See WithdrawParams for more details
  const params: WithdrawParams = {
    user: user.publicKey,
    dca: dcaPubKey,
    inputMint: USDC_DEVNET,
    withdrawInAmount: BigInt(1_000_000),
  };

  const { tx } = await dca.withdraw(params);

  const txid = await sendAndConfirmTransaction(connection, tx, [user]);

  console.log('Withdraw: ', { txid });
}

async function closeDCA(dcaPubKey) {
  const params: CloseDCAParams = {
    user: user.publicKey,
    dca: dcaPubKey,
  };

  const { tx } = await dca.closeDCA(params);

  const txid = await sendAndConfirmTransaction(connection, tx, [user]);

  console.log('Close DCA: ', { txid });
}

async function main() {
  const dcaPubKey = await createDCA();
  console.log('DCA Pub Key: ', { dcaPubKey });

  const dcaAccount = await dca.fetchDCA(dcaPubKey);
  console.log('DCA Account Data: ', { dcaAccount });

  const dcaAccounts = await dca.getCurrentByUser(user.publicKey);
  console.log({ dcaAccounts });

  await dca.getBalancesByAccount(dcaPubKey);

  await withdraw(dcaPubKey);

  await closeDCA(dcaPubKey);
}

main();

Change Log

2.0.7

  • Renamed getAllByUser to getCurrentByUser. This method name reflects more accurately the return value of the method -> returns all active DCA accounts (not closed)
  • Added getClosedByUser. This method returns DCA Accounts that are already closed and not returned by getCurrentByUser.
    • getClosedByUser returns different values from getCurrentByUser because it is no longer on the blockchain. E.g.:
      [
          {
            createdAt: <BN: 64820460>,
            updatedAt: <BN: 6487378a>,
            userKey: [PublicKey [PublicKey(EKnD4oLXP3B2uDigQiUmQHEBX6fBt33kJBWjQRmVgpXB)]],
            dcaKey: [PublicKey [PublicKey(Dtkhyuhv5X3nPtU47JSTh4PRQLo5vuUYdHz6vtvX6b1a)]],
            inputMint: [PublicKey [PublicKey(EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v)]],
            outputMint: [PublicKey [PublicKey(So11111111111111111111111111111111111111112)]],
            inDeposited: <BN: 989680>,
            inAmountPerCycle: <BN: 1e8480>,
            cycleFrequency: <BN: 3c>
          },
          {
            createdAt: <BN: 64873dbb>,
            updatedAt: <BN: 64873ef2>,
            userKey: [PublicKey [PublicKey(EKnD4oLXP3B2uDigQiUmQHEBX6fBt33kJBWjQRmVgpXB)]],
            dcaKey: [PublicKey [PublicKey(AK7fnjQW3XPcy13NjcAp1mbUCezUTkSvxcabYvj83HiL)]],
            inputMint: [PublicKey [PublicKey(EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v)]],
            outputMint: [PublicKey [PublicKey(So11111111111111111111111111111111111111112)]],
            inDeposited: <BN: 989680>,
            inAmountPerCycle: <BN: 1e8480>,
            cycleFrequency: <BN: 3c>
          }
        ]
    • You can stil get fill history of the closed account with getFillHistory e.g. getFillHistory(closedDcaAccounts[0].publicKey)
  • Added trackDca. Not meant to be used directly. This automatically gets called when creating a new DCA account so that our tracker keeps a off chain record of the DCA Account data and maintain its data even after the dca account has been deleted on-chain
3.0.0-beta.1

3 months ago

3.0.0-beta.0

3 months ago

2.3.19

4 months ago

2.3.18

4 months ago

2.3.17

4 months ago

2.3.16

4 months ago

2.3.13

4 months ago

2.3.15

4 months ago

2.3.14

4 months ago

2.3.12

4 months ago

2.3.11

4 months ago

2.3.9

4 months ago

2.3.10

4 months ago

2.3.8

5 months ago

2.3.7

5 months ago

2.3.6

5 months ago

3.1.6-alpha

5 months ago

3.1.5-alpha

5 months ago

3.1.4-alpha

5 months ago

3.0.1-alpha

6 months ago

3.0.8-alpha

5 months ago

3.0.4-alpha

5 months ago

3.0.7-alpha

5 months ago

3.0.0-alpha

6 months ago

3.0.6-alpha

5 months ago

3.1.1-alpha

5 months ago

3.0.3-alpha

5 months ago

3.0.9-alpha

5 months ago

2.3.0

9 months ago

2.3.2

7 months ago

2.3.1

7 months ago

2.3.4

7 months ago

2.3.3

7 months ago

2.3.5

7 months ago

3.1.0-alpha

5 months ago

2.2.9

9 months ago

2.2.8

10 months ago

3.0.2-alpha

6 months ago

3.1.3-alpha

5 months ago

3.0.5-alpha

5 months ago

0.3.2-alpha

1 year ago

0.2.7-alpha

1 year ago

2.2.1

11 months ago

2.0.3

11 months ago

2.2.0

11 months ago

2.0.2

12 months ago

2.2.3

11 months ago

2.0.5

11 months ago

2.2.2

11 months ago

2.0.4

11 months ago

2.2.5

11 months ago

2.0.7

11 months ago

2.2.4

11 months ago

2.0.6

11 months ago

2.2.7

11 months ago

2.0.9

11 months ago

2.2.6

11 months ago

2.0.8

11 months ago

0.3.5-alpha

1 year ago

2.0.1

12 months ago

2.0.0

12 months ago

0.3.1-alpha

1 year ago

2.1.9

11 months ago

0.3.4-alpha

1 year ago

0.2.6-alpha

1 year ago

0.3.7-alpha

12 months ago

2.1.2

11 months ago

2.1.1

11 months ago

0.2.9-alpha

1 year ago

2.1.4

11 months ago

2.1.3

11 months ago

2.1.6

11 months ago

2.1.5

11 months ago

2.1.8

11 months ago

2.1.7

11 months ago

0.3.0-alpha

1 year ago

2.1.0

11 months ago

0.3.6-alpha

12 months ago

0.2.8-alpha

1 year ago

0.3.3-alpha

1 year ago

0.2.5-alpha

1 year ago

0.2.4-alpha

1 year ago

0.2.3-alpha

1 year ago

0.2.2-alpha

1 year ago

0.2.1-alpha

1 year ago

0.2.0-alpha

1 year ago

0.1.9-alpha

1 year ago

0.1.8-alpha

1 year ago