CompoundLeverageStrategy.sol
Compound Docs: https://docs.compound.finance/
Properties
Property | Type | Modifier | Description |
---|---|---|---|
balancerVault | address | public; constant | The address of the Balancer Vault contract (to facilitate flash loans). |
weth | address | public; constant | The address of the WETH token. |
_positionId | uint256 | private | The identifier for the strategy position that is maintained by the Factor Leverage Vault. |
_vaultManager | IERC721 | private | The address of the Vault Manager contract which facilitates vault admin operations (the underlying Leverage Vault contract in this instance). |
_asset | IERC20 | private | The address of the asset token (i.e. collateral and token being levered). |
_debtToken | IERC20 | private | The address of the debt token (i.e. borrow asset for flash loan). |
_assetPool | address | public | The address of the |
_debtPool | address | public | The address of the |
flMode | uint8 | private | The flash loan mode used to determine the relevant operations to receive the flash loan:
|
Events
LeverageAdded()
Emitted when leverage is added to the position (i.e. this Leverage Strategy contract).
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The amount of |
debt | uint256 | The amount of |
LeverageRemoved()
Emitted when leverage is removed from the leveraged position (i.e. this Leverage Strategy contract).
Arguments
Argument | Type | Description |
---|---|---|
debt | uint256 | The amount of |
LeverageClosed()
Emitted when the leveraged position is closed (i.e. fully repaid).
Arguments
Argument | Type | Description |
---|---|---|
assetBalance | uint256 | The amount of |
debtBalance | uint256 | The amount of |
AssetSwitched()
Emitted when the underlying collateral asset for the leveraged position is switched.
Arguments
Argument | Type | Description |
---|---|---|
newAsset | address | The address of the token used to collateralize the newly switched leverage position. |
balance | uint256 | The amount of |
Withdraw()
Emitted when asset
is withdrawn from the Compound lending pool.
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The amount of |
Repay()
Emitted when the Compound lending pool debt
is repaid.
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The amount of |
Supply()
Emitted when asset
is supplied to the Compound lending pool.
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The amount of |
Borrow()
Emitted when debt
is borrowed from the Compound lending pool (which collateralizes the provided asset
).
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The amount of |
WithdrawTokenInCaseStuck()
Emitted when stuck tokens are withdrawn from the Strategy contract.
Arguments
Argument | Type | Description |
---|---|---|
tokenAddress | address | The address of the token to be withdrawn. |
amount | uint256 | The amount of |
RewardClaimed()
Emitted when Compound rewards are claimed as COMP tokens and transferred to the msg.sender
(i.e. user).
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The amount of rewards claimed denominated in COMP tokens. |
RewardClaimedSupply()
Emitted when Compound rewards are claimed as asset
tokens and transferred to the msg.sender
(i.e. user).
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The amount of rewards claimed denominated in COMP tokens. |
RewardClaimedRepay()
Emitted when Compound rewards are claimed as debt
tokens and transferred to the msg.sender
(i.e. user).
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The amount of rewards claimed denominated in COMP tokens. |
LeverageChargeFee()
Emitted when a leverage fee is charged.
Arguments
Argument | Type | Description |
---|---|---|
amount | uint256 | The leverage fee charged denominated in |
Constructor
Factor Strategy contracts implements OpenZeppelin's upgradeable contracts design and locks the strategy contract upon initialization to avoid contract takeover by an attacker. More info can be found on OpenZeppelin Docs.
Modifiers
onlyOwner()
Checks if the msg.sender
address is equal to the owner
of the position as maintained by the Leverage Vault contract.
Methods
initialize() - public
initializer
public
initializer
Initializes the Strategy contract with the __positionId
and initial address configurations.
Parameters
Params | Type | Description |
---|---|---|
__positionId | uint256 | The identifier of the leveraged position as maintained by the underlying Leverage Vault contract. |
_vaultManagerAddress | address | The address of the Vault Manager contract (the underlying Leverage Vault contract in this instance). |
_assetAddress | address | The address of the Strategy's asset token (i.e. collateral and token being levered). |
_debtAddress | address | The address of the Strategy's debt token (i.e. borrow asset for flash loan). |
_assetPoolAddress | address | The address of the |
_debtPoolAddress | address | The address of the |
vaultManager() - public
view
returns
public
view
returns
Returns the address of the Factor Vault Manager contract which contains vault admin functions (e.g. upgrade strategy, set fees, etc.).
Returns
Type | Description |
---|---|
address | The address of the Factor Vault Manager. |
positionId() - public
view
returns
public
view
returns
Returns the position identifier maintained by the underlying Leverage Vault contract. Each strategy implementation (such as this contract) is tied to a positionId
.
Returns
Type | Description |
---|---|
uint256 | The identifier of the position on the underlying Leverage Vault contract. |
asset() - public
view
returns
public
view
returns
Returns the address of the token configured as the main asset for this leverage Strategy contract (i.e. collateralized asset).
Returns
Type | Description |
---|---|
address | The address of the Strategy's |
debtToken() - public
view
returns
public
view
returns
Returns the address of the debt token which is used for taking out a flash loan (i.e. borrow token) for this Strategy contract.
Returns
Type | Description |
---|---|
address | The address of the Strategy's |
assetPool() - public
view
returns
public
view
returns
Returns the address of the Compound lending pool where the _asset
is being collateralized.
Returns
Type | Description |
---|---|
address | The address of the Strategy's |
debtPool() - public
view
returns
public
view
returns
Returns the address of the Compound lending pool where the _debtToken
was borrowed from.
Returns
Type | Description |
---|---|
address | The address of the Strategy's |
assetBalance() - public
view
returns
public
view
returns
Returns the total collateralized balance (i.e. _asset
supplied) of the Strategy contract that is held by the Compound lending pool.
Returns
Type | Description |
---|---|
uint256 | The total |
debtBalance() - public
view
returns
public
view
returns
Returns the outstanding debt balance (i.e. _debtToken
borrowed) which the Strategy contract owes to the Compound lending contract.
Returns
Type | Description |
---|---|
uint256 | The total |
owner() - public
view
returns
public
view
returns
Returns the address of the owner
of the leveraged position.
Returns
Type | Description |
---|---|
address | The address of the |
addLeverage() - external
payable
external
payable
Adds leverage to a position by executing the following:
Transfer
asset
fromowner
to the Strategy contractSupply
asset
to the Compound poolIf
debt
is specified, executes a flash loan to borrow configureddebt
amount on Balancer vaultBalancer vault calls
receiveFlashLoan()
to trigger flash swap and flash loan debt repayment via_flAddLeverage()
Parameters
Params | Type | Description |
---|---|---|
amount | uint256 | The amount of |
debt | uint256 | The amount of |
data | bytes | The Balancer |
Events
Events | Description |
---|---|
Emits the results of the leverage addition. |
_flAddLeverage() - internal
internal
An internal function which is meant to be called by the Balancer vault contract which repays the flash loan on Balancer by borrowing debt
against the new asset
collateralized amount on Compound.
Swap
debt
forasset
(via swapBySelf())Supply newly acquired
asset
to the Compound pool (i.e. increase total collateral value)Borrow
_debtToken
from the Compound pool to repay Balancer flash loanRepay the Balancer flash loan
Parameters
Params | Type | Description |
---|---|---|
params | bytes | Encoded bytes data which consists of:
|
feeAmount | uint256 | The flash loan fee amount to be repaid denominated in |
removeLeverage() - external
onlyOwner
external
onlyOwner
Removes a specified amount of leverage from the Strategy contract by executing the following:
Query the outstanding
debt
on the Compound lending poolExecutes a flash loan to borrow
debt
from the Balancer vault equivalent to the outstandingdebt
on the Compound poolBalancer vault calls
receiveFlashLoan()
to trigger flash swap and flash loan debt repayment via_flRemoveLeverage()
Transfer withdrawn
asset
to theowner
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
amount | uint256 | The amount of |
data | bytes | The Balancer |
Events
Events | Description |
---|---|
Emits the results of the leverage removal. |
_flRemoveLeverage() - internal
internal
An internal function which is meant to be called by the Balancer vault contract which repays the flash loan on Balancer by withdrawing asset
from the Compound lending pool.
Queries the outstanding
debt
on the Compound lending poolRepays all of the outstanding
debt
on CompoundWithdraws the specified
amount
ofasset
(as perremoveLeverage()
)Swap the withdrawn
asset
fordebt
(via swapBySelf())Calculates the remaining Balancer flash loan
debt
outstandingBorrow the oustanding flash loan
debt
amount (including fees) from Compound lending poolRepay the Balancer flash loan
Parameters
Params | Type | Description |
---|---|---|
params | bytes | Encoded bytes data which consists of:
|
feeAmount | uint256 | The flash loan fee amount to be repaid denominated in |
swapBySelf() - public
returns
public
returns
Allows the msg.sender
to conduct a swap via the OpenOcean aggregator.
Parameters
Params | Type | Description |
---|---|---|
tokenIn | address | The address of the token being swapped from. |
tokenOut | address | The address of the token being swapped to. |
amount | uint256 | The amount of |
data | bytes | The bytes data which consists of the swap method and params. |
Returns
Type | Description |
---|---|
uint256 | The amount of |
supply() - external
onlyOwner
external
onlyOwner
Enables the owner
to transfer strategy asset
from the msg.sender
(i.e. depositor) to the Strategy contract which is then supplied to the Compound lending pool. The Strategy contract will hold the balance on behalf of the owner
.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
amount | uint256 | The amount of |
Events
Events | Description |
---|---|
Emits the amount of |
leverageFeeCharge() - internal
returns
internal
returns
Charges the fee for the leverage strategy and transfers the fee to the fee recipient. The fee configs are maintained by the Vault Manager contract.
Parameters
Params | Type | Description |
---|---|---|
amount | uint256 | The amount of |
token | address | The token which will be collected as fees (i.e. the |
Events
Events | Description |
---|---|
Emits the leverage fee that was charged. |
Returns
Type | Description |
---|---|
uint256 | The leverage fee charged denominated in |
borrow() - external
onlyOwner
external
onlyOwner
Enables the owner
to utilize the collateralized asset
in the Compound lending pool to borrow the specified amount
of debt
. The debt
is transferred to the msg.owner
(i.e. owner
) with the Strategy contract holding the debt on behalf of the owner
.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
amount | uint256 | The amount of |
Events
Events | Description |
---|---|
Emits the amount of |
repay() - external
onlyOwner
external
onlyOwner
Enables the owner
to repay a specified amount
of the debt
for a borrow position on the Compound lending pool. The debt
is repaid on behalf of the Strategy contract which holds the owner
's debt obligations.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
amount | uint256 | The amount of |
Events
Events | Description |
---|---|
Emits the amount of |
withdraw() - external
onlyOwner
external
onlyOwner
Enables the owner
to withdraw a specified amount
of asset
from the Compound lending pool. The Strategy contract executes the withdrawal as it holds deposit balance on behalf of the owner
. The corresponding amount
of asset
is transferred to the owner
address.
Note that due to accrued fees to the iToken, the amount returned by the Compound lending pool might be higher than the given amount
. In such cases, the higher of the expected amount or final amount is returned to the owner
.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
amount | uint256 | The amount of |
Events
Events | Description |
---|---|
Emits the amount of |
switchAsset() - external
onlyOwner
external
onlyOwner
Switches the position's underlying asset
to a newly specified newAsset
token.
Query the outstanding
debt
on the Compound lending poolExecutes a flash loan to borrow
debt
from the Balancer vault equivalent to the outstanding debt on the Compound vaultBalancer vault calls
receiveFlashLoan()
to trigger flash swap and flash loan debt repayment via_flSwitchAsset()
Allowed New Asset Tokens
The newAsset
address must have been registered by the Vault Manager. The list of approved debt tokens is maintained by the Leverage Vault contract.
Amount Validation
The Factor SDK provides convenience functions for the safe handling of the specified amount
. By utilizing the Factor SDK, you can avoid spending gas on misconfigured amounts and minimize dust amounts from swaps.
Parameters
Params | Type | Description |
---|---|---|
newAsset | address | The address of the token which will be switched with the current |
amount | uint256 | The amount of existing |
data | bytes | The Balancer |
_flSwitchAsset() - internal
internal
An internal function which is meant to be called by the Balancer vault contract which switches the asset
collateralizing the leveraged position.
Queries the outstanding
debt
on the Compound lending poolRepays all of the outstanding
debt
on CompoundWithdraws all of the
asset
from the existing position on CompoundSwaps the specified
amount
(perswitchAsset()
) of the withdrawnasset
fornewAsset
(via swapBySelf())Change the Factor Strategy contract properties to align with the
newAsset
Supply the swapped
newAsset
to thenewAsset
Compound lending poolBorrow the initial
debt
balance from Compound using thenewAsset
as collateralRepay the Balancer flash loan
Parameters
Params | Type | Description |
---|---|---|
params | bytes | Encoded bytes data which consists of:
|
feeAmount | uint256 | The flash loan fee amount to be repaid denominated in |
Events
Events | Description |
---|---|
Emits the results of the asset switch and new asset balance denominated in |
closeLeverage() - external
onlyOwner
external
onlyOwner
Closes the leveraged position and transfers the remaining asset
and debt
amounts to the owner
. If the asset
amount
specified is greater than the debt value, the position value will be returned in debt
tokens.
Executes a flash loan to borrow existing
debt
from the Balancer vault equivalent to the outstanding debt on the Compound lending poolBalancer vault calls
receiveFlashLoan()
to trigger flash swap and flash loan debt repayment via_flCloseLeverage()
Transfers the remaining
asset
anddebt
tokens to theowner
(i.e. depositor)
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
amount | uint256 | The amount of |
data | bytes | The Balancer |
Events
Events | Description |
---|---|
Emits the results of the leverage position closure. |
_flCloseLeverage() - internal
internal
An internal function which is meant to be called by the Balancer vault contract which switches the debt
of the leveraged position with a provided newDebtToken
.
Queries the outstanding
debt
on the Compound lending poolRepays all of the outstanding
debt
on CompoundWithdraws all of the
asset
from the existing position on CompoundSwaps the specified
amount
ofasset
(percloseLeverage()
) fordebt
(via swapBySelf())Repay the Balancer flash loan using the acquired
debt
Parameters
Params | Type | Description |
---|---|---|
params | bytes | Encoded bytes data which consists of:
|
feeAmount | uint256 | The flash loan fee amount to be repaid denominated in the old |
withdrawTokenInCaseStuck() - external
onlyOwner
external
onlyOwner
Enables the owner
to withdraw any tokenAddress
that is stuck in the Strategy contract.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
tokenAddress | address | The address of the token to withdraw. |
amount | uint256 | The amount of |
Events
Events | Description |
---|---|
Emits the amount of |
version() - external
pure
returns
external
pure
returns
Returns the strategy version which is hardcoded based on contract deployment.
Returns
Type | Description |
---|---|
string | The Strategy contract version. |
An internal function that is meant to be called by the UUPSUpgradeable contract that checks if the new implementation is registered by the Factor Vault Manager.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
newImplementation | address | The address of the proposed Strategy contract. |
receiveFlashLoan() - external
override
nonReentrant
external
override
nonReentrant
An external function meant to be called by the Balancer vault contract to check the validity of the vault receiving the flash loan (i.e. can the flash loan be repaid). Reads the configured flMode
at the time of execution to determine the correct operation to call.
Modifiers
Modifier | Description |
---|---|
nonReentrant | Ensures that there are no nested (reentrant calls). More info on OpenZeppelin Docs. |
Parameters
Params | Type | Description |
---|---|---|
tokens | address[] | The list of token addresses associated with the flash loan operations. |
amounts | uint256[] | The amount of each token in |
feeAmounts | uint256[] | The flash loan fee amount of each token in |
params | bytes | The Balancer |
claimRewards() - external
onlyOwner
external
onlyOwner
Allows the position owner
to claim Compound rewards in COMP tokens.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Events
Events | Description |
---|---|
Emits the amount of rewards claimed. Denominated in COMP. |
claimRewardsSupply() - external
onlyOwner
external
onlyOwner
Allows the position owner
to claim Compound rewards in asset
tokens.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
amountOutMinimum | uint256 | The minimum amount of |
Events
Events | Description |
---|---|
Emits the amount of rewards claimed. Denominated in COMP. |
claimRewardsRepay() - external
onlyOwner
external
onlyOwner
Allows the position owner
to claim Compound rewards in debt
tokens.
Modifiers
Modifier | Description |
---|---|
onlyOwner | Ensures that only the specified |
Parameters
Params | Type | Description |
---|---|---|
amountOutMinimum | uint256 | The minimum amount of |
Events
Events | Description |
---|---|
Emits the amount of rewards claimed. Denominated in COMP. |
Last updated