diff options
Diffstat (limited to 'packages/deployer/src')
-rw-r--r-- | packages/deployer/src/cli.ts | 153 | ||||
-rw-r--r-- | packages/deployer/src/commands.ts | 14 | ||||
-rw-r--r-- | packages/deployer/src/compiler.ts | 264 | ||||
-rw-r--r-- | packages/deployer/src/deployer.ts | 219 | ||||
-rw-r--r-- | packages/deployer/src/globals.d.ts | 6 | ||||
-rw-r--r-- | packages/deployer/src/index.ts | 2 | ||||
-rw-r--r-- | packages/deployer/src/monorepo_scripts/postpublish.ts | 8 | ||||
-rw-r--r-- | packages/deployer/src/monorepo_scripts/stage_docs.ts | 8 | ||||
-rw-r--r-- | packages/deployer/src/solc/bin_paths.ts | 20 | ||||
-rw-r--r-- | packages/deployer/src/utils/compiler.ts | 107 | ||||
-rw-r--r-- | packages/deployer/src/utils/constants.ts | 4 | ||||
-rw-r--r-- | packages/deployer/src/utils/contract.ts | 80 | ||||
-rw-r--r-- | packages/deployer/src/utils/encoder.ts | 18 | ||||
-rw-r--r-- | packages/deployer/src/utils/error_reporter.ts | 18 | ||||
-rw-r--r-- | packages/deployer/src/utils/fs_wrapper.ts | 12 | ||||
-rw-r--r-- | packages/deployer/src/utils/types.ts | 95 | ||||
-rw-r--r-- | packages/deployer/src/utils/utils.ts | 8 |
17 files changed, 0 insertions, 1036 deletions
diff --git a/packages/deployer/src/cli.ts b/packages/deployer/src/cli.ts deleted file mode 100644 index 7f1ae708a..000000000 --- a/packages/deployer/src/cli.ts +++ /dev/null @@ -1,153 +0,0 @@ -#!/usr/bin/env node -// We need the above pragma since this script will be run as a command-line tool. - -import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; -import * as _ from 'lodash'; -import * as path from 'path'; -import * as Web3 from 'web3'; -import * as yargs from 'yargs'; - -import { commands } from './commands'; -import { constants } from './utils/constants'; -import { consoleReporter } from './utils/error_reporter'; -import { CliOptions, CompilerOptions, DeployerOptions } from './utils/types'; - -const DEFAULT_OPTIMIZER_ENABLED = false; -const DEFAULT_CONTRACTS_DIR = path.resolve('src/contracts'); -const DEFAULT_ARTIFACTS_DIR = path.resolve('src/artifacts'); -const DEFAULT_NETWORK_ID = 50; -const DEFAULT_JSONRPC_URL = 'http://localhost:8545'; -const DEFAULT_GAS_PRICE = (10 ** 9 * 2).toString(); -const DEFAULT_CONTRACTS_LIST = '*'; - -/** - * Compiles all contracts with options passed in through CLI. - * @param argv Instance of process.argv provided by yargs. - */ -async function onCompileCommandAsync(argv: CliOptions): Promise<void> { - const opts: CompilerOptions = { - contractsDir: argv.contractsDir, - networkId: argv.networkId, - optimizerEnabled: argv.shouldOptimize, - artifactsDir: argv.artifactsDir, - specifiedContracts: getContractsSetFromList(argv.contracts), - }; - await commands.compileAsync(opts); -} -/** - * Deploys a single contract with provided name and args. - * @param argv Instance of process.argv provided by yargs. - */ -async function onDeployCommandAsync(argv: CliOptions): Promise<void> { - const url = argv.jsonrpcUrl; - const provider = new Web3.providers.HttpProvider(url); - const web3Wrapper = new Web3Wrapper(provider); - const networkId = await web3Wrapper.getNetworkIdAsync(); - const compilerOpts: CompilerOptions = { - contractsDir: argv.contractsDir, - networkId, - optimizerEnabled: argv.shouldOptimize, - artifactsDir: argv.artifactsDir, - specifiedContracts: getContractsSetFromList(argv.contracts), - }; - await commands.compileAsync(compilerOpts); - - const defaults = { - gasPrice: new BigNumber(argv.gasPrice), - from: argv.account, - }; - const deployerOpts: DeployerOptions = { - artifactsDir: argv.artifactsDir, - jsonrpcUrl: argv.jsonrpcUrl, - networkId, - defaults, - }; - const deployerArgsString = argv.args as string; - const deployerArgs = deployerArgsString.split(','); - await commands.deployAsync(argv.contract as string, deployerArgs, deployerOpts); -} -/** - * Creates a set of contracts to compile. - * @param contracts Comma separated list of contracts to compile - */ -function getContractsSetFromList(contracts: string): Set<string> { - const specifiedContracts = new Set(); - if (contracts === '*') { - return new Set(['*']); - } - const contractsArray = contracts.split(','); - _.forEach(contractsArray, contractName => { - specifiedContracts.add(contractName); - }); - return specifiedContracts; -} -/** - * Provides extra required options for deploy command. - * @param yargsInstance yargs instance provided in builder function callback. - */ -function deployCommandBuilder(yargsInstance: any) { - return yargsInstance - .option('contract', { - type: 'string', - description: 'name of contract to deploy, exluding .sol extension', - }) - .option('args', { - type: 'string', - description: 'comma separated list of constructor args to deploy contract with', - }) - .demandOption(['contract', 'args']) - .help().argv; -} - -(() => { - const identityCommandBuilder = _.identity; - return yargs - .option('contracts-dir', { - type: 'string', - default: DEFAULT_CONTRACTS_DIR, - description: 'path of contracts directory to compile', - }) - .option('network-id', { - type: 'number', - default: DEFAULT_NETWORK_ID, - description: 'mainnet=1, kovan=42, testrpc=50', - }) - .option('should-optimize', { - type: 'boolean', - default: DEFAULT_OPTIMIZER_ENABLED, - description: 'enable optimizer', - }) - .option('artifacts-dir', { - type: 'string', - default: DEFAULT_ARTIFACTS_DIR, - description: 'path to write contracts artifacts to', - }) - .option('jsonrpc-url', { - type: 'string', - default: DEFAULT_JSONRPC_URL, - description: 'url of JSON RPC', - }) - .option('gas-price', { - type: 'string', - default: DEFAULT_GAS_PRICE, - description: 'gasPrice to be used for transactions', - }) - .option('account', { - type: 'string', - description: 'account to use for deploying contracts', - }) - .option('contracts', { - type: 'string', - default: DEFAULT_CONTRACTS_LIST, - description: 'comma separated list of contracts to compile', - }) - .command('compile', 'compile contracts', identityCommandBuilder, consoleReporter(onCompileCommandAsync)) - .command( - 'deploy', - 'deploy a single contract with provided arguments', - deployCommandBuilder, - consoleReporter(onDeployCommandAsync), - ) - .help().argv; -})(); diff --git a/packages/deployer/src/commands.ts b/packages/deployer/src/commands.ts deleted file mode 100644 index 8e544a60b..000000000 --- a/packages/deployer/src/commands.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Compiler } from './compiler'; -import { Deployer } from './deployer'; -import { CompilerOptions, DeployerOptions } from './utils/types'; - -export const commands = { - async compileAsync(opts: CompilerOptions): Promise<void> { - const compiler = new Compiler(opts); - await compiler.compileAsync(); - }, - async deployAsync(contractName: string, args: any[], opts: DeployerOptions): Promise<void> { - const deployer = new Deployer(opts); - await deployer.deployAndSaveAsync(contractName, args); - }, -}; diff --git a/packages/deployer/src/compiler.ts b/packages/deployer/src/compiler.ts deleted file mode 100644 index a3c8004ec..000000000 --- a/packages/deployer/src/compiler.ts +++ /dev/null @@ -1,264 +0,0 @@ -import { - ContractSource, - ContractSources, - EnumerableResolver, - FallthroughResolver, - FSResolver, - NameResolver, - NPMResolver, - Resolver, - URLResolver, -} from '@0xproject/sol-resolver'; -import { ContractAbi } from '@0xproject/types'; -import { logUtils, promisify } from '@0xproject/utils'; -import chalk from 'chalk'; -import * as ethUtil from 'ethereumjs-util'; -import * as fs from 'fs'; -import 'isomorphic-fetch'; -import * as _ from 'lodash'; -import * as path from 'path'; -import * as requireFromString from 'require-from-string'; -import * as semver from 'semver'; -import solc = require('solc'); - -import { binPaths } from './solc/bin_paths'; -import { - createDirIfDoesNotExistAsync, - getContractArtifactIfExistsAsync, - getNormalizedErrMsg, - parseDependencies, - parseSolidityVersionRange, -} from './utils/compiler'; -import { constants } from './utils/constants'; -import { fsWrapper } from './utils/fs_wrapper'; -import { - CompilerOptions, - ContractArtifact, - ContractNetworkData, - ContractNetworks, - ContractSourceData, - ContractSpecificSourceData, -} from './utils/types'; -import { utils } from './utils/utils'; - -const ALL_CONTRACTS_IDENTIFIER = '*'; -const SOLC_BIN_DIR = path.join(__dirname, '..', '..', 'solc_bin'); - -/** - * The Compiler facilitates compiling Solidity smart contracts and saves the results - * to artifact files. - */ -export class Compiler { - private _resolver: Resolver; - private _nameResolver: NameResolver; - private _contractsDir: string; - private _networkId: number; - private _optimizerEnabled: boolean; - private _artifactsDir: string; - private _specifiedContracts: Set<string> = new Set(); - /** - * 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._specifiedContracts = opts.specifiedContracts; - this._nameResolver = new NameResolver(path.resolve(this._contractsDir)); - const resolver = new FallthroughResolver(); - resolver.appendResolver(new URLResolver()); - const packagePath = path.resolve(''); - resolver.appendResolver(new NPMResolver(packagePath)); - resolver.appendResolver(new FSResolver()); - resolver.appendResolver(this._nameResolver); - this._resolver = resolver; - } - /** - * Compiles selected Solidity files found in `contractsDir` and writes JSON artifacts to `artifactsDir`. - */ - public async compileAsync(): Promise<void> { - await createDirIfDoesNotExistAsync(this._artifactsDir); - await createDirIfDoesNotExistAsync(SOLC_BIN_DIR); - let contractNamesToCompile: string[] = []; - if (this._specifiedContracts.has(ALL_CONTRACTS_IDENTIFIER)) { - const allContracts = this._nameResolver.getAll(); - contractNamesToCompile = _.map(allContracts, contractSource => - path.basename(contractSource.path, constants.SOLIDITY_FILE_EXTENSION), - ); - } else { - contractNamesToCompile = Array.from(this._specifiedContracts.values()); - } - for (const contractNameToCompile of contractNamesToCompile) { - await this._compileContractAsync(contractNameToCompile); - } - } - /** - * Compiles contract and saves artifact to artifactsDir. - * @param fileName Name of contract with '.sol' extension. - */ - private async _compileContractAsync(contractName: string): Promise<void> { - const contractSource = this._resolver.resolve(contractName); - const currentArtifactIfExists = await getContractArtifactIfExistsAsync(this._artifactsDir, contractName); - const sourceTreeHashHex = `0x${this._getSourceTreeHash(contractSource.path).toString('hex')}`; - - let shouldCompile = false; - if (_.isUndefined(currentArtifactIfExists)) { - shouldCompile = true; - } else { - const currentArtifact = currentArtifactIfExists as ContractArtifact; - shouldCompile = - currentArtifact.networks[this._networkId].optimizer_enabled !== this._optimizerEnabled || - currentArtifact.networks[this._networkId].source_tree_hash !== sourceTreeHashHex; - } - if (!shouldCompile) { - return; - } - const solcVersionRange = parseSolidityVersionRange(contractSource.source); - const availableCompilerVersions = _.keys(binPaths); - const solcVersion = semver.maxSatisfying(availableCompilerVersions, solcVersionRange); - const fullSolcVersion = binPaths[solcVersion]; - const compilerBinFilename = path.join(SOLC_BIN_DIR, fullSolcVersion); - let solcjs: string; - const isCompilerAvailableLocally = fs.existsSync(compilerBinFilename); - if (isCompilerAvailableLocally) { - solcjs = fs.readFileSync(compilerBinFilename).toString(); - } else { - logUtils.log(`Downloading ${fullSolcVersion}...`); - const url = `${constants.BASE_COMPILER_URL}${fullSolcVersion}`; - const response = await fetch(url); - if (response.status !== 200) { - throw new Error(`Failed to load ${fullSolcVersion}`); - } - solcjs = await response.text(); - fs.writeFileSync(compilerBinFilename, solcjs); - } - const solcInstance = solc.setupMethods(requireFromString(solcjs, compilerBinFilename)); - - logUtils.log(`Compiling ${contractName} with Solidity v${solcVersion}...`); - const source = contractSource.source; - const absoluteFilePath = contractSource.path; - const standardInput: solc.StandardInput = { - language: 'Solidity', - sources: { - [absoluteFilePath]: { - urls: [`file://${absoluteFilePath}`], - }, - }, - settings: { - optimizer: { - enabled: this._optimizerEnabled, - }, - outputSelection: { - '*': { - '*': [ - 'abi', - 'evm.bytecode.object', - 'evm.bytecode.sourceMap', - 'evm.deployedBytecode.object', - 'evm.deployedBytecode.sourceMap', - ], - }, - }, - }, - }; - const compiled: solc.StandardOutput = JSON.parse( - solcInstance.compileStandardWrapper(JSON.stringify(standardInput), importPath => { - const sourceCodeIfExists = this._resolver.resolve(importPath); - return { contents: sourceCodeIfExists.source }; - }), - ); - - if (!_.isUndefined(compiled.errors)) { - const SOLIDITY_WARNING = 'warning'; - const errors = _.filter(compiled.errors, entry => entry.severity !== SOLIDITY_WARNING); - const warnings = _.filter(compiled.errors, entry => entry.severity === SOLIDITY_WARNING); - if (!_.isEmpty(errors)) { - errors.forEach(error => { - const normalizedErrMsg = getNormalizedErrMsg(error.formattedMessage || error.message); - logUtils.log(chalk.red(normalizedErrMsg)); - }); - process.exit(1); - } else { - warnings.forEach(warning => { - const normalizedWarningMsg = getNormalizedErrMsg(warning.formattedMessage || warning.message); - logUtils.log(chalk.yellow(normalizedWarningMsg)); - }); - } - } - const compiledData = compiled.contracts[absoluteFilePath][contractName]; - if (_.isUndefined(compiledData)) { - throw new Error( - `Contract ${contractName} not found in ${absoluteFilePath}. Please make sure your contract has the same name as it's file name`, - ); - } - const abi: ContractAbi = compiledData.abi; - const bytecode = `0x${compiledData.evm.bytecode.object}`; - const runtimeBytecode = `0x${compiledData.evm.deployedBytecode.object}`; - const sourceMap = compiledData.evm.bytecode.sourceMap; - const sourceMapRuntime = compiledData.evm.deployedBytecode.sourceMap; - const unresolvedSourcePaths = _.keys(compiled.sources); - const sources = _.map( - unresolvedSourcePaths, - unresolvedSourcePath => this._resolver.resolve(unresolvedSourcePath).path, - ); - const updated_at = Date.now(); - const contractNetworkData: ContractNetworkData = { - solc_version: solcVersion, - source_tree_hash: sourceTreeHashHex, - optimizer_enabled: this._optimizerEnabled, - abi, - bytecode, - runtime_bytecode: runtimeBytecode, - updated_at, - source_map: sourceMap, - source_map_runtime: sourceMapRuntime, - sources, - }; - - let newArtifact: ContractArtifact; - if (!_.isUndefined(currentArtifactIfExists)) { - const currentArtifact = currentArtifactIfExists as ContractArtifact; - newArtifact = { - ...currentArtifact, - networks: { - ...currentArtifact.networks, - [this._networkId]: contractNetworkData, - }, - }; - } else { - newArtifact = { - contract_name: contractName, - networks: { - [this._networkId]: contractNetworkData, - }, - }; - } - - const artifactString = utils.stringifyWithFormatting(newArtifact); - const currentArtifactPath = `${this._artifactsDir}/${contractName}.json`; - await fsWrapper.writeFileAsync(currentArtifactPath, artifactString); - logUtils.log(`${contractName} artifact saved!`); - } - /** - * Gets the source tree hash for a file and its dependencies. - * @param fileName Name of contract file. - */ - private _getSourceTreeHash(importPath: string): Buffer { - const contractSource = this._resolver.resolve(importPath); - const dependencies = parseDependencies(contractSource); - const sourceHash = ethUtil.sha3(contractSource.source); - if (dependencies.length === 0) { - return sourceHash; - } else { - const dependencySourceTreeHashes = _.map(dependencies, (dependency: string) => - this._getSourceTreeHash(dependency), - ); - const sourceTreeHashesBuffer = Buffer.concat([sourceHash, ...dependencySourceTreeHashes]); - const sourceTreeHash = ethUtil.sha3(sourceTreeHashesBuffer); - return sourceTreeHash; - } - } -} diff --git a/packages/deployer/src/deployer.ts b/packages/deployer/src/deployer.ts deleted file mode 100644 index ad05417b1..000000000 --- a/packages/deployer/src/deployer.ts +++ /dev/null @@ -1,219 +0,0 @@ -import { AbiType, ConstructorAbi, ContractAbi, Provider, TxData } from '@0xproject/types'; -import { logUtils } from '@0xproject/utils'; -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, - ContractNetworkData, - DeployerOptions, - ProviderDeployerOptions, - UrlDeployerOptions, -} 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; - -/** - * The Deployer facilitates deploying Solidity smart contracts to the blockchain. - * It can be used to build custom migration scripts. - */ -export class Deployer { - public web3Wrapper: Web3Wrapper; - private _artifactsDir: string; - private _networkId: number; - private _defaults: Partial<TxData>; - - /** - * Instantiate a new instance of the Deployer class. - * @param opts Deployer options, including either an RPC url or Provider instance. - * @returns A Deployer instance - */ - constructor(opts: DeployerOptions) { - this._artifactsDir = opts.artifactsDir; - this._networkId = opts.networkId; - this._defaults = opts.defaults; - let provider: Provider; - if (_.isUndefined((opts as ProviderDeployerOptions).provider)) { - const jsonrpcUrl = (opts as UrlDeployerOptions).jsonrpcUrl; - if (_.isUndefined(jsonrpcUrl)) { - throw new Error(`Deployer options don't contain provider nor jsonrpcUrl. Please pass one of them`); - } - provider = new Web3.providers.HttpProvider(jsonrpcUrl); - } else { - provider = (opts as ProviderDeployerOptions).provider; - } - this.web3Wrapper = new Web3Wrapper(provider, this._defaults); - } - /** - * Loads a contract's corresponding artifacts and deploys it with the supplied constructor arguments. - * @param contractName Name of the contract to deploy. Must match name of an artifact in supplied artifacts directory. - * @param args Array of contract constructor arguments. - * @return Deployed contract instance. - */ - public async deployAsync(contractName: string, args: any[] = []): Promise<Web3.ContractInstance> { - const contractArtifactIfExists: ContractArtifact = this._loadContractArtifactIfExists(contractName); - const contractNetworkDataIfExists: ContractNetworkData = this._getContractNetworkDataFromArtifactIfExists( - contractArtifactIfExists, - ); - const data = contractNetworkDataIfExists.bytecode; - const from = await this._getFromAddressAsync(); - const gas = await this._getAllowableGasEstimateAsync(data); - const txData = { - gasPrice: this._defaults.gasPrice, - from, - data, - gas, - }; - const abi = contractNetworkDataIfExists.abi; - const constructorAbi = _.find(abi, { type: AbiType.Constructor }) as ConstructorAbi; - const constructorArgs = _.isUndefined(constructorAbi) ? [] : constructorAbi.inputs; - if (constructorArgs.length !== args.length) { - const constructorSignature = `constructor(${_.map(constructorArgs, arg => `${arg.type} ${arg.name}`).join( - ', ', - )})`; - throw new Error( - `${contractName} expects ${constructorArgs.length} constructor params: ${constructorSignature}. Got ${ - args.length - }`, - ); - } - const web3ContractInstance = await this._deployFromAbiAsync(abi, args, txData); - logUtils.log(`${contractName}.sol successfully deployed at ${web3ContractInstance.address}`); - const contractInstance = new Contract(web3ContractInstance, this._defaults); - return contractInstance; - } - /** - * Loads a contract's artifact, deploys it with supplied constructor arguments, and saves the updated data - * back to the artifact file. - * @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<Web3.ContractInstance> { - 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: ContractAbi, args: any[], txData: TxData): Promise<any> { - const contract: Web3.Contract<Web3.ContractInstance> = 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)) { - logUtils.log(`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<void> { - const contractArtifactIfExists: ContractArtifact = this._loadContractArtifactIfExists(contractName); - const contractNetworkDataIfExists: ContractNetworkData = this._getContractNetworkDataFromArtifactIfExists( - contractArtifactIfExists, - ); - const abi = contractNetworkDataIfExists.abi; - const encodedConstructorArgs = encoder.encodeConstructorArgsFromAbi(args, abi); - const newContractData = { - ...contractNetworkDataIfExists, - address: contractAddress, - constructor_args: encodedConstructorArgs, - }; - const newArtifact = { - ...contractArtifactIfExists, - networks: { - ...contractArtifactIfExists.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} at ${artifactPath}`); - } - } - /** - * Gets data for current networkId stored in artifact. - * @param contractArtifact The contract artifact. - * @return Network specific contract data. - */ - private _getContractNetworkDataFromArtifactIfExists(contractArtifact: ContractArtifact): ContractNetworkData { - const contractNetworkDataIfExists = contractArtifact.networks[this._networkId]; - if (_.isUndefined(contractNetworkDataIfExists)) { - throw new Error(`Data not found in artifact for contract: ${contractArtifact.contract_name}`); - } - return contractNetworkDataIfExists; - } - /** - * 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<string> { - 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<number> { - 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/globals.d.ts b/packages/deployer/src/globals.d.ts deleted file mode 100644 index 94e63a32d..000000000 --- a/packages/deployer/src/globals.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -declare module '*.json' { - const json: any; - /* tslint:disable */ - export default json; - /* tslint:enable */ -} diff --git a/packages/deployer/src/index.ts b/packages/deployer/src/index.ts deleted file mode 100644 index 186d644ef..000000000 --- a/packages/deployer/src/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { Deployer } from './deployer'; -export { Compiler } from './compiler'; diff --git a/packages/deployer/src/monorepo_scripts/postpublish.ts b/packages/deployer/src/monorepo_scripts/postpublish.ts deleted file mode 100644 index dcb99d0f7..000000000 --- a/packages/deployer/src/monorepo_scripts/postpublish.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { postpublishUtils } from '@0xproject/monorepo-scripts'; - -import * as packageJSON from '../package.json'; -import * as tsConfigJSON from '../tsconfig.json'; - -const cwd = `${__dirname}/..`; -// tslint:disable-next-line:no-floating-promises -postpublishUtils.runAsync(packageJSON, tsConfigJSON, cwd); diff --git a/packages/deployer/src/monorepo_scripts/stage_docs.ts b/packages/deployer/src/monorepo_scripts/stage_docs.ts deleted file mode 100644 index e732ac8eb..000000000 --- a/packages/deployer/src/monorepo_scripts/stage_docs.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { postpublishUtils } from '@0xproject/monorepo-scripts'; - -import * as packageJSON from '../package.json'; -import * as tsConfigJSON from '../tsconfig.json'; - -const cwd = `${__dirname}/..`; -// tslint:disable-next-line:no-floating-promises -postpublishUtils.publishDocsToStagingAsync(packageJSON, tsConfigJSON, cwd); diff --git a/packages/deployer/src/solc/bin_paths.ts b/packages/deployer/src/solc/bin_paths.ts deleted file mode 100644 index 1b5e8c6f1..000000000 --- a/packages/deployer/src/solc/bin_paths.ts +++ /dev/null @@ -1,20 +0,0 @@ -export interface BinaryPaths { - [key: string]: string; -} - -export const binPaths: BinaryPaths = { - '0.4.10': 'soljson-v0.4.10+commit.f0d539ae.js', - '0.4.11': 'soljson-v0.4.11+commit.68ef5810.js', - '0.4.12': 'soljson-v0.4.12+commit.194ff033.js', - '0.4.13': 'soljson-v0.4.13+commit.fb4cb1a.js', - '0.4.14': 'soljson-v0.4.14+commit.c2215d46.js', - '0.4.15': 'soljson-v0.4.15+commit.bbb8e64f.js', - '0.4.16': 'soljson-v0.4.16+commit.d7661dd9.js', - '0.4.17': 'soljson-v0.4.17+commit.bdeb9e52.js', - '0.4.18': 'soljson-v0.4.18+commit.9cf6e910.js', - '0.4.19': 'soljson-v0.4.19+commit.c4cbbb05.js', - '0.4.20': 'soljson-v0.4.20+commit.3155dd80.js', - '0.4.21': 'soljson-v0.4.21+commit.dfe3193c.js', - '0.4.22': 'soljson-v0.4.22+commit.4cb486ee.js', - '0.4.23': 'soljson-v0.4.23+commit.124ca40d.js', -}; diff --git a/packages/deployer/src/utils/compiler.ts b/packages/deployer/src/utils/compiler.ts deleted file mode 100644 index c571b2581..000000000 --- a/packages/deployer/src/utils/compiler.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { ContractSource, ContractSources } from '@0xproject/sol-resolver'; -import { logUtils } from '@0xproject/utils'; -import * as _ from 'lodash'; -import * as path from 'path'; -import * as solc from 'solc'; - -import { constants } from './constants'; -import { fsWrapper } from './fs_wrapper'; -import { ContractArtifact } from './types'; - -/** - * Gets contract data on network or returns if an artifact does not exist. - * @param artifactsDir Path to the artifacts directory. - * @param contractName Name of contract. - * @return Contract data on network or undefined. - */ -export async function getContractArtifactIfExistsAsync( - artifactsDir: string, - contractName: string, -): Promise<ContractArtifact | void> { - let contractArtifact; - const currentArtifactPath = `${artifactsDir}/${contractName}.json`; - try { - const opts = { - encoding: 'utf8', - }; - const contractArtifactString = await fsWrapper.readFileAsync(currentArtifactPath, opts); - contractArtifact = JSON.parse(contractArtifactString); - return contractArtifact; - } catch (err) { - logUtils.log(`Artifact for ${contractName} does not exist`); - return undefined; - } -} - -/** - * Creates a directory if it does not already exist. - * @param artifactsDir Path to the directory. - */ -export async function createDirIfDoesNotExistAsync(dirPath: string): Promise<void> { - if (!fsWrapper.doesPathExistSync(dirPath)) { - logUtils.log(`Creating directory at ${dirPath}...`); - await fsWrapper.mkdirAsync(dirPath); - } -} - -/** - * Searches Solidity source code for compiler version range. - * @param source Source code of contract. - * @return Solc compiler version range. - */ -export function parseSolidityVersionRange(source: string): string { - const SOLIDITY_VERSION_RANGE_REGEX = /pragma\s+solidity\s+(.*);/; - const solcVersionRangeMatch = source.match(SOLIDITY_VERSION_RANGE_REGEX); - if (_.isNull(solcVersionRangeMatch)) { - throw new Error('Could not find Solidity version range in source'); - } - const solcVersionRange = solcVersionRangeMatch[1]; - return solcVersionRange; -} - -/** - * 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. - */ -export function getNormalizedErrMsg(errMsg: string): string { - const SOLIDITY_FILE_EXTENSION_REGEX = /(.*\.sol)/; - const errPathMatch = errMsg.match(SOLIDITY_FILE_EXTENSION_REGEX); - 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; -} - -/** - * Parses the contract source code and extracts the dendencies - * @param source Contract source code - * @return List of dependendencies - */ -export function parseDependencies(contractSource: ContractSource): string[] { - // TODO: Use a proper parser - const source = contractSource.source; - const IMPORT_REGEX = /(import\s)/; - const DEPENDENCY_PATH_REGEX = /"([^"]+)"/; // Source: https://github.com/BlockChainCompany/soljitsu/blob/master/lib/shared.js - const dependencies: string[] = []; - const lines = source.split('\n'); - _.forEach(lines, line => { - if (!_.isNull(line.match(IMPORT_REGEX))) { - const dependencyMatch = line.match(DEPENDENCY_PATH_REGEX); - if (!_.isNull(dependencyMatch)) { - let dependencyPath = dependencyMatch[1]; - if (dependencyPath.startsWith('.')) { - dependencyPath = path.join(path.dirname(contractSource.path), dependencyPath); - } - dependencies.push(dependencyPath); - } - } - }); - return dependencies; -} diff --git a/packages/deployer/src/utils/constants.ts b/packages/deployer/src/utils/constants.ts deleted file mode 100644 index 6f9dfb994..000000000 --- a/packages/deployer/src/utils/constants.ts +++ /dev/null @@ -1,4 +0,0 @@ -export const constants = { - SOLIDITY_FILE_EXTENSION: '.sol', - BASE_COMPILER_URL: 'https://ethereum.github.io/solc-bin/bin/', -}; diff --git a/packages/deployer/src/utils/contract.ts b/packages/deployer/src/utils/contract.ts deleted file mode 100644 index e8dd5218a..000000000 --- a/packages/deployer/src/utils/contract.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { schemas, SchemaValidator } from '@0xproject/json-schemas'; -import { AbiType, ContractAbi, EventAbi, FunctionAbi, MethodAbi, TxData } from '@0xproject/types'; -import { promisify } from '@0xproject/utils'; -import * as _ from 'lodash'; -import * as Web3 from 'web3'; - -export class Contract implements Web3.ContractInstance { - public address: string; - public abi: ContractAbi; - private _contract: Web3.ContractInstance; - private _defaults: Partial<TxData>; - 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<TxData>) { - 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 FunctionAbi[]; - _.forEach(functionsAbi, (functionAbi: MethodAbi) => { - if (functionAbi.constant) { - const cbStyleCallFunction = this._contract[functionAbi.name].call; - this[functionAbi.name] = promisify(cbStyleCallFunction, this._contract); - this[functionAbi.name].call = promisify(cbStyleCallFunction, this._contract); - } else { - const cbStyleFunction = this._contract[functionAbi.name]; - const cbStyleCallFunction = this._contract[functionAbi.name].call; - const cbStyleEstimateGasFunction = this._contract[functionAbi.name].estimateGas; - this[functionAbi.name] = this._promisifyWithDefaultParams(cbStyleFunction); - this[functionAbi.name].estimateGasAsync = promisify(cbStyleEstimateGasFunction); - this[functionAbi.name].sendTransactionAsync = this._promisifyWithDefaultParams(cbStyleFunction); - this[functionAbi.name].call = promisify(cbStyleCallFunction, this._contract); - } - }); - } - private _populateEvents(): void { - const eventsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Event) as EventAbi[]; - _.forEach(eventsAbi, (eventAbi: EventAbi) => { - this[eventAbi.name] = this._contract[eventAbi.name]; - }); - } - private _promisifyWithDefaultParams(fn: (...args: any[]) => void): (...args: any[]) => Promise<any> { - const promisifiedWithDefaultParams = async (...args: any[]) => { - const promise = new Promise((resolve, reject) => { - const lastArg = args[args.length - 1]; - let txData: Partial<TxData> = {}; - 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 deleted file mode 100644 index 806efbbca..000000000 --- a/packages/deployer/src/utils/encoder.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { AbiDefinition, AbiType, ContractAbi, DataItem } from '@0xproject/types'; -import * as _ from 'lodash'; -import * as web3Abi from 'web3-eth-abi'; - -export const encoder = { - encodeConstructorArgsFromAbi(args: any[], abi: ContractAbi): string { - const constructorTypes: string[] = []; - _.each(abi, (element: AbiDefinition) => { - if (element.type === AbiType.Constructor) { - _.each(element.inputs, (input: DataItem) => { - constructorTypes.push(input.type); - }); - } - }); - const encodedParameters = web3Abi.encodeParameters(constructorTypes, args); - return encodedParameters; - }, -}; diff --git a/packages/deployer/src/utils/error_reporter.ts b/packages/deployer/src/utils/error_reporter.ts deleted file mode 100644 index 4e73307f0..000000000 --- a/packages/deployer/src/utils/error_reporter.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { logUtils } from '@0xproject/utils'; - -/** - * Makes an async function no-throw printing errors to the console - * @param asyncFn async function to wrap - * @return Wrapped version of the passed function - */ -export function consoleReporter<T>(asyncFn: (arg: T) => Promise<void>): (arg: T) => Promise<void> { - const noThrowFnAsync = async (arg: T) => { - try { - const result = await asyncFn(arg); - return result; - } catch (err) { - logUtils.log(`${err}`); - } - }; - return noThrowFnAsync; -} diff --git a/packages/deployer/src/utils/fs_wrapper.ts b/packages/deployer/src/utils/fs_wrapper.ts deleted file mode 100644 index e02c83f27..000000000 --- a/packages/deployer/src/utils/fs_wrapper.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { promisify } from '@0xproject/utils'; -import * as fs from 'fs'; - -export const fsWrapper = { - readdirAsync: promisify<string[]>(fs.readdir), - readFileAsync: promisify<string>(fs.readFile), - writeFileAsync: promisify<undefined>(fs.writeFile), - mkdirAsync: promisify<undefined>(fs.mkdir), - doesPathExistSync: fs.existsSync, - rmdirSync: fs.rmdirSync, - removeFileAsync: promisify<undefined>(fs.unlink), -}; diff --git a/packages/deployer/src/utils/types.ts b/packages/deployer/src/utils/types.ts deleted file mode 100644 index a20d0f627..000000000 --- a/packages/deployer/src/utils/types.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { ContractAbi, Provider, 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]: ContractNetworkData; -} - -export interface ContractNetworkData { - solc_version: string; - optimizer_enabled: boolean; - source_tree_hash: string; - abi: ContractAbi; - bytecode: string; - runtime_bytecode: string; - address?: string; - constructor_args?: string; - updated_at: number; - source_map: string; - source_map_runtime: string; - sources: string[]; -} - -export interface SolcErrors { - [key: string]: boolean; -} - -export interface CliOptions extends yargs.Arguments { - artifactsDir: string; - contractsDir: string; - jsonrpcUrl: string; - networkId: number; - shouldOptimize: boolean; - gasPrice: string; - account?: string; - contract?: string; - args?: string; -} - -export interface CompilerOptions { - contractsDir: string; - networkId: number; - optimizerEnabled: boolean; - artifactsDir: string; - specifiedContracts: Set<string>; -} - -export interface BaseDeployerOptions { - artifactsDir: string; - networkId: number; - defaults: Partial<TxData>; -} - -export interface ProviderDeployerOptions extends BaseDeployerOptions { - provider: Provider; -} - -export interface UrlDeployerOptions extends BaseDeployerOptions { - jsonrpcUrl: string; -} - -export type DeployerOptions = UrlDeployerOptions | ProviderDeployerOptions; - -export interface ContractSourceData { - [contractName: string]: ContractSpecificSourceData; -} - -export interface ContractSpecificSourceData { - solcVersionRange: string; - sourceHash: Buffer; - sourceTreeHash: Buffer; -} - -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 deleted file mode 100644 index 9b1e59f9d..000000000 --- a/packages/deployer/src/utils/utils.ts +++ /dev/null @@ -1,8 +0,0 @@ -export const utils = { - stringifyWithFormatting(obj: any): string { - const jsonReplacer: null = null; - const numberOfJsonSpaces = 4; - const stringifiedObj = JSON.stringify(obj, jsonReplacer, numberOfJsonSpaces); - return stringifiedObj; - }, -}; |