npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

@dhedge/v2-sdk

v1.10.1

Published

๐Ÿ›  An SDK for building applications on top of dHEDGE V2

Downloads

606

Readme

dHEDGE V2 SDK

๐Ÿ›  A complete implementation for building applications on top of dHEDGE V2

Features:

  • Built with ethers.js
  • Create and manage V2 pools from background app or your own dApp
  • Easy-to-use functions to trade assets, provide liquidity or stake assets
  • Useful for creating automated trading bots
  • Use in your Javascript or Typescript project with full Typescript source
  • All protocols and networks of dHEDGE App are supported

Installation

Node

npm install @dhedge/v2-sdk

Yarn

yarn add @dhedge/v2-sdk

Usage

Table of Contents

Initial setup


If you want to use 1Inch to trade pool assets you need to apply for an API key at 1Inch Dev Portal. Then you need to copy .env.example file to .env and set your API key there.

ONEINCH_API_KEY=YOUR_API_KEY_FROM_1INCH

Initialize the sdk with an ethers wallet and the network.

import { Dhedge, Dapp, Network, ethers } from "@dhedge/v2-sdk";

const privateKey = "YOUR_PRIVATE_KEY";
const providerUrl = "https://polygon-mainnet.infura.io/v3/{YOUR_PROJECT_ID}"

const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const walletWithProvider = new ethers.Wallet(privateKey, provider);

const dhedge = new Dhedge(walletWithProvider, Network.POLYGON);

General Pool Management


1. Create a pool

USDC and DAI enabled assets, but only USDC available for deposit.

const usdcTokenAddress = "USDC_TOKEN_ADDRESS"
const daiTokenAddress = "DAI_TOKEN_ADDRESS"
const pool = await dhedge.createPool(
  "Day Ralio",
  "Awesome Fund",
  "DRAF",
  [
    [usdcTokenAddress, true],
    [daiTokenAddress, false],
  ],
  10
)
console.log("created pool with address", pool.address)

2. Load pool

const poolAddress = "YOUR_POOL_ADDRESS"
const pool = await dhedge.loadPool(poolAddress)

3. Get pool composition

const composition = await pool.getComposition();

4. Change pool assets (enable/disable)

Change pool assets to allow DAI for deposits. Also enable WETH as an asset, but shouldn't be allowed as deposit.

const enabledAssets = [
  { asset: "USDC_TOKEN_ADDRESS", isDeposit: true },
  { asset: "DAI_TOKEN_ADDRESS", isDeposit: true },
  { asset: "WETH_TOKEN_ADDRESS", isDeposit: false },
]
const tx = await pool.changeAssets(enabledAssets)

5. Set trader

Set an account with trading permissions

const tx = await pool.setTrader("TRADER_ACCOUNT_ADDRESS")

6. Approve asset for deposit

Before depositing an asset into a Pool, it needs to be approved.

Approve unlimited amount of USDC to deposit into Pool.

const tx = await pool.approveDeposit("USDC_TOKEN_ADDRESS", ethers.constants.MaxUint256);

7. Deposit asset into pool

Deposit 1 USDC into Pool

const usdcDepositAmount = "100000"
const tx = await pool.deposit("USDC_TOKEN_ADDRESS", usdcDepositAmount);

8. Withdraw from pool

Withdraw 1.00002975 pool tokens. Note that this cannot be called if set as Trader account

const poolTokensWithdrawAmount = "1000029750000000000"
const tx = await pool.withdraw(poolTokensWithdrawAmount);

9. Approve pool asset for trading & staking

Before trading an asset on platforms like Sushiswap it needs to be approved.

Approve unlimited amount of USDC to trade on Sushiswap

const tx = await pool.approve(
  Dapp.SUSHISWAP,
  "USDC_TOKEN_ADDRESS",
  ethers.constants.MaxInt256
)

10. Trade pool assets

Trade 1 USDC into DAI on Sushiswap (other options: TOROS, QUICKSWAP, BALANCER, or ONEINCH)

const amountIn = "1000000"
const slippage = 0.5
const tx = await pool.trade(
  Dapp.SUSHISWAP,
  "USDC_TOKEN_ADDRESS",
  "DAI_TOKEN_ADDRESS",
  amountIn,
  slippage
)

Liquidity


Uniswap-v2 style

For Uniswap-v2 like protocols, such as sushiswap, we use addLiquidity, removeLiquidity, stake, and unstake, and harvestRewards

  1. Add USDC/DAI into a Sushiswap liquidity pool
const amountUsdc = "1000000"
const amountDai = "997085"
const tx = await pool.addLiquidity(
  Dapp.SUSHISWAP,
  "USDC_TOKEN_ADDRESS",
  "DAI_TOKEN_ADDRESS",
  amountUsdc,
  amountDai
)
  1. Remove USDC/DAI worth of 1 Sushiswap LP from the liquidity pool
const amountSlpUsdcDai = "1000000000000000000"
const tx = await pool.removeLiquidity(
  Dapp.SUSHISWAP,
  "USDC_TOKEN_ADDRESS",
  "DAI_TOKEN_ADDRESS",
  amountSlpUsdcDai
)
  1. Approve unlimited amound of SLP USDC-DAI token for staking on Sushiswap
const tx = await pool.approveStaking(
  Dapp.SUSHISWAP,
  "SLP_USDC_DAI_TOKEN_ADDRESS",
  ethers.constants.MaxInt256
)
  1. Stake 1 Sushiswap LP USDC/DAI token
