From 2705bcce158a0fd96c5974561449a4edfabef10f Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Fri, 22 Nov 2019 09:54:59 +1100 Subject: [PATCH] Massage the migrations to match contract-addresses --- packages/migrations/package.json | 14 ++++++--- packages/migrations/src/migrate_snapshot.ts | 2 +- packages/migrations/src/migration.ts | 29 +++++++++---------- .../test/snapshot_addresses_test.ts | 24 +++++++++++++++ packages/migrations/tsconfig.json | 4 +-- 5 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 packages/migrations/test/snapshot_addresses_test.ts diff --git a/packages/migrations/package.json b/packages/migrations/package.json index 52105a9f92..8fd18c63b8 100644 --- a/packages/migrations/package.json +++ b/packages/migrations/package.json @@ -5,18 +5,21 @@ "node": ">=6.12" }, "description": "0x smart contract migrations", - "main": "lib/index.js", - "types": "lib/index.d.ts", + "main": "lib/src/index.js", + "types": "lib/src/index.d.ts", "scripts": { "build": "tsc -b", "build:ci": "yarn build", "clean": "shx rm -rf lib ${npm_package_config_snapshot_name} ${npm_package_config_snapshot_name}-*.zip", "lint": "tslint --format stylish --project .", "fix": "tslint --fix --format stylish --project .", + "test": "yarn run_mocha", + "test:circleci": "yarn test", + "run_mocha": "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --bail --timeout 30000 --exit", "migrate:v2": "run-s build script:migrate:v2", "migrate:v2:snapshot": "run-s build script:migrate:v2:snapshot", - "script:migrate:v2": "node ./lib/migrate.js", - "script:migrate:v2:snapshot": "node ./lib/migrate_snapshot.js", + "script:migrate:v2": "node ./lib/src/migrate.js", + "script:migrate:v2:snapshot": "node ./lib/src/migrate_snapshot.js", "diff_docs": "git diff --exit-code ./docs", "s3:sync_md_docs": "aws s3 sync ./docs s3://docs-markdown/${npm_package_name}/v${npm_package_version} --profile 0xproject --region us-east-1 --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers", "docs:md": "ts-doc-gen --sourceDir='$PROJECT_FILES' --output=$MD_FILE_DIR --fileExtension=mdx --tsconfig=./typedoc-tsconfig.json", @@ -48,6 +51,9 @@ "make-promises-safe": "^1.1.0", "npm-run-all": "^4.1.2", "shx": "^0.2.2", + "dirty-chai": "^2.0.1", + "mocha": "^6.2.0", + "chai": "^4.0.1", "tslint": "5.11.0", "typedoc": "^0.15.0", "typescript": "3.0.1", diff --git a/packages/migrations/src/migrate_snapshot.ts b/packages/migrations/src/migrate_snapshot.ts index 2392d47ecd..b1c25f1e58 100644 --- a/packages/migrations/src/migrate_snapshot.ts +++ b/packages/migrations/src/migrate_snapshot.ts @@ -12,7 +12,7 @@ import { runMigrationsAsync } from './migration'; let providerConfigs; let provider: Web3ProviderEngine; let txDefaults; - const packageJsonPath = path.join(__dirname, '..', 'package.json'); + const packageJsonPath = path.join(__dirname, '../..', 'package.json'); const packageJsonString = fs.readFileSync(packageJsonPath, 'utf8'); const packageJson = JSON.parse(packageJsonString); if (packageJson.config === undefined || packageJson.config.snapshot_name === undefined) { diff --git a/packages/migrations/src/migration.ts b/packages/migrations/src/migration.ts index 0f5f4b70a5..38f1c5f494 100644 --- a/packages/migrations/src/migration.ts +++ b/packages/migrations/src/migration.ts @@ -176,10 +176,19 @@ export async function runMigrationsAsync( await exchange.registerAssetProxy(multiAssetProxy.address).awaitTransactionSuccessAsync(txDefaults); await exchange.registerAssetProxy(staticCallProxy.address).awaitTransactionSuccessAsync(txDefaults); + // Forwarder + const forwarder = await ForwarderContract.deployFrom0xArtifactAsync( + artifacts.Forwarder, + provider, + txDefaults, + artifacts, + exchange.address, + encodeERC20AssetData(etherToken.address), + ); // Fake the above transactions so our nonce increases and we result with the same addresses // while AssetProxyOwner is disabled (TODO: @dekz remove) - const dummyTransactionCount = 8; - for (let index = 0; index < dummyTransactionCount; index++) { + const dummyTransactionCount = 7; + for (let index = 0; index <= dummyTransactionCount; index++) { await web3Wrapper.sendTransactionAsync({ to: txDefaults.from, from: txDefaults.from, value: new BigNumber(0) }); } @@ -238,7 +247,6 @@ export async function runMigrationsAsync( zrxProxy, zrxToken.address, ); - await erc20Proxy.addAuthorizedAddress(zrxVault.address).awaitTransactionSuccessAsync(txDefaults); // Note we use TestStakingContract as the deployed bytecode of a StakingContract // has the tokens hardcoded @@ -259,6 +267,8 @@ export async function runMigrationsAsync( stakingLogic.address, ); + await erc20Proxy.addAuthorizedAddress(zrxVault.address).awaitTransactionSuccessAsync(txDefaults); + // Reference the Proxy as the StakingContract for setup const stakingDel = await new TestStakingContract(stakingProxy.address, provider, txDefaults); await stakingProxy.addAuthorizedAddress(txDefaults.from).awaitTransactionSuccessAsync(txDefaults); @@ -271,16 +281,6 @@ export async function runMigrationsAsync( await stakingLogic.addAuthorizedAddress(txDefaults.from).awaitTransactionSuccessAsync(txDefaults); await stakingLogic.addExchangeAddress(exchange.address).awaitTransactionSuccessAsync(txDefaults); - // Forwarder - const forwarder = await ForwarderContract.deployFrom0xArtifactAsync( - artifacts.Forwarder, - provider, - txDefaults, - artifacts, - exchange.address, - encodeERC20AssetData(etherToken.address), - ); - const contractAddresses = { erc20Proxy: erc20Proxy.address, erc721Proxy: erc721Proxy.address, @@ -299,14 +299,13 @@ export async function runMigrationsAsync( multiAssetProxy: multiAssetProxy.address, staticCallProxy: staticCallProxy.address, devUtils: devUtils.address, - exchangeV2: constants.NULL_ADDRESS, + exchangeV2: exchange.address, zrxVault: zrxVault.address, staking: stakingLogic.address, stakingProxy: stakingProxy.address, uniswapBridge: constants.NULL_ADDRESS, eth2DaiBridge: constants.NULL_ADDRESS, }; - return contractAddresses; } diff --git a/packages/migrations/test/snapshot_addresses_test.ts b/packages/migrations/test/snapshot_addresses_test.ts new file mode 100644 index 0000000000..9d7a4e330e --- /dev/null +++ b/packages/migrations/test/snapshot_addresses_test.ts @@ -0,0 +1,24 @@ +import { ChainId, getContractAddressesForChainOrThrow } from '@0x/contract-addresses'; +import { devConstants, web3Factory } from '@0x/dev-utils'; +import * as chai from 'chai'; +import * as dirtyChai from 'dirty-chai'; +import 'mocha'; + +import { runMigrationsAsync } from '../src/migration'; + +chai.use(dirtyChai); + +const expect = chai.expect; + +describe('addresses', () => { + it('should contain the same addresses as contract-addresses', async () => { + const providerConfigs = { shouldUseInProcessGanache: true }; + const provider = web3Factory.getRpcProvider(providerConfigs); + const txDefaults = { + from: devConstants.TESTRPC_FIRST_ADDRESS, + }; + const migrationAddresses = await runMigrationsAsync(provider, txDefaults); + const expectedAddresses = getContractAddressesForChainOrThrow(ChainId.Ganache); + expect(migrationAddresses).to.include(expectedAddresses); + }); +}); diff --git a/packages/migrations/tsconfig.json b/packages/migrations/tsconfig.json index 56689eaa34..718e623c74 100644 --- a/packages/migrations/tsconfig.json +++ b/packages/migrations/tsconfig.json @@ -2,7 +2,7 @@ "extends": "../../tsconfig", "compilerOptions": { "outDir": "lib", - "rootDir": "src" + "rootDir": "." }, - "include": ["src/**/*"] + "include": ["src/**/*", "test/**/*"] }