@hifi/flash-swap
Version:
Flash swap implementations for liquidating underwater accounts
134 lines (98 loc) • 3.86 kB
Markdown
for liquidating underwater accounts.
The build artifacts can be browsed via [unpkg.com](https://unpkg.com/browse/@hifi/flash-swap@latest/).
With yarn:
```bash
$ yarn add @hifi/flash-swap
```
Or npm:
```bash
$ npm install @hifi/flash-swap
```
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.
You are not supposed to import the smart contracts. Instead, you should interact with the Uniswap pool
directly. For example, with the [UniswapV2Pair](https://github.com/Uniswap/v2-core/blob/v1.0.1/contracts/UniswapV2Pair.sol)
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
[ ](https://docs.uniswap.org/protocol/V2/concepts/core-concepts/flash-swaps).
Example for Uniswap V2:
```javascript
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"],
[ ],
);
await pair.swap(token0Amount, token1Amount, to, data);
}
```
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.
Example for Uniswap V3:
```javascript
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,
});
}
```
```bash
$ 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
```
```bash
$ 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.md) © Mainframe Group Inc.
Flash swap implementations