Technical Docs
Position

Position

Description

The Position contract represents one position of an individual user. A single user can own multiple Position contracts, each representing a different combination of collateral, debt, and base tokens.

Position contracts are deployed by the PositionFactory contract via the createPosition function.

Write Functions

add

Adds to the position.

🚨 This method requires the caller to approve the Position contract to spend _cAmt of the caller's C_TOKEN via the token's ERC-20 approve (opens in a new tab) function.

🚨 Fees are collected in terms of C_TOKEN.

function add(
    uint256 _cAmt,
    uint256 _ltv,
    uint256 _swapAmtOutMin,
    uint24 _poolFee,
    address _client
) external payable;

Input Parameters

Parameter NameTypeDescription
_cAmtuint256The amount of C_TOKEN to be supplied for this transaction-specific loan.

Units: C_DECIMALS
_ltvuint256The desired loan-to-value ratio for this transaction-specific loan.

Ex: 75 is 75%
_swapAmtOutMinuint256The minimum amount of output B_TOKEN from the swap, in order for the transaction to go through.
_poolFeeuint256The Uniswap pool fee tier. (opens in a new tab)

Ex: 3000 for 0.30%
_clientaddressThe address of the client operator.

❗ Use address(0) if not using a client.

addWithPermit

Adds to the position using an ERC-2612 (opens in a new tab) permit, obviating the need for a separate approve transaction.

🚨 Fees are collected in terms of C_TOKEN.

function addWithPermit(
    uint256 _cAmt,
    uint256 _ltv,
    uint256 _swapAmtOutMin,
    uint24 _poolFee,
    address _client,
    uint256 _deadline,
    uint8 _v,
    bytes32 _r,
    bytes32 _s
) external payable;

Input Parameters

Parameter NameTypeDescription
_cAmtuint256The amount of C_TOKEN to be supplied for this transaction-specific loan.

Units: C_DECIMALS
_ltvuint256The desired loan-to-value ratio for this transaction-specific loan.

Ex: 75 is 75%
_swapAmtOutMinuint256The minimum amount of output B_TOKEN from the swap, in order for the transaction to go through.
_poolFeeuint256The Uniswap pool fee tier. (opens in a new tab)

Ex: 3000 for 0.30%
_clientaddressThe address of the client operator.

❗ Use address(0) if not using a client.
_deadlineuint256The expiration timestamp of the permit.

Ex: block.timestamp + 5 minutes
_vuint8The v parameter of ERC-712 signature for the permit.
_rbytes32The r parameter of ERC-712 signature for the permit.
_sbytes32The s parameter of ERC-712 signature for the permit.

addLeverage

Adds leverage to the position using the B_TOKEN balance on Aave.

🚨 Fees are collected in terms of D_TOKEN.

function addLeverage(
    uint256 _dAmt,
    uint256 _swapAmtOutMin,
    uint24 _poolFee,
    address _client
) external payable;

Input Parameters

Parameter NameTypeDescription
_dAmtuint256The amount of D_TOKEN to borrow.

To calculate the upper bound of this amount, getUserAccountData on Aave's Pool contract (opens in a new tab) and getAssetPrice on Aave's Oracle contract (opens in a new tab) must be called.

Here is the pseudocode to calculate the max borrow amount:

(cTotalUSD, dTotalUSD,,, ltv,) = AavePool.getUserAccountData(PositionAddr)

dTokenPriceUSD = AaveOracle.getAssetPrice(D_TOKEN)

dMaxUSD = (cTotalUSD * ltv) / 1e4

maxBorrowUSD = dMaxUSD - dTotalUSD

maxBorrowAmt = (maxBorrowUSD * 10 ** (D_DECIMALS)) / dTokenPriceUSD
_swapAmtOutMinuint256The minimum amount of output B_TOKEN from the swap, in order for the transaction to go through.
_poolFeeuint24The Uniswap pool fee tier. (opens in a new tab)

Ex: 3000 for 0.30%
_clientaddressThe address of the client operator.

❗ Use address(0) if not using a client.

reduce

Reduces a position based on the amount of B_TOKEN and C_TOKEN withdrawn.

🚨 The B_TOKEN is withdrawn from Aave and swapped for the D_TOKEN to repay the loan. Only profitable positions are able fully reduce the position and withdraw all funds.

