Skip to content

Commit

Permalink
feat: refactored the pool variable to immutable in atoken/debt tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
The-3D committed Oct 1, 2021
1 parent 50b0fdf commit 8e4d226
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 62 deletions.
2 changes: 0 additions & 2 deletions contracts/interfaces/IInitializableAToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ interface IInitializableAToken {

/**
* @notice Initializes the aToken
* @param pool The address of the pool where this aToken will be used
* @param treasury The address of the Aave treasury, receiving the fees on this aToken
* @param underlyingAsset The address of the underlying asset of this aToken (E.g. WETH for aWETH)
* @param incentivesController The smart contract managing potential incentives distribution
Expand All @@ -44,7 +43,6 @@ interface IInitializableAToken {
* @param params A set of encoded parameters for additional initialization
*/
function initialize(
IPool pool,
address treasury,
address underlyingAsset,
IAaveIncentivesController incentivesController,
Expand Down
2 changes: 0 additions & 2 deletions contracts/interfaces/IInitializableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ interface IInitializableDebtToken {

/**
* @notice Initializes the debt token.
* @param pool The address of the pool where this aToken will be used
* @param underlyingAsset The address of the underlying asset of this aToken (E.g. WETH for aWETH)
* @param incentivesController The smart contract managing potential incentives distribution
* @param debtTokenDecimals The decimals of the debtToken, same as the underlying asset's
Expand All @@ -41,7 +40,6 @@ interface IInitializableDebtToken {
* @param params A set of encoded parameters for additional initialization
*/
function initialize(
IPool pool,
address underlyingAsset,
IAaveIncentivesController incentivesController,
uint8 debtTokenDecimals,
Expand Down
2 changes: 2 additions & 0 deletions contracts/mocks/upgradeability/MockAToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {IPool} from '../../interfaces/IPool.sol';
import {IAaveIncentivesController} from '../../interfaces/IAaveIncentivesController.sol';

contract MockAToken is AToken {
constructor(IPool pool) AToken(pool) {}

function getRevision() internal pure override returns (uint256) {
return 0x2;
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/mocks/upgradeability/MockStableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
pragma solidity 0.8.7;

import {StableDebtToken} from '../../protocol/tokenization/StableDebtToken.sol';
import {IPool} from '../../interfaces/IPool.sol';

contract MockStableDebtToken is StableDebtToken {
constructor(IPool pool) StableDebtToken(pool) {}

function getRevision() internal pure override returns (uint256) {
return 0x3;
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/mocks/upgradeability/MockVariableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
pragma solidity 0.8.7;

import {VariableDebtToken} from '../../protocol/tokenization/VariableDebtToken.sol';
import {IPool} from '../../interfaces/IPool.sol';

contract MockVariableDebtToken is VariableDebtToken {
constructor(IPool pool) VariableDebtToken(pool) {}

function getRevision() internal pure override returns (uint256) {
return 0x3;
}
Expand Down
12 changes: 3 additions & 9 deletions contracts/protocol/libraries/logic/ConfiguratorLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ library ConfiguratorLogic {
input.aTokenImpl,
abi.encodeWithSelector(
IInitializableAToken.initialize.selector,
pool,
input.treasury,
input.underlyingAsset,
IAaveIncentivesController(input.incentivesController),
Expand All @@ -68,7 +67,6 @@ library ConfiguratorLogic {
input.stableDebtTokenImpl,
abi.encodeWithSelector(
IInitializableDebtToken.initialize.selector,
pool,
input.underlyingAsset,
IAaveIncentivesController(input.incentivesController),
input.underlyingAssetDecimals,
Expand All @@ -82,7 +80,6 @@ library ConfiguratorLogic {
input.variableDebtTokenImpl,
abi.encodeWithSelector(
IInitializableDebtToken.initialize.selector,
pool,
input.underlyingAsset,
IAaveIncentivesController(input.incentivesController),
input.underlyingAssetDecimals,
Expand Down Expand Up @@ -126,11 +123,10 @@ library ConfiguratorLogic {
{
DataTypes.ReserveData memory reserveData = cachedPool.getReserveData(input.asset);

(, , , uint256 decimals, ,) = cachedPool.getConfiguration(input.asset).getParams();
(, , , uint256 decimals, , ) = cachedPool.getConfiguration(input.asset).getParams();

bytes memory encodedCall = abi.encodeWithSelector(
IInitializableAToken.initialize.selector,
cachedPool,
input.treasury,
input.asset,
input.incentivesController,
Expand All @@ -151,11 +147,10 @@ library ConfiguratorLogic {
) public {
DataTypes.ReserveData memory reserveData = cachedPool.getReserveData(input.asset);

(, , , uint256 decimals, ,) = cachedPool.getConfiguration(input.asset).getParams();
(, , , uint256 decimals, , ) = cachedPool.getConfiguration(input.asset).getParams();

bytes memory encodedCall = abi.encodeWithSelector(
IInitializableDebtToken.initialize.selector,
cachedPool,
input.asset,
input.incentivesController,
decimals,
Expand Down Expand Up @@ -183,11 +178,10 @@ library ConfiguratorLogic {
) public {
DataTypes.ReserveData memory reserveData = cachedPool.getReserveData(input.asset);

(, , , uint256 decimals, ,) = cachedPool.getConfiguration(input.asset).getParams();
(, , , uint256 decimals, , ) = cachedPool.getConfiguration(input.asset).getParams();

bytes memory encodedCall = abi.encodeWithSelector(
IInitializableDebtToken.initialize.selector,
cachedPool,
input.asset,
input.incentivesController,
decimals,
Expand Down
15 changes: 8 additions & 7 deletions contracts/protocol/tokenization/AToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract AToken is

bytes32 public DOMAIN_SEPARATOR;

IPool internal _pool;
IPool internal immutable _pool;
address internal _treasury;
address internal _underlyingAsset;

Expand All @@ -54,9 +54,12 @@ contract AToken is
return ATOKEN_REVISION;
}

constructor(IPool pool) {
_pool = pool;
}

/// @inheritdoc IInitializableAToken
function initialize(
IPool pool,
address treasury,
address underlyingAsset,
IAaveIncentivesController incentivesController,
Expand Down Expand Up @@ -86,14 +89,13 @@ contract AToken is
_setSymbol(aTokenSymbol);
_setDecimals(aTokenDecimals);

_pool = pool;
_treasury = treasury;
_underlyingAsset = underlyingAsset;
_incentivesController = incentivesController;

emit Initialized(
underlyingAsset,
address(pool),
address(_pool),
treasury,
address(incentivesController),
aTokenDecimals,
Expand Down Expand Up @@ -309,17 +311,16 @@ contract AToken is
bool validate
) internal {
address underlyingAsset = _underlyingAsset;
IPool pool = _pool;

uint256 index = pool.getReserveNormalizedIncome(underlyingAsset);
uint256 index = _pool.getReserveNormalizedIncome(underlyingAsset);

uint256 fromBalanceBefore = super.balanceOf(from).rayMul(index);
uint256 toBalanceBefore = super.balanceOf(to).rayMul(index);

super._transfer(from, to, Helpers.castUint128(amount.rayDiv(index)));

if (validate) {
pool.finalizeTransfer(underlyingAsset, from, to, amount, fromBalanceBefore, toBalanceBefore);
_pool.finalizeTransfer(underlyingAsset, from, to, amount, fromBalanceBefore, toBalanceBefore);
}

emit BalanceTransfer(from, to, amount, index);
Expand Down
2 changes: 2 additions & 0 deletions contracts/protocol/tokenization/DelegationAwareAToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {AToken} from './AToken.sol';
* @dev The underlying asset needs to be compatible with the COMP delegation interface
*/
contract DelegationAwareAToken is AToken {
constructor(IPool pool) AToken(pool) {}

modifier onlyPoolAdmin() {
IACLManager aclManager = IACLManager(IPool(_pool).getAddressesProvider().getACLManager());
require(aclManager.isPoolAdmin(msg.sender), Errors.CALLER_NOT_POOL_ADMIN);
Expand Down
10 changes: 6 additions & 4 deletions contracts/protocol/tokenization/StableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
mapping(address => uint40) internal _timestamps;
uint40 internal _totalSupplyTimestamp;

IPool internal _pool;
IPool internal immutable _pool;
address internal _underlyingAsset;

constructor(IPool pool) {
_pool = pool;
}

/// @inheritdoc IInitializableDebtToken
function initialize(
IPool pool,
address underlyingAsset,
IAaveIncentivesController incentivesController,
uint8 debtTokenDecimals,
Expand All @@ -53,7 +56,6 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
_setSymbol(debtTokenSymbol);
_setDecimals(debtTokenDecimals);

_pool = pool;
_underlyingAsset = underlyingAsset;
_incentivesController = incentivesController;

Expand All @@ -69,7 +71,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {

emit Initialized(
underlyingAsset,
address(pool),
address(_pool),
address(incentivesController),
debtTokenDecimals,
debtTokenName,
Expand Down
10 changes: 6 additions & 4 deletions contracts/protocol/tokenization/VariableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {

uint256 public constant DEBT_TOKEN_REVISION = 0x2;

IPool internal _pool;
IPool internal immutable _pool;
address internal _underlyingAsset;

constructor(IPool pool) {
_pool = pool;
}

/// @inheritdoc IInitializableDebtToken
function initialize(
IPool pool,
address underlyingAsset,
IAaveIncentivesController incentivesController,
uint8 debtTokenDecimals,
Expand All @@ -48,7 +51,6 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
_setSymbol(debtTokenSymbol);
_setDecimals(debtTokenDecimals);

_pool = pool;
_underlyingAsset = underlyingAsset;
_incentivesController = incentivesController;

Expand All @@ -64,7 +66,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {

emit Initialized(
underlyingAsset,
address(pool),
address(_pool),
address(incentivesController),
debtTokenDecimals,
debtTokenName,
Expand Down
36 changes: 17 additions & 19 deletions helpers/contracts-deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ export const deployDefaultReserveInterestRateStrategy = async (
eContractid.DefaultReserveInterestRateStrategy
);

export const deployGenericStableDebtToken = async () =>
export const deployGenericStableDebtToken = async (poolAddress: tEthereumAddress) =>
withSave(
await new StableDebtTokenFactory(await getFirstSigner()).deploy(),
await new StableDebtTokenFactory(await getFirstSigner()).deploy(poolAddress),
eContractid.StableDebtToken
);

export const deployGenericVariableDebtToken = async () =>
export const deployGenericVariableDebtToken = async (poolAddress: tEthereumAddress) =>
withSave(
await new VariableDebtTokenFactory(await getFirstSigner()).deploy(),
await new VariableDebtTokenFactory(await getFirstSigner()).deploy(poolAddress),
eContractid.VariableDebtToken
);

Expand All @@ -262,12 +262,11 @@ export const deployGenericAToken = async ([
symbol,
]: [tEthereumAddress, tEthereumAddress, tEthereumAddress, tEthereumAddress, string, string]) => {
const instance = await withSave(
await new ATokenFactory(await getFirstSigner()).deploy(),
await new ATokenFactory(await getFirstSigner()).deploy(poolAddress),
eContractid.AToken
);

await instance.initialize(
poolAddress,
treasuryAddress,
underlyingAssetAddress,
incentivesController,
Expand All @@ -280,24 +279,23 @@ export const deployGenericAToken = async ([
return instance;
};

export const deployGenericATokenImpl = async () =>
withSave(await new ATokenFactory(await getFirstSigner()).deploy(), eContractid.AToken);
export const deployGenericATokenImpl = async (poolAddress: tEthereumAddress) =>
withSave(await new ATokenFactory(await getFirstSigner()).deploy(poolAddress), eContractid.AToken);

export const deployDelegationAwareAToken = async ([
pool,
poolAddress,
underlyingAssetAddress,
treasuryAddress,
incentivesController,
name,
symbol,
]: [tEthereumAddress, tEthereumAddress, tEthereumAddress, tEthereumAddress, string, string]) => {
const instance = await withSave(
await new DelegationAwareATokenFactory(await getFirstSigner()).deploy(),
await new DelegationAwareATokenFactory(await getFirstSigner()).deploy(poolAddress),
eContractid.DelegationAwareAToken
);

await instance.initialize(
pool,
treasuryAddress,
underlyingAssetAddress,
incentivesController,
Expand All @@ -310,9 +308,9 @@ export const deployDelegationAwareAToken = async ([
return instance;
};

export const deployDelegationAwareATokenImpl = async () =>
export const deployDelegationAwareATokenImpl = async (poolAddress: tEthereumAddress) =>
withSave(
await new DelegationAwareATokenFactory(await getFirstSigner()).deploy(),
await new DelegationAwareATokenFactory(await getFirstSigner()).deploy(poolAddress),
eContractid.DelegationAwareAToken
);

Expand Down Expand Up @@ -366,11 +364,11 @@ export const deployMockStableDebtToken = async (
args: [tEthereumAddress, tEthereumAddress, tEthereumAddress, string, string, string]
) => {
const instance = await withSave(
await new MockStableDebtTokenFactory(await getFirstSigner()).deploy(),
await new MockStableDebtTokenFactory(await getFirstSigner()).deploy(args[0]),
eContractid.MockStableDebtToken
);

await instance.initialize(args[0], args[1], args[2], '18', args[3], args[4], args[5]);
await instance.initialize(args[1], args[2], '18', args[3], args[4], args[5]);

return instance;
};
Expand All @@ -382,11 +380,11 @@ export const deployMockVariableDebtToken = async (
args: [tEthereumAddress, tEthereumAddress, tEthereumAddress, string, string, string]
) => {
const instance = await withSave(
await new MockVariableDebtTokenFactory(await getFirstSigner()).deploy(),
await new MockVariableDebtTokenFactory(await getFirstSigner()).deploy(args[0]),
eContractid.MockVariableDebtToken
);

await instance.initialize(args[0], args[1], args[2], '18', args[3], args[4], args[5]);
await instance.initialize(args[1], args[2], '18', args[3], args[4], args[5]);

return instance;
};
Expand All @@ -403,11 +401,11 @@ export const deployMockAToken = async (
]
) => {
const instance = await withSave(
await new MockATokenFactory(await getFirstSigner()).deploy(),
await new MockATokenFactory(await getFirstSigner()).deploy(args[0]),
eContractid.MockAToken
);

await instance.initialize(args[0], args[2], args[1], args[3], '18', args[4], args[5], args[6]);
await instance.initialize(args[2], args[1], args[3], '18', args[4], args[5], args[6]);

return instance;
};
Expand Down
Loading

0 comments on commit 8e4d226

Please sign in to comment.