Skip to content
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

Better way to get SNX in integration tests #1303

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion contracts/MintableSynthetix.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ contract MintableSynthetix is BaseSynthetix {
) public BaseSynthetix(_proxy, _tokenState, _owner, _totalSupply, _resolver) {}

/* ========== INTERNALS =================== */

function _mintSecondary(address account, uint amount) internal {
tokenState.setBalanceOf(account, tokenState.balanceOf(account).add(amount));
emitTransfer(address(this), account, amount);
Expand Down
1 change: 0 additions & 1 deletion hardhat/tasks/task-test-integration-l2.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ task('test:integration:l2', 'run isolated layer 2 production tests')
if (taskArguments.deploy) {
await deployInstance({
useOvm: true,
ignoreCustomParameters: true,
providerUrl,
providerPort,
});
Expand Down
71 changes: 41 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions test/integration/behaviors/erc20.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ const { assert } = require('../../contracts/common');

function itBehavesLikeAnERC20({ ctx }) {
describe('erc20 functionality', () => {
let user;
let owner, user;
let Synthetix;

let userBalance;

const amountToTransfer = ethers.utils.parseEther('1');

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

owner = ctx.users.owner;
user = ctx.users.someUser;
});

Expand All @@ -19,10 +22,8 @@ function itBehavesLikeAnERC20({ ctx }) {
});

describe('when the owner transfers SNX to the user', () => {
const amountToTransfer = ethers.utils.parseEther('1');

before('transfer', async () => {
Synthetix = Synthetix.connect(ctx.users.owner);
Synthetix = Synthetix.connect(owner);

const tx = await Synthetix.transfer(user.address, amountToTransfer);
await tx.wait();
Expand Down
5 changes: 0 additions & 5 deletions test/integration/dual/withdraw.integration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const ethers = require('ethers');
const { assert } = require('../../contracts/common');
const { bootstrapDual } = require('../utils/bootstrap');
const { ensureBalance } = require('../utils/balances');
const { finalizationOnL1 } = require('../utils/watchers');

describe('withdraw() integration tests (L1, L2)', () => {
Expand All @@ -24,10 +23,6 @@ describe('withdraw() integration tests (L1, L2)', () => {
owner = ctx.l2.users.owner;
});

before('ensure the owner has SNX on L2', async () => {
await ensureBalance({ ctx: ctx.l2, symbol: 'SNX', user: owner, balance: amountToWithdraw });
});

before('record balances', async () => {
ownerBalance = await Synthetix.balanceOf(owner.address);
});
Expand Down
5 changes: 0 additions & 5 deletions test/integration/dual/withdrawTo.integration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const ethers = require('ethers');
const { assert } = require('../../contracts/common');
const { bootstrapDual } = require('../utils/bootstrap');
const { ensureBalance } = require('../utils/balances');
const { finalizationOnL1 } = require('../utils/watchers');

describe('withdrawTo() integration tests (L1, L2)', () => {
Expand All @@ -25,10 +24,6 @@ describe('withdrawTo() integration tests (L1, L2)', () => {
user = ctx.l2.users.someUser;
});

before('ensure the owner has SNX on L2', async () => {
await ensureBalance({ ctx: ctx.l2, symbol: 'SNX', user: owner, balance: amountToWithdraw });
});

before('record balances', async () => {
ownerBalance = await Synthetix.balanceOf(owner.address);
});
Expand Down
64 changes: 63 additions & 1 deletion test/integration/utils/balances.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const ethers = require('ethers');
const { deposit } = require('./bridge');
const { toBytes32 } = require('../../..');

async function ensureBalance({ ctx, symbol, user, balance }) {
const token = _getTokenFromSymbol({ ctx, symbol });
const currentBalance = await token.balanceOf(user.address);
Expand All @@ -20,24 +24,82 @@ async function _getTokens({ ctx, symbol, user, amount }) {
}

async function _getSNX({ ctx, user, amount }) {
const Synthetix = ctx.contracts.Synthetix.connect(ctx.users.owner);
let { Synthetix } = ctx.contracts;

const ownerTransferable = await Synthetix.transferableSynthetix(ctx.users.owner.address);
if (ownerTransferable.lt(amount)) {
await _getSNXForOwner({ ctx, amount: amount.sub(ownerTransferable) });
}

Synthetix = Synthetix.connect(ctx.users.owner);
const tx = await Synthetix.transfer(user.address, amount);
await tx.wait();
}

async function _getSNXForOwner({ ctx, amount }) {
if (!ctx.useOvm) {
throw new Error('There is no more SNX!');
} else {
if (ctx.l1) {
await _getSNXForOwnerOnL2ByDepositing({ ctx: ctx.l1, amount });
} else {
await _getSNXForOwnerOnL2ByHackMinting({ ctx, amount });
}
}
}

async function _getSNXForOwnerOnL2ByDepositing({ ctx, amount }) {
await deposit({ ctx, from: ctx.users.owner, to: ctx.users.owner, amount });
}

async function _getSNXForOwnerOnL2ByHackMinting({ ctx, amount }) {
const owner = ctx.users.owner;

let { Synthetix, AddressResolver } = ctx.contracts;

const bridgeName = toBytes32('SynthetixBridgeToBase');
const bridgeAddress = await AddressResolver.getAddress(bridgeName);

let tx;

AddressResolver = AddressResolver.connect(owner);
tx = await AddressResolver.importAddresses([bridgeName], [owner.address]);
await tx.wait();
tx = await AddressResolver.rebuildCaches([Synthetix.address]);
await tx.wait();

Synthetix = Synthetix.connect(owner);
tx = await Synthetix.mintSecondary(owner.address, amount);
await tx.wait();

tx = await AddressResolver.importAddresses([bridgeName], [bridgeAddress]);
await tx.wait();
tx = await AddressResolver.rebuildCaches([Synthetix.address]);
await tx.wait();
}

async function _getsUSD({ ctx, user, amount }) {
const Synthetix = ctx.contracts.Synthetix.connect(ctx.users.owner);

let tx;

const requiredSNX = await _getSNXAmountRequiredForsUSDAmount({ ctx, amount });
await ensureBalance({ ctx, symbol: 'SNX', user, balance: requiredSNX });

tx = await Synthetix.issueSynths(amount);
await tx.wait();

tx = await Synthetix.transfer(user.address, amount);
await tx.wait();
}

async function _getSNXAmountRequiredForsUSDAmount({ ctx, amount }) {
// TODO: Do not assume that the c-ratio is 600%
// TODO: Do not assume that 1 SNX = 1 sUSD

return amount.mul(ethers.BigNumber.from('6'));
}

function _getTokenFromSymbol({ ctx, symbol }) {
if (symbol === 'SNX') {
return ctx.contracts.Synthetix;
Expand Down
Loading