function reduce(
    uint24 _poolFee,
    bool _exactOutput,
    uint256 _swapAmtOutMin,
    uint256 _withdrawCAmt,
    uint256 _withdrawBAmt
) external payable;

Input Parameters

Parameter NameTypeDescription
_poolFeeuint24The Uniswap pool fee tier. (opens in a new tab)

Ex: 3000 for 0.30%
_exactOutputboolWhether to swap via exact output (opens in a new tab) or exact input (opens in a new tab) on Uniswap.

If the position was profitable, use exact output. Otherwise, use exact input. Profitability can be determined via event (opens in a new tab) analysis.

Use true for exact output and false for exact input.
_swapAmtOutMinuint256The minimum amount of output D_TOKEN from the swap, in order for the transaction to go through.

❗ Only used if _exactOutput = false. Use 0 if _exactOutput = true.
_withdrawCAmtuint256The amount of C_TOKEN to withdraw from Aave after repaying some (or all) of the debt.

INSTRUCTIONS ON HOW TO CALCULATE GO HERE.

Units: C_DECIMALS
_withdrawBAmtuint256The amount of B_TOKEN to withdraw from Aave and swap for the D_TOKEN to repay the loan.

INSTRUCTIONS ON HOW TO CALCULATE GO HERE.

Units: B_DECIMALS

addCollateral

Increases the collateral amount backing the Position contract's loan.

🚨 This method requires the caller to approve the Position contract to spend _cAmt of the caller's C_TOKEN via the token's ERC-20 approve (opens in a new tab) function.

function addCollateral(
    uint256 _cAmt
) external payable;

Input Parameters

Parameter NameTypeDescription
_cAmtuint256The amount of C_TOKEN to be supplied to increase the Position contract's loan.

Units: C_DECIMALS

addCollateralWithPermit

Increases the collateral amount backing the Position contract's loan using an ERC-2612 (opens in a new tab) permit, obviating the need for a separate approve transaction.

function addCollateralWithPermit(
    uint256 _cAmt
    uint256 _deadline,
    uint8 _v,
    bytes32 _r,
    bytes32 _s
) external payable;

Input Parameters

Parameter NameTypeDescription
_cAmtuint256The amount of C_TOKEN to be supplied to increase the Position contract's loan.

Units: C_DECIMALS
_deadlineuint256The expiration timestamp of the permit.

Ex: block.timestamp + 5 minutes
_vuint8The v parameter of ERC-712 signature for the permit.
_rbytes32The r parameter of ERC-712 signature for the permit.
_sbytes32The s parameter of ERC-712 signature for the permit.

withdraw

Withdraws the specified amount of collateral from Aave and transfers it to the specified recipient.

🚨 INSTRUCTIONS ON HOW TO CALCULATE MAX C AMOUNT AND MAX B AMOUNT TO WITHDRAW HERE

function withdraw(
    address _token,
    uint256 _amt,
    address _recipient,
) external payable;

Input Parameters

Parameter NameTypeDescription
_tokenaddressThe address of the token to withdraw. This can be the Position contract's C_TOKEN or B_TOKEN.
_amtuint256The amount to withdraw.

If withdrawing the Position contract's C_TOKEN, and the debt is fully paid off, use the maximum integer, type(uint256).max.

Units: C_DECIMALS or B_DECIMALS
_recipientaddressThe address of the account that will receive the withdrawn collateral.

repay

Repays outstanding debt to Aave.

One use-case for this function is when a position was not profitable and the amount of B_TOKEN was not enough to repay the loan. If the Position owner wants to retrieve any remaining collateral, more D_TOKEN must be acquired elsewhere to pay off the remaining debt via this function.

🚨 This method requires the caller to approve the Position contract to spend _dAmt of the caller's D_TOKEN via the token's ERC-20 approve (opens in a new tab) function.

function repay(
    uint256 _dAmt,
) external payable;

Input Parameters

Parameter NameTypeDescription
_dAmtuint256The amount of D_TOKEN to repay the loan on Aave.

The debtOwed is the Position contract's variable debt token balance.

To get the variable debt token address, call getReserveData(D_TOKEN) on Aave's Pool contract (opens in a new tab), where D_TOKEN is the token to pay Aave back with.

