From 6e1a549fcb5cc9257ee984a906db78dd2568042b Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Wed, 27 Jun 2018 15:13:57 -0700 Subject: Use ledger subprovider --- packages/migrations/package.json | 22 ++++-- packages/migrations/src/1.0.0/migration.ts | 2 +- .../migrations/src/2.0.0-beta-kovan/artifacts.ts | 13 --- .../migrations/src/2.0.0-beta-kovan/constants.ts | 14 ---- .../migrations/src/2.0.0-beta-kovan/migration.ts | 91 --------------------- .../migrations/src/2.0.0-beta-testnet/artifacts.ts | 13 +++ .../migrations/src/2.0.0-beta-testnet/migration.ts | 92 ++++++++++++++++++++++ packages/migrations/src/2.0.0/migration.ts | 2 +- packages/migrations/src/artifact_writer.ts | 26 ------ packages/migrations/src/globals.d.ts | 23 ++++++ packages/migrations/src/index.ts | 2 +- packages/migrations/src/migrate.ts | 35 +++++--- packages/migrations/src/types.ts | 36 +++++++++ packages/migrations/src/utils/artifact_writer.ts | 26 ++++++ packages/migrations/src/utils/constants.ts | 15 ++++ packages/migrations/src/utils/provider_factory.ts | 37 +++++++++ 16 files changed, 281 insertions(+), 168 deletions(-) delete mode 100644 packages/migrations/src/2.0.0-beta-kovan/artifacts.ts delete mode 100644 packages/migrations/src/2.0.0-beta-kovan/constants.ts delete mode 100644 packages/migrations/src/2.0.0-beta-kovan/migration.ts create mode 100644 packages/migrations/src/2.0.0-beta-testnet/artifacts.ts create mode 100644 packages/migrations/src/2.0.0-beta-testnet/migration.ts delete mode 100644 packages/migrations/src/artifact_writer.ts create mode 100644 packages/migrations/src/utils/artifact_writer.ts create mode 100644 packages/migrations/src/utils/constants.ts create mode 100644 packages/migrations/src/utils/provider_factory.ts (limited to 'packages') diff --git a/packages/migrations/package.json b/packages/migrations/package.json index 11212bc1a..7f5a789e4 100644 --- a/packages/migrations/package.json +++ b/packages/migrations/package.json @@ -17,23 +17,23 @@ "tslint --project . --exclude **/src/v2/contract_wrappers/**/* --exclude **/src/v1/contract_wrappers/**/*", "migrate:v1": "run-s build compile:v1 script:migrate:v1", "migrate:v2": "run-s build compile:v2 script:migrate:v2", - "migrate:v2-beta-kovan": "run-s build compile:v2-beta-kovan script:migrate:v2-beta-kovan", + "migrate:v2-beta-testnet": "run-s build compile:v2-beta-testnet script:migrate:v2-beta-testnet", "script:migrate:v1": "node ./lib/migrate.js --contracts-version 1.0.0", "script:migrate:v2": "node ./lib/migrate.js --contracts-version 2.0.0", - "script:migrate:v2-beta-kovan": "node ./lib/migrate.js --contracts-version 2.0.0-beta-kovan", + "script:migrate:v2-beta-testnet": "node ./lib/migrate.js --contracts-version 2.0.0-beta-testnet", "generate_contract_wrappers": "run-p generate_contract_wrappers:*", "generate_contract_wrappers:v1": "abi-gen --abis ${npm_package_config_abis_v1} --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/1.0.0/contract_wrappers --backend ethers", "generate_contract_wrappers:v2": "abi-gen --abis ${npm_package_config_abis_v2} --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/2.0.0/contract_wrappers --backend ethers", - "generate_contract_wrappers:v2-beta-kovan": - "abi-gen --abis ${npm_package_config_abis_v2BetaKovan} --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/2.0.0-beta-kovan/contract_wrappers --backend ethers", + "generate_contract_wrappers:v2-beta-testnet": + "abi-gen --abis ${npm_package_config_abis_v2BetaTestnet} --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/2.0.0-beta-testnet/contract_wrappers --backend ethers", "compile:v1": "sol-compiler --artifacts-dir artifacts/1.0.0 --contracts Exchange_v1,DummyERC20Token,ZRXToken,WETH9,TokenTransferProxy_v1,MultiSigWallet,MultiSigWalletWithTimeLock,MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress,TokenRegistry", "compile:v2": "sol-compiler --artifacts-dir artifacts/2.0.0 --contracts ERC20Token,DummyERC20Token,ERC721Token,DummyERC721Token,ERC20Proxy,ERC721Proxy,Exchange,MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress,ZRXToken,WETH9,IWallet,IValidator", - "compile:v2-beta-kovan": - "sol-compiler --artifacts-dir artifacts/2.0.0-beta-kovan --contracts AssetProxyOwner,ERC20Proxy,ERC721Proxy,Exchange" + "compile:v2-beta-testnet": + "sol-compiler --artifacts-dir artifacts/2.0.0-beta-testnet --contracts AssetProxyOwner,ERC20Proxy,ERC721Proxy,Exchange" }, "config": { "abis": { @@ -41,7 +41,7 @@ "artifacts/1.0.0/@(DummyERC20Token|TokenTransferProxy_v1|Exchange_v1|TokenRegistry|MultiSigWallet|MultiSigWalletWithTimeLock|MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress|TokenRegistry|ZRXToken|WETH9).json", "v2": "artifacts/2.0.0/@(ERC20Token|DummyERC20Token|ERC721Token|DummyERC721Token|ERC20Proxy|ERC721Proxy|Exchange|AssetProxyOwner|ZRXToken|WETH9|IWallet|IValidator).json", - "v2BetaKovan": "artifacts/2.0.0-beta-kovan/@(ERC20Proxy|ERC721Proxy|Exchange|AssetProxyOwner).json" + "v2BetaTestnet": "artifacts/2.0.0-beta-testnet/@(ERC20Proxy|ERC721Proxy|Exchange|AssetProxyOwner).json" } }, "license": "Apache-2.0", @@ -62,12 +62,18 @@ "@0xproject/base-contract": "^0.3.4", "@0xproject/order-utils": "^1.0.0", "@0xproject/sol-compiler": "^0.5.2", + "@0xproject/subproviders": "^0.10.4", "@0xproject/typescript-typings": "^0.4.1", "@0xproject/utils": "^0.7.1", "@0xproject/web3-wrapper": "^0.7.1", + "@ledgerhq/hw-app-eth": "^4.3.0", "ethereum-types": "^0.0.1", "ethers": "3.0.22", - "lodash": "^4.17.4" + "lodash": "^4.17.4", + "web3-provider-engine": "^14.0.4" + }, + "optionalDependencies": { + "@ledgerhq/hw-transport-node-hid": "^4.3.0" }, "publishConfig": { "access": "public" diff --git a/packages/migrations/src/1.0.0/migration.ts b/packages/migrations/src/1.0.0/migration.ts index 3398537e5..6cc8dc4ef 100644 --- a/packages/migrations/src/1.0.0/migration.ts +++ b/packages/migrations/src/1.0.0/migration.ts @@ -2,7 +2,7 @@ import { BigNumber, NULL_BYTES } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider, TxData } from 'ethereum-types'; -import { ArtifactWriter } from '../artifact_writer'; +import { ArtifactWriter } from '../utils/artifact_writer'; import { erc20TokenInfo } from '../utils/token_info'; import { artifacts } from './artifacts'; diff --git a/packages/migrations/src/2.0.0-beta-kovan/artifacts.ts b/packages/migrations/src/2.0.0-beta-kovan/artifacts.ts deleted file mode 100644 index 5e7bb2de7..000000000 --- a/packages/migrations/src/2.0.0-beta-kovan/artifacts.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { ContractArtifact } from '@0xproject/sol-compiler'; - -import * as AssetProxyOwner from '../../artifacts/2.0.0-beta-kovan/AssetProxyOwner.json'; -import * as ERC20Proxy from '../../artifacts/2.0.0-beta-kovan/ERC20Proxy.json'; -import * as ERC721Proxy from '../../artifacts/2.0.0-beta-kovan/ERC721Proxy.json'; -import * as Exchange from '../../artifacts/2.0.0-beta-kovan/Exchange.json'; - -export const artifacts = { - AssetProxyOwner: (AssetProxyOwner as any) as ContractArtifact, - Exchange: (Exchange as any) as ContractArtifact, - ERC20Proxy: (ERC20Proxy as any) as ContractArtifact, - ERC721Proxy: (ERC721Proxy as any) as ContractArtifact, -}; diff --git a/packages/migrations/src/2.0.0-beta-kovan/constants.ts b/packages/migrations/src/2.0.0-beta-kovan/constants.ts deleted file mode 100644 index e4fc8339d..000000000 --- a/packages/migrations/src/2.0.0-beta-kovan/constants.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; - -export const constants = { - ASSET_PROXY_OWNER_OWNERS: [ - '0x9df8137872ac09a8fee71d0da5c7539923fb9bf0', - '0xcf34d44db312d188789f43a63d11cf2bebb4da15', - '0x73fd50f2a6beac9cdac9fe87ef68a18edc415831', - ], - ASSET_PROXY_OWNER_TIMELOCK: new BigNumber(0), - ASSET_PROXY_OWNER_CONFIRMATIONS: new BigNumber(1), - ERC20_PROXY_ID: new BigNumber(0), - ERC721_PROXY_ID: new BigNumber(1), - NULL_ADDRESS: '0x0000000000000000000000000000000000000000', -}; diff --git a/packages/migrations/src/2.0.0-beta-kovan/migration.ts b/packages/migrations/src/2.0.0-beta-kovan/migration.ts deleted file mode 100644 index bc0cbe090..000000000 --- a/packages/migrations/src/2.0.0-beta-kovan/migration.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; -import { Provider, TxData } from 'ethereum-types'; - -import { ArtifactWriter } from '../artifact_writer'; - -import { artifacts } from './artifacts'; -import { constants } from './constants'; -import { AssetProxyOwnerContract } from './contract_wrappers/asset_proxy_owner'; -import { ERC20ProxyContract } from './contract_wrappers/e_r_c20_proxy'; -import { ERC721ProxyContract } from './contract_wrappers/e_r_c721_proxy'; -import { ExchangeContract } from './contract_wrappers/exchange'; - -/** - * Custom migrations should be defined in this function. This will be called with the CLI 'migrate:v2' command. - * Migrations could be written to run in parallel, but if you want contract addresses to be created deterministically, - * the migration should be written to run synchronously. - * @param provider Web3 provider instance. - * @param artifactsDir The directory with compiler artifact files. - * @param txDefaults Default transaction values to use when deploying contracts. - */ -export const runV2BetaKovanMigrationsAsync = async ( - provider: Provider, - artifactsDir: string, - txDefaults: Partial, -) => { - const web3Wrapper = new Web3Wrapper(provider); - const networkId = await web3Wrapper.getNetworkIdAsync(); - const artifactsWriter = new ArtifactWriter(artifactsDir, networkId); - - // Deploy AssetProxies - const erc20proxy = await ERC20ProxyContract.deployFrom0xArtifactAsync(artifacts.ERC20Proxy, provider, txDefaults); - artifactsWriter.saveArtifact(erc20proxy); - const erc721proxy = await ERC721ProxyContract.deployFrom0xArtifactAsync( - artifacts.ERC721Proxy, - provider, - txDefaults, - ); - artifactsWriter.saveArtifact(erc721proxy); - - // Deploy Exchange - const exchange = await ExchangeContract.deployFrom0xArtifactAsync(artifacts.Exchange, provider, txDefaults); - artifactsWriter.saveArtifact(exchange); - - // Register AssetProxies in Exchange - await web3Wrapper.awaitTransactionSuccessAsync( - await exchange.registerAssetProxy.sendTransactionAsync( - constants.ERC20_PROXY_ID, - erc20proxy.address, - constants.NULL_ADDRESS, - ), - ); - await web3Wrapper.awaitTransactionSuccessAsync( - await exchange.registerAssetProxy.sendTransactionAsync( - constants.ERC721_PROXY_ID, - erc721proxy.address, - constants.NULL_ADDRESS, - ), - ); - - // Deploy AssetProxyOwner - const assetProxyOwner = await AssetProxyOwnerContract.deployFrom0xArtifactAsync( - artifacts.AssetProxyOwner, - provider, - txDefaults, - constants.ASSET_PROXY_OWNER_OWNERS, - [erc20proxy.address, erc721proxy.address], - constants.ASSET_PROXY_OWNER_CONFIRMATIONS, - constants.ASSET_PROXY_OWNER_TIMELOCK, - ); - artifactsWriter.saveArtifact(assetProxyOwner); - - // Authorize Exchange contracts to call AssetProxies - await web3Wrapper.awaitTransactionSuccessAsync( - await erc20proxy.addAuthorizedAddress.sendTransactionAsync(exchange.address), - ); - await web3Wrapper.awaitTransactionSuccessAsync( - await erc721proxy.addAuthorizedAddress.sendTransactionAsync(exchange.address), - ); - - // Transfer ownership of AssetProxies and Exchange to AssetProxyOwner - await web3Wrapper.awaitTransactionSuccessAsync( - await erc20proxy.transferOwnership.sendTransactionAsync(assetProxyOwner.address), - ); - await web3Wrapper.awaitTransactionSuccessAsync( - await erc721proxy.transferOwnership.sendTransactionAsync(assetProxyOwner.address), - ); - await web3Wrapper.awaitTransactionSuccessAsync( - await exchange.transferOwnership.sendTransactionAsync(assetProxyOwner.address), - ); -}; diff --git a/packages/migrations/src/2.0.0-beta-testnet/artifacts.ts b/packages/migrations/src/2.0.0-beta-testnet/artifacts.ts new file mode 100644 index 000000000..c9a341eb3 --- /dev/null +++ b/packages/migrations/src/2.0.0-beta-testnet/artifacts.ts @@ -0,0 +1,13 @@ +import { ContractArtifact } from '@0xproject/sol-compiler'; + +import * as AssetProxyOwner from '../../artifacts/2.0.0-beta-testnet/AssetProxyOwner.json'; +import * as ERC20Proxy from '../../artifacts/2.0.0-beta-testnet/ERC20Proxy.json'; +import * as ERC721Proxy from '../../artifacts/2.0.0-beta-testnet/ERC721Proxy.json'; +import * as Exchange from '../../artifacts/2.0.0-beta-testnet/Exchange.json'; + +export const artifacts = { + AssetProxyOwner: (AssetProxyOwner as any) as ContractArtifact, + Exchange: (Exchange as any) as ContractArtifact, + ERC20Proxy: (ERC20Proxy as any) as ContractArtifact, + ERC721Proxy: (ERC721Proxy as any) as ContractArtifact, +}; diff --git a/packages/migrations/src/2.0.0-beta-testnet/migration.ts b/packages/migrations/src/2.0.0-beta-testnet/migration.ts new file mode 100644 index 000000000..df41fd401 --- /dev/null +++ b/packages/migrations/src/2.0.0-beta-testnet/migration.ts @@ -0,0 +1,92 @@ +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { Provider, TxData } from 'ethereum-types'; + +import { ArtifactWriter } from '../utils/artifact_writer'; + +import { constants } from '../utils/constants'; + +import { artifacts } from './artifacts'; +import { AssetProxyOwnerContract } from './contract_wrappers/asset_proxy_owner'; +import { ERC20ProxyContract } from './contract_wrappers/e_r_c20_proxy'; +import { ERC721ProxyContract } from './contract_wrappers/e_r_c721_proxy'; +import { ExchangeContract } from './contract_wrappers/exchange'; + +/** + * Custom migrations should be defined in this function. This will be called with the CLI 'migrate:v2-beta-testnet' command. + * Migrations could be written to run in parallel, but if you want contract addresses to be created deterministically, + * the migration should be written to run synchronously. + * @param provider Web3 provider instance. + * @param artifactsDir The directory with compiler artifact files. + * @param txDefaults Default transaction values to use when deploying contracts. + */ +export const runV2TestnetMigrationsAsync = async ( + provider: Provider, + artifactsDir: string, + txDefaults: Partial, +) => { + const web3Wrapper = new Web3Wrapper(provider); + const networkId = await web3Wrapper.getNetworkIdAsync(); + const artifactsWriter = new ArtifactWriter(artifactsDir, networkId); + + // Deploy AssetProxies + const erc20proxy = await ERC20ProxyContract.deployFrom0xArtifactAsync(artifacts.ERC20Proxy, provider, txDefaults); + artifactsWriter.saveArtifact(erc20proxy); + const erc721proxy = await ERC721ProxyContract.deployFrom0xArtifactAsync( + artifacts.ERC721Proxy, + provider, + txDefaults, + ); + artifactsWriter.saveArtifact(erc721proxy); + + // Deploy Exchange + const exchange = await ExchangeContract.deployFrom0xArtifactAsync(artifacts.Exchange, provider, txDefaults); + artifactsWriter.saveArtifact(exchange); + + // Register AssetProxies in Exchange + const oldAssetProxy = constants.NULL_ADDRESS; + await web3Wrapper.awaitTransactionSuccessAsync( + await exchange.registerAssetProxy.sendTransactionAsync( + constants.ERC20_PROXY_ID, + erc20proxy.address, + oldAssetProxy, + ), + ); + await web3Wrapper.awaitTransactionSuccessAsync( + await exchange.registerAssetProxy.sendTransactionAsync( + constants.ERC721_PROXY_ID, + erc721proxy.address, + oldAssetProxy, + ), + ); + + // Deploy AssetProxyOwner + const assetProxyOwner = await AssetProxyOwnerContract.deployFrom0xArtifactAsync( + artifacts.AssetProxyOwner, + provider, + txDefaults, + constants.ASSET_PROXY_OWNER_OWNERS, + [erc20proxy.address, erc721proxy.address], + constants.ASSET_PROXY_OWNER_CONFIRMATIONS, + constants.ASSET_PROXY_OWNER_TIMELOCK, + ); + artifactsWriter.saveArtifact(assetProxyOwner); + + // Authorize Exchange contracts to call AssetProxies + await web3Wrapper.awaitTransactionSuccessAsync( + await erc20proxy.addAuthorizedAddress.sendTransactionAsync(exchange.address), + ); + await web3Wrapper.awaitTransactionSuccessAsync( + await erc721proxy.addAuthorizedAddress.sendTransactionAsync(exchange.address), + ); + + // Transfer ownership of AssetProxies and Exchange to AssetProxyOwner + await web3Wrapper.awaitTransactionSuccessAsync( + await erc20proxy.transferOwnership.sendTransactionAsync(assetProxyOwner.address), + ); + await web3Wrapper.awaitTransactionSuccessAsync( + await erc721proxy.transferOwnership.sendTransactionAsync(assetProxyOwner.address), + ); + await web3Wrapper.awaitTransactionSuccessAsync( + await exchange.transferOwnership.sendTransactionAsync(assetProxyOwner.address), + ); +}; diff --git a/packages/migrations/src/2.0.0/migration.ts b/packages/migrations/src/2.0.0/migration.ts index 76a31e2a0..91a4cbd13 100644 --- a/packages/migrations/src/2.0.0/migration.ts +++ b/packages/migrations/src/2.0.0/migration.ts @@ -3,7 +3,7 @@ import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider, TxData } from 'ethereum-types'; -import { ArtifactWriter } from '../artifact_writer'; +import { ArtifactWriter } from '../utils/artifact_writer'; import { erc20TokenInfo, erc721TokenInfo } from '../utils/token_info'; import { artifacts } from './artifacts'; diff --git a/packages/migrations/src/artifact_writer.ts b/packages/migrations/src/artifact_writer.ts deleted file mode 100644 index 2da5a09dd..000000000 --- a/packages/migrations/src/artifact_writer.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { BaseContract } from '@0xproject/base-contract'; -import { ContractArtifact } from '@0xproject/sol-compiler'; -import * as fs from 'fs'; -import * as path from 'path'; - -export class ArtifactWriter { - private _artifactsDir: string; - private _networkId: number; - constructor(artifactsDir: string, networkId: number) { - this._artifactsDir = artifactsDir; - this._networkId = networkId; - } - // This updates the artifact file but does not update the `artifacts` module above. It will not - // contain the saved artifact changes. - public saveArtifact(contract: BaseContract): void { - const contractName = contract.contractName; - const artifactFile = path.join(this._artifactsDir, `${contractName}.json`); - const artifact: ContractArtifact = JSON.parse(fs.readFileSync(artifactFile).toString()); - artifact.networks[this._networkId] = { - address: contract.address, - links: {}, - constructorArgs: JSON.stringify(contract.constructorArgs), - }; - fs.writeFileSync(artifactFile, JSON.stringify(artifact, null, '\t')); - } -} diff --git a/packages/migrations/src/globals.d.ts b/packages/migrations/src/globals.d.ts index 94e63a32d..b11849b05 100644 --- a/packages/migrations/src/globals.d.ts +++ b/packages/migrations/src/globals.d.ts @@ -4,3 +4,26 @@ declare module '*.json' { export default json; /* tslint:enable */ } + +declare module '@ledgerhq/hw-app-eth' { + class Eth { + public transport: LedgerTransport; + constructor(transport: LedgerTransport); + public getAddress( + path: string, + boolDisplay?: boolean, + boolChaincode?: boolean, + ): Promise<{ publicKey: string; address: string; chainCode: string }>; + public signTransaction(path: string, rawTxHex: string): Promise; + public getAppConfiguration(): Promise<{ arbitraryDataEnabled: number; version: string }>; + public signPersonalMessage(path: string, messageHex: string): Promise; + } + export default Eth; +} + +declare module '@ledgerhq/hw-transport-node-hid' { + export default class TransportNodeHid implements LedgerTransport { + public static create(): Promise; + public close(): Promise; + } +} diff --git a/packages/migrations/src/index.ts b/packages/migrations/src/index.ts index bb4b1f1e2..188f566df 100644 --- a/packages/migrations/src/index.ts +++ b/packages/migrations/src/index.ts @@ -1,3 +1,3 @@ export { runV1MigrationsAsync } from './1.0.0/migration'; export { runV2MigrationsAsync } from './2.0.0/migration'; -export { runV2BetaKovanMigrationsAsync } from './2.0.0-beta-kovan/migration'; +export { runV2TestnetMigrationsAsync } from './2.0.0-beta-testnet/migration'; diff --git a/packages/migrations/src/migrate.ts b/packages/migrations/src/migrate.ts index 35e086382..d9cc1ae01 100644 --- a/packages/migrations/src/migrate.ts +++ b/packages/migrations/src/migrate.ts @@ -6,13 +6,15 @@ import { Provider } from 'ethereum-types'; import * as yargs from 'yargs'; import { runV1MigrationsAsync } from './1.0.0/migration'; -import { runV2BetaKovanMigrationsAsync } from './2.0.0-beta-kovan/migration'; +import { runV2TestnetMigrationsAsync } from './2.0.0-beta-testnet/migration'; import { runV2MigrationsAsync } from './2.0.0/migration'; +import { providerFactory } from './utils/provider_factory'; + enum ContractVersions { V1 = '1.0.0', V2 = '2.0.0', - V2BetaKovan = '2.0.0-beta-kovan', + V2Testnet = '2.0.0-beta-testnet', } const args = yargs.argv; @@ -24,17 +26,24 @@ const args = yargs.argv; }; const contractsVersion = args.contractsVersion; const artifactsDir = `artifacts/${contractsVersion}`; - if (contractsVersion === ContractVersions.V1) { - await runV1MigrationsAsync(provider, artifactsDir, txDefaults); - } else if (contractsVersion === ContractVersions.V2) { - await runV2MigrationsAsync(provider, artifactsDir, txDefaults); - } else { - const web3Wrapper = new Web3Wrapper(provider); - const accounts = await web3Wrapper.getAvailableAddressesAsync(); - const kovanTxDefaults = { - from: accounts[0], - }; - await runV2BetaKovanMigrationsAsync(provider, artifactsDir, kovanTxDefaults); + switch (contractsVersion) { + case ContractVersions.V1: + await runV1MigrationsAsync(provider, artifactsDir, txDefaults); + break; + case ContractVersions.V2: + await runV2MigrationsAsync(provider, artifactsDir, txDefaults); + break; + case ContractVersions.V2Testnet: + const ledgerProvider = await providerFactory.getLedgerProviderAsync(); + const web3Wrapper = new Web3Wrapper(ledgerProvider); + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + const testnetTxDefaults = { + from: accounts[0], + }; + await runV2TestnetMigrationsAsync(ledgerProvider, artifactsDir, testnetTxDefaults); + break; + default: + throw new Error(`Unsupported contract version: ${contractsVersion}`); } process.exit(0); })().catch(err => { diff --git a/packages/migrations/src/types.ts b/packages/migrations/src/types.ts index 65f685797..7347f27fe 100644 --- a/packages/migrations/src/types.ts +++ b/packages/migrations/src/types.ts @@ -1,3 +1,4 @@ +import { ECSignature } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; export interface ERC20Token { @@ -27,3 +28,38 @@ export enum ContractName { EtherDelta = 'EtherDelta', Arbitrage = 'Arbitrage', } + +export interface LedgerCommunicationClient { + close: () => Promise; +} + +export interface LedgerGetAddressResult { + address: string; + publicKey: string; + chainCode: string; +} + +export interface ECSignatureString { + v: string; + r: string; + s: string; +} + +export interface LedgerGetAddressResult { + address: string; + publicKey: string; + chainCode: string; +} + +export interface LedgerEthereumClient { + // shouldGetChainCode is defined as `true` instead of `boolean` because other types rely on the assumption + // that we get back the chain code and we don't have dependent types to express it properly + getAddress: ( + derivationPath: string, + askForDeviceConfirmation: boolean, + shouldGetChainCode: true, + ) => Promise; + signTransaction: (derivationPath: string, rawTxHex: string) => Promise; + signPersonalMessage: (derivationPath: string, messageHex: string) => Promise; + transport: LedgerCommunicationClient; +} diff --git a/packages/migrations/src/utils/artifact_writer.ts b/packages/migrations/src/utils/artifact_writer.ts new file mode 100644 index 000000000..2da5a09dd --- /dev/null +++ b/packages/migrations/src/utils/artifact_writer.ts @@ -0,0 +1,26 @@ +import { BaseContract } from '@0xproject/base-contract'; +import { ContractArtifact } from '@0xproject/sol-compiler'; +import * as fs from 'fs'; +import * as path from 'path'; + +export class ArtifactWriter { + private _artifactsDir: string; + private _networkId: number; + constructor(artifactsDir: string, networkId: number) { + this._artifactsDir = artifactsDir; + this._networkId = networkId; + } + // This updates the artifact file but does not update the `artifacts` module above. It will not + // contain the saved artifact changes. + public saveArtifact(contract: BaseContract): void { + const contractName = contract.contractName; + const artifactFile = path.join(this._artifactsDir, `${contractName}.json`); + const artifact: ContractArtifact = JSON.parse(fs.readFileSync(artifactFile).toString()); + artifact.networks[this._networkId] = { + address: contract.address, + links: {}, + constructorArgs: JSON.stringify(contract.constructorArgs), + }; + fs.writeFileSync(artifactFile, JSON.stringify(artifact, null, '\t')); + } +} diff --git a/packages/migrations/src/utils/constants.ts b/packages/migrations/src/utils/constants.ts new file mode 100644 index 000000000..21887ab16 --- /dev/null +++ b/packages/migrations/src/utils/constants.ts @@ -0,0 +1,15 @@ +import { BigNumber } from '@0xproject/utils'; + +export const constants = { + ASSET_PROXY_OWNER_OWNERS: [ + '0x9df8137872ac09a8fee71d0da5c7539923fb9bf0', + '0xcf34d44db312d188789f43a63d11cf2bebb4da15', + '0x73fd50f2a6beac9cdac9fe87ef68a18edc415831', + ], + ASSET_PROXY_OWNER_TIMELOCK: new BigNumber(0), + ASSET_PROXY_OWNER_CONFIRMATIONS: new BigNumber(1), + ERC20_PROXY_ID: '0xf47261b0', + ERC721_PROXY_ID: '0x08e937fa', + NULL_ADDRESS: '0x0000000000000000000000000000000000000000', + RPC_URL: 'http://localhost:8545', +}; diff --git a/packages/migrations/src/utils/provider_factory.ts b/packages/migrations/src/utils/provider_factory.ts new file mode 100644 index 000000000..14b1be982 --- /dev/null +++ b/packages/migrations/src/utils/provider_factory.ts @@ -0,0 +1,37 @@ +import { LedgerSubprovider } from '@0xproject/subproviders'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import Eth from '@ledgerhq/hw-app-eth'; +import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'; +import { Provider } from 'ethereum-types'; +import ProviderEngine = require('web3-provider-engine'); +import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); + +import { LedgerEthereumClient } from '../types'; + +import { constants } from './constants'; + +async function ledgerEthereumNodeJsClientFactoryAsync(): Promise { + const ledgerConnection = await TransportNodeHid.create(); + const ledgerEthClient = new Eth(ledgerConnection); + return ledgerEthClient; +} +export const providerFactory = { + async getLedgerProviderAsync(): Promise { + const provider = new ProviderEngine(); + provider.addProvider( + new RpcSubprovider({ + rpcUrl: constants.RPC_URL, + }), + ); + const web3Wrapper = new Web3Wrapper(provider); + const networkId = await web3Wrapper.getNetworkIdAsync(); + const ledgerWalletConfigs = { + networkId, + ledgerEthereumClientFactoryAsync: ledgerEthereumNodeJsClientFactoryAsync, + }; + const ledgerSubprovider = new LedgerSubprovider(ledgerWalletConfigs); + provider.addProvider(ledgerSubprovider); + provider.start(); + return provider; + }, +}; -- cgit