Skip to content

Commit

Permalink
Add Staking UI Helpers code (#26)
Browse files Browse the repository at this point in the history
* feat: Add UI Helpers
* feat: Add deployment tasks for stake ui helpers
* ci: Fix dependencies files
* ci: Bump solidity-coverage version
  • Loading branch information
miguelmtzinf authored Jan 24, 2023
1 parent 2e76a22 commit d5ab9e7
Show file tree
Hide file tree
Showing 19 changed files with 25,881 additions and 6,803 deletions.
6 changes: 6 additions & 0 deletions contracts/interfaces/BPTPriceFeedI.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

interface BPTPriceFeedI {
function latestAnswer() external view returns (uint256);
}
31 changes: 31 additions & 0 deletions contracts/interfaces/IEIP2612Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;

import {IERC20} from './IERC20.sol';

/**
* @title interface EIP2612
* @author Aave
* @dev Generic interface for the EIP2612 permit function
*/
interface IEIP2612Token is IERC20 {
/**
* @dev implements the permit function as for https://github.com/ethereum/EIPs/blob/8a34d644aacf0f9f8f00815307fd7dd5da07655f/EIPS/eip-2612.md
* @param owner the owner of the funds
* @param spender the spender
* @param value the amount
* @param deadline the deadline timestamp, type(uint256).max for max deadline
* @param v signature param
* @param s signature param
* @param r signature param
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external virtual;
}
8 changes: 8 additions & 0 deletions contracts/interfaces/IERC20WithNonce.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

import {IERC20} from '../lib/ERC20.sol';

interface IERC20WithNonce is IERC20 {
function _nonces(address user) external view returns (uint256);
}
6 changes: 6 additions & 0 deletions contracts/interfaces/IPriceOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

interface IPriceOracle {
function getAssetPrice(address asset) external view returns (uint256);
}
21 changes: 21 additions & 0 deletions contracts/interfaces/IStakedAaveImplWithInitialize.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;

interface IStakedAaveImplWithInitialize {
function initialize(
address aaveGovernance,
string calldata name,
string calldata symbol,
uint8 decimals
) external;

function stake(address onBehalfOf, uint256 amount) external;

function redeem(address to, uint256 amount) external;

function cooldown() external;

function claimRewards(address to, uint256 amount) external;

function balanceOf(address user) external view returns (uint256);
}
27 changes: 27 additions & 0 deletions contracts/interfaces/IStakedToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;
pragma experimental ABIEncoderV2;

interface IStakedToken {
struct AssetData {
uint128 emissionPerSecond;
uint128 lastUpdateTimestamp;
uint256 index;
}

function totalSupply() external view returns (uint256);

function COOLDOWN_SECONDS() external view returns (uint256);

function UNSTAKE_WINDOW() external view returns (uint256);

function DISTRIBUTION_END() external view returns (uint256);

function assets(address asset) external view returns (AssetData memory);

function balanceOf(address user) external view returns (uint256);

function getTotalRewardsBalance(address user) external view returns (uint256);

function stakersCooldowns(address user) external view returns (uint256);
}
80 changes: 80 additions & 0 deletions contracts/interfaces/StakeUIHelperI.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;
pragma experimental ABIEncoderV2;

interface StakeUIHelperI {
struct AssetUIData {
uint256 stakeTokenTotalSupply;
uint256 stakeCooldownSeconds;
uint256 stakeUnstakeWindow;
uint256 stakeTokenPriceEth;
uint256 rewardTokenPriceEth;
uint256 stakeApy;
uint128 distributionPerSecond;
uint256 distributionEnd;
uint256 stakeTokenUserBalance;
uint256 underlyingTokenUserBalance;
uint256 userCooldown;
uint256 userIncentivesToClaim;
uint256 userPermitNonce;
}

struct GeneralStakeUIData {
uint256 stakeTokenTotalSupply;
uint256 stakeCooldownSeconds;
uint256 stakeUnstakeWindow;
uint256 stakeTokenPriceEth;
uint256 rewardTokenPriceEth;
uint256 stakeApy;
uint128 distributionPerSecond;
uint256 distributionEnd;
}

struct UserStakeUIData {
uint256 stakeTokenUserBalance;
uint256 underlyingTokenUserBalance;
uint256 userCooldown;
uint256 userIncentivesToClaim;
uint256 userPermitNonce;
}

function getStkAaveData(address user) external view returns (AssetUIData memory);

function getStkBptData(address user) external view returns (AssetUIData memory);

function getStkGeneralAaveData() external view returns (GeneralStakeUIData memory);

function getStkGeneralBptData() external view returns (GeneralStakeUIData memory);

function getStkUserAaveData(address user) external view returns (UserStakeUIData memory);

function getStkUserBptData(address user) external view returns (UserStakeUIData memory);

/// @dev This will return user + general for fallback
function getUserUIData(address user)
external
view
returns (
AssetUIData memory,
AssetUIData memory,
uint256
);

function getGeneralStakeUIData()
external
view
returns (
GeneralStakeUIData memory,
GeneralStakeUIData memory,
uint256
);

function getUserStakeUIData(address user)
external
view
returns (
UserStakeUIData memory,
UserStakeUIData memory,
uint256
);
}
46 changes: 46 additions & 0 deletions contracts/misc/AaveStakingHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;

import {IStakedAaveImplWithInitialize} from '../interfaces/IStakedAaveImplWithInitialize.sol';
import {IEIP2612Token} from '../interfaces/IEIP2612Token.sol';

/**
* @title StakingHelper contract
* @author Aave
* @dev implements a staking function that allows staking through the EIP2612 capabilities of the AAVE token
**/

contract AaveStakingHelper {
IStakedAaveImplWithInitialize public immutable STAKE;
IEIP2612Token public immutable AAVE;

constructor(address stake, address aave) public {
STAKE = IStakedAaveImplWithInitialize(stake);
AAVE = IEIP2612Token(aave);
//approves the stake to transfer uint256.max tokens from this contract
//avoids approvals on every stake action
IEIP2612Token(aave).approve(address(stake), type(uint256).max);
}

/**
* @dev stakes on behalf of msg.sender using signed approval.
* The function expects a valid signed message from the user, and executes a permit()
* to approve the transfer. The helper then stakes on behalf of the user
* @param user the user for which the staking is being executed
* @param amount the amount to stake
* @param v signature param
* @param r signature param
* @param s signature param
**/
function stake(
address user,
uint256 amount,
uint8 v,
bytes32 r,
bytes32 s
) external {
AAVE.permit(user, address(this), amount, type(uint256).max, v, r, s);
AAVE.transferFrom(user, address(this), amount);
STAKE.stake(user, amount);
}
}
Loading

0 comments on commit d5ab9e7

Please sign in to comment.