const amountSlpUsdcDai = "1000000000000000000"
const tx = await pool.stake(
  Dapp.SUSHISWAP,
  "SLP_USDC_DAI_TOKEN_ADDRESS",
  amountSlpUsdcDai
)
  1. Unstake 1 Sushiswap LP USDC/DAI token
const amountSlpUsdcDai = "1000000000000000000"
const tx = await pool.unstake(
  Dapp.SUSHISWAP,
  "SLP_USDC_DAI_TOKEN_ADDRESS",
  amountSlpUsdcDai
)
  1. Harvest rewards from staked Sushiswap LP USDC/DAI tokens
const tx = await pool.harvestRewards(
  Dapp.SUSHISWAP,
  "SLP_USDC_DAI_TOKEN_ADDRESS"
)

Balancer

For Balancer, we use joinBalancerPool, exitBalancerPool, and harvestBalancerRewards

  1. Add 0.00002 WBTC, 1 USDC and 0.0002 WETH to a WBTC/USDC/WETH Balancer pool
const balancerPoolId = "0x03cd191f589d12b0582a99808cf19851e468e6b500010000000000000000000a"
const assets = [WBTC_TOKEN_ADDRESS, USDC_TOKEN_ADDRESS, WETH_TOKEN_ADDRESS];
const amounts = ["2000", "1000000", "200000000000000"];
const tx = await pool.joinBalancerPool(balancerPoolId, assets, amounts)
  1. Remove all tokens from WBTC/USDC/WETH Balancer pool
const amount = await dhedge.utils.getBalance(BALANCER_LP_TOKEN_ADDRESS, pool.address)
const tx = await pool.exitBalancerPool(balancerPoolId, assets, amount)
  1. Harvest rewards from Balancer
const tx = await pool.harvestBalancerRewards()

Uniswap-v3 style

For Arrakis, we use increaseLiquidity to stake or increase lp, and decreaseLiquidity, and claimFees. see example in the arrakis test


For Uniswap v3, we use approveUniswapV3Liquidity, addLiquidityUniswapV3, decreaseLiquidity, increaseLiquidity, and claimFees.

  1. Add liquidity of 100 USDC and 0.00043 WETH to a UniswapV3 pool (here price range is used)

await pool.approveUniswapV3Liquidity(
  USDC_ADDRESS,
  ethers.constants.MaxInt256
);
await pool.approveUniswapV3Liquidity(
  WETH_ADDRESS,
  ethers.constants.MaxInt256
);
const tx = await pool.addLiquidityUniswapV3(
  WETH_ADDRESS
  USDC_ADDRESS,
  '430000000000000', // wethBalance
  '100000000',     // usdcBalance
  2000,
  3000,
  null,
  null,
  FeeAmount.MEDIUM,
)
  1. Remove 50% liquidity from the existing pool
tokenId = await nonfungiblePositionManager.tokenOfOwnerByIndex(pool.address,0).toString();
const tx = await pool.decreaseLiquidity(
  Dapp.UNISWAPV3,
  tokenId,
  50 // precent
);
  1. Increase liquidity in the existing WETH/USDC pool
const result = await pool.increaseLiquidity(
  Dapp.UNISWAPV3,
  tokenId,
  new BigNumber(3000).times(1e6).toFixed(0), // usdc
  new BigNumber(1).times(1e18).toFixed(0) // eth
);
  1. Claim fees
const tx = await pool.claimFees(Dapp.UNISWAPV3, tokenId);

VelodromeV2 / Ramses / Aerodrome

For VelodromeV2 / Ramses / Aerodrome , we use addLiquidityV2, stakeInGauge, unstakeFromGauge, removeLiquidityV2, and claimFees.

Add liquidity of 100 USDC and 0.00043 WETH to USDC/WETH Ramses pool (for Velodrome just use Dapp.VELODROMEV2, for Aerodrome Dapp.AERODROME). see example in the arrakis test, velodromeV2 test and aerdodrome test

const tx = await pool.addLiquidityV2(
  Dapp.RAMSES,
  USDC_ADDRESS,
  WETH_ADDRESS,
  '10000000',
  '430000000000000',
  false
)

Lending/Borrowing Aave


For Aave, we use lend, withdrawDeposit, borrow and repay

1. Deposit 1 USDC into Aave lending pool
const tx = await pool.lend(Dapp.AAVE, USDC_TOKEN_ADDRESS, "1000000")
2. Withdraw 1 USDC from Aave lending pool
const tx = await pool.withdrawDeposit(Dapp.AAVE, USDC_TOKEN_ADDRESS, "1000000")
3. Borrow 0.0001 WETH from Aave lending pool
const tx = await pool.borrow(Dapp.AAVE, WETH_TOKEN_ADDRESS, "100000000000000");
4. Repay 0.0001 WETH to Aave lending pool
const tx = await pool.repay(Dapp.AAVE, WETH_TOKEN_ADDRESS, "100000000000000");

The Flat Money Protocol


1. Minting UNIT : deposit 1 rETH to mint UNIT
      const depositAmount = new BigNumber(1).times(1e18).toString();
      const tx = await pool.mintUnitViaFlatMoney(
        depositAmount,
        0.5,  // slippage, 0.5%
        5,  // maxKeeperFeeInUsd, $5
      );
2. Redeeming UNIT : Redeem 1 UNIT for rETH
      const withdrawAmount = new BigNumber(1).times(1e18).toString();
      const tx = await pool.redeemUnitViaFlatMoney(
        withdrawAmount,
        0.5,  // slippage, 0.5%
        5, // maxKeeperFeeInUsd, $5
      );
3. Cancel announced order
      await pool.cancelOrderViaFlatMoney();