TenderLeverageStrategy.sol
Properties
balancerVault
address
public; constant
The address of the Balancer Vault contract (to facilitate flash loans).
_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 asset lending pool.
_debtPool
address
public
The address of the debt lending pool.
tenderUniTroller
address
public; constant
The address of the Tender.fi Unitroller contract which proxies Comptroller logic and maintains stores state.
tenderVester
address
public; constant
The address of the Tender.fi Vesting contract which facilitates the vested TND rewards.
flMode
uint8
private
The flash loan mode used to determine the relevant operations to receive the flash loan:
1 = addLeverage
2 = removeLeverage
3 = switchAsset
4 = switchDebt
5 = closeLeverage
Events
LeverageAdded()
Emitted when leverage is added to the position (i.e. this Leverage Strategy contract).
Arguments
amount
uint256
The amount of asset that was supplied to the lending pool.
debt
uint256
The amount of debt that was taken out as a flash loan denominated in _debtToken.
LeverageRemoved()
Emitted when leverage is removed from the leveraged position (i.e. this Leverage Strategy contract).
Arguments
debt
uint256
The amount of debt that was repaid.
LeverageClosed()
Emitted when the leveraged position is closed (i.e. fully repaid).
Arguments
assetBalance
uint256
The amount of asset that was returned after closing the position.
debtBalance
uint256
The amount of debt that was returned after closing the position.
AssetSwitched()
Emitted when the underlying collateral asset for the leveraged position is switched.
Arguments
newAsset
address
The address of the token used to collateralize the newly switched leverage position.
balance
uint256
The amount of newAsset that was collateralized.
DebtSwitched()
Emitted when the debt token for the leveraged position is switched.
Arguments
newDebt
address
The address of the token that was borrowed (i.e. the debt token).
balance
uint256
The amount of newDebt that is owed (i.e. the debt amount).
Withdraw()
Emitted when asset is withdrawn from the Tender.fi lending pool.
Arguments
amount
uint256
The amount of asset withdrawn from the Tender.fi lending pool.
Repay()
Emitted when the Tender.fi lending pool debt is repaid.
Arguments
amount
uint256
The amount of debt that was repaid.
Supply()
Emitted when asset is supplied to the Tender.fi lending pool.
Arguments
amount
uint256
The amount of asset supplied to the Tender.fi lending pool.
Borrow()
Emitted when debt is borrowed from the Tender.fi lending pool (which collateralizes the provided asset).
Arguments
amount
uint256
The amount of debt that was borrowed from the Tender.fi lending pool.
WithdrawTokenInCaseStuck()
Emitted when stuck tokens are withdrawn from the Strategy contract.
Arguments
tokenAddress
address
The address of the token to be withdrawn.
amount
uint256
The amount of asset withdrawn from the Tender.fi lending pool.
RewardClaimed()
Emitted when Tender.fi rewards are claimed as TND tokens and transferred to the msg.sender (i.e. user).
Arguments
amount
uint256
The amount of rewards claimed denominated in TND tokens.
RewardClaimedSupply()
Emitted when Tender.fi rewards are claimed as asset tokens and transferred to the msg.sender (i.e. user).
Arguments
amount
uint256
The amount of rewards claimed denominated in TND tokens.
RewardClaimedRepay()
Emitted when Tender.fi rewards are claimed as debt tokens and transferred to the msg.sender (i.e. user).
Arguments
amount
uint256
The amount of rewards claimed denominated in TND tokens.
LeverageChargeFee()
Emitted when a leverage fee is charged.
Arguments
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 initializerInitializes the Strategy contract with the __positionId and initial address configurations.
Parameters
__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 asset lending pool.
_debtPoolAddress
address
The address of the debt lending pool.
vaultManager() - public view returns
public view returnsReturns the address of the Factor Vault Manager contract which contains vault admin functions (e.g. upgrade strategy, set fees, etc.).
Returns
address
The address of the Factor Vault Manager.
positionId() - public view returns
public view returnsReturns the position identifier maintained by the underlying Leverage Vault contract. Each strategy implementation (such as this contract) is tied to a positionId.
Returns
uint256
The identifier of the position on the underlying Leverage Vault contract.
asset() - public view returns
public view returnsReturns the address of the token configured as the main asset for this leverage Strategy contract (i.e. collateralized asset).
Returns
address
The address of the Strategy's _asset.
debtToken() - public view returns
public view returnsReturns the address of the debt token which is used for taking out a flash loan (i.e. borrow token) for this Strategy contract.
Returns
address
The address of the Strategy's _debtToken .
assetPool() - public view returns
public view returnsReturns the address of the Tender.fi lending pool where the _asset is being collateralized.
Returns
address
The address of the Strategy's _assetPool.
debtPool() - public view returns
public view returnsReturns the address of the Tender.fi lending pool where the _debtToken was borrowed from.
Returns
address
The address of the Strategy's _debtPool.
assetBalance() - public view returns
public view returnsReturns the total collateralized balance (i.e. _asset supplied) of the Strategy contract that is held by the Tender.fi lending pool.
Returns
uint256
The total _asset supplied to the Tender.fi lending pool.
debtBalance() - public view returns
public view returnsReturns the outstanding debt balance (i.e. _debtToken borrowed) which the Strategy contract owes to the Tender.fi lending contract.
Returns
uint256
The total _debtToken that needs to be repaid to the Tender.fi lending pool.
owner() - public view returns
public view returnsReturns the address of the owner of the leveraged position.
Returns
address
The address of the owner of the position.
addLeverage() - external payable
external payableAdds leverage to a position by executing the following:
Transfer
assetfromownerto the Strategy contractSupply
assetto the Tender.fi poolIf
debtis specified, executes a flash loan to borrow configureddebtamount on Balancer vaultBalancer vault calls
receiveFlashLoan()to trigger flash swap and flash loan debt repayment via_flAddLeverage()
Parameters
amount
uint256
The amount of _asset being supplied.
debt
uint256
The amount of _debtToken to be flash loaned.
data
bytes
The Balancer userData which in this context is used for pool-specific instructions needed to do the calculations.
Events
Emits the results of the leverage addition.
_flAddLeverage() - internal
internalAn 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 Tender.fi.
Swap
debtforasset(via swapBySelf())Supply newly acquired
assetto the Tender.fi pool (i.e. increase total collateral value)Borrow
_debtTokenfrom the Tender.fi pool to repay Balancer flash loanRepay the Balancer flash loan
Parameters
params
bytes
Encoded bytes data which consists of:
amount = The number of _debtToken that was flash loaned.
data = The Balancer userData.
feeAmount
uint256
The flash loan fee amount to be repaid denominated in _debtToken.
removeLeverage() - external onlyOwner
external onlyOwnerRemoves a specified amount of leverage from the Strategy contract by executing the following:
Query the outstanding
debton the Tender.fi lending poolExecutes a flash loan to borrow
debtfrom the Balancer vault equivalent to the outstandingdebton the Tender.fi poolBalancer vault calls
receiveFlashLoan()to trigger flash swap and flash loan debt repayment via_flRemoveLeverage()Transfer withdrawn
assetto theowner
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
amount
uint256
The amount of asset being removed.
data
bytes
The Balancer userData which in this context is used for pool-specific instructions needed to do the calculations.
Events
Emits the results of the leverage removal.
_flRemoveLeverage() - internal
internalAn 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 Tender.fi lending pool.
Queries the outstanding
debton the Tender.fi lending poolRepays all of the outstanding
debton Tender.fiWithdraws the specified
amountofasset(as perremoveLeverage())Swap the withdrawn
assetfordebt(via swapBySelf())Calculates the remaining Balancer flash loan
debtoutstandingBorrow the oustanding flash loan
debtamount (including fees) from Tender.fi lending poolRepay the Balancer flash loan
Parameters
params
bytes
Encoded bytes data which consists of:
amount = The number of _asset that is being removed from the Strategy contract.
data = The Balancer userData.
feeAmount
uint256
The flash loan fee amount to be repaid denominated in _debtToken.
swapBySelf() - public returns
public returnsAllows the msg.sender to conduct a swap via the OpenOcean aggregator.
Parameters
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 tokenIn being swapped.
data
bytes
The bytes data which consists of the swap method and params.
Returns
uint256
The amount of tokenOut resulting from the swap minus the leverage strategy fees.
supply() - external onlyOwner
external onlyOwnerEnables the owner to transfer strategy asset from the msg.sender(i.e. depositor) to the Strategy contract which is then supplied to the Tender.fi lending pool. The Strategy contract will hold the balance on behalf of the owner.
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
amount
uint256
The amount of asset that was used for leverage.
Events
Emits the amount of asset that was supplied.
Returns
uint256
The leverage fee charged denominated in token.
borrow() - external onlyOwner
external onlyOwnerEnables the owner to utilize the collateralized asset in the Tender.fi 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
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
amount
uint256
The amount of debt to be borrowed.
Events
Emits the amount of debt that was borrowed.
repay() - external onlyOwner
external onlyOwnerEnables the owner to repay a specified amount of the debt for a borrow position on the Tender.fi lending pool. The debt is repaid on behalf of the Strategy contract which holds the owner's debt obligations.
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
amount
uint256
The amount of debt to be repaid.
Events
Emits the amount of debt that was repaid.
withdraw() - external onlyOwner
external onlyOwnerEnables the owner to withdraw a specified amount of asset from the Tender.fi 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 Tender.fi 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
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
amount
uint256
The amount of asset to be withdrawn.
Events
Emits the amount of asset that was withdrawn.
switchAsset() - external onlyOwner
external onlyOwnerSwitches the position's underlying asset to a newly specified newAsset token.
Query the outstanding
debton the Tender.fi lending poolExecutes a flash loan to borrow
debtfrom the Balancer vault equivalent to the outstanding debt on the Tender.fi vaultBalancer vault calls
receiveFlashLoan()to trigger flash swap and flash loan debt repayment via_flSwitchAsset()
Parameters
newAsset
address
The address of the token which will be switched with the current asset.
amount
uint256
The amount of existing asset that is being switched for the newAsset.
data
bytes
The Balancer userData which in this context is used for pool-specific instructions needed to do the calculations.
_flSwitchAsset() - internal
internalAn internal function which is meant to be called by the Balancer vault contract which switches the asset collateralizing the leveraged position.
Queries the outstanding
debton the Tender.fi lending poolRepays all of the outstanding
debton Tender.fiWithdraws all of the
assetfrom the existing position on Tender.fiSwaps the specified
amount(perswitchAsset()) of the withdrawnassetfornewAsset(via swapBySelf())Change the Factor Strategy contract properties to align with the
newAssetSupply the swapped
newAssetto thenewAssetTender.fi lending poolBorrow the initial
debtbalance from Tender.fi using thenewAssetas collateralRepay the Balancer flash loan
Parameters
params
bytes
Encoded bytes data which consists of:
newAsset = The address of the token that is being used as the new collateral
amount = The number of _asset that is being switched.
data = The Balancer userData.
feeAmount
uint256
The flash loan fee amount to be repaid denominated in _debtToken.
Events
Emits the results of the asset switch and new asset balance denominated in newAsset.
switchDebt() - external onlyOwner
external onlyOwnerSwitches the debt of the leverage position for a specified newDebtToken.
Executes a flash loan to borrow existing
debtfrom the Balancer vault equivalent to the outstanding debt on the Tender.fi lending poolBalancer vault calls
receiveFlashLoan()to trigger flash swap and flash loan debt repayment via_flSwitchDebt()
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
newDebtToken
address
The address of the token which will be switched with the current debt.
newDebt
uint256
The amount of newDebtToken that is being switched in place of the old debt.
data
bytes
The Balancer userData which in this context is used for pool-specific instructions needed to do the calculations.
_flSwitchDebt() - internal
internalAn internal function which is meant to be called by the Balancer vault contract which switches the debt of a leveraged position with a provided newDebtToken.
Queries the outstanding
debton the Tender.fi lending poolRepays all of the outstanding
debton Tender.fiChange the Factor Strategy contract properties to align with the
newDebtTokenBorrow the provided
newDebtamount ofnewDebtTokenfrom the corresponding Tender.fi pool using the untouched underlyingassetas collateralSwaps the specified
newDebtamount (perswitchDebt()) of thenewDebtTokenfor the previousdebttoken (via swapBySelf())Repay the Balancer flash loan using the acquired
debt
Parameters
params
bytes
Encoded bytes data which consists of:
newDebtToken = The address of the token to be used as the new debt
newDebt = The number of newDebtToken that is being borrowed
poolAddress = The address of the corresponding Tender.fi lending pool.
data = The Balancer userData.
feeAmount
uint256
The flash loan fee amount to be repaid denominated in the old debt.
Events
Emits the results of the asset switch and new asset balance denominated in newAsset.
closeLeverage() - external onlyOwner
external onlyOwnerCloses 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
debtfrom the Balancer vault equivalent to the outstanding debt on the Tender.fi lending poolBalancer vault calls
receiveFlashLoan()to trigger flash swap and flash loan debt repayment via_flCloseLeverage()Transfers the remaining
assetanddebttokens to theowner(i.e. depositor)
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
amount
uint256
The amount of asset that will be swapped for debt.
data
bytes
The Balancer userData which in this context is used for pool-specific instructions needed to do the calculations.
Events
Emits the results of the leverage position closure.
_flCloseLeverage() - internal
internalAn 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
debton the Tender.fi lending poolRepays all of the outstanding
debton Tender.fiWithdraws all of the
assetfrom the existing position on Tender.fiSwaps the specified
amountofasset(percloseLeverage()) fordebt(via swapBySelf())Repay the Balancer flash loan using the acquired
debt
Parameters
params
bytes
Encoded bytes data which consists of:
amount = The number of asset that is swapped for debt
data = The Balancer userData.
feeAmount
uint256
The flash loan fee amount to be repaid denominated in the old debt.
version() - external pure returns
external pure returnsReturns the strategy version which is hardcoded based on contract deployment.
Returns
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
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
newImplementation
address
The address of the proposed Strategy contract.
_claimRewardsInternal() - private onlyOwner
private onlyOwnerA private function which faciliates the claiming of Tender.fi rewards based on the specified output token. If the token is not specified, the rewards will be claimed as TND else the claimed TND rewards will be swapped for the specified token.
If the claimRewardsSupply (from claimRewardsSupply()) or claimRewardsRepay (from claimRewardsRepay()) selector is specified, the claimed rewards will be swapped for the specified token and supplied/repaid to the corresponding Tender.fi lending pool.
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
pool
address
The address of the Tender.fi lending pool from which rewards are being claimed.
token
address
The token that the claimed rewards will be received in (i.e. tokenOut).
fnSelector
bytes4
The bytes signature of the target function on the Tender.fi Reward contract.
amountOutMinimum
uint256
The minimum amount of asset received from the claim else the transaction reverts.
Events
Emits the amount of rewards claimed. Denominated in TND.
claimRewards() - external onlyOwner
external onlyOwnerAllows the position owner to claim Tender.fi rewards in TND tokens.
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
claimRewardsSupply() - external onlyOwner
external onlyOwnerAllows the position owner to claim Tender.fi rewards and supply the claimed value as asset tokens to the relevant Tender.fi lending pool.
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
amountOutMinimum
uint256
The minimum amount of asset received from swapping the claimed rewards else the transaction reverts.
claimRewardsRepay() - external onlyOwner
external onlyOwnerAllows the position owner to claim Tender.fi rewards and repay the claimed value as debt tokens to the relevant Tender.fi lending pool.
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
amountOutMinimum
uint256
The minimum amount of debt received from swapping the claimed rewards else the transaction reverts.
withdrawTokenInCaseStuck() - external onlyOwner
external onlyOwnerEnables the owner to withdraw any tokenAddress that is stuck in the Strategy contract.
Modifiers
onlyOwner
Ensures that only the specified owner of the contract is able to execute administrative tasks. More info on OpenZeppelin Docs.
Parameters
tokenAddress
address
The address of the token to withdraw.
amount
uint256
The amount of tokenAddress to be withdrawn.
Events
Emits the amount of tokenAddress that was withdrawn as well as the tokenAddress.
leverageFeeCharge() - internal returns
internal returnsCharges 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
amount
uint256
The amount of token that was used for leverage.
Events
Emits the leverage fee that was charged.
receiveFlashLoan() - external override nonReentrant
external override nonReentrantAn 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
nonReentrant
Ensures that there are no nested (reentrant calls). More info on OpenZeppelin Docs.
Parameters
tokens
address[]
The list of token addresses associated with the flash loan operations.
amounts
uint256[]
The amount of each token in tokens to be used for the flash loan operation. The order of each token amount must correspond with the order of the token address in tokens.
feeAmounts
uint256[]
The flash loan fee amount of each token in tokens to be used for the flash loan operation. The order of each token amount must correspond with the order of the token address in tokens.
params
bytes
The Balancer userData passed in as calldata.
Last updated
Was this helpful?