Skip to content

Commit

Permalink
Fix tests that broke due to Truffle upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
petejkim committed May 12, 2020
1 parent 9343b15 commit 7172076
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 616 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
build/
node_modules/
coverage/
Expand All @@ -6,7 +7,6 @@ package-lock.json
.idea/
#local codebuild results
artifacts/
.DS_Store
contracts/.DS_Store
token.json
credentials.json
Expand Down
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,54 @@

Fiat tokens on the [CENTRE](https://centre.io) network.

# Setup
## Setup

Tests need node v8.0.0 or higher, as they depend on async/await functionality.
Interacting with eth is very async-y so await makes it much easier to write
tests. Depends on truffle and testrpc for testing.
Requirements:

install truffle: `npm install -g truffle`
- Node >= v12
- Yarn

install ganache-cli: `npm install -g ganache-cli`
```
$ git clone git@github.com:centrehq/centre-tokens.git
$ cd centre-tokens
$ npm i -g yarn # Install yarn if you don't already have it
$ yarn install # Install dependencies
```

install project npm dependencies: `npm install`
## Testing

# Testing
First, make sure Ganache is running.

All tests are run with: `npm run truffle-test`
```
$ yarn ganache
```

or run a specific file of tests with: `npm run truffle-test -- [file]`
Run all tests:

to generate test coverage on all tests run: `npm test`
```
$ yarn test
```

# Contracts
To run tests in a specific file, run:

```
$ yarn test [path/to/file]
```

to generate test coverage, run:

```
$ yarn coverage
```

## Contracts

The implementation uses 2 separate contracts - a proxy contract
(`FiatTokenProxy.sol`)and an implementation contract(`FiatToken.sol`). This
(`FiatTokenProxy.sol`) and an implementation contract (`FiatToken.sol`). This
allows upgrading the contract, as a new implementation contact can be deployed
and the Proxy updated to point to it.

## FiatToken
### FiatToken

The FiatToken offers a number of capabilities, which briefly are described
below. There are more [detailed design docs](./doc/tokendesign.md) in the `doc`
Expand Down
48 changes: 26 additions & 22 deletions migrations/2_deploy_usdc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ var FiatTokenProxy = artifacts.require("./FiatTokenProxy.sol");
// Any address will do, preferably one we generated
var throwawayAddress = "0x64e078a8aa15a41b85890265648e965de686bae6";

module.exports = function (deployer, network, accounts) {
if (network == "development" || network == "coverage") {
module.exports = function (deployer, network) {
if (["development", "coverage", "test"].includes(network)) {
// Change these to the cold storage addresses provided by ops
// these are the deterministic addresses from ganache, so the private keys are well known
// and match the values we use in the tests
var admin = "0x2f560290fef1b3ada194b6aa9c40aa71f8e95598";
var masterMinter = "0x3e5e9111ae8eb78fe1cc3bb8915d5d461f3ef9a9";
var pauser = "0xaca94ef8bd5ffee41947b4585a84bda5a3d3da6e";
var blacklister = "0xd03ea8624c8c5987235048901fb614fdca89b117";
var owner = "0xe11ba2b4d45eaed5996cd0823791e0c93114882d";
var admin = "0x2F560290FEF1B3Ada194b6aA9c40aa71f8e95598";
var masterMinter = "0x3E5e9111Ae8eB78Fe1CC3bb8915d5D461F3Ef9A9";
var pauser = "0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E";
var blacklister = "0xd03ea8624C8C5987235048901fB614fDcA89b117";
var owner = "0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d";
}

console.log("deploying impl");

var fiatTokenImpl;
var tokenProxy;
// deploy implementation contract
deployer
.deploy(FiatTokenV1)
.then(function (impl) {
fiatTokenImpl = impl;
.then(function () {
return FiatTokenV1.deployed();
})
.then(function (fiatTokenV1) {
console.log("initializing impl with dummy values");
return impl.initialize(
return fiatTokenV1.initialize(
"",
"",
"",
Expand All @@ -37,23 +37,27 @@ module.exports = function (deployer, network, accounts) {
throwawayAddress
);
})
.then(function (initDone) {
.then(function () {
console.log("deploying proxy");
return deployer.deploy(FiatTokenProxy, fiatTokenImpl.address);
return deployer.deploy(FiatTokenProxy, FiatTokenV1.address);
})
.then(function () {
return FiatTokenProxy.deployed();
})
.then(function (proxy) {
tokenProxy = proxy;
.then(function (fiatTokenProxy) {
console.log("reassigning proxy admin");
// need to change admin first, or the call to initialize won't work
// since admin can only call methods in the proxy, and not forwarded methods
return proxy.changeAdmin(admin);
return fiatTokenProxy.changeAdmin(admin);
})
.then(function () {
return FiatTokenV1.at(FiatTokenProxy.address);
})
.then(function (changeAdminDone) {
.then(function (fiatTokenProxyAsFiatTokenV1) {
console.log("initializing proxy");
// Pretend that the proxy address is a FiatTokenV1
// this is fine because the proxy will forward all the calls to the FiatTokenV1 impl
tokenProxy = FiatTokenV1.at(tokenProxy.address);
return tokenProxy.initialize(
return fiatTokenProxyAsFiatTokenV1.initialize(
"USD//C",
"USDC",
"USD",
Expand All @@ -64,7 +68,7 @@ module.exports = function (deployer, network, accounts) {
owner
);
})
.then(function (initDone) {
console.log("Deployer proxy at ", tokenProxy.address);
.then(function () {
console.log("deployed proxy at ", FiatTokenProxy.address);
});
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"test": "test"
},
"scripts": {
"dev": "lite-server",
"coverage": "yarn run coverage",
"test": "scripts/start-ganache.sh && truffle test"
"fmt": "prettier --write **/*.sol **/*.js **/*.json **/*.md *.js *.json *.md",
"coverage": "truffle run coverage",
"ganache": "ganache-cli --accounts=15 --deterministic --defaultBalanceEther=1000000 --quiet",
"test": "truffle test"
},
"repository": {
"type": "git",
Expand Down
76 changes: 45 additions & 31 deletions test/ABITests.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Tx = require("ethereumjs-tx");
var Transaction = require("ethereumjs-tx").Transaction;
var tokenUtils = require("./TokenTestUtils");
var FiatToken = tokenUtils.FiatToken;
var name = tokenUtils.name;
Expand Down Expand Up @@ -68,9 +68,11 @@ async function run_tests(newToken, accounts) {
// sanity check for pausable
it("abi004 FiatToken pause() is public", async function () {
let badData = functionSignature("pause()");
var tx = new Tx({
nonce: web3.toHex(web3.eth.getTransactionCount(pauserAccount)),
gasPrice: web3.toHex(web3.toWei("20", "gwei")),
var tx = new Transaction({
nonce: web3.utils.toHex(
await web3.eth.getTransactionCount(pauserAccount)
),
gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
gasLimit: 100000,
to: token.address,
value: 0,
Expand All @@ -87,9 +89,11 @@ async function run_tests(newToken, accounts) {

it("abi040 Blacklistable constructor is not a function", async function () {
let badData = functionSignature("Blacklistable()");
var tx = new Tx({
nonce: web3.toHex(web3.eth.getTransactionCount(pauserAccount)),
gasPrice: web3.toHex(web3.toWei("20", "gwei")),
var tx = new Transaction({
nonce: web3.utils.toHex(
await web3.eth.getTransactionCount(pauserAccount)
),
gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
gasLimit: 100000,
to: token.address,
value: 0,
Expand All @@ -104,9 +108,11 @@ async function run_tests(newToken, accounts) {

it("abi042 Ownable constructor is not a function", async function () {
let badData = functionSignature("Ownable()");
var tx = new Tx({
nonce: web3.toHex(web3.eth.getTransactionCount(pauserAccount)),
gasPrice: web3.toHex(web3.toWei("20", "gwei")),
var tx = new Transaction({
nonce: web3.utils.toHex(
await web3.eth.getTransactionCount(pauserAccount)
),
gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
gasLimit: 100000,
to: token.address,
value: 0,
Expand All @@ -121,9 +127,11 @@ async function run_tests(newToken, accounts) {

it("abi005 Pausable constructor is not a function", async function () {
let badData = functionSignature("Pausable()");
var tx = new Tx({
nonce: web3.toHex(web3.eth.getTransactionCount(pauserAccount)),
gasPrice: web3.toHex(web3.toWei("20", "gwei")),
var tx = new Transaction({
nonce: web3.utils.toHex(
await web3.eth.getTransactionCount(pauserAccount)
),
gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
gasLimit: 100000,
to: token.address,
value: 0,
Expand All @@ -138,9 +146,11 @@ async function run_tests(newToken, accounts) {

it("abi043 FiatTokenProxy constructor is not a function", async function () {
let badData = functionSignature("FiatTokenProxy()");
var tx = new Tx({
nonce: web3.toHex(web3.eth.getTransactionCount(pauserAccount)),
gasPrice: web3.toHex(web3.toWei("20", "gwei")),
var tx = new Transaction({
nonce: web3.utils.toHex(
await web3.eth.getTransactionCount(pauserAccount)
),
gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
gasLimit: 100000,
to: token.address,
value: 0,
Expand All @@ -155,7 +165,7 @@ async function run_tests(newToken, accounts) {

it("abi027 UpgradeabilityProxy constructor", async function () {
let badData = msgData("UpgradeabilityProxy(address)", arbitraryAccount);
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
arbitraryAccount,
arbitraryAccountPrivateKey,
Expand All @@ -166,7 +176,7 @@ async function run_tests(newToken, accounts) {

it("abi055 Proxy constructor is not a function", async function () {
let badData = functionSignature("Proxy()");
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
arbitraryAccount,
arbitraryAccountPrivateKey,
Expand All @@ -177,7 +187,7 @@ async function run_tests(newToken, accounts) {

it("abi056 Proxy _delegate is internal", async function () {
let badData = msgData("_delegate(address)", arbitraryAccount);
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
arbitraryAccount,
arbitraryAccountPrivateKey,
Expand All @@ -188,7 +198,7 @@ async function run_tests(newToken, accounts) {

it("abi057 Proxy _willFallback is internal", async function () {
let badData = functionSignature("_willFallback()");
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
arbitraryAccount,
arbitraryAccountPrivateKey,
Expand All @@ -199,7 +209,7 @@ async function run_tests(newToken, accounts) {

it("abi058 Proxy _fallback is internal", async function () {
let badData = functionSignature("_fallback()");
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
arbitraryAccount,
arbitraryAccountPrivateKey,
Expand All @@ -210,7 +220,7 @@ async function run_tests(newToken, accounts) {

it("abi050 Upgradeability implementation is internal", async function () {
let badData = msgData("UpgradeabilityProxy(address)", arbitraryAccount);
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
arbitraryAccount,
arbitraryAccountPrivateKey,
Expand All @@ -224,7 +234,7 @@ async function run_tests(newToken, accounts) {
"AdminUpgradeabillityProxy(address)",
arbitraryAccount
);
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
arbitraryAccount,
arbitraryAccountPrivateKey,
Expand All @@ -238,7 +248,7 @@ async function run_tests(newToken, accounts) {
"AdminUpgradeabillityProxy(address)",
arbitraryAccount
);
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
arbitraryAccount,
arbitraryAccountPrivateKey,
Expand All @@ -249,9 +259,11 @@ async function run_tests(newToken, accounts) {

it("abi041 FiatToken constructor is not a function", async function () {
let badData = functionSignature("FiatToken()");
var tx = new Tx({
nonce: web3.toHex(web3.eth.getTransactionCount(pauserAccount)),
gasPrice: web3.toHex(web3.toWei("20", "gwei")),
var tx = new Transaction({
nonce: web3.utils.toHex(
await web3.eth.getTransactionCount(pauserAccount)
),
gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
gasLimit: 100000,
to: token.address,
value: 0,
Expand All @@ -266,9 +278,11 @@ async function run_tests(newToken, accounts) {

it("abi025 setOwner is internal", async function () {
let badData = msgData("setOwner(address)", pauserAccount);
var tx = new Tx({
nonce: web3.toHex(web3.eth.getTransactionCount(tokenOwnerAccount)),
gasPrice: web3.toHex(web3.toWei("20", "gwei")),
var tx = new Transaction({
nonce: web3.utils.toHex(
await web3.eth.getTransactionCount(tokenOwnerAccount)
),
gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
gasLimit: 100000,
to: token.address,
value: 0,
Expand All @@ -286,7 +300,7 @@ async function run_tests(newToken, accounts) {
"_upgradeTo(string,address)",
pauserAccount
);
let raw = makeRawTransaction(
let raw = await makeRawTransaction(
badData,
tokenOwnerAccount,
tokenOwnerPrivateKey,
Expand Down
Loading

0 comments on commit 7172076

Please sign in to comment.