aave-protocol-v2/test/helpers/scenario-engine.ts

257 lines
6.5 KiB
TypeScript
Raw Normal View History

2020-07-13 08:54:08 +00:00
import {TestEnv, SignerWithAddress} from './make-suite';
import {
mint,
approve,
deposit,
borrow,
withdraw,
repay,
setUseAsCollateral,
swapBorrowRateMode,
rebalanceStableBorrowRate,
redirectInterestStream,
redirectInterestStreamOf,
allowInterestRedirectionTo,
2020-07-13 08:54:08 +00:00
} from './actions';
import {RateMode} from '../../helpers/types';
export interface Action {
name: string;
args?: any;
expected: string;
revertMessage?: string;
}
export interface Story {
description: string;
actions: Action[];
}
export interface Scenario {
title: string;
description: string;
stories: Story[];
}
export const executeStory = async (story: Story, testEnv: TestEnv) => {
for (const action of story.actions) {
const {users} = testEnv;
await executeAction(action, users, testEnv);
}
};
2020-07-13 08:54:08 +00:00
const executeAction = async (action: Action, users: SignerWithAddress[], testEnv: TestEnv) => {
2020-07-07 10:07:31 +00:00
const {reserve, user: userIndex, borrowRateMode} = action.args;
const {name, expected, revertMessage} = action;
2020-07-13 08:54:08 +00:00
if (!name || name === '') {
throw 'Action name is missing';
}
2020-07-13 08:54:08 +00:00
if (!reserve || reserve === '') {
throw 'Invalid reserve selected for deposit';
}
2020-07-13 08:54:08 +00:00
if (!userIndex || userIndex === '') {
throw `Invalid user selected to deposit into the ${reserve} reserve`;
}
2020-07-13 08:54:08 +00:00
if (!expected || expected === '') {
throw `An expected resut for action ${name} is required`;
}
2020-07-07 10:07:31 +00:00
let rateMode: string = RateMode.None;
2020-07-13 08:54:08 +00:00
if (borrowRateMode) {
if (borrowRateMode === 'none') {
2020-07-07 10:07:31 +00:00
RateMode.None;
2020-07-13 08:54:08 +00:00
} else if (borrowRateMode === 'stable') {
2020-07-07 10:07:31 +00:00
rateMode = RateMode.Stable;
2020-07-13 08:54:08 +00:00
} else if (borrowRateMode === 'variable') {
2020-07-07 10:07:31 +00:00
rateMode = RateMode.Variable;
} else {
//random value, to test improper selection of the parameter
2020-07-13 08:54:08 +00:00
rateMode = '4';
2020-07-07 10:07:31 +00:00
}
}
const user = users[parseInt(userIndex)];
switch (name) {
2020-07-13 08:54:08 +00:00
case 'mint':
const {amount} = action.args;
2020-07-13 08:54:08 +00:00
if (!amount || amount === '') {
throw `Invalid amount of ${reserve} to mint`;
}
await mint(reserve, amount, user);
break;
2020-07-13 08:54:08 +00:00
case 'approve':
await approve(reserve, user, testEnv);
break;
2020-07-13 08:54:08 +00:00
case 'deposit':
{
const {amount, sendValue} = action.args;
2020-07-13 08:54:08 +00:00
if (!amount || amount === '') {
throw `Invalid amount to deposit into the ${reserve} reserve`;
}
2020-07-13 08:54:08 +00:00
await deposit(reserve, amount, user, sendValue, expected, testEnv, revertMessage);
}
break;
case 'withdraw':
{
const {amount} = action.args;
2020-07-13 08:54:08 +00:00
if (!amount || amount === '') {
throw `Invalid amount to withdraw from the ${reserve} reserve`;
}
await withdraw(reserve, amount, user, expected, testEnv, revertMessage);
}
break;
2020-07-13 08:54:08 +00:00
case 'borrow':
{
2020-07-07 10:07:31 +00:00
const {amount, timeTravel} = action.args;
2020-07-13 08:54:08 +00:00
if (!amount || amount === '') {
throw `Invalid amount to borrow from the ${reserve} reserve`;
}
2020-07-13 08:54:08 +00:00
await borrow(reserve, amount, rateMode, user, timeTravel, expected, testEnv, revertMessage);
}
break;
2020-07-13 08:54:08 +00:00
case 'repay':
{
2020-06-30 12:09:28 +00:00
const {amount, borrowRateMode, sendValue} = action.args;
let {onBehalfOf: onBehalfOfIndex} = action.args;
2020-07-13 08:54:08 +00:00
if (!amount || amount === '') {
throw `Invalid amount to repay into the ${reserve} reserve`;
}
2020-07-13 08:54:08 +00:00
let userToRepayOnBehalf: SignerWithAddress;
2020-07-13 08:54:08 +00:00
if (!onBehalfOfIndex || onBehalfOfIndex === '') {
console.log(
2020-07-13 08:54:08 +00:00
'WARNING: No onBehalfOf specified for a repay action. Defaulting to the repayer address'
);
userToRepayOnBehalf = user;
} else {
userToRepayOnBehalf = users[parseInt(onBehalfOfIndex)];
}
await repay(
reserve,
amount,
2020-06-30 12:09:28 +00:00
rateMode,
user,
userToRepayOnBehalf,
sendValue,
expected,
testEnv,
revertMessage
);
}
break;
2020-07-13 08:54:08 +00:00
case 'setUseAsCollateral':
{
const {useAsCollateral} = action.args;
2020-07-13 08:54:08 +00:00
if (!useAsCollateral || useAsCollateral === '') {
throw `A valid value for useAsCollateral needs to be set when calling setUseReserveAsCollateral on reserve ${reserve}`;
}
2020-07-13 08:54:08 +00:00
await setUseAsCollateral(reserve, user, useAsCollateral, expected, testEnv, revertMessage);
}
break;
2020-07-13 08:54:08 +00:00
case 'swapBorrowRateMode':
2020-07-07 10:07:31 +00:00
await swapBorrowRateMode(reserve, user, rateMode, expected, testEnv, revertMessage);
break;
2020-07-13 08:54:08 +00:00
case 'rebalanceStableBorrowRate':
{
const {target: targetIndex} = action.args;
2020-07-13 08:54:08 +00:00
if (!targetIndex || targetIndex === '') {
throw `A target must be selected when trying to rebalance a stable rate`;
}
const target = users[parseInt(targetIndex)];
2020-07-13 08:54:08 +00:00
await rebalanceStableBorrowRate(reserve, user, target, expected, testEnv, revertMessage);
}
break;
2020-07-13 08:54:08 +00:00
case 'redirectInterestStream':
{
const {to: toIndex} = action.args;
2020-07-13 08:54:08 +00:00
if (!toIndex || toIndex === '') {
throw `A target must be selected when trying to redirect the interest`;
}
const toUser = users[parseInt(toIndex)];
await redirectInterestStream(
reserve,
user,
toUser.address,
expected,
testEnv,
revertMessage
);
}
break;
2020-07-13 08:54:08 +00:00
case 'redirectInterestStreamOf':
{
const {from: fromIndex, to: toIndex} = action.args;
2020-07-13 08:54:08 +00:00
if (!fromIndex || fromIndex === '') {
throw `A from address must be specified when trying to redirect the interest`;
}
2020-07-13 08:54:08 +00:00
if (!toIndex || toIndex === '') {
throw `A target must be selected when trying to redirect the interest`;
}
const toUser = users[parseInt(toIndex)];
const fromUser = users[parseInt(fromIndex)];
await redirectInterestStreamOf(
reserve,
user,
fromUser.address,
toUser.address,
expected,
testEnv,
revertMessage
);
}
break;
2020-07-13 08:54:08 +00:00
case 'allowInterestRedirectionTo':
{
const {to: toIndex} = action.args;
2020-07-13 08:54:08 +00:00
if (!toIndex || toIndex === '') {
throw `A target must be selected when trying to redirect the interest`;
}
const toUser = users[parseInt(toIndex)];
await allowInterestRedirectionTo(
reserve,
user,
toUser.address,
expected,
testEnv,
revertMessage
);
}
break;
default:
throw `Invalid action requested: ${name}`;
}
};