Skip to content

Commit

Permalink
Use truffle/contract to instantiate legacy contracts in tests
Browse files Browse the repository at this point in the history
Fix a number of tests
  • Loading branch information
elenadimitrova committed Nov 20, 2020
1 parent 1e4730f commit ff3f290
Show file tree
Hide file tree
Showing 12 changed files with 1,167 additions and 216 deletions.
994 changes: 965 additions & 29 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"-": "0.0.1",
"@codechecks/client": "^0.1.10",
"@truffle/config": "^1.2.33",
"@truffle/contract": "^4.2.31",
"@truffle/hdwallet-provider": "^1.2.0",
"bn-chai": "^1.0.1",
"bn.js": "^5.1.3",
Expand Down
21 changes: 14 additions & 7 deletions test/baseWallet.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/* global artifacts */
const ethers = require("ethers");
const truffleAssert = require("truffle-assertions");
const TruffleContract = require("@truffle/contract");

const OldWalletV13Contract = require("../build-legacy/v1.3.0/BaseWallet");
const OldWalletV16Contract = require("../build-legacy/v1.6.0/BaseWallet");

const OldWalletV13 = TruffleContract(OldWalletV13Contract);
const OldWalletV16 = TruffleContract(OldWalletV16Contract);

const Proxy = artifacts.require("Proxy");
const BaseWallet = artifacts.require("BaseWallet");

// const OldWalletV16 = require("../build-legacy/v1.6.0/BaseWallet");
// const OldWalletV13 = require("../build-legacy/v1.3.0/BaseWallet");
let OldWalletV13;
let OldWalletV16;

