aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/contract.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-14 01:52:08 +0800
committerGitHub <noreply@github.com>2017-11-14 01:52:08 +0800
commit58a318b754c3d3d854e36f4b56b37f7de8c0913a (patch)
treed8e3e52fe55e1c3c4e90299708fa8197f9b2002e /packages/0x.js/src/contract.ts
parenta74ec0effa818a86233fe64cb0dad2c61bbb4bb6 (diff)
parentff07f490025447ff11bbdb68ef46304e981f5696 (diff)
downloaddexon-sol-tools-58a318b754c3d3d854e36f4b56b37f7de8c0913a.tar
dexon-sol-tools-58a318b754c3d3d854e36f4b56b37f7de8c0913a.tar.gz
dexon-sol-tools-58a318b754c3d3d854e36f4b56b37f7de8c0913a.tar.bz2
dexon-sol-tools-58a318b754c3d3d854e36f4b56b37f7de8c0913a.tar.lz
dexon-sol-tools-58a318b754c3d3d854e36f4b56b37f7de8c0913a.tar.xz
dexon-sol-tools-58a318b754c3d3d854e36f4b56b37f7de8c0913a.tar.zst
dexon-sol-tools-58a318b754c3d3d854e36f4b56b37f7de8c0913a.zip
Merge pull request #214 from 0xProject/monoRepo
Switch over to Lerna + Yarn Workspaces setup for a mono-repo approach
Diffstat (limited to 'packages/0x.js/src/contract.ts')
-rw-r--r--packages/0x.js/src/contract.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/packages/0x.js/src/contract.ts b/packages/0x.js/src/contract.ts
new file mode 100644
index 000000000..1aacc65dc
--- /dev/null
+++ b/packages/0x.js/src/contract.ts
@@ -0,0 +1,80 @@
+import * as Web3 from 'web3';
+import * as _ from 'lodash';
+import promisify = require('es6-promisify');
+import {SchemaValidator, schemas} from '0x-json-schemas';
+import {AbiType} from './types';
+
+export class Contract implements Web3.ContractInstance {
+ public address: string;
+ public abi: Web3.ContractAbi;
+ private contract: Web3.ContractInstance;
+ private defaults: Partial<Web3.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<Web3.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);
+ _.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<any> {
+ const promisifiedWithDefaultParams = (...args: any[]) => {
+ const promise = new Promise((resolve, reject) => {
+ const lastArg = args[args.length - 1];
+ let txData: Partial<Web3.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;
+ }
+}