0.1.8-alpha • Published 1 year ago

jup-dca-sdk v0.1.8-alpha

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Install

npm i jup-dca-sdk

Example Usage

import { CloseDCAParams, CreateDCAParams, 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: CreateDCAParams = {
    user: user.publicKey,
    inAmount: BigInt(5_000_000),
    inAmountPerCycle: BigInt(1_000_000),
    cycleSecondsApart: BigInt(86400),
    inputMint: USDC_DEVNET,
    outputMint: RANDOM_TOKEN,
    minPrice: null,
    maxPrice: null,
    startAt: null
  }

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

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

  return dcaPubKey
}

// this is for topping up the DCA account
async function deposit(dcaPubKey) {
  const params: DepositParams = {
    user: user.publicKey,
    dca: dcaPubKey,
    amount: BigInt(1_000_000)
  }

  const { tx } = await dca.deposit(params)

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

  console.log("Deposit: ", { txid })
}

// this is for withdrawing from program ATA
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.getAllByUser(user.publicKey);
  console.log({ dcaAccounts })

  await dca.getBalancesByAccount(dcaPubKey)

  await deposit(dcaPubKey)

  await withdraw(dcaPubKey)

  await closeDCA(dcaPubKey)
}

main()

To-Do

  1. For deposit, check that user has sufficient inToken?
  2. For withdrawal, check that user's dca accounts has sufficient tokens
0.1.8-alpha

1 year ago

0.1.7-alpha

1 year ago

0.1.6-alpha

1 year ago

0.1.5-alpha

1 year ago

0.1.4-alpha

1 year ago

0.1.3-alpha

1 year ago

0.1.2-alpha

1 year ago

0.1.1-alpha

1 year ago

0.1.0-alpha

1 year ago

0.0.9-alpha

1 year ago

0.0.8-alpha

1 year ago

0.0.7-alpha

1 year ago

0.0.6-alpha

1 year ago

0.0.5-alpha

1 year ago

0.0.4-alpha

1 year ago

0.0.3-alpha

1 year ago

0.0.2-alpha

1 year ago

0.0.1-alpha

1 year ago

1.0.0

1 year ago