Skip to content

Adds integration tests for opening and closing a loan #1330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions test/integration/behaviors/short.behavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const ethers = require('ethers');
const {
utils: { parseEther },
} = ethers;
const { approveIfNeeded } = require('../utils/approve');
const { assert } = require('../../contracts/common');
const { toBytes32 } = require('../../../index');
const { ensureBalance } = require('../utils/balances');
const { exchangeSynths } = require('../utils/exchanging');
const { skipWaitingPeriod } = require('../utils/skip');

function itCanOpenAndCloseShort({ ctx }) {
describe('shorting', () => {
const amountToDeposit = parseEther('1000'); // sUSD
const amountToBorrow = parseEther('1'); // sETH

let user;
let CollateralShort, SynthsUSD, CollateralStateShort;

before('target contracts and users', () => {
({ CollateralShort, SynthsUSD, CollateralStateShort } = ctx.contracts);

user = ctx.users.someUser;
});

before('ensure user should have sUSD', async () => {
await ensureBalance({ ctx, symbol: 'sUSD', user, balance: parseEther('10000') });
});

before('ensure sETH supply exists', async () => {
// CollateralManager.getShortRate requires existing sETH else div by zero
await exchangeSynths({
ctx,
src: 'sUSD',
dest: 'sETH',
amount: parseEther('10'),
user: ctx.users.otherUser,
});
});

describe('open and close a short', async () => {
let tx, loan, loanId;

describe('opening a loan', () => {
before('approve the synths for collateral short', async () => {
await approveIfNeeded({
token: SynthsUSD,
owner: user,
beneficiary: CollateralShort,
amount: parseEther('10000'), // sUSD
});
});

before('open the loan', async () => {
CollateralShort = CollateralShort.connect(user);

tx = await CollateralShort.open(amountToDeposit, amountToBorrow, toBytes32('sETH'));

const { events } = await tx.wait();
const event = events.find(l => l.event === 'LoanCreated');
loanId = event.args.id;

loan = await CollateralStateShort.getLoan(user.address, loanId);
});

it('shows the loan amount is non zero when opened', async () => {
assert.bnEqual(loan.amount, parseEther('1'));
});

describe('closing a loan', () => {
let interactionDelay, CollateralShortAsOwner;

before('skip waiting period by setting interaction delay to zero', async () => {
CollateralShortAsOwner = CollateralShort.connect(ctx.users.owner);
interactionDelay = await CollateralShortAsOwner.interactionDelay();

await CollateralShortAsOwner.setInteractionDelay('0');
});

before('close the loan', async () => {
await exchangeSynths({
ctx,
src: 'sUSD',
dest: 'sETH',
amount: parseEther('1000'),
user,
});

// Ignore settlement period for sUSD --> sETH closing the loan
await skipWaitingPeriod({ ctx });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajsantander @justinjmoses updated to use skipWaitingPeriod new pattern


tx = await CollateralShort.close(loanId);
loan = await CollateralStateShort.getLoan(user.address, loanId);
});

after('restore waiting period', async () => {
await CollateralShortAsOwner.setInteractionDelay(interactionDelay);
});

it('shows the loan amount is zero when closed', async () => {
assert.bnEqual(loan.amount, '0');
});
});
});
});
});
}

module.exports = {
itCanOpenAndCloseShort,
};
9 changes: 9 additions & 0 deletions test/integration/l1/Shorts.l1.integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { bootstrapL1 } = require('../utils/bootstrap');
const { itCanOpenAndCloseShort } = require('../behaviors/short.behavior');

describe('Shorts integration tests (L1)', () => {
const ctx = this;
bootstrapL1({ ctx });

itCanOpenAndCloseShort({ ctx });
});
11 changes: 11 additions & 0 deletions test/integration/utils/exchanging.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ async function exchangeSomething({ ctx }) {
await tx.wait();
}

async function exchangeSynths({ ctx, src, dest, amount, user }) {
let { Synthetix } = ctx.contracts;
Synthetix = Synthetix.connect(user);

await ensureBalance({ ctx, symbol: src, user, balance: amount });

const tx = await Synthetix.exchange(toBytes32(src), amount, toBytes32(dest));
await tx.wait();
}

module.exports = {
exchangeSomething,
exchangeSynths,
};