Skip to content

Commit

Permalink
fix: Refactor tokens, move domain separator function to IncentivizedE…
Browse files Browse the repository at this point in the history
…RC20
  • Loading branch information
LHerskind committed Nov 12, 2021
1 parent 24eb3f2 commit c033f9d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 72 deletions.
33 changes: 1 addition & 32 deletions contracts/protocol/tokenization/AToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
using WadRayMath for uint256;
using SafeERC20 for IERC20;

bytes public constant EIP712_REVISION = bytes('1');
bytes32 internal constant EIP712_DOMAIN =
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)');
bytes32 public constant PERMIT_TYPEHASH =
keccak256('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)');

Expand All @@ -34,9 +31,6 @@ contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
/// @dev owner => next valid nonce to submit with permit()
mapping(address => uint256) public _nonces;

bytes32 internal _domainSeparator;
uint256 internal immutable _chainId;

IPool internal immutable _pool;
address internal _treasury;
address internal _underlyingAsset;
Expand All @@ -55,7 +49,6 @@ contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
IncentivizedERC20(pool.getAddressesProvider(), 'ATOKEN_IMPL', 'ATOKEN_IMPL', 0)
{
_pool = pool;
_chainId = block.chainid;
}

/// @inheritdoc IInitializableAToken
Expand All @@ -76,15 +69,7 @@ contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
_underlyingAsset = underlyingAsset;
_incentivesController = incentivesController;

_domainSeparator = keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes(name())),
keccak256(EIP712_REVISION),
block.chainid,
address(this)
)
);
_domainSeparator = _calculateDomainSeparator();

emit Initialized(
underlyingAsset,
Expand All @@ -98,22 +83,6 @@ contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
);
}

function DOMAIN_SEPARATOR() public view returns (bytes32) {
if (block.chainid == _chainId) {
return _domainSeparator;
}
return
keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes(name())),
keccak256(EIP712_REVISION),
block.chainid,
address(this)
)
);
}

/// @inheritdoc IAToken
function burn(
address user,
Expand Down
28 changes: 28 additions & 0 deletions contracts/protocol/tokenization/IncentivizedERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ abstract contract IncentivizedERC20 is Context, IERC20, IERC20Detailed {
IAaveIncentivesController internal _incentivesController;
IPoolAddressesProvider internal _addressesProvider;

bytes public constant EIP712_REVISION = bytes('1');
bytes32 internal constant EIP712_DOMAIN =
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)');

bytes32 internal _domainSeparator;
uint256 internal immutable _chainId;

constructor(
IPoolAddressesProvider addressesProvider,
string memory name,
Expand All @@ -55,6 +62,7 @@ abstract contract IncentivizedERC20 is Context, IERC20, IERC20Detailed {
_name = name;
_symbol = symbol;
_decimals = decimals;
_chainId = block.chainid;
}

/// @inheritdoc IERC20Detailed
Expand Down Expand Up @@ -228,4 +236,24 @@ abstract contract IncentivizedERC20 is Context, IERC20, IERC20Detailed {
function _setDecimals(uint8 newDecimals) internal {
_decimals = newDecimals;
}

function DOMAIN_SEPARATOR() public view returns (bytes32) {
if (block.chainid == _chainId) {
return _domainSeparator;
}
return _calculateDomainSeparator();
}

function _calculateDomainSeparator() internal view returns (bytes32) {
return
keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes(name())),
keccak256(EIP712_REVISION),
block.chainid,
address(this)
)
);
}
}
10 changes: 1 addition & 9 deletions contracts/protocol/tokenization/StableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
_underlyingAsset = underlyingAsset;
_incentivesController = incentivesController;

_domainSeparator = keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes(name())),
keccak256(EIP712_REVISION),
block.chainid,
address(this)
)
);
_domainSeparator = _calculateDomainSeparator();

emit Initialized(
underlyingAsset,
Expand Down
10 changes: 1 addition & 9 deletions contracts/protocol/tokenization/VariableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
_underlyingAsset = underlyingAsset;
_incentivesController = incentivesController;

_domainSeparator = keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes(name())),
keccak256(EIP712_REVISION),
block.chainid,
address(this)
)
);
_domainSeparator = _calculateDomainSeparator();

emit Initialized(
underlyingAsset,
Expand Down
22 changes: 0 additions & 22 deletions contracts/protocol/tokenization/base/DebtTokenBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@ abstract contract DebtTokenBase is
ICreditDelegationToken
{
mapping(address => mapping(address => uint256)) internal _borrowAllowances;
bytes public constant EIP712_REVISION = bytes('1');
bytes32 internal constant EIP712_DOMAIN =
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)');
bytes32 public constant DELEGATION_WITH_SIG_TYPEHASH =
keccak256(
'DelegationWithSig(address delegator,address delegatee,uint256 value,uint256 nonce,uint256 deadline)'
);
mapping(address => uint256) public _nonces;
bytes32 internal _domainSeparator;
uint256 internal immutable _chainId;
IPool internal immutable _pool;

/**
Expand All @@ -43,23 +38,6 @@ abstract contract DebtTokenBase is
IncentivizedERC20(pool.getAddressesProvider(), 'DEBT_TOKEN_IMPL', 'DEBT_TOKEN_IMPL', 0)
{
_pool = pool;
_chainId = block.chainid;
}

function DOMAIN_SEPARATOR() public view returns (bytes32) {
if (block.chainid == _chainId) {
return _domainSeparator;
}
return
keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes(name())),
keccak256(EIP712_REVISION),
block.chainid,
address(this)
)
);
}

/// @inheritdoc ICreditDelegationToken
Expand Down

0 comments on commit c033f9d

Please sign in to comment.