Skip to content

Commit

Permalink
Merge pull request #187 from Blueberryfi/liquidation-update
Browse files Browse the repository at this point in the history
Emergency Funds within Liquidator Bot
  • Loading branch information
0xSpraggins authored May 2, 2024
2 parents 3450b1e + c8578cd commit bf633ac
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions contracts/liquidation/AuraLiquidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ contract AuraLiquidator is BaseLiquidator {
function initialize(
IBank bank,
address treasury,
address emergencyFund,
address poolAddressesProvider,
address auraSpell,
address balancerVault,
Expand All @@ -77,6 +78,7 @@ contract AuraLiquidator is BaseLiquidator {
_swapRouter = swapRouter;
_weth = weth;

_emergencyFund = emergencyFund;
_transferOwnership(owner);
}

Expand Down
27 changes: 27 additions & 0 deletions contracts/liquidation/BaseLiquidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ abstract contract BaseLiquidator is IBlueberryLiquidator, SwapRegistry, IERC1155
/// @dev aave pool addresses provider
IPoolAddressesProvider private _poolAddressesProvider;

/// @dev The address of the emergency fund that will cover any extra costs for liquidation
address internal _emergencyFund;

/*//////////////////////////////////////////////////////////////////////////
FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -134,6 +137,12 @@ abstract contract BaseLiquidator is IBlueberryLiquidator, SwapRegistry, IERC1155
// forceApprove aave pool to get back debt
IERC20(asset).forceApprove(address(_pool), amount + premium);

// Have the emergency fund cover any extra costs
uint256 assetBalance = IERC20(asset).balanceOf(address(this));
if (assetBalance < amount + premium) {
_accessEmergencyFunds(asset, (amount + premium) - assetBalance);
}

// reset position id
_POS_ID = 0;

Expand All @@ -151,6 +160,24 @@ abstract contract BaseLiquidator is IBlueberryLiquidator, SwapRegistry, IERC1155
}
}

/**
* @notice Sets the address of the emergency fund
* @param emergencyFund The address of the emergency fund
*/
function setEmergencyFund(address emergencyFund) external onlyOwner {
if (emergencyFund == address(0)) revert Errors.ZERO_ADDRESS();
_emergencyFund = emergencyFund;
}

/**
* @notice Sends emergency funds to the contract to cover unprofitable liquidations
* @param asset The address of the asset to withdraw from the contract
* @param amount Amount of asset to send to the liquidator bot
*/
function _accessEmergencyFunds(address asset, uint256 amount) internal {
IERC20(asset).transferFrom(_emergencyFund, address(this), amount);
}

/**
* @notice Sets the Aave Lending Pool and Aave Pool Address Provider during initialization
* @param poolAddressesProvider Address of Aave's PoolAddressesProvider
Expand Down
3 changes: 3 additions & 0 deletions contracts/liquidation/ConvexLiquidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ contract ConvexLiquidator is BaseLiquidator {
function initialize(
IBank bank,
address treasury,
address emergencyFund,
address poolAddressesProvider,
address convexSpell,
address balancerVault,
Expand All @@ -83,6 +84,8 @@ contract ConvexLiquidator is BaseLiquidator {
_curveOracle = IConvexSpell(convexSpell).getCrvOracle();

_weth = weth;
_emergencyFund = emergencyFund;

_transferOwnership(owner);
}

Expand Down
5 changes: 4 additions & 1 deletion contracts/liquidation/IchiLiquidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ contract IchiLiquidator is BaseLiquidator {
function initialize(
IBank bank,
address treasury,
address emergencyFund,
address poolAddressesProvider,
address ichiSpell,
address swapRouter,
Expand All @@ -77,6 +78,7 @@ contract IchiLiquidator is BaseLiquidator {
_swapRouter = swapRouter;
_weth = weth;

_emergencyFund = emergencyFund;
_transferOwnership(owner);
}

Expand Down Expand Up @@ -118,7 +120,8 @@ contract IchiLiquidator is BaseLiquidator {
function _unwrapLpToken(IBank.Position memory posInfo) internal returns (address) {
// Withdraw ERC1155 liquidiation
if (posInfo.collToken == address(IIchiSpell(_spell).getWIchiFarm())) {
IWIchiFarm(posInfo.collToken).burn(posInfo.collId, type(uint256).max);
uint256 balance = IERC1155(posInfo.collToken).balanceOf(address(this), posInfo.collId);
IWIchiFarm(posInfo.collToken).burn(posInfo.collId, balance);
(uint256 pid, ) = IWIchiFarm(posInfo.collToken).decodeId(posInfo.collId);

return IIchiFarm(IWIchiFarm(posInfo.collToken).getIchiFarm()).lpToken(pid);
Expand Down
2 changes: 2 additions & 0 deletions contracts/liquidation/ShortLongLiquidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contract ShortLongLiquidator is BaseLiquidator {
function initialize(
IBank bank,
address treasury,
address emergencyFund,
address poolAddressesProvider,
address shortLongSpell,
address balancerVault,
Expand All @@ -67,6 +68,7 @@ contract ShortLongLiquidator is BaseLiquidator {
_balancerVault = balancerVault;
_swapRouter = swapRouter;
_weth = weth;
_emergencyFund = emergencyFund;
_transferOwnership(owner);
}

Expand Down
4 changes: 3 additions & 1 deletion test/liquidator/AuraLiquidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Aura Liquidator', () => {
let admin: SignerWithAddress;
let alice: SignerWithAddress;
let treasury: SignerWithAddress;
let emergengyFund: SignerWithAddress;

let usdc: ERC20;
let dai: ERC20;
Expand All @@ -37,7 +38,7 @@ describe('Aura Liquidator', () => {
before(async () => {
await fork();

[admin, alice, treasury] = await ethers.getSigners();
[admin, alice, treasury, emergengyFund] = await ethers.getSigners();
usdc = <ERC20>await ethers.getContractAt('ERC20', USDC);
usdc = <ERC20>await ethers.getContractAt('ERC20', USDC);
dai = <ERC20>await ethers.getContractAt('ERC20', DAI);
Expand Down Expand Up @@ -77,6 +78,7 @@ describe('Aura Liquidator', () => {
[
bank.address,
treasury.address,
emergengyFund.address,
POOL_ADDRESSES_PROVIDER,
spell.address,
BALANCER_VAULT,
Expand Down
4 changes: 3 additions & 1 deletion test/liquidator/ConvexLiquidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('Convex Liquidator', () => {
let admin: SignerWithAddress;
let alice: SignerWithAddress;
let treasury: SignerWithAddress;
let emergengyFund: SignerWithAddress;

let usdc: ERC20;
let mockOracle: MockOracle;
Expand All @@ -40,7 +41,7 @@ describe('Convex Liquidator', () => {

before(async () => {
await fork(1);
[admin, alice, treasury] = await ethers.getSigners();
[admin, alice, treasury, emergengyFund] = await ethers.getSigners();
usdc = <ERC20>await ethers.getContractAt('ERC20', USDC);
dai = <ERC20>await ethers.getContractAt('ERC20', DAI);
usdc = <ERC20>await ethers.getContractAt('ERC20', USDC);
Expand Down Expand Up @@ -80,6 +81,7 @@ describe('Convex Liquidator', () => {
[
bank.address,
treasury.address,
emergengyFund.address,
POOL_ADDRESSES_PROVIDER,
spell.address,
BALANCER_VAULT,
Expand Down
4 changes: 3 additions & 1 deletion test/liquidator/IchiLiquidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('Ichi Liquidator', () => {
let admin: SignerWithAddress;
let alice: SignerWithAddress;
let treasury: SignerWithAddress;
let emergengyFund: SignerWithAddress;

let usdc: ERC20;
let ichi: MockIchiV2;
Expand All @@ -50,7 +51,7 @@ describe('Ichi Liquidator', () => {
before(async () => {
await fork();

[admin, alice, treasury] = await ethers.getSigners();
[admin, alice, treasury, emergengyFund] = await ethers.getSigners();
usdc = <ERC20>await ethers.getContractAt('ERC20', USDC);
ichi = <MockIchiV2>await ethers.getContractAt('MockIchiV2', ICHI);
ichiV1 = <ERC20>await ethers.getContractAt('ERC20', ICHIV1);
Expand Down Expand Up @@ -95,6 +96,7 @@ describe('Ichi Liquidator', () => {
[
bank.address,
treasury.address,
emergengyFund.address,
POOL_ADDRESSES_PROVIDER,
spell.address,
UNISWAP_V3_ROUTER,
Expand Down
4 changes: 3 additions & 1 deletion test/liquidator/ShortLongLiquidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('ShortLong Liquidator', () => {
let admin: SignerWithAddress;
let alice: SignerWithAddress;
let treasury: SignerWithAddress;
let emergengyFund: SignerWithAddress;

let usdc: ERC20;
let crv: ERC20;
Expand All @@ -31,7 +32,7 @@ describe('ShortLong Liquidator', () => {
let positionId: BigNumber;

before(async () => {
[admin, alice, treasury] = await ethers.getSigners();
[admin, alice, treasury, emergengyFund] = await ethers.getSigners();
usdc = <ERC20>await ethers.getContractAt('ERC20', USDC);
crv = <ERC20>await ethers.getContractAt('ERC20', CRV);
const protocol = await setupShortLongProtocol();
Expand All @@ -46,6 +47,7 @@ describe('ShortLong Liquidator', () => {
[
bank.address,
treasury.address,
emergengyFund.address,
POOL_ADDRESSES_PROVIDER,
spell.address,
BALANCER_VAULT,
Expand Down

0 comments on commit bf633ac

Please sign in to comment.