Skip to content

Commit

Permalink
Merge pull request #12 from Augmint/test_improvements
Browse files Browse the repository at this point in the history
Test improvements
  • Loading branch information
Peter Petrovics authored Feb 14, 2018
2 parents fc44d95 + f24bfa8 commit 6d9cc1c
Show file tree
Hide file tree
Showing 27 changed files with 651 additions and 707 deletions.
10 changes: 7 additions & 3 deletions migrations/10_deploy_Locker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ module.exports = function(deployer) {
deployer.deploy(Locker, TokenAEur.address, MonetarySupervisor.address);
deployer.then(async () => {
const tokenAEur = TokenAEur.at(TokenAEur.address);
await tokenAEur.grantMultiplePermissions(Locker.address, ["NoFeeTransferContracts"]);
const monetarySupervisor = await MonetarySupervisor.at(MonetarySupervisor.address);
await monetarySupervisor.grantMultiplePermissions(Locker.address, ["LockerContracts"]);
const monetarySupervisor = MonetarySupervisor.at(MonetarySupervisor.address);
const locker = Locker.at(Locker.address);
await Promise.all([
tokenAEur.grantPermission(Locker.address, "NoFeeTransferContracts"),
monetarySupervisor.grantPermission(Locker.address, "LockerContracts"),
locker.addLockProduct(50000, 60, 100, true) // to be used in tests to make unit test independent
]);
});
};
6 changes: 2 additions & 4 deletions migrations/11_deploy_Exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ const TokenAEur = artifacts.require("./TokenAEur.sol");
const SafeMath = artifacts.require("./SafeMath.sol");
const Exchange = artifacts.require("./Exchange.sol");

module.exports = function(deployer, network, accounts) {
module.exports = function(deployer) {
deployer.link(SafeMath, Exchange);
deployer.deploy(Exchange, TokenAEur.address);
deployer.then(async () => {
const exchange = Exchange.at(Exchange.address);

const tokenAEur = TokenAEur.at(TokenAEur.address);
await tokenAEur.grantMultiplePermissions(Exchange.address, ["NoFeeTransferContracts"]);
await tokenAEur.grantPermission(Exchange.address, "NoFeeTransferContracts");
});
};
8 changes: 5 additions & 3 deletions migrations/6_deploy_TokenAEur.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module.exports = async function(deployer, network, accounts) {
);

const tokenAEur = TokenAEur.at(TokenAEur.address);
await tokenAEur.grantMultiplePermissions(accounts[0], ["MonetaryBoard"]);
await tokenAEur.grantMultiplePermissions(FeeAccount.address, ["NoFeeTransferContracts"]);
await tokenAEur.grantMultiplePermissions(AugmintReserves.address, ["NoFeeTransferContracts"]);
await Promise.all([
tokenAEur.grantPermission(accounts[0], "MonetaryBoard"),
tokenAEur.grantPermission(FeeAccount.address, "NoFeeTransferContracts"),
tokenAEur.grantPermission(AugmintReserves.address, "NoFeeTransferContracts")
]);
};
12 changes: 8 additions & 4 deletions migrations/8_deploy_MonetarySupervisor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ module.exports = async function(deployer) {
50000000 /* allowedLtdDifferenceAmount =5,000 A-EUR - if totalLoan and totalLock difference is less than that
then allow loan or lock even if ltdDifference limit would go off with it */
);
const interestEarnedAccount = InterestEarnedAccount.at(InterestEarnedAccount.address);
await interestEarnedAccount.grantMultiplePermissions(MonetarySupervisor.address, ["MonetarySupervisorContract"]);

