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

@hifi/flash-swap

v1.13.0

Published

Flash swap implementations for liquidating underwater accounts

Downloads

153

Readme

Hifi Flash Swap npm (scoped)

Flash swap implementations for liquidating underwater accounts.

The build artifacts can be browsed via unpkg.com.

Installation

With yarn:

$ yarn add @hifi/flash-swap

Or npm:

$ npm install @hifi/flash-swap

Usage

The node package that you just installed contains both Solidity and JavaScript code. The former is the smart contracts themselves; the latter, the smart contract ABIs and the TypeChain bindings.

FlashUniswapV2

Solidity

You are not supposed to import the smart contracts. Instead, you should interact with the Uniswap pool directly. For example, with the UniswapV2Pair contract you would call the swap function, and then Uniswap will forward the call to the FlashUniswapV2 contract. You can read more about flash swaps work in Uniswap V2 on docs.uniswap.org.

JavaScript

Example for Uniswap V2:

import { defaultAbiCoder } from "@ethersproject/abi";
import { parseUnits } from "@ethersproject/units";
import { UniswapV2Pair__factory } from "@hifi/flash-swap/dist/types/factories/contracts/UniswapV2Pair__factory";

async function flashSwap() {
  const signer = "..."; // Get hold of an ethers.js Signer
  const pairFactory = new UniswapV2Pair__factory(signer);
  const pair = pairFactory.attach("0x...");

  const token0Amount = parseUnits("100", 18);
  const token1Amount = parseUnits("0", 18);
  const to = "0x..."; // Address of FlashUniswapV2, get it from https://docs.hifi.finance

  const borrower = "0x...";
  const hToken = "0x...";
  const collateral = "0x...";
  const turnout = parseUnits("1", 18);
  const data = defaultAbiCoder.encode(
    ["address", "address", "address", "uint256"],
    [borrower, hToken, collateral, turnout],
  );

  await pair.swap(token0Amount, token1Amount, to, data);
}

FlashUniswapV3

Solidity

To interact with the FlashUniswapV3 contract, you will call the flashLiquidate function directly. This function performs the flash swap internally and requires you to pass the necessary liquidation parameters as a FlashLiquidateParams object.

JavaScript

Example for Uniswap V3:

import { parseUnits } from "@ethersproject/units";
import { FlashUniswapV3__factory } from "@hifi/flash-swap/dist/types/factories/FlashUniswapV3__factory";

async function flashLiquidate() {
  const signer = "..."; // Get hold of an ethers.js Signer
  const flashUniswapV3Factory = new FlashUniswapV3__factory(signer);
  const flashUniswapV3 = flashUniswapV3Factory.attach("0x...");

  const borrower = "0x...";
  const hToken = "0x...";
  const collateral = "0x...";
  const poolFee = 3000;
  const turnout = parseUnits("1", 18);
  const underlyingAmount = parseUnits("100", 18);

  await flashUniswapV3.flashLiquidate({
    borrower: borrower,
    bond: hToken,
    collateral: collateral,
    poolFee: poolFee,
    turnout: turnout,
    underlyingAmount: underlyingAmount,
  });
}

Deployment

Flash Uniswap V2

$ yarn hardhat deploy:contract:flash-uniswap-v2 \
    --balance-sheet ${BALANCE_SHEET} \
    --uni-v2-factory ${UNI_V2_FACTORY} \
    --uni-v2-pair-init-code-hash ${UNI_V2_PAIR_INIT_CODE_HASH} \
    --confirmations 5 \
    --network ${NETWORK} \
    --print true \
    --verify true

Flash Uniswap V3

$ yarn hardhat deploy:contract:flash-uniswap-v3 \
    --balance-sheet ${BALANCE_SHEET} \
    --uni-v3-factory ${UNI_V3_FACTORY} \
    --confirmations 5 \
    --network ${NETWORK} \
    --print true \
    --verify true

License

LGPL v3 © Mainframe Group Inc.