2020-07-13 08:54:08 +00:00
|
|
|
import {TestEnv, SignerWithAddress} from './make-suite';
|
2020-06-13 09:12:49 +00:00
|
|
|
import {
|
|
|
|
mint,
|
|
|
|
approve,
|
|
|
|
deposit,
|
|
|
|
borrow,
|
2020-08-19 10:56:39 +00:00
|
|
|
withdraw,
|
2020-06-13 09:12:49 +00:00
|
|
|
repay,
|
|
|
|
setUseAsCollateral,
|
|
|
|
swapBorrowRateMode,
|
2020-09-09 19:16:39 +00:00
|
|
|
rebalanceStableBorrowRate
|
2020-07-13 08:54:08 +00:00
|
|
|
} from './actions';
|
|
|
|
import {RateMode} from '../../helpers/types';
|
2020-06-12 20:12:53 +00:00
|
|
|
|
|
|
|
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;
|
2020-06-12 20:12:53 +00:00
|
|
|
const {name, expected, revertMessage} = action;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!name || name === '') {
|
|
|
|
throw 'Action name is missing';
|
2020-06-12 20:12:53 +00:00
|
|
|
}
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!reserve || reserve === '') {
|
|
|
|
throw 'Invalid reserve selected for deposit';
|
2020-06-12 20:12:53 +00:00
|
|
|
}
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!userIndex || userIndex === '') {
|
2020-06-12 20:12:53 +00:00
|
|
|
throw `Invalid user selected to deposit into the ${reserve} reserve`;
|
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!expected || expected === '') {
|
2020-06-12 20:12:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 09:12:49 +00:00
|
|
|
const user = users[parseInt(userIndex)];
|
2020-06-12 20:12:53 +00:00
|
|
|
|
|
|
|
switch (name) {
|
2020-07-13 08:54:08 +00:00
|
|
|
case 'mint':
|
2020-06-12 20:12:53 +00:00
|
|
|
const {amount} = action.args;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!amount || amount === '') {
|
2020-06-12 20:12:53 +00:00
|
|
|
throw `Invalid amount of ${reserve} to mint`;
|
|
|
|
}
|
|
|
|
|
2020-06-13 09:12:49 +00:00
|
|
|
await mint(reserve, amount, user);
|
2020-06-12 20:12:53 +00:00
|
|
|
break;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
case 'approve':
|
2020-06-13 09:12:49 +00:00
|
|
|
await approve(reserve, user, testEnv);
|
2020-06-12 20:12:53 +00:00
|
|
|
break;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
case 'deposit':
|
2020-06-12 20:12:53 +00:00
|
|
|
{
|
2020-09-09 10:47:27 +00:00
|
|
|
const {amount, sendValue, onBehalfOf: onBehalfOfIndex} = action.args;
|
|
|
|
const onBehalfOf = onBehalfOfIndex
|
|
|
|
? users[parseInt(onBehalfOfIndex)].address
|
|
|
|
: user.address;
|
2020-06-12 20:12:53 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!amount || amount === '') {
|
2020-06-12 20:12:53 +00:00
|
|
|
throw `Invalid amount to deposit into the ${reserve} reserve`;
|
|
|
|
}
|
|
|
|
|
2020-09-09 10:47:27 +00:00
|
|
|
await deposit(
|
|
|
|
reserve,
|
|
|
|
amount,
|
|
|
|
user,
|
|
|
|
onBehalfOf,
|
|
|
|
sendValue,
|
|
|
|
expected,
|
|
|
|
testEnv,
|
|
|
|
revertMessage
|
|
|
|
);
|
2020-06-12 20:12:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-08-19 10:56:39 +00:00
|
|
|
case 'withdraw':
|
2020-06-13 09:12:49 +00:00
|
|
|
{
|
|
|
|
const {amount} = action.args;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!amount || amount === '') {
|
2020-08-19 10:56:39 +00:00
|
|
|
throw `Invalid amount to withdraw from the ${reserve} reserve`;
|
2020-06-13 09:12:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 10:56:39 +00:00
|
|
|
await withdraw(reserve, amount, user, expected, testEnv, revertMessage);
|
2020-06-13 09:12:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-07-13 08:54:08 +00:00
|
|
|
case 'borrow':
|
2020-06-13 09:12:49 +00:00
|
|
|
{
|
2020-07-07 10:07:31 +00:00
|
|
|
const {amount, timeTravel} = action.args;
|
2020-06-13 09:12:49 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!amount || amount === '') {
|
2020-06-13 09:12:49 +00:00
|
|
|
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);
|
2020-06-13 09:12:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
case 'repay':
|
2020-06-13 09:12:49 +00:00
|
|
|
{
|
2020-06-30 12:09:28 +00:00
|
|
|
const {amount, borrowRateMode, sendValue} = action.args;
|
2020-06-13 09:12:49 +00:00
|
|
|
let {onBehalfOf: onBehalfOfIndex} = action.args;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!amount || amount === '') {
|
2020-06-13 09:12:49 +00:00
|
|
|
throw `Invalid amount to repay into the ${reserve} reserve`;
|
|
|
|
}
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-06-13 09:12:49 +00:00
|
|
|
let userToRepayOnBehalf: SignerWithAddress;
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!onBehalfOfIndex || onBehalfOfIndex === '') {
|
2020-06-13 09:12:49 +00:00
|
|
|
console.log(
|
2020-07-13 08:54:08 +00:00
|
|
|
'WARNING: No onBehalfOf specified for a repay action. Defaulting to the repayer address'
|
2020-06-13 09:12:49 +00:00
|
|
|
);
|
|
|
|
userToRepayOnBehalf = user;
|
|
|
|
} else {
|
|
|
|
userToRepayOnBehalf = users[parseInt(onBehalfOfIndex)];
|
|
|
|
}
|
|
|
|
|
|
|
|
await repay(
|
|
|
|
reserve,
|
|
|
|
amount,
|
2020-06-30 12:09:28 +00:00
|
|
|
rateMode,
|
2020-06-13 09:12:49 +00:00
|
|
|
user,
|
|
|
|
userToRepayOnBehalf,
|
|
|
|
sendValue,
|
|
|
|
expected,
|
|
|
|
testEnv,
|
|
|
|
revertMessage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
case 'setUseAsCollateral':
|
2020-06-13 09:12:49 +00:00
|
|
|
{
|
|
|
|
const {useAsCollateral} = action.args;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!useAsCollateral || useAsCollateral === '') {
|
2020-06-13 09:12:49 +00:00
|
|
|
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);
|
2020-06-13 09:12:49 +00:00
|
|
|
}
|
|
|
|
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);
|
2020-06-13 09:12:49 +00:00
|
|
|
break;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
case 'rebalanceStableBorrowRate':
|
2020-06-13 09:12:49 +00:00
|
|
|
{
|
|
|
|
const {target: targetIndex} = action.args;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (!targetIndex || targetIndex === '') {
|
2020-06-13 09:12:49 +00:00
|
|
|
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);
|
2020-06-13 09:12:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-09-09 19:16:39 +00:00
|
|
|
|
2020-06-12 20:12:53 +00:00
|
|
|
default:
|
|
|
|
throw `Invalid action requested: ${name}`;
|
|
|
|
}
|
|
|
|
};
|