Skip to content

Commit

Permalink
Merge pull request #50 from renproject/typescripts-tests
Browse files Browse the repository at this point in the history
Use Truffle's native Typescript support
  • Loading branch information
0x31 committed May 13, 2019
2 parents d34bb9d + 11d2ac1 commit 218d722
Show file tree
Hide file tree
Showing 22 changed files with 1,271 additions and 516 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ coverage.json
.env
/build
/.merged
/test
local_*
test-ts/typings/bindings/*.ts
bindings.go

types

!node_modules/openzeppelin-solidity/
node_modules/openzeppelin-solidity/*
!node_modules/openzeppelin-solidity/contracts/
2 changes: 1 addition & 1 deletion migrations/1_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference types="../test-ts/typings/truffle" />
/// <reference types="../types/truffle-contracts" />

const BN = require("bn.js");

Expand Down
27 changes: 15 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
"version": "0.0.3",
"repository": "https://github.io/renproject/darknode-sol",
"scripts": {
"clean": "if [ -d \"test\" ]; then rm -r test; fi;",
"test": "if [ -d \"test\" ]; then yarn run warn; else yarn run test:ts; fi;",
"coverage": "if [ -d \"test\" ]; then yarn run warn; else yarn run coverage:ts; fi;",
"merge": "sol-merger \"./contracts/**/*.sol\"",
"warn": "echo \"Refusing to overwrite 'test' directory. Run '\\033[1;33myarn run clean\\033[0m'.\n\"",
"test:ts": "trap \"yarn run clean\" INT TERM; (yarn run bindings:ts && tsc && truffle test) && yarn run clean",
"coverage:ts": "trap \"yarn run clean\" INT TERM; (yarn run bindings:ts && tsc && solidity-coverage) && yarn run clean",
"bindings:ts": "truffle compile && abi-gen --abis 'build/contracts/*.json' --output ./test-ts/typings/bindings --template './test-ts/typings/bindings/templates/contract.handlebars' --partials './test-ts/typings/bindings/templates/partials/*.handlebars' 1> /dev/null",
"generate": "truffle compile && typechain --target truffle './build/**/*.json'",
"coverage": "yarn run generate && solidity-coverage",
"test": "yarn run generate && truffle test",
"bindings:ts": "truffle compile && typechain --target web3-1.0.0 './build/**/*.json'",
"bindings:go": "solc --allow-paths . --combined-json bin,abi,userdoc,devdoc,metadata contracts/**/*.sol | abigen -pkg bindings --out bindings.go --abi -",
"merge": "sol-merger \"./contracts/**/*.sol\"",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"postinstall": "patch-package",
"deployToKovan": "truffle migrate --skipDryRun --network kovan && yarn run merge .merged/kovan",
"deployToMainnet": "truffle migrate --skipDryRun --network mainnet && yarn run merge .merged/mainnet",
"coveralls": "cat ./coverage/lcov.info | coveralls"
"deployToMainnet": "truffle migrate --skipDryRun --network mainnet && yarn run merge .merged/mainnet"
},
"dependencies": {
"openzeppelin-solidity": "2.2.0"
},
"devDependencies": {
"@0x/abi-gen": "^2.0.9",
"@types/chai": "^4.1.7",
"@types/chai-as-promised": "^7.1.0",
"@types/crypto-js": "^3.1.43",
Expand All @@ -38,13 +35,18 @@
"eth-gas-reporter": "^0.2.0",
"ganache-cli": "^6.4.3",
"js-sha3": "^0.8.0",
"patch-package": "^6.1.2",
"postinstall-postinstall": "^2.0.0",
"sol-merger": "^0.1.3",
"solc": "^0.5.8",
"solidity-coverage": "github:rotcivegaf/solidity-coverage#5875f5b7bc74d447f3312c9c0e9fc7814b482477",
"truffle": "5.0.15",
"truffle-hdwallet-provider": "1.0.8",
"truffle-typings": "^1.0.5",
"ts-generator": "^0.0.8",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"typechain": "^0.3.8",
"typescript": "^3.4.5",
"underscore": "^1.9.1",
"web3": "^1.0.0-beta.52",
Expand All @@ -53,6 +55,7 @@
"web3-providers": "^1.0.0-beta.52"
},
"resolutions": {
"solc": "^0.5.8"
"solc": "^0.5.8",
"solidity-coverage": "github:rotcivegaf/solidity-coverage#5875f5b7bc74d447f3312c9c0e9fc7814b482477"
}
}
11 changes: 11 additions & 0 deletions patches/solidity-coverage+0.5.11.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
diff --git a/node_modules/solidity-coverage/lib/truffleConfig.js b/node_modules/solidity-coverage/lib/truffleConfig.js
index 51eb480..a22a277 100644
--- a/node_modules/solidity-coverage/lib/truffleConfig.js
+++ b/node_modules/solidity-coverage/lib/truffleConfig.js
@@ -1,5 +1,6 @@
module.exports = function truffleConfig(port, gasLimit, gasPrice) {
return `
+ require("ts-node/register");
module.exports = {
networks: {
development: {
14 changes: 14 additions & 0 deletions patches/typechain+0.3.14.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/node_modules/typechain/dist/targets/truffle/generation.js b/node_modules/typechain/dist/targets/truffle/generation.js
index 893422e..4255ed6 100644
--- a/node_modules/typechain/dist/targets/truffle/generation.js
+++ b/node_modules/typechain/dist/targets/truffle/generation.js
@@ -4,7 +4,8 @@ const typeParser_1 = require("../../parser/typeParser");
function codegen(contracts) {
const template = `
/// <reference types="truffle-typings" />
-import { BigNumber } from "bignumber.js";
+import BN from "bn.js";
+type BigNumber = string | number | BN;

${contracts.map(generateContractInterface).join("\n")}

71 changes: 0 additions & 71 deletions test-ts/typings/bindings/templates/contract.handlebars

This file was deleted.

8 changes: 0 additions & 8 deletions test-ts/typings/bindings/templates/partials/event.handlebars

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions test-ts/typings/logs.d.ts

This file was deleted.

42 changes: 0 additions & 42 deletions test-ts/typings/truffle.d.ts

This file was deleted.

23 changes: 9 additions & 14 deletions test-ts/CompatibleERC20.ts → test/CompatibleERC20.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import BN from "bn.js";

import "./helper/testUtils";
import { CompatibleERC20TestInstance, ReturnsFalseTokenInstance } from "../types/truffle-contracts";

import { ERC20Contract } from "./typings/bindings/erc20";

import { CompatibleERC20TestArtifact, CompatibleERC20TestContract } from "./typings/bindings/compatible_erc20_test";
import { CompatibleERC20FunctionsArtifact, CompatibleERC20FunctionsContract } from "./typings/bindings/compatible_erc20_functions";

const CompatibleERC20Functions = artifacts.require("CompatibleERC20Functions") as CompatibleERC20FunctionsArtifact;
const CompatibleERC20Test = artifacts.require("CompatibleERC20Test") as CompatibleERC20TestArtifact;
const CompatibleERC20Test = artifacts.require("CompatibleERC20Test");
const NormalToken = artifacts.require("NormalToken");
const ReturnsFalseToken = artifacts.require("ReturnsFalseToken");
const NonCompliantToken = artifacts.require("NonCompliantToken");
const TokenWithFees = artifacts.require("TokenWithFees");

contract("CompatibleERC20", (accounts) => {

let mock: CompatibleERC20TestContract;
let mock: CompatibleERC20TestInstance;

before(async () => {
mock = await CompatibleERC20Test.new();
Expand All @@ -33,7 +28,7 @@ contract("CompatibleERC20", (accounts) => {

for (const testCase of testCases) {
context(testCase.desc, async () => {
let token: ERC20Contract;
let token: ReturnsFalseTokenInstance;
const FEE = VALUE.mul(new BN(testCase.fees)).div(new BN(1000));

before(async () => {
Expand Down Expand Up @@ -82,7 +77,7 @@ contract("CompatibleERC20", (accounts) => {
// Approve and deposit
await token.approve(mock.address, 0);
await mock.naiveDeposit(token.address, VALUE)
.should.be.rejectedWith(null, /revert/);
.should.be.rejectedWith(/revert/);

// Compare balances after depositing
(await token.balanceOf(accounts[0])).should.bignumber.equal(before);
Expand All @@ -97,7 +92,7 @@ contract("CompatibleERC20", (accounts) => {
// Approve and deposit
await token.approve(mock.address, 0);
await mock.deposit(token.address, VALUE)
.should.be.rejectedWith(null, /revert/);
.should.be.rejectedWith(/revert/);

// Compare balances after depositing
(await token.balanceOf(accounts[0])).should.bignumber.equal(before);
Expand All @@ -111,7 +106,7 @@ contract("CompatibleERC20", (accounts) => {

// Withdraw
await mock.withdraw(token.address, VALUE.mul(new BN(2)))
.should.be.rejectedWith(null, /revert/);
.should.be.rejectedWith(/revert/);

// Compare balances after depositing
(await token.balanceOf(accounts[0])).should.bignumber.equal(before);
Expand All @@ -133,7 +128,7 @@ contract("CompatibleERC20", (accounts) => {
// Approve twice without resetting allowance
await mock.approve(token.address, NEW_VALUE);
await mock.approve(token.address, NEW_VALUE)
.should.be.rejectedWith(null, /revert/);
.should.be.rejectedWith(/revert/);

// Can transfer from the contract
await token.transferFrom(mock.address, accounts[0], NEW_VALUE.sub(NEW_FEE));
Expand All @@ -158,7 +153,7 @@ contract("CompatibleERC20", (accounts) => {
await token.approve(mock.address, VALUE);
if (testCase.fees) {
await mock.naiveDeposit(token.address, VALUE)
.should.be.rejectedWith(null, "incorrect balance in deposit");
.should.be.rejectedWith("incorrect balance in deposit");
await token.approve(mock.address, 0);
} else {
await mock.naiveDeposit(token.address, VALUE);
Expand Down
Loading

0 comments on commit 218d722

Please sign in to comment.