const interestEarnedAccount = InterestEarnedAccount.at(InterestEarnedAccount.address);
const tokenAEur = TokenAEur.at(TokenAEur.address);
await tokenAEur.grantMultiplePermissions(MonetarySupervisor.address, ["MonetarySupervisorContract"]);
await tokenAEur.grantMultiplePermissions(MonetarySupervisor.address, ["NoFeeTransferContracts"]);
const augmintReserves = AugmintReserves.at(AugmintReserves.address);
await Promise.all([
interestEarnedAccount.grantPermission(MonetarySupervisor.address, "MonetarySupervisorContract"),
tokenAEur.grantPermission(MonetarySupervisor.address, "MonetarySupervisorContract"),
tokenAEur.grantPermission(MonetarySupervisor.address, "NoFeeTransferContracts"),
augmintReserves.grantPermission(MonetarySupervisor.address, "MonetarySupervisorContract")
]);
};
10 changes: 6 additions & 4 deletions migrations/9_deploy_LoanManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ module.exports = function(deployer, network, accounts) {
);
deployer.then(async () => {
const lm = LoanManager.at(LoanManager.address);
await lm.grantMultiplePermissions(accounts[0], ["MonetaryBoard"]);
const tokenAEur = TokenAEur.at(TokenAEur.address);
await tokenAEur.grantMultiplePermissions(LoanManager.address, ["NoFeeTransferContracts"]);

const monetarySupervisor = MonetarySupervisor.at(MonetarySupervisor.address);
await monetarySupervisor.grantMultiplePermissions(LoanManager.address, ["LoanManagerContracts"]);

await Promise.all([
lm.grantPermission(accounts[0], "MonetaryBoard"),
tokenAEur.grantPermission(LoanManager.address, "NoFeeTransferContracts"),
monetarySupervisor.grantPermission(LoanManager.address, "LoanManagerContracts")
]);

const onTest =
web3.version.network == 999 ||
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"bignumber.js": "5.0.0",
"stringifier": "1.3.0",
"babel-register": "6.26.0",
"ganache-cli": "6.0.3",
"ganache-cli": "6.1.0-beta.0",
"random-seed": "0.3.0",
"truffle": "4.0.6"
}
Expand Down
27 changes: 13 additions & 14 deletions test/ExchangeMatching.test.js → test/exchangeMatching.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
const testHelper = new require("./helpers/testHelper.js");
const monetarySupervisorTestHelpers = require("./helpers/monetarySupervisorTestHelpers.js");
const tokenAceTestHelper = require("./helpers/tokenAceTestHelper.js");
const exchangeTestHelper = require("./helpers/exchangeTestHelper.js");
const testHelpers = new require("./helpers/testHelpers.js");
const tokenTestHelpers = require("./helpers/tokenTestHelpers.js");
const exchangeTestHelper = require("./helpers/exchangeTestHelpers.js");

const TOKEN_BUY = 0;
const TOKEN_SELL = 1;

let snapshotId;
let monetarySupervisor, tokenAce, exchange;
let augmintToken = null;
let exchange = null;
const maker = web3.eth.accounts[1];
const taker = web3.eth.accounts[2];

contract("Exchange matching tests", accounts => {
before(async function() {
tokenAce = await tokenAceTestHelper.newTokenAceMock();
monetarySupervisor = await monetarySupervisorTestHelpers.newMonetarySupervisorMock(tokenAce);
augmintToken = await tokenTestHelpers.initAugmintToken();

await monetarySupervisor.issueToReserve(1000000000);
await monetarySupervisorTestHelpers.withdrawFromReserve(maker, 100000000);
await monetarySupervisorTestHelpers.withdrawFromReserve(taker, 100000000);
await tokenTestHelpers.issueToReserve(1000000000);
await tokenTestHelpers.withdrawFromReserve(maker, 100000000);
await tokenTestHelpers.withdrawFromReserve(taker, 100000000);

exchange = await exchangeTestHelper.newExchangeMock(tokenAce);
exchange = await exchangeTestHelper.getExchange();
});

beforeEach(async function() {
snapshotId = await testHelper.takeSnapshot();
snapshotId = await testHelpers.takeSnapshot();
});

afterEach(async function() {
await testHelper.revertSnapshot(snapshotId);
await testHelpers.revertSnapshot(snapshotId);
});

it("should match two matching orders (buy token fully filled)", async function() {
Expand Down Expand Up @@ -94,7 +93,7 @@ contract("Exchange matching tests", accounts => {

await exchangeTestHelper.newOrder(this, buyOrder);
await exchangeTestHelper.newOrder(this, sellOrder);
await testHelper.expectThrow(exchange.matchOrders(buyOrder.id, sellOrder.id));
await testHelpers.expectThrow(exchange.matchOrders(buyOrder.id, sellOrder.id));
});

it("should match multiple orders"); // ensure edge cases of passing the same order twice
Expand Down
53 changes: 26 additions & 27 deletions test/ExchangeOrders.test.js → test/exchangeOrders.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
const testHelper = new require("./helpers/testHelper.js");
const tokenAceTestHelper = require("./helpers/tokenAceTestHelper.js");
const monetarySupervisorTestHelpers = require("./helpers/monetarySupervisorTestHelpers.js");
const exchangeTestHelper = require("./helpers/exchangeTestHelper.js");
const testHelpers = new require("./helpers/testHelpers.js");
const tokenTestHelpers = require("./helpers/tokenTestHelpers.js");
const exchangeTestHelper = require("./helpers/exchangeTestHelpers.js");

const TOKEN_BUY = 0;
const TOKEN_SELL = 1;
const makers = [web3.eth.accounts[1], web3.eth.accounts[2]];

let snapshotId;
let tokenAce, exchange, monetarySupervisor;
const makers = [web3.eth.accounts[1], web3.eth.accounts[2]];
let augmintToken = null;
let exchange = null;

contract("Exchange orders tests", accounts => {
before(async function() {
tokenAce = await tokenAceTestHelper.newTokenAceMock();
monetarySupervisor = await monetarySupervisorTestHelpers.newMonetarySupervisorMock(tokenAce);
augmintToken = await tokenTestHelpers.initAugmintToken();

await monetarySupervisor.issueToReserve(1000000000);
await tokenTestHelpers.issueToReserve(1000000000);

await Promise.all(makers.map(maker => monetarySupervisorTestHelpers.withdrawFromReserve(maker, 100000000)));
await Promise.all(makers.map(maker => tokenTestHelpers.withdrawFromReserve(maker, 100000000)));

exchange = await exchangeTestHelper.newExchangeMock(tokenAce);
exchange = await exchangeTestHelper.getExchange();
});

beforeEach(async function() {
snapshotId = await testHelper.takeSnapshot();
snapshotId = await testHelpers.takeSnapshot();
});

afterEach(async function() {
await testHelper.revertSnapshot(snapshotId);
await testHelpers.revertSnapshot(snapshotId);
});

it("place buy token orders", async function() {
Expand All @@ -47,8 +46,8 @@ contract("Exchange orders tests", accounts => {
viaAugmintToken: false
};

const tx = await tokenAce.approve(exchange.address, order.amount * 2, { from: order.maker });
testHelper.logGasUse(this, tx, "approve");
const tx = await augmintToken.approve(exchange.address, order.amount * 2, { from: order.maker });
testHelpers.logGasUse(this, tx, "approve");

await exchangeTestHelper.newOrder(this, order);
await exchangeTestHelper.newOrder(this, order);
Expand All @@ -63,10 +62,10 @@ contract("Exchange orders tests", accounts => {
viaAugmintToken: false
};

const tx = await tokenAce.approve(exchange.address, order.amount - 1, { from: order.maker });
testHelper.logGasUse(this, tx, "approve");
const tx = await augmintToken.approve(exchange.address, order.amount - 1, { from: order.maker });
testHelpers.logGasUse(this, tx, "approve");

await testHelper.expectThrow(exchange.placeSellTokenOrder(order.price, order.amount, { from: order.maker }));
await testHelpers.expectThrow(exchange.placeSellTokenOrder(order.price, order.amount, { from: order.maker }));
});

it("place a sell token order via AugmintToken", async function() {
Expand All @@ -84,19 +83,19 @@ contract("Exchange orders tests", accounts => {

it("shouldn't place a SELL token order with 0 price", async function() {
const price = 11000;
await testHelper.expectThrow(tokenAce.transferAndNotify(exchange.address, 0, price, { from: makers[0] }));
await testHelpers.expectThrow(augmintToken.transferAndNotify(exchange.address, 0, price, { from: makers[0] }));
});

it("shouldn't place a BUY token order with 0 price", async function() {
const price = 11000;
await testHelper.expectThrow(exchange.placeBuyTokenOrder(price, { value: 0 }));
await testHelpers.expectThrow(exchange.placeBuyTokenOrder(price, { value: 0 }));
});

it("no SELL token order when user doesn't have enough ACE", async function() {
const price = 11000;
const userBal = await tokenAce.balanceOf(makers[0]);
await testHelper.expectThrow(
tokenAce.transferAndNotify(exchange.address, userBal + 1, price, { from: makers[0] })
const userBal = await augmintToken.balanceOf(makers[0]);
await testHelpers.expectThrow(
augmintToken.transferAndNotify(exchange.address, userBal + 1, price, { from: makers[0] })
);
});

Expand All @@ -120,8 +119,8 @@ contract("Exchange orders tests", accounts => {

await exchangeTestHelper.newOrder(this, buyOrder);
await exchangeTestHelper.newOrder(this, sellOrder);
await testHelper.expectThrow(exchange.cancelBuyTokenOrder(buyOrder.id, { from: accounts[0] }));
await testHelper.expectThrow(exchange.cancelSellTokenOrder(sellOrder.id, { from: accounts[0] }));
await testHelpers.expectThrow(exchange.cancelBuyTokenOrder(buyOrder.id, { from: accounts[0] }));
await testHelpers.expectThrow(exchange.cancelSellTokenOrder(sellOrder.id, { from: accounts[0] }));
});

it("should return x buy orders from offset", async function() {
Expand Down Expand Up @@ -158,7 +157,7 @@ contract("Exchange orders tests", accounts => {
const orders = [];
for (let i = 0; i < orderCount; i++) {
orders.push(
tokenAce.transferAndNotify(exchange.address, i + 1, 10000 + i, {
augmintToken.transferAndNotify(exchange.address, i + 1, 10000 + i, {
from: makers[0]
})
);
Expand All @@ -185,6 +184,6 @@ contract("Exchange orders tests", accounts => {
});

it("should only allow the token contract call transferNotification", async function() {
await testHelper.expectThrow(exchange.transferNotification(accounts[0], 1000, 0, { from: accounts[0] }));
await testHelpers.expectThrow(exchange.transferNotification(accounts[0], 1000, 0, { from: accounts[0] }));
});
});
42 changes: 20 additions & 22 deletions test/ExchangeRandom.test.js → test/exchangeRandom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const RandomSeed = require("random-seed");
const testHelper = new require("./helpers/testHelper.js");
const tokenAceTestHelper = require("./helpers/tokenAceTestHelper.js");
const monetarySupervisorTestHelpers = require("./helpers/monetarySupervisorTestHelpers.js");
const exchangeTestHelper = require("./helpers/exchangeTestHelper.js");

const testHelpers = new require("./helpers/testHelpers.js");
const tokenTestHelpers = require("./helpers/tokenTestHelpers.js");
const exchangeTestHelper = require("./helpers/exchangeTestHelpers.js");

const ONEWEI = 1000000000000000000;
const ETH_ROUND = 1000000000000; // 6 decimals places max in ETH
Expand All @@ -19,9 +19,10 @@ const MAX_TOKEN = 10000000; // 1,000 ACE
const TEST_ACCS_CT = web3.eth.accounts.length;
const ACC_INIT_ACE = 100000000;
const CHUNK_SIZE = 100;

let tokenAce, monetarySupervisor, exchange;
const random = new RandomSeed("Have the same test data");

let augmintToken = null;
let exchange = null;
let buyTokenOrders = null;
let sellTokenOrders = null;
let matches = [];
Expand Down Expand Up @@ -66,18 +67,15 @@ const getOrderToFill = async () => {
*/
contract("Exchange random tests", accounts => {
before(async function() {
tokenAce = await tokenAceTestHelper.newTokenAceMock();
monetarySupervisor = await monetarySupervisorTestHelpers.newMonetarySupervisorMock(tokenAce);
await monetarySupervisor.issueToReserve(TEST_ACCS_CT * ACC_INIT_ACE);
augmintToken = await tokenTestHelpers.initAugmintToken();
await tokenTestHelpers.issueToReserve(TEST_ACCS_CT * ACC_INIT_ACE);

console.log(`\x1b[2m\t*** Topping up ${TEST_ACCS_CT} accounts each with ${ACC_INIT_ACE / 10000} A-EURO\x1b[0m`);
await Promise.all(
accounts
.slice(0, TEST_ACCS_CT)
.map(acc => monetarySupervisorTestHelpers.withdrawFromReserve(acc, ACC_INIT_ACE))
accounts.slice(0, TEST_ACCS_CT).map(acc => tokenTestHelpers.withdrawFromReserve(acc, ACC_INIT_ACE))
);

exchange = await exchangeTestHelper.newExchangeMock(tokenAce);
exchange = await exchangeTestHelper.getExchange();
});

it("place x buy / sell orders", async function() {
Expand All @@ -104,15 +102,15 @@ contract("Exchange random tests", accounts => {
if (order.orderType === TOKEN_BUY) {
tx = exchange.placeBuyTokenOrder(order.price, { value: order.amount, from: order.maker });
} else {
tx = tokenAce.transferAndNotify(exchange.address, order.amount, order.price, {
tx = augmintToken.transferAndNotify(exchange.address, order.amount, order.price, {
from: order.maker
});
}
return tx;
})
);
txs.map(tx =>
testHelper.logGasUse(
testHelpers.logGasUse(
this,
tx,
typeof tx.logs[0].args.weiAmount === "undefined"
Expand All @@ -125,7 +123,7 @@ contract("Exchange random tests", accounts => {
});

it("should fill x matching orders", async function() {
const snapshotId = await testHelper.takeSnapshot();
const snapshotId = await testHelpers.takeSnapshot();
//await exchangeTestHelper.printOrderBook(10);

let match = await getOrderToFill();
Expand All @@ -148,11 +146,11 @@ contract("Exchange random tests", accounts => {

//await exchangeTestHelper.printOrderBook(10);

await testHelper.revertSnapshot(snapshotId);
await testHelpers.revertSnapshot(snapshotId);
});

it("should match x orders at once (matchMultipleOrders)", async function() {
const snapshotId = await testHelper.takeSnapshot();
const snapshotId = await testHelpers.takeSnapshot();
//await exchangeTestHelper.printOrderBook(10);

// convert & transpose matches to the format required by matchMultipleOrders
Expand All @@ -167,19 +165,19 @@ contract("Exchange random tests", accounts => {
);

const tx = await exchange.matchMultipleOrders(matchArgs.buyTokenIds, matchArgs.sellTokenIds);
testHelper.logGasUse(this, tx, "matchMultipleOrders");
testHelpers.logGasUse(this, tx, "matchMultipleOrders");

//await exchangeTestHelper.printOrderBook(10);

const stateAfter = await exchangeTestHelper.getState();
assert.equal(stateAfter.sellCount, stateAfterAllMatch.sellCount, "sellCount should == after 1by1 matching all");
assert.equal(stateAfter.buyCount, stateAfterAllMatch.buyCount, "buyCount should == after 1by1 matching all");

await testHelper.revertSnapshot(snapshotId);
await testHelpers.revertSnapshot(snapshotId);
});

it("should cancel all orders", async function() {
const snapshotId = await testHelper.takeSnapshot();
const snapshotId = await testHelpers.takeSnapshot();
//await exchangeTestHelper.printOrderBook(10);
//const stateBefore = await exchangeTestHelper.getState();

Expand All @@ -199,6 +197,6 @@ contract("Exchange random tests", accounts => {
assert.equal(stateAfter.sellCount, 0);
assert.equal(stateAfter.buyCount, 0);

await testHelper.revertSnapshot(snapshotId);
await testHelpers.revertSnapshot(snapshotId);
});
});
File renamed without changes.
Loading

0 comments on commit 6d9cc1c

Please sign in to comment.