Skip to content

Commit

Permalink
Merge pull request #3 from balancer-labs/optimised-atoken-provider
Browse files Browse the repository at this point in the history
feat: update StaticATokenRateProvider to bypass waToken for gas savings
  • Loading branch information
TomAFrench authored Jan 13, 2022
2 parents 77a3c58 + 7302077 commit 949ebe4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
14 changes: 11 additions & 3 deletions contracts/StaticATokenRateProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@ import "./interfaces/IStaticAToken.sol";
* @notice Returns the value of a wrapped (static) aToken in terms of the underlying (dynamic) aToken
*/
contract StaticATokenRateProvider is IRateProvider {
IStaticAToken public immutable waToken;
ILendingPool public immutable lendingPool;
address public immutable asset;

constructor(IStaticAToken _waToken) {
waToken = _waToken;
lendingPool = _waToken.LENDING_POOL();
asset = _waToken.ASSET();
}

/**
* @return The value of the wrapped aToken in terms of the underlying aToken
*/
function getRate() external view override returns (uint256) {
// This pulls the implementation of used in the StaticAToken contract
// except avoiding storing relevant variables in storage for gas reasons.
// solhint-disable-next-line max-line-length
// see: https://github.com/aave/protocol-v2/blob/ac58fea62bb8afee23f66197e8bce6d79ecda292/contracts/protocol/tokenization/StaticATokenLM.sol#L255-L257
uint256 rate = lendingPool.getReserveNormalizedIncome(asset);

// getRate returns a 18 decimal fixed point number, but `rate` has 27 decimals (i.e. a 'ray' value)
// so we need to convert it.
return waToken.rate() / 10**9;
return rate / 10**9;
}
}
22 changes: 22 additions & 0 deletions contracts/interfaces/ILendingPool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity ^0.8.0;

interface ILendingPool {
/**
* @dev returns a 27 decimal fixed point 'ray' value so a rate of 1 is represented as 1e27
*/
function getReserveNormalizedIncome(address asset) external view returns (uint256);
}
12 changes: 12 additions & 0 deletions contracts/interfaces/IStaticAToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@

pragma solidity ^0.8.0;

import "./ILendingPool.sol";

interface IStaticAToken {
/**
* @dev returns the address of the staticAToken's underlying asset
*/
function ASSET() external view returns (address);

/**
* @dev returns the address of the staticAToken's lending pool
*/
function LENDING_POOL() external view returns (ILendingPool);

/**
* @dev returns a 27 decimal fixed point 'ray' value so a rate of 1 is represented as 1e27
*/
Expand Down

0 comments on commit 949ebe4

Please sign in to comment.