Merge pull request #178 from aave/feat/2.5-permitDelegation

Feat/2.5 permit delegation
This commit is contained in:
The-3D 2021-07-19 02:56:08 +02:00 committed by GitHub
commit a4fc0aa073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 427 additions and 15 deletions

View File

@ -69,6 +69,26 @@ interface IStableDebtToken is IInitializableDebtToken {
uint256 rate
) external returns (bool);
/**
* @dev implements the credit delegation with ERC712 signature
* @param delegator The delegator of the credit
* @param delegatee The delegatee that can use the credit
* @param value The amount to be delegated
* @param deadline The deadline timestamp, type(uint256).max for max deadline
* @param v Signature param
* @param s Signature param
* @param r Signature param
*/
function permitDelegation(
address delegator,
address delegatee,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Burns debt of `user`
* - The resulting rate is the weighted average between the rate of the new debt

View File

@ -36,6 +36,26 @@ interface IVariableDebtToken is IScaledBalanceToken, IInitializableDebtToken {
uint256 index
) external returns (bool);
/**
* @dev implements the credit delegation with ERC712 signature
* @param delegator The delegator of the credit
* @param delegatee The delegatee that can use the credit
* @param value The amount to be delegated
* @param deadline The deadline timestamp, type(uint256).max for max deadline
* @param v Signature param
* @param s Signature param
* @param r Signature param
*/
function permitDelegation(
address delegator,
address delegatee,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Emitted when variable debt is burnt
* @param user The user which debt has been burned

View File

@ -5,6 +5,6 @@ import {StableDebtToken} from '../../protocol/tokenization/StableDebtToken.sol';
contract MockStableDebtToken is StableDebtToken {
function getRevision() internal pure override returns (uint256) {
return 0x2;
return 0x3;
}
}

View File

@ -5,6 +5,6 @@ import {VariableDebtToken} from '../../protocol/tokenization/VariableDebtToken.s
contract MockVariableDebtToken is VariableDebtToken {
function getRevision() internal pure override returns (uint256) {
return 0x2;
return 0x3;
}
}

View File

@ -18,7 +18,15 @@ import {Errors} from '../libraries/helpers/Errors.sol';
contract StableDebtToken is IStableDebtToken, DebtTokenBase {
using WadRayMath for uint256;
uint256 public constant DEBT_TOKEN_REVISION = 0x1;
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_DELEGATION_TYPEHASH =
keccak256(
'PermitDelegation(address delegator,address delegatee,uint256 value,uint256 nonce,uint256 deadline)'
);
uint256 public constant DEBT_TOKEN_REVISION = 0x2;
uint256 internal _avgStableRate;
mapping(address => uint40) internal _timestamps;
@ -29,6 +37,9 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
address internal _underlyingAsset;
IAaveIncentivesController internal _incentivesController;
mapping(address => uint256) public _nonces;
bytes32 public DOMAIN_SEPARATOR;
/**
* @dev Initializes the debt token.
* @param pool The address of the lending pool where this aToken will be used
@ -47,6 +58,13 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
string memory debtTokenSymbol,
bytes calldata params
) public override initializer {
uint256 chainId;
//solium-disable-next-line
assembly {
chainId := chainid()
}
_setName(debtTokenName);
_setSymbol(debtTokenSymbol);
_setDecimals(debtTokenDecimals);
@ -55,6 +73,16 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
_underlyingAsset = underlyingAsset;
_incentivesController = incentivesController;
DOMAIN_SEPARATOR = keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes(debtTokenName)),
keccak256(EIP712_REVISION),
chainId,
address(this)
)
);
emit Initialized(
underlyingAsset,
address(pool),
@ -256,6 +284,51 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
emit Transfer(user, address(0), amount);
}
/**
* @dev implements the credit delegation with ERC712 signature
* @param delegator The delegator of the credit
* @param delegatee The delegatee that can use the credit
* @param value The amount to be delegated
* @param deadline The deadline timestamp, type(uint256).max for max deadline
* @param v Signature param
* @param s Signature param
* @param r Signature param
*/
function permitDelegation(
address delegator,
address delegatee,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external override {
require(delegator != address(0), 'INVALID_DELEGATOR');
//solium-disable-next-line
require(block.timestamp <= deadline, 'INVALID_EXPIRATION');
uint256 currentValidNonce = _nonces[delegator];
bytes32 digest =
keccak256(
abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR,
keccak256(
abi.encode(
PERMIT_DELEGATION_TYPEHASH,
delegator,
delegatee,
value,
currentValidNonce,
deadline
)
)
)
);
require(delegator == ecrecover(digest, v, r, s), 'INVALID_SIGNATURE');
_nonces[delegator] = currentValidNonce.add(1);
_approveDelegation(delegator, delegatee, value);
}
/**
* @dev Calculates the increase in balance since the last user interaction
* @param user The address of the user for which the interest is being accumulated

View File

@ -17,12 +17,23 @@ import {IAaveIncentivesController} from '../../interfaces/IAaveIncentivesControl
contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
using WadRayMath for uint256;
uint256 public constant DEBT_TOKEN_REVISION = 0x1;
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_DELEGATION_TYPEHASH =
keccak256(
'PermitDelegation(address delegator,address delegatee,uint256 value,uint256 nonce,uint256 deadline)'
);
uint256 public constant DEBT_TOKEN_REVISION = 0x2;
ILendingPool internal _pool;
address internal _underlyingAsset;
IAaveIncentivesController internal _incentivesController;
mapping(address => uint256) public _nonces;
bytes32 public DOMAIN_SEPARATOR;
/**
* @dev Initializes the debt token.
* @param pool The address of the lending pool where this aToken will be used
@ -41,6 +52,13 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
string memory debtTokenSymbol,
bytes calldata params
) public override initializer {
uint256 chainId;
//solium-disable-next-line
assembly {
chainId := chainid()
}
_setName(debtTokenName);
_setSymbol(debtTokenSymbol);
_setDecimals(debtTokenDecimals);
@ -49,6 +67,16 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
_underlyingAsset = underlyingAsset;
_incentivesController = incentivesController;
DOMAIN_SEPARATOR = keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes(debtTokenName)),
keccak256(EIP712_REVISION),
chainId,
address(this)
)
);
emit Initialized(
underlyingAsset,
address(pool),
@ -135,6 +163,51 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
emit Burn(user, amount, index);
}
/**
* @dev implements the credit delegation with ERC712 signature
* @param delegator The delegator of the credit
* @param delegatee The delegatee that can use the credit
* @param value The amount to be delegated
* @param deadline The deadline timestamp, type(uint256).max for max deadline
* @param v Signature param
* @param s Signature param
* @param r Signature param
*/
function permitDelegation(
address delegator,
address delegatee,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external override {
require(delegator != address(0), 'INVALID_DELEGATOR');
//solium-disable-next-line
require(block.timestamp <= deadline, 'INVALID_EXPIRATION');
uint256 currentValidNonce = _nonces[delegator];
bytes32 digest =
keccak256(
abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR,
keccak256(
abi.encode(
PERMIT_DELEGATION_TYPEHASH,
delegator,
delegatee,
value,
currentValidNonce,
deadline
)
)
)
);
require(delegator == ecrecover(digest, v, r, s), 'INVALID_SIGNATURE');
_nonces[delegator] = currentValidNonce.add(1);
_approveDelegation(delegator, delegatee, value);
}
/**
* @dev Returns the principal debt balance of the user from
* @return The debt balance of the user since the last burn/mint action

View File

@ -38,8 +38,7 @@ abstract contract DebtTokenBase is
* force a delegator HF to go below 1)
**/
function approveDelegation(address delegatee, uint256 amount) external override {
_borrowAllowances[_msgSender()][delegatee] = amount;
emit BorrowAllowanceDelegated(_msgSender(), delegatee, _getUnderlyingAssetAddress(), amount);
_approveDelegation(_msgSender(), delegatee, amount);
}
/**
@ -118,6 +117,15 @@ abstract contract DebtTokenBase is
revert('ALLOWANCE_NOT_SUPPORTED');
}
function _approveDelegation(
address delegator,
address delegatee,
uint256 amount
) internal {
_borrowAllowances[delegator][delegatee] = amount;
emit BorrowAllowanceDelegated(delegator, delegatee, _getUnderlyingAssetAddress(), amount);
}
function _decreaseBorrowAllowance(
address delegator,
address delegatee,

View File

@ -142,14 +142,8 @@ export const linkBytecode = (artifact: BuidlerArtifact | Artifact, libraries: an
};
export const getParamPerNetwork = <T>(param: iParamsPerNetwork<T>, network: eNetwork) => {
const {
main,
ropsten,
kovan,
coverage,
buidlerevm,
tenderlyMain,
} = param as iEthereumParamsPerNetwork<T>;
const { main, ropsten, kovan, coverage, buidlerevm, tenderlyMain } =
param as iEthereumParamsPerNetwork<T>;
const { matic, mumbai } = param as iPolygonParamsPerNetwork<T>;
const { xdai } = param as iXDaiParamsPerNetwork<T>;
if (process.env.FORK) {
@ -327,6 +321,48 @@ export const buildFlashLiquidationAdapterParams = (
);
};
export const buildPermitDelegationParams = (
chainId: number,
token: tEthereumAddress,
revision: string,
tokenName: string,
delegator: tEthereumAddress,
delegatee: tEthereumAddress,
nonce: number,
deadline: string,
value: tStringTokenSmallUnits
) => ({
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
PermitDelegation: [
{ name: 'delegator', type: 'address' },
{ name: 'delegatee', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
],
},
primaryType: 'PermitDelegation' as const,
domain: {
name: tokenName,
version: revision,
chainId: chainId,
verifyingContract: token,
},
message: {
delegator,
delegatee,
value,
nonce,
deadline,
},
});
export const verifyContract = async (
id: string,
instance: Contract,

View File

@ -0,0 +1,170 @@
import { MAX_UINT_AMOUNT, ZERO_ADDRESS } from '../../helpers/constants';
import { BUIDLEREVM_CHAINID } from '../../helpers/buidler-constants';
import {
buildPermitDelegationParams,
buildPermitParams,
convertToCurrencyDecimals,
getSignatureFromTypedData,
} from '../../helpers/contracts-helpers';
import { expect } from 'chai';
import { BigNumber, ethers } from 'ethers';
import { makeSuite, TestEnv } from './helpers/make-suite';
import { DRE } from '../../helpers/misc-utils';
import { waitForTx } from '../../helpers/misc-utils';
import { _TypedDataEncoder } from 'ethers/lib/utils';
const { parseEther } = ethers.utils;
makeSuite('Permit Delegation', (testEnv: TestEnv) => {
const mintedAmount = '1000';
let daiMintedAmount: BigNumber;
let wethMintedAmount: BigNumber;
it('Checks the domain separator', async () => {
const { variableDebtDai, stableDebtDai, weth, dai } = testEnv;
const variableSeparator = await variableDebtDai.DOMAIN_SEPARATOR();
const stableSeparator = await stableDebtDai.DOMAIN_SEPARATOR();
const variableDomain = {
name: await variableDebtDai.name(),
version: '1',
chainId: DRE.network.config.chainId,
verifyingContract: variableDebtDai.address,
};
const stableDomain = {
name: await stableDebtDai.name(),
version: '1',
chainId: DRE.network.config.chainId,
verifyingContract: stableDebtDai.address,
};
const variableDomainSeparator = _TypedDataEncoder.hashDomain(variableDomain);
const stableDomainSeparator = _TypedDataEncoder.hashDomain(stableDomain);
expect(variableSeparator).to.be.equal(
variableDomainSeparator,
'Invalid variable domain separator'
);
expect(stableSeparator).to.be.equal(stableDomainSeparator, 'Invalid stable domain separator');
});
it('Setup the lending pool', async () => {
const {
pool,
weth,
dai,
deployer: user1,
users: [user2, user3],
} = testEnv;
daiMintedAmount = await convertToCurrencyDecimals(dai.address, mintedAmount);
wethMintedAmount = await convertToCurrencyDecimals(weth.address, mintedAmount);
await dai.mint(daiMintedAmount);
await dai.approve(pool.address, daiMintedAmount);
await pool.deposit(dai.address, daiMintedAmount, user1.address, 0);
await weth.connect(user2.signer).mint(wethMintedAmount);
await weth.connect(user2.signer).approve(pool.address, wethMintedAmount);
await pool.connect(user2.signer).deposit(weth.address, wethMintedAmount, user2.address, 0);
});
it('User 3 borrows variable interest dai on behalf of user 2 via permit', async () => {
const {
pool,
variableDebtDai,
stableDebtDai,
weth,
dai,
deployer: user1,
users: [user2, user3],
} = testEnv;
const chainId = DRE.network.config.chainId || BUIDLEREVM_CHAINID;
const expiration = MAX_UINT_AMOUNT;
const nonce = (await variableDebtDai._nonces(user2.address)).toNumber();
const permitAmount = daiMintedAmount.div(3);
const msgParams = buildPermitDelegationParams(
chainId,
variableDebtDai.address,
'1',
await variableDebtDai.name(),
user2.address,
user3.address,
nonce,
expiration,
permitAmount.toString()
);
const user2PrivateKey = require('../../test-wallets.js').accounts[1].secretKey;
if (!user2PrivateKey) {
throw new Error('INVALID_OWNER_PK');
}
expect(
(await variableDebtDai.borrowAllowance(user2.address, user3.address)).toString()
).to.be.equal('0');
const { v, r, s } = getSignatureFromTypedData(user2PrivateKey, msgParams);
await variableDebtDai
.connect(user1.signer)
.permitDelegation(user2.address, user3.address, permitAmount, expiration, v, r, s);
expect(
(await variableDebtDai.borrowAllowance(user2.address, user3.address)).toString()
).to.be.equal(permitAmount);
await pool.connect(user3.signer).borrow(dai.address, permitAmount, 2, 0, user2.address);
expect(
(await variableDebtDai.borrowAllowance(user2.address, user3.address)).toString()
).to.be.equal('0');
});
it('User 3 borrows stable interest dai on behalf of user 2 via permit', async () => {
const {
pool,
variableDebtDai,
stableDebtDai,
weth,
dai,
deployer: user1,
users: [user2, user3],
} = testEnv;
const chainId = DRE.network.config.chainId || BUIDLEREVM_CHAINID;
const expiration = MAX_UINT_AMOUNT;
const nonce = (await stableDebtDai._nonces(user2.address)).toNumber();
const permitAmount = daiMintedAmount.div(3);
const msgParams = buildPermitDelegationParams(
chainId,
stableDebtDai.address,
'1',
await stableDebtDai.name(),
user2.address,
user3.address,
nonce,
expiration,
permitAmount.toString()
);
const user2PrivateKey = require('../../test-wallets.js').accounts[1].secretKey;
if (!user2PrivateKey) {
throw new Error('INVALID_OWNER_PK');
}
expect(
(await stableDebtDai.borrowAllowance(user2.address, user3.address)).toString()
).to.be.equal('0');
const { v, r, s } = getSignatureFromTypedData(user2PrivateKey, msgParams);
await stableDebtDai
.connect(user1.signer)
.permitDelegation(user2.address, user3.address, permitAmount, expiration, v, r, s);
expect(
(await stableDebtDai.borrowAllowance(user2.address, user3.address)).toString()
).to.be.equal(permitAmount);
await pool
.connect(user3.signer)
.borrow(dai.address, daiMintedAmount.div(10), 1, 0, user2.address);
expect(
(await stableDebtDai.borrowAllowance(user2.address, user3.address)).toString()
).to.be.equal(permitAmount.sub(daiMintedAmount.div(10)));
});
});

View File

@ -14,6 +14,8 @@ import {
getUniswapLiquiditySwapAdapter,
getUniswapRepayAdapter,
getFlashLiquidationAdapter,
getVariableDebtToken,
getStableDebtToken,
} from '../../../helpers/contracts-getters';
import { eEthereumNetwork, eNetwork, tEthereumAddress } from '../../../helpers/types';
import { LendingPool } from '../../../types/LendingPool';
@ -37,7 +39,7 @@ import { WETH9Mocked } from '../../../types/WETH9Mocked';
import { WETHGateway } from '../../../types/WETHGateway';
import { solidity } from 'ethereum-waffle';
import { AaveConfig } from '../../../markets/aave';
import { FlashLiquidationAdapter } from '../../../types';
import { FlashLiquidationAdapter, StableDebtToken, VariableDebtToken } from '../../../types';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { usingTenderly } from '../../../helpers/tenderly-utils';
@ -63,6 +65,8 @@ export interface TestEnv {
aWETH: AToken;
dai: MintableERC20;
aDai: AToken;
variableDebtDai: VariableDebtToken;
stableDebtDai: StableDebtToken;
aUsdc: AToken;
usdc: MintableERC20;
aave: MintableERC20;
@ -93,6 +97,8 @@ const testEnv: TestEnv = {
aWETH: {} as AToken,
dai: {} as MintableERC20,
aDai: {} as AToken,
variableDebtDai: {} as VariableDebtToken,
stableDebtDai: {} as StableDebtToken,
aUsdc: {} as AToken,
usdc: {} as MintableERC20,
aave: {} as MintableERC20,
@ -147,6 +153,10 @@ export async function initializeMakeSuite() {
const reservesTokens = await testEnv.helpersContract.getAllReservesTokens();
const daiAddress = reservesTokens.find((token) => token.symbol === 'DAI')?.tokenAddress;
const {
variableDebtTokenAddress: variableDebtDaiAddress,
stableDebtTokenAddress: stableDebtDaiAddress,
} = await testEnv.helpersContract.getReserveTokensAddresses(daiAddress || '');
const usdcAddress = reservesTokens.find((token) => token.symbol === 'USDC')?.tokenAddress;
const aaveAddress = reservesTokens.find((token) => token.symbol === 'AAVE')?.tokenAddress;
const wethAddress = reservesTokens.find((token) => token.symbol === 'WETH')?.tokenAddress;
@ -159,6 +169,8 @@ export async function initializeMakeSuite() {
}
testEnv.aDai = await getAToken(aDaiAddress);
testEnv.variableDebtDai = await getVariableDebtToken(variableDebtDaiAddress);
testEnv.stableDebtDai = await getStableDebtToken(stableDebtDaiAddress);
testEnv.aUsdc = await getAToken(aUsdcAddress);
testEnv.aWETH = await getAToken(aWEthAddress);