From 4b3e0383235ca4ca0127f24c2e05543bb45a56bb Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Wed, 29 Nov 2017 22:02:43 -0800 Subject: Add contracts to packages, fix most linting errors --- packages/contracts/deploy/src/utils/contract.ts | 81 +++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 packages/contracts/deploy/src/utils/contract.ts (limited to 'packages/contracts/deploy/src/utils/contract.ts') diff --git a/packages/contracts/deploy/src/utils/contract.ts b/packages/contracts/deploy/src/utils/contract.ts new file mode 100644 index 000000000..e9c49c9f1 --- /dev/null +++ b/packages/contracts/deploy/src/utils/contract.ts @@ -0,0 +1,81 @@ +import {schemas, SchemaValidator} from '@0xproject/json-schemas'; +import promisify = require('es6-promisify'); +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); + _.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); + _.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; + } +} -- cgit v1.2.3