From b20f34adb7726c4389012c2373bacc7e1b05b620 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 18 Jan 2018 15:12:56 +0100 Subject: Change file layout --- packages/deployer/src/cli.ts | 4 +- packages/deployer/src/commands.ts | 19 ++ packages/deployer/src/compiler.ts | 251 +++++++++++++++++++++ packages/deployer/src/deployer.ts | 180 +++++++++++++++ .../deployer/src/migrations/config/token_info.ts | 2 +- packages/deployer/src/migrations/migrate.ts | 4 +- packages/deployer/src/src/commands.ts | 19 -- packages/deployer/src/src/compiler.ts | 251 --------------------- packages/deployer/src/src/deployer.ts | 180 --------------- packages/deployer/src/src/utils/constants.ts | 3 - packages/deployer/src/src/utils/contract.ts | 81 ------- packages/deployer/src/src/utils/encoder.ts | 20 -- packages/deployer/src/src/utils/fs_wrapper.ts | 11 - packages/deployer/src/src/utils/types.ts | 97 -------- packages/deployer/src/src/utils/utils.ts | 13 -- packages/deployer/src/test/deploy_test.ts | 8 +- packages/deployer/src/utils/constants.ts | 3 + packages/deployer/src/utils/contract.ts | 81 +++++++ packages/deployer/src/utils/encoder.ts | 20 ++ packages/deployer/src/utils/fs_wrapper.ts | 11 + packages/deployer/src/utils/types.ts | 97 ++++++++ packages/deployer/src/utils/utils.ts | 13 ++ 22 files changed, 684 insertions(+), 684 deletions(-) create mode 100644 packages/deployer/src/commands.ts create mode 100644 packages/deployer/src/compiler.ts create mode 100644 packages/deployer/src/deployer.ts delete mode 100644 packages/deployer/src/src/commands.ts delete mode 100644 packages/deployer/src/src/compiler.ts delete mode 100644 packages/deployer/src/src/deployer.ts delete mode 100644 packages/deployer/src/src/utils/constants.ts delete mode 100644 packages/deployer/src/src/utils/contract.ts delete mode 100644 packages/deployer/src/src/utils/encoder.ts delete mode 100644 packages/deployer/src/src/utils/fs_wrapper.ts delete mode 100644 packages/deployer/src/src/utils/types.ts delete mode 100644 packages/deployer/src/src/utils/utils.ts create mode 100644 packages/deployer/src/utils/constants.ts create mode 100644 packages/deployer/src/utils/contract.ts create mode 100644 packages/deployer/src/utils/encoder.ts create mode 100644 packages/deployer/src/utils/fs_wrapper.ts create mode 100644 packages/deployer/src/utils/types.ts create mode 100644 packages/deployer/src/utils/utils.ts (limited to 'packages/deployer') diff --git a/packages/deployer/src/cli.ts b/packages/deployer/src/cli.ts index 53ae3bcfa..decb37fdc 100644 --- a/packages/deployer/src/cli.ts +++ b/packages/deployer/src/cli.ts @@ -5,8 +5,8 @@ import * as path from 'path'; import * as Web3 from 'web3'; import * as yargs from 'yargs'; -import { commands } from './src/commands'; -import { CliOptions, CompilerOptions, DeployerOptions } from './src/utils/types'; +import { commands } from './commands'; +import { CliOptions, CompilerOptions, DeployerOptions } from './utils/types'; const DEFAULT_OPTIMIZER_ENABLED = false; const DEFAULT_CONTRACTS_DIR = path.resolve('contracts'); diff --git a/packages/deployer/src/commands.ts b/packages/deployer/src/commands.ts new file mode 100644 index 000000000..2acef8e8f --- /dev/null +++ b/packages/deployer/src/commands.ts @@ -0,0 +1,19 @@ +import { migrator } from './migrations/migrate'; +import { Compiler } from './compiler'; +import { Deployer } from './deployer'; +import { CompilerOptions, DeployerOptions } from './utils/types'; + +export const commands = { + async compileAsync(opts: CompilerOptions): Promise { + const compiler = new Compiler(opts); + await compiler.compileAllAsync(); + }, + async migrateAsync(opts: DeployerOptions): Promise { + const deployer = new Deployer(opts); + await migrator.runMigrationsAsync(deployer); + }, + async deployAsync(contractName: string, args: any[], opts: DeployerOptions): Promise { + const deployer = new Deployer(opts); + await deployer.deployAndSaveAsync(contractName, args); + }, +}; diff --git a/packages/deployer/src/compiler.ts b/packages/deployer/src/compiler.ts new file mode 100644 index 000000000..63db6c865 --- /dev/null +++ b/packages/deployer/src/compiler.ts @@ -0,0 +1,251 @@ +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; +import * as path from 'path'; +import solc = require('solc'); +import * as Web3 from 'web3'; + +import { binPaths } from './solc/bin_paths'; +import { fsWrapper } from './utils/fs_wrapper'; +import { + CompilerOptions, + ContractArtifact, + ContractData, + ContractNetworks, + ContractSources, + ImportContents, +} from './utils/types'; +import { utils } from './utils/utils'; + +const SOLIDITY_FILE_EXTENSION = '.sol'; + +export class Compiler { + private _contractsDir: string; + private _networkId: number; + private _optimizerEnabled: number; + private _artifactsDir: string; + private _contractSourcesIfExists?: ContractSources; + private _solcErrors: Set; + /** + * Recursively retrieves Solidity source code from directory. + * @param dirPath Directory to search. + * @return Mapping of contract name to contract source. + */ + private static async _getContractSourcesAsync(dirPath: string): Promise { + let dirContents: string[] = []; + try { + dirContents = await fsWrapper.readdirAsync(dirPath); + } catch (err) { + throw new Error(`No directory found at ${dirPath}`); + } + let sources: ContractSources = {}; + for (const name of dirContents) { + const contentPath = `${dirPath}/${name}`; + if (path.extname(name) === SOLIDITY_FILE_EXTENSION) { + try { + const opts = { + encoding: 'utf8', + }; + sources[name] = await fsWrapper.readFileAsync(contentPath, opts); + utils.consoleLog(`Reading ${name} source...`); + } catch (err) { + utils.consoleLog(`Could not find file at ${contentPath}`); + } + } else { + try { + const nestedSources = await Compiler._getContractSourcesAsync(contentPath); + sources = { + ...sources, + ...nestedSources, + }; + } catch (err) { + utils.consoleLog(`${contentPath} is not a directory or ${SOLIDITY_FILE_EXTENSION} file`); + } + } + } + return sources; + } + /** + * Searches Solidity source code for compiler version. + * @param source Source code of contract. + * @return Solc compiler version. + */ + private static _parseSolidityVersion(source: string): string { + const solcVersionMatch = source.match(/(?:solidity\s\^?)([0-9]{1,2}[.][0-9]{1,2}[.][0-9]{1,2})/); + if (_.isNull(solcVersionMatch)) { + throw new Error('Could not find Solidity version in source'); + } + const solcVersion = solcVersionMatch[1]; + return solcVersion; + } + /** + * Normalizes the path found in the error message. + * Example: converts 'base/Token.sol:6:46: Warning: Unused local variable' + * to 'Token.sol:6:46: Warning: Unused local variable' + * This is used to prevent logging the same error multiple times. + * @param errMsg An error message from the compiled output. + * @return The error message with directories truncated from the contract path. + */ + private static _getNormalizedErrMsg(errMsg: string): string { + const errPathMatch = errMsg.match(/(.*\.sol)/); + if (_.isNull(errPathMatch)) { + throw new Error('Could not find a path in error message'); + } + const errPath = errPathMatch[0]; + const baseContract = path.basename(errPath); + const normalizedErrMsg = errMsg.replace(errPath, baseContract); + return normalizedErrMsg; + } + /** + * Instantiates a new instance of the Compiler class. + * @param opts Options specifying directories, network, and optimization settings. + * @return An instance of the Compiler class. + */ + constructor(opts: CompilerOptions) { + this._contractsDir = opts.contractsDir; + this._networkId = opts.networkId; + this._optimizerEnabled = opts.optimizerEnabled; + this._artifactsDir = opts.artifactsDir; + this._solcErrors = new Set(); + } + /** + * Compiles all Solidity files found in contractsDir and writes JSON artifacts to artifactsDir. + */ + public async compileAllAsync(): Promise { + await this._createArtifactsDirIfDoesNotExistAsync(); + this._contractSourcesIfExists = await Compiler._getContractSourcesAsync(this._contractsDir); + const contractBaseNames = _.keys(this._contractSourcesIfExists); + const compiledContractPromises = _.map(contractBaseNames, async (contractBaseName: string): Promise => { + return this._compileContractAsync(contractBaseName); + }); + await Promise.all(compiledContractPromises); + + this._solcErrors.forEach(errMsg => { + utils.consoleLog(errMsg); + }); + } + /** + * Compiles contract and saves artifact to artifactsDir. + * @param contractBaseName Name of contract with '.sol' extension. + */ + private async _compileContractAsync(contractBaseName: string): Promise { + if (_.isUndefined(this._contractSourcesIfExists)) { + throw new Error('Contract sources not yet initialized'); + } + + const source = this._contractSourcesIfExists[contractBaseName]; + const contractName = path.basename(contractBaseName, SOLIDITY_FILE_EXTENSION); + const currentArtifactPath = `${this._artifactsDir}/${contractName}.json`; + const sourceHash = `0x${ethUtil.sha3(source).toString('hex')}`; + + let currentArtifactString: string; + let currentArtifact: ContractArtifact; + let oldNetworks: ContractNetworks; + let shouldCompile: boolean; + try { + const opts = { + encoding: 'utf8', + }; + currentArtifactString = await fsWrapper.readFileAsync(currentArtifactPath, opts); + currentArtifact = JSON.parse(currentArtifactString); + oldNetworks = currentArtifact.networks; + const oldNetwork: ContractData = oldNetworks[this._networkId]; + shouldCompile = + _.isUndefined(oldNetwork) || + oldNetwork.keccak256 !== sourceHash || + oldNetwork.optimizer_enabled !== this._optimizerEnabled; + } catch (err) { + shouldCompile = true; + } + + if (!shouldCompile) { + return; + } + + const input = { + [contractBaseName]: source, + }; + const solcVersion = Compiler._parseSolidityVersion(source); + const fullSolcVersion = binPaths[solcVersion]; + const solcBinPath = `./solc/solc_bin/${fullSolcVersion}`; + const solcBin = require(solcBinPath); + const solcInstance = solc.setupMethods(solcBin); + + utils.consoleLog(`Compiling ${contractBaseName}...`); + const sourcesToCompile = { + sources: input, + }; + const compiled = solcInstance.compile( + sourcesToCompile, + this._optimizerEnabled, + this._findImportsIfSourcesExist.bind(this), + ); + + if (!_.isUndefined(compiled.errors)) { + _.each(compiled.errors, errMsg => { + const normalizedErrMsg = Compiler._getNormalizedErrMsg(errMsg); + this._solcErrors.add(normalizedErrMsg); + }); + } + + const contractIdentifier = `${contractBaseName}:${contractName}`; + const abi: Web3.ContractAbi = JSON.parse(compiled.contracts[contractIdentifier].interface); + const unlinked_binary = `0x${compiled.contracts[contractIdentifier].bytecode}`; + const updated_at = Date.now(); + const contractData: ContractData = { + solc_version: solcVersion, + keccak256: sourceHash, + optimizer_enabled: this._optimizerEnabled, + abi, + unlinked_binary, + updated_at, + }; + + let newArtifact: ContractArtifact; + if (!_.isUndefined(currentArtifactString)) { + newArtifact = { + ...currentArtifact, + networks: { + ...oldNetworks, + [this._networkId]: contractData, + }, + }; + } else { + newArtifact = { + contract_name: contractName, + networks: { + [this._networkId]: contractData, + }, + }; + } + + const artifactString = utils.stringifyWithFormatting(newArtifact); + await fsWrapper.writeFileAsync(currentArtifactPath, artifactString); + utils.consoleLog(`${contractBaseName} artifact saved!`); + } + /** + * Callback to resolve dependencies with `solc.compile`. + * Throws error if contractSources not yet initialized. + * @param importPath Path to an imported dependency. + * @return Import contents object containing source code of dependency. + */ + private _findImportsIfSourcesExist(importPath: string): ImportContents { + if (_.isUndefined(this._contractSourcesIfExists)) { + throw new Error('Contract sources not yet initialized'); + } + const contractBaseName = path.basename(importPath); + const source = this._contractSourcesIfExists[contractBaseName]; + const importContents: ImportContents = { + contents: source, + }; + return importContents; + } + /** + * Creates the artifacts directory if it does not already exist. + */ + private async _createArtifactsDirIfDoesNotExistAsync(): Promise { + if (!fsWrapper.doesPathExistSync(this._artifactsDir)) { + utils.consoleLog('Creating artifacts directory...'); + await fsWrapper.mkdirAsync(this._artifactsDir); + } + } +} diff --git a/packages/deployer/src/deployer.ts b/packages/deployer/src/deployer.ts new file mode 100644 index 000000000..6f03581e8 --- /dev/null +++ b/packages/deployer/src/deployer.ts @@ -0,0 +1,180 @@ +import { TxData } from '@0xproject/types'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as _ from 'lodash'; +import * as Web3 from 'web3'; + +import { Contract } from './utils/contract'; +import { encoder } from './utils/encoder'; +import { fsWrapper } from './utils/fs_wrapper'; +import { ContractArtifact, ContractData, DeployerOptions } from './utils/types'; +import { utils } from './utils/utils'; + +// Gas added to gas estimate to make sure there is sufficient gas for deployment. +const EXTRA_GAS = 200000; + +export class Deployer { + public web3Wrapper: Web3Wrapper; + private _artifactsDir: string; + private _jsonrpcPort: number; + private _networkId: number; + private _defaults: Partial; + + constructor(opts: DeployerOptions) { + this._artifactsDir = opts.artifactsDir; + this._jsonrpcPort = opts.jsonrpcPort; + this._networkId = opts.networkId; + const jsonrpcUrl = `http://localhost:${this._jsonrpcPort}`; + const web3Provider = new Web3.providers.HttpProvider(jsonrpcUrl); + this._defaults = opts.defaults; + this.web3Wrapper = new Web3Wrapper(web3Provider, this._defaults); + } + /** + * Loads contract artifact and deploys contract with given arguments. + * @param contractName Name of the contract to deploy. Must match name of an artifact in artifacts directory. + * @param args Array of contract constructor arguments. + * @return Deployed contract instance. + */ + public async deployAsync(contractName: string, args: any[] = []): Promise { + const contractArtifact: ContractArtifact = this._loadContractArtifactIfExists(contractName); + const contractData: ContractData = this._getContractDataFromArtifactIfExists(contractArtifact); + const data = contractData.unlinked_binary; + const from = await this._getFromAddressAsync(); + const gas = await this._getAllowableGasEstimateAsync(data); + const txData = { + gasPrice: this._defaults.gasPrice, + from, + data, + gas, + }; + const abi = contractData.abi; + const web3ContractInstance = await this._deployFromAbiAsync(abi, args, txData); + utils.consoleLog(`${contractName}.sol successfully deployed at ${web3ContractInstance.address}`); + const contractInstance = new Contract(web3ContractInstance, this._defaults); + return contractInstance; + } + /** + * Loads contract artifact, deploys with given arguments, and saves updated data to artifact. + * @param contractName Name of the contract to deploy. Must match name of an artifact in artifacts directory. + * @param args Array of contract constructor arguments. + * @return Deployed contract instance. + */ + public async deployAndSaveAsync(contractName: string, args: any[] = []): Promise { + const contractInstance = await this.deployAsync(contractName, args); + await this._saveContractDataToArtifactAsync(contractName, contractInstance.address, args); + return contractInstance; + } + /** + * Deploys a contract given its ABI, arguments, and transaction data. + * @param abi ABI of contract to deploy. + * @param args Constructor arguments to use in deployment. + * @param txData Tx options used for deployment. + * @return Promise that resolves to a web3 contract instance. + */ + private async _deployFromAbiAsync(abi: Web3.ContractAbi, args: any[], txData: Web3.TxData): Promise { + const contract: Web3.Contract = this.web3Wrapper.getContractFromAbi(abi); + const deployPromise = new Promise((resolve, reject) => { + /** + * Contract is inferred as 'any' because TypeScript + * is not able to read 'new' from the Contract interface + */ + (contract as any).new(...args, txData, (err: Error, res: any): any => { + if (err) { + reject(err); + } else if (_.isUndefined(res.address) && !_.isUndefined(res.transactionHash)) { + utils.consoleLog(`transactionHash: ${res.transactionHash}`); + } else { + resolve(res); + } + }); + }); + return deployPromise; + } + /** + * Updates a contract artifact's address and encoded constructor arguments. + * @param contractName Name of contract. Must match an existing artifact. + * @param contractAddress Contract address to save to artifact. + * @param args Contract constructor arguments that will be encoded and saved to artifact. + */ + private async _saveContractDataToArtifactAsync( + contractName: string, + contractAddress: string, + args: any[], + ): Promise { + const contractArtifact: ContractArtifact = this._loadContractArtifactIfExists(contractName); + const contractData: ContractData = this._getContractDataFromArtifactIfExists(contractArtifact); + const abi = contractData.abi; + const encodedConstructorArgs = encoder.encodeConstructorArgsFromAbi(args, abi); + const newContractData = { + ...contractData, + address: contractAddress, + constructor_args: encodedConstructorArgs, + }; + const newArtifact = { + ...contractArtifact, + networks: { + ...contractArtifact.networks, + [this._networkId]: newContractData, + }, + }; + const artifactString = utils.stringifyWithFormatting(newArtifact); + const artifactPath = `${this._artifactsDir}/${contractName}.json`; + await fsWrapper.writeFileAsync(artifactPath, artifactString); + } + /** + * Loads a contract artifact, if it exists. + * @param contractName Name of the contract, without the extension. + * @return The contract artifact. + */ + private _loadContractArtifactIfExists(contractName: string): ContractArtifact { + const artifactPath = `${this._artifactsDir}/${contractName}.json`; + try { + const contractArtifact: ContractArtifact = require(artifactPath); + return contractArtifact; + } catch (err) { + throw new Error(`Artifact not found for contract: ${contractName}`); + } + } + /** + * Gets data for current networkId stored in artifact. + * @param contractArtifact The contract artifact. + * @return Network specific contract data. + */ + private _getContractDataFromArtifactIfExists(contractArtifact: ContractArtifact): ContractData { + const contractData = contractArtifact.networks[this._networkId]; + if (_.isUndefined(contractData)) { + throw new Error(`Data not found in artifact for contract: ${contractArtifact.contract_name}`); + } + return contractData; + } + /** + * Gets the address to use for sending a transaction. + * @return The default from address. If not specified, returns the first address accessible by web3. + */ + private async _getFromAddressAsync(): Promise { + let from: string; + if (_.isUndefined(this._defaults.from)) { + const accounts = await this.web3Wrapper.getAvailableAddressesAsync(); + from = accounts[0]; + } else { + from = this._defaults.from; + } + return from; + } + /** + * Estimates the gas required for a transaction. + * If gas would be over the block gas limit, the max allowable gas is returned instead. + * @param data Bytecode to estimate gas for. + * @return Gas estimate for transaction data. + */ + private async _getAllowableGasEstimateAsync(data: string): Promise { + const block = await this.web3Wrapper.getBlockAsync('latest'); + let gas: number; + try { + const gasEstimate: number = await this.web3Wrapper.estimateGasAsync(data); + gas = Math.min(gasEstimate + EXTRA_GAS, block.gasLimit); + } catch (err) { + gas = block.gasLimit; + } + return gas; + } +} diff --git a/packages/deployer/src/migrations/config/token_info.ts b/packages/deployer/src/migrations/config/token_info.ts index ec0ed3c0e..7e822fc3b 100644 --- a/packages/deployer/src/migrations/config/token_info.ts +++ b/packages/deployer/src/migrations/config/token_info.ts @@ -1,4 +1,4 @@ -import { constants } from '../../src/utils/constants'; +import { constants } from '../../utils/constants'; import { Token } from '../../types'; export const tokenInfo: Token[] = [ diff --git a/packages/deployer/src/migrations/migrate.ts b/packages/deployer/src/migrations/migrate.ts index 3efd1f9a6..393806b45 100644 --- a/packages/deployer/src/migrations/migrate.ts +++ b/packages/deployer/src/migrations/migrate.ts @@ -2,8 +2,8 @@ import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; -import { Deployer } from './../src/deployer'; -import { constants } from './../src/utils/constants'; +import { Deployer } from '../deployer'; +import { constants } from '../utils/constants'; import { tokenInfo } from './config/token_info'; export const migrator = { diff --git a/packages/deployer/src/src/commands.ts b/packages/deployer/src/src/commands.ts deleted file mode 100644 index b87b9e632..000000000 --- a/packages/deployer/src/src/commands.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { migrator } from './../migrations/migrate'; -import { Compiler } from './compiler'; -import { Deployer } from './deployer'; -import { CompilerOptions, DeployerOptions } from './utils/types'; - -export const commands = { - async compileAsync(opts: CompilerOptions): Promise { - const compiler = new Compiler(opts); - await compiler.compileAllAsync(); - }, - async migrateAsync(opts: DeployerOptions): Promise { - const deployer = new Deployer(opts); - await migrator.runMigrationsAsync(deployer); - }, - async deployAsync(contractName: string, args: any[], opts: DeployerOptions): Promise { - const deployer = new Deployer(opts); - await deployer.deployAndSaveAsync(contractName, args); - }, -}; diff --git a/packages/deployer/src/src/compiler.ts b/packages/deployer/src/src/compiler.ts deleted file mode 100644 index dbcaed707..000000000 --- a/packages/deployer/src/src/compiler.ts +++ /dev/null @@ -1,251 +0,0 @@ -import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; -import * as path from 'path'; -import solc = require('solc'); -import * as Web3 from 'web3'; - -import { binPaths } from './../solc/bin_paths'; -import { fsWrapper } from './utils/fs_wrapper'; -import { - CompilerOptions, - ContractArtifact, - ContractData, - ContractNetworks, - ContractSources, - ImportContents, -} from './utils/types'; -import { utils } from './utils/utils'; - -const SOLIDITY_FILE_EXTENSION = '.sol'; - -export class Compiler { - private _contractsDir: string; - private _networkId: number; - private _optimizerEnabled: number; - private _artifactsDir: string; - private _contractSourcesIfExists?: ContractSources; - private _solcErrors: Set; - /** - * Recursively retrieves Solidity source code from directory. - * @param dirPath Directory to search. - * @return Mapping of contract name to contract source. - */ - private static async _getContractSourcesAsync(dirPath: string): Promise { - let dirContents: string[] = []; - try { - dirContents = await fsWrapper.readdirAsync(dirPath); - } catch (err) { - throw new Error(`No directory found at ${dirPath}`); - } - let sources: ContractSources = {}; - for (const name of dirContents) { - const contentPath = `${dirPath}/${name}`; - if (path.extname(name) === SOLIDITY_FILE_EXTENSION) { - try { - const opts = { - encoding: 'utf8', - }; - sources[name] = await fsWrapper.readFileAsync(contentPath, opts); - utils.consoleLog(`Reading ${name} source...`); - } catch (err) { - utils.consoleLog(`Could not find file at ${contentPath}`); - } - } else { - try { - const nestedSources = await Compiler._getContractSourcesAsync(contentPath); - sources = { - ...sources, - ...nestedSources, - }; - } catch (err) { - utils.consoleLog(`${contentPath} is not a directory or ${SOLIDITY_FILE_EXTENSION} file`); - } - } - } - return sources; - } - /** - * Searches Solidity source code for compiler version. - * @param source Source code of contract. - * @return Solc compiler version. - */ - private static _parseSolidityVersion(source: string): string { - const solcVersionMatch = source.match(/(?:solidity\s\^?)([0-9]{1,2}[.][0-9]{1,2}[.][0-9]{1,2})/); - if (_.isNull(solcVersionMatch)) { - throw new Error('Could not find Solidity version in source'); - } - const solcVersion = solcVersionMatch[1]; - return solcVersion; - } - /** - * Normalizes the path found in the error message. - * Example: converts 'base/Token.sol:6:46: Warning: Unused local variable' - * to 'Token.sol:6:46: Warning: Unused local variable' - * This is used to prevent logging the same error multiple times. - * @param errMsg An error message from the compiled output. - * @return The error message with directories truncated from the contract path. - */ - private static _getNormalizedErrMsg(errMsg: string): string { - const errPathMatch = errMsg.match(/(.*\.sol)/); - if (_.isNull(errPathMatch)) { - throw new Error('Could not find a path in error message'); - } - const errPath = errPathMatch[0]; - const baseContract = path.basename(errPath); - const normalizedErrMsg = errMsg.replace(errPath, baseContract); - return normalizedErrMsg; - } - /** - * Instantiates a new instance of the Compiler class. - * @param opts Options specifying directories, network, and optimization settings. - * @return An instance of the Compiler class. - */ - constructor(opts: CompilerOptions) { - this._contractsDir = opts.contractsDir; - this._networkId = opts.networkId; - this._optimizerEnabled = opts.optimizerEnabled; - this._artifactsDir = opts.artifactsDir; - this._solcErrors = new Set(); - } - /** - * Compiles all Solidity files found in contractsDir and writes JSON artifacts to artifactsDir. - */ - public async compileAllAsync(): Promise { - await this._createArtifactsDirIfDoesNotExistAsync(); - this._contractSourcesIfExists = await Compiler._getContractSourcesAsync(this._contractsDir); - const contractBaseNames = _.keys(this._contractSourcesIfExists); - const compiledContractPromises = _.map(contractBaseNames, async (contractBaseName: string): Promise => { - return this._compileContractAsync(contractBaseName); - }); - await Promise.all(compiledContractPromises); - - this._solcErrors.forEach(errMsg => { - utils.consoleLog(errMsg); - }); - } - /** - * Compiles contract and saves artifact to artifactsDir. - * @param contractBaseName Name of contract with '.sol' extension. - */ - private async _compileContractAsync(contractBaseName: string): Promise { - if (_.isUndefined(this._contractSourcesIfExists)) { - throw new Error('Contract sources not yet initialized'); - } - - const source = this._contractSourcesIfExists[contractBaseName]; - const contractName = path.basename(contractBaseName, SOLIDITY_FILE_EXTENSION); - const currentArtifactPath = `${this._artifactsDir}/${contractName}.json`; - const sourceHash = `0x${ethUtil.sha3(source).toString('hex')}`; - - let currentArtifactString: string; - let currentArtifact: ContractArtifact; - let oldNetworks: ContractNetworks; - let shouldCompile: boolean; - try { - const opts = { - encoding: 'utf8', - }; - currentArtifactString = await fsWrapper.readFileAsync(currentArtifactPath, opts); - currentArtifact = JSON.parse(currentArtifactString); - oldNetworks = currentArtifact.networks; - const oldNetwork: ContractData = oldNetworks[this._networkId]; - shouldCompile = - _.isUndefined(oldNetwork) || - oldNetwork.keccak256 !== sourceHash || - oldNetwork.optimizer_enabled !== this._optimizerEnabled; - } catch (err) { - shouldCompile = true; - } - - if (!shouldCompile) { - return; - } - - const input = { - [contractBaseName]: source, - }; - const solcVersion = Compiler._parseSolidityVersion(source); - const fullSolcVersion = binPaths[solcVersion]; - const solcBinPath = `../solc/solc_bin/${fullSolcVersion}`; - const solcBin = require(solcBinPath); - const solcInstance = solc.setupMethods(solcBin); - - utils.consoleLog(`Compiling ${contractBaseName}...`); - const sourcesToCompile = { - sources: input, - }; - const compiled = solcInstance.compile( - sourcesToCompile, - this._optimizerEnabled, - this._findImportsIfSourcesExist.bind(this), - ); - - if (!_.isUndefined(compiled.errors)) { - _.each(compiled.errors, errMsg => { - const normalizedErrMsg = Compiler._getNormalizedErrMsg(errMsg); - this._solcErrors.add(normalizedErrMsg); - }); - } - - const contractIdentifier = `${contractBaseName}:${contractName}`; - const abi: Web3.ContractAbi = JSON.parse(compiled.contracts[contractIdentifier].interface); - const unlinked_binary = `0x${compiled.contracts[contractIdentifier].bytecode}`; - const updated_at = Date.now(); - const contractData: ContractData = { - solc_version: solcVersion, - keccak256: sourceHash, - optimizer_enabled: this._optimizerEnabled, - abi, - unlinked_binary, - updated_at, - }; - - let newArtifact: ContractArtifact; - if (!_.isUndefined(currentArtifactString)) { - newArtifact = { - ...currentArtifact, - networks: { - ...oldNetworks, - [this._networkId]: contractData, - }, - }; - } else { - newArtifact = { - contract_name: contractName, - networks: { - [this._networkId]: contractData, - }, - }; - } - - const artifactString = utils.stringifyWithFormatting(newArtifact); - await fsWrapper.writeFileAsync(currentArtifactPath, artifactString); - utils.consoleLog(`${contractBaseName} artifact saved!`); - } - /** - * Callback to resolve dependencies with `solc.compile`. - * Throws error if contractSources not yet initialized. - * @param importPath Path to an imported dependency. - * @return Import contents object containing source code of dependency. - */ - private _findImportsIfSourcesExist(importPath: string): ImportContents { - if (_.isUndefined(this._contractSourcesIfExists)) { - throw new Error('Contract sources not yet initialized'); - } - const contractBaseName = path.basename(importPath); - const source = this._contractSourcesIfExists[contractBaseName]; - const importContents: ImportContents = { - contents: source, - }; - return importContents; - } - /** - * Creates the artifacts directory if it does not already exist. - */ - private async _createArtifactsDirIfDoesNotExistAsync(): Promise { - if (!fsWrapper.doesPathExistSync(this._artifactsDir)) { - utils.consoleLog('Creating artifacts directory...'); - await fsWrapper.mkdirAsync(this._artifactsDir); - } - } -} diff --git a/packages/deployer/src/src/deployer.ts b/packages/deployer/src/src/deployer.ts deleted file mode 100644 index 6f03581e8..000000000 --- a/packages/deployer/src/src/deployer.ts +++ /dev/null @@ -1,180 +0,0 @@ -import { TxData } from '@0xproject/types'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; -import * as _ from 'lodash'; -import * as Web3 from 'web3'; - -import { Contract } from './utils/contract'; -import { encoder } from './utils/encoder'; -import { fsWrapper } from './utils/fs_wrapper'; -import { ContractArtifact, ContractData, DeployerOptions } from './utils/types'; -import { utils } from './utils/utils'; - -// Gas added to gas estimate to make sure there is sufficient gas for deployment. -const EXTRA_GAS = 200000; - -export class Deployer { - public web3Wrapper: Web3Wrapper; - private _artifactsDir: string; - private _jsonrpcPort: number; - private _networkId: number; - private _defaults: Partial; - - constructor(opts: DeployerOptions) { - this._artifactsDir = opts.artifactsDir; - this._jsonrpcPort = opts.jsonrpcPort; - this._networkId = opts.networkId; - const jsonrpcUrl = `http://localhost:${this._jsonrpcPort}`; - const web3Provider = new Web3.providers.HttpProvider(jsonrpcUrl); - this._defaults = opts.defaults; - this.web3Wrapper = new Web3Wrapper(web3Provider, this._defaults); - } - /** - * Loads contract artifact and deploys contract with given arguments. - * @param contractName Name of the contract to deploy. Must match name of an artifact in artifacts directory. - * @param args Array of contract constructor arguments. - * @return Deployed contract instance. - */ - public async deployAsync(contractName: string, args: any[] = []): Promise { - const contractArtifact: ContractArtifact = this._loadContractArtifactIfExists(contractName); - const contractData: ContractData = this._getContractDataFromArtifactIfExists(contractArtifact); - const data = contractData.unlinked_binary; - const from = await this._getFromAddressAsync(); - const gas = await this._getAllowableGasEstimateAsync(data); - const txData = { - gasPrice: this._defaults.gasPrice, - from, - data, - gas, - }; - const abi = contractData.abi; - const web3ContractInstance = await this._deployFromAbiAsync(abi, args, txData); - utils.consoleLog(`${contractName}.sol successfully deployed at ${web3ContractInstance.address}`); - const contractInstance = new Contract(web3ContractInstance, this._defaults); - return contractInstance; - } - /** - * Loads contract artifact, deploys with given arguments, and saves updated data to artifact. - * @param contractName Name of the contract to deploy. Must match name of an artifact in artifacts directory. - * @param args Array of contract constructor arguments. - * @return Deployed contract instance. - */ - public async deployAndSaveAsync(contractName: string, args: any[] = []): Promise { - const contractInstance = await this.deployAsync(contractName, args); - await this._saveContractDataToArtifactAsync(contractName, contractInstance.address, args); - return contractInstance; - } - /** - * Deploys a contract given its ABI, arguments, and transaction data. - * @param abi ABI of contract to deploy. - * @param args Constructor arguments to use in deployment. - * @param txData Tx options used for deployment. - * @return Promise that resolves to a web3 contract instance. - */ - private async _deployFromAbiAsync(abi: Web3.ContractAbi, args: any[], txData: Web3.TxData): Promise { - const contract: Web3.Contract = this.web3Wrapper.getContractFromAbi(abi); - const deployPromise = new Promise((resolve, reject) => { - /** - * Contract is inferred as 'any' because TypeScript - * is not able to read 'new' from the Contract interface - */ - (contract as any).new(...args, txData, (err: Error, res: any): any => { - if (err) { - reject(err); - } else if (_.isUndefined(res.address) && !_.isUndefined(res.transactionHash)) { - utils.consoleLog(`transactionHash: ${res.transactionHash}`); - } else { - resolve(res); - } - }); - }); - return deployPromise; - } - /** - * Updates a contract artifact's address and encoded constructor arguments. - * @param contractName Name of contract. Must match an existing artifact. - * @param contractAddress Contract address to save to artifact. - * @param args Contract constructor arguments that will be encoded and saved to artifact. - */ - private async _saveContractDataToArtifactAsync( - contractName: string, - contractAddress: string, - args: any[], - ): Promise { - const contractArtifact: ContractArtifact = this._loadContractArtifactIfExists(contractName); - const contractData: ContractData = this._getContractDataFromArtifactIfExists(contractArtifact); - const abi = contractData.abi; - const encodedConstructorArgs = encoder.encodeConstructorArgsFromAbi(args, abi); - const newContractData = { - ...contractData, - address: contractAddress, - constructor_args: encodedConstructorArgs, - }; - const newArtifact = { - ...contractArtifact, - networks: { - ...contractArtifact.networks, - [this._networkId]: newContractData, - }, - }; - const artifactString = utils.stringifyWithFormatting(newArtifact); - const artifactPath = `${this._artifactsDir}/${contractName}.json`; - await fsWrapper.writeFileAsync(artifactPath, artifactString); - } - /** - * Loads a contract artifact, if it exists. - * @param contractName Name of the contract, without the extension. - * @return The contract artifact. - */ - private _loadContractArtifactIfExists(contractName: string): ContractArtifact { - const artifactPath = `${this._artifactsDir}/${contractName}.json`; - try { - const contractArtifact: ContractArtifact = require(artifactPath); - return contractArtifact; - } catch (err) { - throw new Error(`Artifact not found for contract: ${contractName}`); - } - } - /** - * Gets data for current networkId stored in artifact. - * @param contractArtifact The contract artifact. - * @return Network specific contract data. - */ - private _getContractDataFromArtifactIfExists(contractArtifact: ContractArtifact): ContractData { - const contractData = contractArtifact.networks[this._networkId]; - if (_.isUndefined(contractData)) { - throw new Error(`Data not found in artifact for contract: ${contractArtifact.contract_name}`); - } - return contractData; - } - /** - * Gets the address to use for sending a transaction. - * @return The default from address. If not specified, returns the first address accessible by web3. - */ - private async _getFromAddressAsync(): Promise { - let from: string; - if (_.isUndefined(this._defaults.from)) { - const accounts = await this.web3Wrapper.getAvailableAddressesAsync(); - from = accounts[0]; - } else { - from = this._defaults.from; - } - return from; - } - /** - * Estimates the gas required for a transaction. - * If gas would be over the block gas limit, the max allowable gas is returned instead. - * @param data Bytecode to estimate gas for. - * @return Gas estimate for transaction data. - */ - private async _getAllowableGasEstimateAsync(data: string): Promise { - const block = await this.web3Wrapper.getBlockAsync('latest'); - let gas: number; - try { - const gasEstimate: number = await this.web3Wrapper.estimateGasAsync(data); - gas = Math.min(gasEstimate + EXTRA_GAS, block.gasLimit); - } catch (err) { - gas = block.gasLimit; - } - return gas; - } -} diff --git a/packages/deployer/src/src/utils/constants.ts b/packages/deployer/src/src/utils/constants.ts deleted file mode 100644 index 8871a470d..000000000 --- a/packages/deployer/src/src/utils/constants.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const constants = { - NULL_BYTES: '0x', -}; diff --git a/packages/deployer/src/src/utils/contract.ts b/packages/deployer/src/src/utils/contract.ts deleted file mode 100644 index 546e82dfb..000000000 --- a/packages/deployer/src/src/utils/contract.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { schemas, SchemaValidator } from '@0xproject/json-schemas'; -import { promisify } from '@0xproject/utils'; -import * as _ from 'lodash'; -import * as Web3 from 'web3'; - -import { AbiType } from './types'; - -export class Contract implements Web3.ContractInstance { - public address: string; - public abi: Web3.ContractAbi; - private _contract: Web3.ContractInstance; - private _defaults: Partial; - private _validator: SchemaValidator; - // This class instance is going to be populated with functions and events depending on the ABI - // and we don't know their types in advance - [name: string]: any; - constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial) { - this._contract = web3ContractInstance; - this.address = web3ContractInstance.address; - this.abi = web3ContractInstance.abi; - this._defaults = defaults; - this._populateEvents(); - this._populateFunctions(); - this._validator = new SchemaValidator(); - } - private _populateFunctions(): void { - const functionsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Function) as Web3.FunctionAbi[]; - _.forEach(functionsAbi, (functionAbi: Web3.MethodAbi) => { - if (functionAbi.constant) { - const cbStyleCallFunction = this._contract[functionAbi.name].call; - this[functionAbi.name] = { - callAsync: promisify(cbStyleCallFunction, this._contract), - }; - } else { - const cbStyleFunction = this._contract[functionAbi.name]; - const cbStyleEstimateGasFunction = this._contract[functionAbi.name].estimateGas; - this[functionAbi.name] = { - estimateGasAsync: promisify(cbStyleEstimateGasFunction, this._contract), - sendTransactionAsync: this._promisifyWithDefaultParams(cbStyleFunction), - }; - } - }); - } - private _populateEvents(): void { - const eventsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Event) as Web3.EventAbi[]; - _.forEach(eventsAbi, (eventAbi: Web3.EventAbi) => { - this[eventAbi.name] = this._contract[eventAbi.name]; - }); - } - private _promisifyWithDefaultParams(fn: (...args: any[]) => void): (...args: any[]) => Promise { - const promisifiedWithDefaultParams = async (...args: any[]) => { - const promise = new Promise((resolve, reject) => { - const lastArg = args[args.length - 1]; - let txData: Partial = {}; - if (this._isTxData(lastArg)) { - txData = args.pop(); - } - txData = { - ...this._defaults, - ...txData, - }; - const callback = (err: Error, data: any) => { - if (_.isNull(err)) { - resolve(data); - } else { - reject(err); - } - }; - args.push(txData); - args.push(callback); - fn.apply(this._contract, args); - }); - return promise; - }; - return promisifiedWithDefaultParams; - } - private _isTxData(lastArg: any): boolean { - const isValid = this._validator.isValid(lastArg, schemas.txDataSchema); - return isValid; - } -} diff --git a/packages/deployer/src/src/utils/encoder.ts b/packages/deployer/src/src/utils/encoder.ts deleted file mode 100644 index d5f807774..000000000 --- a/packages/deployer/src/src/utils/encoder.ts +++ /dev/null @@ -1,20 +0,0 @@ -import * as _ from 'lodash'; -import * as Web3 from 'web3'; -import * as web3Abi from 'web3-eth-abi'; - -import { AbiType } from './types'; - -export const encoder = { - encodeConstructorArgsFromAbi(args: any[], abi: Web3.ContractAbi): string { - const constructorTypes: string[] = []; - _.each(abi, (element: Web3.AbiDefinition) => { - if (element.type === AbiType.Constructor) { - _.each(element.inputs, (input: Web3.FunctionParameter) => { - constructorTypes.push(input.type); - }); - } - }); - const encodedParameters = web3Abi.encodeParameters(constructorTypes, args); - return encodedParameters; - }, -}; diff --git a/packages/deployer/src/src/utils/fs_wrapper.ts b/packages/deployer/src/src/utils/fs_wrapper.ts deleted file mode 100644 index 34c7caa0e..000000000 --- a/packages/deployer/src/src/utils/fs_wrapper.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { promisify } from '@0xproject/utils'; -import * as fs from 'fs'; - -export const fsWrapper = { - readdirAsync: promisify(fs.readdir), - readFileAsync: promisify(fs.readFile), - writeFileAsync: promisify(fs.writeFile), - mkdirAsync: promisify(fs.mkdir), - doesPathExistSync: fs.existsSync, - removeFileAsync: promisify(fs.unlink), -}; diff --git a/packages/deployer/src/src/utils/types.ts b/packages/deployer/src/src/utils/types.ts deleted file mode 100644 index e054b9cc2..000000000 --- a/packages/deployer/src/src/utils/types.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { TxData } from '@0xproject/types'; -import * as Web3 from 'web3'; -import * as yargs from 'yargs'; - -export enum AbiType { - Function = 'function', - Constructor = 'constructor', - Event = 'event', - Fallback = 'fallback', -} - -export interface ContractArtifact { - contract_name: string; - networks: ContractNetworks; -} - -export interface ContractNetworks { - [key: number]: ContractData; -} - -export interface ContractData { - solc_version: string; - optimizer_enabled: number; - keccak256: string; - abi: Web3.ContractAbi; - unlinked_binary: string; - address?: string; - constructor_args?: string; - updated_at: number; -} - -export interface SolcErrors { - [key: string]: boolean; -} - -export interface CliOptions extends yargs.Arguments { - artifactsDir: string; - contractsDir: string; - jsonrpcPort: number; - networkId: number; - shouldOptimize: boolean; - gasPrice: string; - account?: string; - contract?: string; - args?: string; -} - -export interface CompilerOptions { - contractsDir: string; - networkId: number; - optimizerEnabled: number; - artifactsDir: string; -} - -export interface DeployerOptions { - artifactsDir: string; - jsonrpcPort: number; - networkId: number; - defaults: Partial; -} - -export interface ContractSources { - [key: string]: string; -} - -export interface ImportContents { - contents: string; -} - -// TODO: Consolidate with 0x.js definitions once types are moved into a separate package. -export enum ZeroExError { - ContractDoesNotExist = 'CONTRACT_DOES_NOT_EXIST', - ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', - UnhandledError = 'UNHANDLED_ERROR', - UserHasNoAssociatedAddress = 'USER_HAS_NO_ASSOCIATED_ADDRESSES', - InvalidSignature = 'INVALID_SIGNATURE', - ContractNotDeployedOnNetwork = 'CONTRACT_NOT_DEPLOYED_ON_NETWORK', - InsufficientAllowanceForTransfer = 'INSUFFICIENT_ALLOWANCE_FOR_TRANSFER', - InsufficientBalanceForTransfer = 'INSUFFICIENT_BALANCE_FOR_TRANSFER', - InsufficientEthBalanceForDeposit = 'INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT', - InsufficientWEthBalanceForWithdrawal = 'INSUFFICIENT_WETH_BALANCE_FOR_WITHDRAWAL', - InvalidJump = 'INVALID_JUMP', - OutOfGas = 'OUT_OF_GAS', - NoNetworkId = 'NO_NETWORK_ID', - SubscriptionNotFound = 'SUBSCRIPTION_NOT_FOUND', -} - -export interface Token { - address?: string; - name: string; - symbol: string; - decimals: number; - ipfsHash: string; - swarmHash: string; -} - -export type DoneCallback = (err?: Error) => void; diff --git a/packages/deployer/src/src/utils/utils.ts b/packages/deployer/src/src/utils/utils.ts deleted file mode 100644 index 4390d8813..000000000 --- a/packages/deployer/src/src/utils/utils.ts +++ /dev/null @@ -1,13 +0,0 @@ -export const utils = { - consoleLog(message: string): void { - /* tslint:disable */ - console.log(message); - /* tslint:enable */ - }, - stringifyWithFormatting(obj: any): string { - const jsonReplacer: null = null; - const numberOfJsonSpaces = 4; - const stringifiedObj = JSON.stringify(obj, jsonReplacer, numberOfJsonSpaces); - return stringifiedObj; - }, -}; diff --git a/packages/deployer/src/test/deploy_test.ts b/packages/deployer/src/test/deploy_test.ts index 51dbf0e5b..e15985e11 100644 --- a/packages/deployer/src/test/deploy_test.ts +++ b/packages/deployer/src/test/deploy_test.ts @@ -1,10 +1,10 @@ import * as chai from 'chai'; import 'mocha'; -import { Compiler } from './../src/compiler'; -import { Deployer } from './../src/deployer'; -import { fsWrapper } from './../src/utils/fs_wrapper'; -import { CompilerOptions, ContractArtifact, ContractData, DoneCallback } from './../src/utils/types'; +import { Compiler } from '../compiler'; +import { Deployer } from '../deployer'; +import { fsWrapper } from '../utils/fs_wrapper'; +import { CompilerOptions, ContractArtifact, ContractData, DoneCallback } from '../utils/types'; import { constructor_args, exchange_binary } from './fixtures/exchange_bin'; import { constants } from './util/constants'; diff --git a/packages/deployer/src/utils/constants.ts b/packages/deployer/src/utils/constants.ts new file mode 100644 index 000000000..8871a470d --- /dev/null +++ b/packages/deployer/src/utils/constants.ts @@ -0,0 +1,3 @@ +export const constants = { + NULL_BYTES: '0x', +}; diff --git a/packages/deployer/src/utils/contract.ts b/packages/deployer/src/utils/contract.ts new file mode 100644 index 000000000..546e82dfb --- /dev/null +++ b/packages/deployer/src/utils/contract.ts @@ -0,0 +1,81 @@ +import { schemas, SchemaValidator } from '@0xproject/json-schemas'; +import { promisify } from '@0xproject/utils'; +import * as _ from 'lodash'; +import * as Web3 from 'web3'; + +import { AbiType } from './types'; + +export class Contract implements Web3.ContractInstance { + public address: string; + public abi: Web3.ContractAbi; + private _contract: Web3.ContractInstance; + private _defaults: Partial; + private _validator: SchemaValidator; + // This class instance is going to be populated with functions and events depending on the ABI + // and we don't know their types in advance + [name: string]: any; + constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial) { + this._contract = web3ContractInstance; + this.address = web3ContractInstance.address; + this.abi = web3ContractInstance.abi; + this._defaults = defaults; + this._populateEvents(); + this._populateFunctions(); + this._validator = new SchemaValidator(); + } + private _populateFunctions(): void { + const functionsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Function) as Web3.FunctionAbi[]; + _.forEach(functionsAbi, (functionAbi: Web3.MethodAbi) => { + if (functionAbi.constant) { + const cbStyleCallFunction = this._contract[functionAbi.name].call; + this[functionAbi.name] = { + callAsync: promisify(cbStyleCallFunction, this._contract), + }; + } else { + const cbStyleFunction = this._contract[functionAbi.name]; + const cbStyleEstimateGasFunction = this._contract[functionAbi.name].estimateGas; + this[functionAbi.name] = { + estimateGasAsync: promisify(cbStyleEstimateGasFunction, this._contract), + sendTransactionAsync: this._promisifyWithDefaultParams(cbStyleFunction), + }; + } + }); + } + private _populateEvents(): void { + const eventsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Event) as Web3.EventAbi[]; + _.forEach(eventsAbi, (eventAbi: Web3.EventAbi) => { + this[eventAbi.name] = this._contract[eventAbi.name]; + }); + } + private _promisifyWithDefaultParams(fn: (...args: any[]) => void): (...args: any[]) => Promise { + const promisifiedWithDefaultParams = async (...args: any[]) => { + const promise = new Promise((resolve, reject) => { + const lastArg = args[args.length - 1]; + let txData: Partial = {}; + if (this._isTxData(lastArg)) { + txData = args.pop(); + } + txData = { + ...this._defaults, + ...txData, + }; + const callback = (err: Error, data: any) => { + if (_.isNull(err)) { + resolve(data); + } else { + reject(err); + } + }; + args.push(txData); + args.push(callback); + fn.apply(this._contract, args); + }); + return promise; + }; + return promisifiedWithDefaultParams; + } + private _isTxData(lastArg: any): boolean { + const isValid = this._validator.isValid(lastArg, schemas.txDataSchema); + return isValid; + } +} diff --git a/packages/deployer/src/utils/encoder.ts b/packages/deployer/src/utils/encoder.ts new file mode 100644 index 000000000..d5f807774 --- /dev/null +++ b/packages/deployer/src/utils/encoder.ts @@ -0,0 +1,20 @@ +import * as _ from 'lodash'; +import * as Web3 from 'web3'; +import * as web3Abi from 'web3-eth-abi'; + +import { AbiType } from './types'; + +export const encoder = { + encodeConstructorArgsFromAbi(args: any[], abi: Web3.ContractAbi): string { + const constructorTypes: string[] = []; + _.each(abi, (element: Web3.AbiDefinition) => { + if (element.type === AbiType.Constructor) { + _.each(element.inputs, (input: Web3.FunctionParameter) => { + constructorTypes.push(input.type); + }); + } + }); + const encodedParameters = web3Abi.encodeParameters(constructorTypes, args); + return encodedParameters; + }, +}; diff --git a/packages/deployer/src/utils/fs_wrapper.ts b/packages/deployer/src/utils/fs_wrapper.ts new file mode 100644 index 000000000..34c7caa0e --- /dev/null +++ b/packages/deployer/src/utils/fs_wrapper.ts @@ -0,0 +1,11 @@ +import { promisify } from '@0xproject/utils'; +import * as fs from 'fs'; + +export const fsWrapper = { + readdirAsync: promisify(fs.readdir), + readFileAsync: promisify(fs.readFile), + writeFileAsync: promisify(fs.writeFile), + mkdirAsync: promisify(fs.mkdir), + doesPathExistSync: fs.existsSync, + removeFileAsync: promisify(fs.unlink), +}; diff --git a/packages/deployer/src/utils/types.ts b/packages/deployer/src/utils/types.ts new file mode 100644 index 000000000..e054b9cc2 --- /dev/null +++ b/packages/deployer/src/utils/types.ts @@ -0,0 +1,97 @@ +import { TxData } from '@0xproject/types'; +import * as Web3 from 'web3'; +import * as yargs from 'yargs'; + +export enum AbiType { + Function = 'function', + Constructor = 'constructor', + Event = 'event', + Fallback = 'fallback', +} + +export interface ContractArtifact { + contract_name: string; + networks: ContractNetworks; +} + +export interface ContractNetworks { + [key: number]: ContractData; +} + +export interface ContractData { + solc_version: string; + optimizer_enabled: number; + keccak256: string; + abi: Web3.ContractAbi; + unlinked_binary: string; + address?: string; + constructor_args?: string; + updated_at: number; +} + +export interface SolcErrors { + [key: string]: boolean; +} + +export interface CliOptions extends yargs.Arguments { + artifactsDir: string; + contractsDir: string; + jsonrpcPort: number; + networkId: number; + shouldOptimize: boolean; + gasPrice: string; + account?: string; + contract?: string; + args?: string; +} + +export interface CompilerOptions { + contractsDir: string; + networkId: number; + optimizerEnabled: number; + artifactsDir: string; +} + +export interface DeployerOptions { + artifactsDir: string; + jsonrpcPort: number; + networkId: number; + defaults: Partial; +} + +export interface ContractSources { + [key: string]: string; +} + +export interface ImportContents { + contents: string; +} + +// TODO: Consolidate with 0x.js definitions once types are moved into a separate package. +export enum ZeroExError { + ContractDoesNotExist = 'CONTRACT_DOES_NOT_EXIST', + ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', + UnhandledError = 'UNHANDLED_ERROR', + UserHasNoAssociatedAddress = 'USER_HAS_NO_ASSOCIATED_ADDRESSES', + InvalidSignature = 'INVALID_SIGNATURE', + ContractNotDeployedOnNetwork = 'CONTRACT_NOT_DEPLOYED_ON_NETWORK', + InsufficientAllowanceForTransfer = 'INSUFFICIENT_ALLOWANCE_FOR_TRANSFER', + InsufficientBalanceForTransfer = 'INSUFFICIENT_BALANCE_FOR_TRANSFER', + InsufficientEthBalanceForDeposit = 'INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT', + InsufficientWEthBalanceForWithdrawal = 'INSUFFICIENT_WETH_BALANCE_FOR_WITHDRAWAL', + InvalidJump = 'INVALID_JUMP', + OutOfGas = 'OUT_OF_GAS', + NoNetworkId = 'NO_NETWORK_ID', + SubscriptionNotFound = 'SUBSCRIPTION_NOT_FOUND', +} + +export interface Token { + address?: string; + name: string; + symbol: string; + decimals: number; + ipfsHash: string; + swarmHash: string; +} + +export type DoneCallback = (err?: Error) => void; diff --git a/packages/deployer/src/utils/utils.ts b/packages/deployer/src/utils/utils.ts new file mode 100644 index 000000000..4390d8813 --- /dev/null +++ b/packages/deployer/src/utils/utils.ts @@ -0,0 +1,13 @@ +export const utils = { + consoleLog(message: string): void { + /* tslint:disable */ + console.log(message); + /* tslint:enable */ + }, + stringifyWithFormatting(obj: any): string { + const jsonReplacer: null = null; + const numberOfJsonSpaces = 4; + const stringifiedObj = JSON.stringify(obj, jsonReplacer, numberOfJsonSpaces); + return stringifiedObj; + }, +}; -- cgit v1.2.3