1.13.0 • Published 4 months ago

@hifi/flash-swap v1.13.0

Weekly downloads
-
License
LGPL-3.0-or-later
Repository
github
Last release
4 months ago

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.