const VersionManager = artifacts.require("VersionManager");
const Registry = artifacts.require("ModuleRegistry");
const SimpleUpgrader = artifacts.require("SimpleUpgrader");
Expand Down Expand Up @@ -49,6 +51,11 @@ contract("BaseWallet", (accounts) => {
}

before(async () => {
OldWalletV13.defaults({ from: accounts[0] });
OldWalletV13.setProvider(web3.currentProvider);
OldWalletV16.defaults({ from: accounts[0] });
OldWalletV16.setProvider(web3.currentProvider);

registry = await Registry.new();
guardianStorage = await GuardianStorage.new();
lockStorage = await LockStorage.new();
Expand Down Expand Up @@ -208,7 +215,7 @@ contract("BaseWallet", (accounts) => {
});
});

describe.skip("Old BaseWallet V1.3", () => {
describe("Old BaseWallet V1.3", () => {
it("should work with new modules", async () => {
const oldWallet = await OldWalletV13.new();
await oldWallet.init(owner, [module1.address]);
Expand All @@ -219,7 +226,7 @@ contract("BaseWallet", (accounts) => {
});
});

describe.skip("Old BaseWallet V1.6", () => {
describe("Old BaseWallet V1.6", () => {
it("should work with new modules", async () => {
const oldWallet = await OldWalletV16.new();
await oldWallet.init(owner, [module1.address]);
Expand Down
35 changes: 16 additions & 19 deletions test/makerV2Manager_loan.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const RelayManager = require("../utils/relay-manager");

const GemJoin = artifacts.require("GemJoin");
const Registry = artifacts.require("ModuleRegistry");

const MakerV2Manager = artifacts.require("MakerV2Manager");
const UpgradedMakerV2Manager = artifacts.require("TestUpgradedMakerV2Manager");
const MakerRegistry = artifacts.require("MakerRegistry");
Expand Down Expand Up @@ -165,15 +164,14 @@ contract("MakerV2Loan", (accounts) => {
const beforeDAI = await dai.balanceOf(walletAddress);
const beforeDAISupply = await dai.totalSupply();

const method = "openLoan";
const params = [walletAddress, collateral.address, collateralAmount.toString(), dai.address, daiAmount.toString()];
let txReceipt;
if (relayed) {
txReceipt = await manager.relay(makerV2, method, params, wallet, [owner]);
const eventTransactionExecuted = await utils.getEvent(txReceipt, relayerManager, "TransactionExecuted");
assert.isTrue(eventTransactionExecuted.args.success, "Relayed tx should succeed");
txReceipt = await manager.relay(makerV2, "openLoan", params, wallet, [owner]);
const { success } = utils.parseRelayReceipt(txReceipt);
assert.isTrue(success, "Relayed tx should succeed");
} else {
const tx = await makerV2[method](...params, { gasLimit: 2000000, from: owner });
const tx = await makerV2.openLoan(...params, { gasLimit: 2000000, from: owner });
txReceipt = tx.receipt;
}
const eventLoanOpened = await utils.getEvent(txReceipt, makerV2, "LoanOpened");
Expand Down Expand Up @@ -652,7 +650,7 @@ contract("MakerV2Loan", (accounts) => {
});
});

describe.skip("Upgrade of MakerV2Manager", () => {
describe("Upgrade of MakerV2Manager", () => {
let upgradedMakerV2;
let daiAmount;
let collateralAmount;
Expand All @@ -672,8 +670,7 @@ contract("MakerV2Loan", (accounts) => {
makerRegistry.address,
uniswapFactory.address,
makerV2.address,
versionManager.address,
{ gasLimit: 10700000 },
versionManager.address
);

// Adding BAT to the registry of supported collateral tokens
Expand All @@ -689,7 +686,7 @@ contract("MakerV2Loan", (accounts) => {
if (withBatVault) {
// Open a BAT vault with the old MakerV2 feature
const batTestAmounts = await getTestAmounts(bat.address);
await bat.mint(walletAddress, batTestAmounts.collateralAmount.add(web3.utils.toWei("0.01")));
await bat.mint(walletAddress, batTestAmounts.collateralAmount.add(new BN(web3.utils.toWei("0.01"))));
loanId2 = await testOpenLoan({
collateralAmount: batTestAmounts.collateralAmount,
daiAmount: batTestAmounts.daiAmount,
Expand All @@ -704,20 +701,20 @@ contract("MakerV2Loan", (accounts) => {
transferManager.address,
relayerManager.address,
], [upgradedMakerV2.address]);
const method = "upgradeWallet";

const lastVersion = await versionManager.lastVersion();
const params = [walletAddress, lastVersion];
const params = [walletAddress, lastVersion.toNumber()];
if (relayed) {
const txR = await manager.relay(versionManager, method, params, wallet, [owner]);
const txTransactionExecuted = await utils.getEvent(txR, cdpManager, "NewCdp");
assert.isTrue(txTransactionExecuted.args.success, "Relayed tx should succeed");
const txR = await manager.relay(versionManager, "upgradeWallet", params, wallet, [owner]);
const { success } = utils.parseRelayReceipt(txR);
assert.isTrue(success, "Relayed tx should succeed");
} else {
await versionManager[method](...params, { gasLimit: 2000000, from: owner });
await versionManager.upgradeWallet(...params, { gasLimit: 2000000, from: owner });
}
// Make sure that the vaults can be manipulated from the upgraded feature
await testChangeCollateral({
loanId: loanId1,
collateralAmount: web3.utils.toWei("0.010"),
collateralAmount: new BN(web3.utils.toWei("0.010")),
add: true,
relayed,
makerV2Manager: upgradedMakerV2,
Expand All @@ -727,7 +724,7 @@ contract("MakerV2Loan", (accounts) => {
if (withBatVault) {
await testChangeCollateral({
loanId: loanId2,
collateralAmount: web3.utils.toWei("0.010"),
collateralAmount: new BN(web3.utils.toWei("0.010")),
add: true,
relayed,
collateral: bat,
Expand Down Expand Up @@ -777,7 +774,7 @@ contract("MakerV2Loan", (accounts) => {
const lastVersion = await versionManager.lastVersion();
await versionManager.upgradeWallet(walletAddress, lastVersion, { gasLimit: 2000000, from: owner });
// Use the bad module to attempt a bad giveVault call
const callData = makerV2.contract.methods.giveVault([walletAddress, utils.numberToBytes32(666)]).encodeABI();
const callData = makerV2.contract.methods.giveVault(walletAddress, utils.numberToBytes32(666)).encodeABI();
await truffleAssert.reverts(badFeature.callContract(makerV2.address, 0, callData, { from: owner }), "MV2: unauthorized loanId");
});
});
Expand Down
14 changes: 6 additions & 8 deletions test/multisig.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* global artifacts */
const truffleAssert = require("truffle-assertions");
const utilities = require("../utils/utilities.js");

const MultiSigWallet = artifacts.require("MultiSigWallet");
const TestRegistry = artifacts.require("TestRegistry");

const MultisigExecutor = require("../utils/multisigexecutor.js");
const utilities = require("../utils/utilities.js");

contract("MultiSigWallet", (accounts) => {
const owner = accounts[0];
const owner1 = accounts[1];
Expand Down Expand Up @@ -45,7 +43,7 @@ contract("MultiSigWallet", (accounts) => {
sortedSigners = utilities.sortWalletByAddress(signers);
}
const sigs = `0x${sortedSigners.map((signer) => {
let joinedSignature = utilities.signMessageHash(signer, messageHash);
let joinedSignature = utilities.signMessageHash(signer, messageHash).slice(2);
if (returnBadSignatures) {
joinedSignature += "a1";
Expand All @@ -58,7 +56,7 @@ contract("MultiSigWallet", (accounts) => {
async function executeSendSuccess(signers) {
let nonce = await multisig.nonce();
const data = reg.contract.methods.register(number).encodeABI();
const signedData = MultisigExecutor.signHash(multisig.address, reg.address, value, data, nonce.toNumber());
const signedData = utilities.signHash(multisig.address, reg.address, value, data, nonce.toNumber());
const signatures = await getSignatures(signedData, signers);

await multisig.execute(reg.address, value, data, signatures);
Expand All @@ -81,7 +79,7 @@ contract("MultiSigWallet", (accounts) => {
nonce = nonce.toNumber() + nonceOffset;
const data = reg.contract.methods.register(number).encodeABI();

const signedData = MultisigExecutor.signHash(multisig.address, reg.address, value, data, nonce);
const signedData = utilities.signHash(multisig.address, reg.address, value, data, nonce);
const signatures = await getSignatures(signedData, signers, sortSigners, returnBadSignatures);

await truffleAssert.reverts(multisig.execute(reg.address, value, data, signatures), errorMessage);
Expand All @@ -90,7 +88,7 @@ contract("MultiSigWallet", (accounts) => {
async function getMultiSigParams(functioName, params) {
const nonce = await multisig.nonce();
const data = multisig.contract.methods[functioName](...params).encodeABI();
const signedData = MultisigExecutor.signHash(multisig.address, multisig.address, 0, data, nonce.toNumber());
const signedData = utilities.signHash(multisig.address, multisig.address, 0, data, nonce.toNumber());
const signatures = await getSignatures(signedData, [owner1, owner2]);
return { data, signatures };
}
Expand Down Expand Up @@ -237,7 +235,7 @@ contract("MultiSigWallet", (accounts) => {

it("should fail with the wrong nonce", async () => {
const nonceOffset = 1;
await executeSendFailure([owner1, owner2], nonceOffset, true, false, "MSW: Badly ordered signatures");
await executeSendFailure([owner1, owner2], nonceOffset, true, false, "MSW: Not enough valid signatures");
});

it("should fail with the wrong signature", async () => {
Expand Down
7 changes: 2 additions & 5 deletions test/relayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ contract("RelayerManager", (accounts) => {
);
});

it.skip("should fail when the gas of the transaction is less then the gasLimit ", async () => {
it("should fail when the gas of the transaction is less then the gasLimit", async () => {
const params = [wallet.address, 2];
const nonce = await utils.getNonceForRelay();
const gasLimit = 2000000;
Expand All @@ -189,11 +189,8 @@ contract("RelayerManager", (accounts) => {
ETH_TOKEN,
ethers.constants.AddressZero,
gasLimit * 0.9];
const txReceipt = await manager.relay(...relayParams);

const { success, error } = utils.parseRelayReceipt(txReceipt);
assert.isFalse(success);
assert.equal(error, "RM: not enough gas provided");
await truffleAssert.reverts(manager.relay(...relayParams), "RM: not enough gas provided");
});

it("should fail when a wrong number of signatures is provided", async () => {
Expand Down
15 changes: 10 additions & 5 deletions test/tokenExchanger.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ const bnChai = require("bn-chai");
const { expect } = chai;
chai.use(bnChai(BN));

const TruffleContract = require("@truffle/contract");

const BaseWalletContract = require("../build-legacy/v1.3.0/BaseWallet.json");

const OldWallet = TruffleContract(BaseWalletContract);

// Paraswap
const AugustusSwapper = artifacts.require("AugustusSwapper");
const Whitelisted = artifacts.require("Whitelisted");
Expand All @@ -32,10 +38,6 @@ const ModuleRegistry = artifacts.require("ModuleRegistry");
const DexRegistry = artifacts.require("DexRegistry");
const Proxy = artifacts.require("Proxy");
const BaseWallet = artifacts.require("BaseWallet");

// const OldWallet = require("../build-legacy/v1.3.0/BaseWallet");
let OldWallet;

const GuardianStorage = artifacts.require("GuardianStorage");
const LockStorage = artifacts.require("LockStorage");
const TokenExchanger = artifacts.require("TokenExchanger");
Expand Down Expand Up @@ -83,6 +85,9 @@ contract("TokenExchanger", (accounts) => {
let versionManager;

before(async () => {
OldWallet.defaults({ from: accounts[0] });
OldWallet.setProvider(web3.currentProvider);

const registry = await ModuleRegistry.new();
dexRegistry = await DexRegistry.new();
guardianStorage = await GuardianStorage.new();
Expand Down Expand Up @@ -409,7 +414,7 @@ contract("TokenExchanger", (accounts) => {
await dexRegistry.setAuthorised([kyberAdapter.address, uniswapV2Adapter.address], [true, true]);
});

it.skip(`lets old wallets call ${method} successfully`, async () => {
it(`lets old wallets call ${method} successfully`, async () => {
// create wallet
const oldWalletImplementation = await OldWallet.new();
const proxy = await Proxy.new(oldWalletImplementation.address);
Expand Down
Loading

0 comments on commit ff3f290

Please sign in to comment.