To pay off the entire debt, add a small buffer to debtOwed.

Ex: _dAmt = debtOwed + 5

Units: D_DECIMALS

repayAndWithdrawWithPermit

Repays outstanding debt to Aave using an ERC-2612 (opens in a new tab) permit, obviating the need for a separate approve transaction.

One use-case for this function is when a position was not profitable and the amount of B_TOKEN was not enough to repay the loan. If the Position owner wants to retrieve any remaining collateral, more D_TOKEN must be acquired elsewhere to pay off the remaining debt via this function.

function repayWithPermit(
    uint256 _dAmt,
    uint256 _deadline,
    uint8 _v,
    bytes32 _r,
    bytes32 _s
) external payable;

Input Parameters

Parameter NameTypeDescription
_dAmtuint256The amount of D_TOKEN to repay the loan on Aave.

The debtOwed is the Position contract's variable debt token balance.

To get the variable debt token address, call getReserveData(D_TOKEN) on Aave's Pool contract (opens in a new tab), where D_TOKEN is the token to pay Aave back with.

To pay off the entire debt, add a small buffer to debtOwed.

Ex: _dAmt = debtOwed + 5

Units: D_DECIMALS
_deadlineuint256The expiration timestamp of the permit.

Ex: block.timestamp + 5 minutes
_vuint8The v parameter of ERC-712 signature for the permit.
_rbytes32The r parameter of ERC-712 signature for the permit.
_sbytes32The s parameter of ERC-712 signature for the permit.

extractNative

Allows the Position contract owner to withdraw all of the contract's native token balance.

function extractNative() external payable;

extractERC20

Allows the Position contract owner to withdraw all of a specified ERC-20 token balance from the contract.

extractERC20(address _token) external payable;

Input Parameters

Parameter NameTypeDescription
_tokenaddressThe address of the ERC-20 token to withdraw from the Position contract.

Read Functions

FEE_COLLECTOR

Returns the address of the FeeCollector contract, which is responsible for collecting and allocating protocol fees.

function FEE_COLLECTOR() external view returns (address);

Output Parameters

Parameter NameTypeDescription
FEE_COLLECTORaddressThe address of the FeeCollector contract.

When add and addLeverage functions are called on the Position contract, the FeeCollector contract collects a fee. Depending on the specific arrangement, a portion of this fee may be allocated to client operators, their users, or both.

OWNER

Returns the owner of the Position contract.

function OWNER() external view returns (address);

Output Parameters

Parameter NameTypeDescription
owneraddressThe address of the account that has owner privileges on the Position contract.

C_TOKEN

Returns the address of the Position contract's collateral token.

function C_TOKEN() external view returns (address);

Output Parameters

Parameter NameTypeDescription
C_TOKENaddressThe address of the Position contract's collateral token.

When adding to a position, the Position contract supplies this token as collateral to Aave.

D_TOKEN

Returns the address of the Position contract's debt token.

function D_TOKEN() external view returns (address);

Output Parameters

Parameter NameTypeDescription
D_TOKENaddressThe address of the Position contract's debt token.

When adding to a position, the Position contract borrows this token from Aave.

B_TOKEN

Returns the address of the Position contract's base token.

function B_TOKEN() external view returns (address);

Output Parameters

Parameter NameTypeDescription
B_TOKENaddressThe address of the Position contract's base token.

When adding to a position, the Position contract swaps its D_TOKEN for this token on Uniswap.

C_DECIMALS

Returns the number of decimals for the Position contract's collateral token.

function C_DECIMALS() external view returns (uint8);

Output Parameters

Parameter NameTypeDescription
C_DECIMALSuint8The ERC-20 token decimals of the Position contract's C_TOKEN.

D_DECIMALS

Returns the number of decimals for the Position contract's debt token.

function D_DECIMALS() external view returns (uint8);

Output Parameters

Parameter NameTypeDescription
D_DECIMALSuint8The ERC-20 token decimals of the Position contract's D_TOKEN.

B_DECIMALS

Returns the number of decimals for the Position contract's base token.

function B_DECIMALS() external view returns (uint8);

Output Parameters

Parameter NameTypeDescription
B_DECIMALSuint8The ERC-20 token decimals of the Position contract's B_TOKEN.