diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-28 09:51:31 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-28 09:51:31 +0800 |
commit | 72e6e1ce8b66809c48e5df61108ff09dea5447ec (patch) | |
tree | 966717683a9f0442f25af01411d2ca0faea4d961 /packages/base-contract | |
parent | 7aa070f9eaef734274df6e6eaa4590fe30d52899 (diff) | |
download | dexon-sol-tools-72e6e1ce8b66809c48e5df61108ff09dea5447ec.tar dexon-sol-tools-72e6e1ce8b66809c48e5df61108ff09dea5447ec.tar.gz dexon-sol-tools-72e6e1ce8b66809c48e5df61108ff09dea5447ec.tar.bz2 dexon-sol-tools-72e6e1ce8b66809c48e5df61108ff09dea5447ec.tar.lz dexon-sol-tools-72e6e1ce8b66809c48e5df61108ff09dea5447ec.tar.xz dexon-sol-tools-72e6e1ce8b66809c48e5df61108ff09dea5447ec.tar.zst dexon-sol-tools-72e6e1ce8b66809c48e5df61108ff09dea5447ec.zip |
Move BaseContract to its own package
Diffstat (limited to 'packages/base-contract')
-rw-r--r-- | packages/base-contract/.npmignore | 5 | ||||
-rw-r--r-- | packages/base-contract/CHANGELOG.md | 5 | ||||
-rw-r--r-- | packages/base-contract/README.md | 53 | ||||
-rw-r--r-- | packages/base-contract/package.json | 39 | ||||
-rw-r--r-- | packages/base-contract/src/index.ts | 68 | ||||
-rw-r--r-- | packages/base-contract/tsconfig.json | 11 | ||||
-rw-r--r-- | packages/base-contract/tslint.json | 3 |
7 files changed, 184 insertions, 0 deletions
diff --git a/packages/base-contract/.npmignore b/packages/base-contract/.npmignore new file mode 100644 index 000000000..ad5ffcd56 --- /dev/null +++ b/packages/base-contract/.npmignore @@ -0,0 +1,5 @@ +.* +yarn-error.log +/scripts/ +/src/ +tsconfig.json diff --git a/packages/base-contract/CHANGELOG.md b/packages/base-contract/CHANGELOG.md new file mode 100644 index 000000000..74e983f1c --- /dev/null +++ b/packages/base-contract/CHANGELOG.md @@ -0,0 +1,5 @@ +# CHANGELOG + +## v0.0.1 - _TBD, 2018_ + + * Initial release (#TBD) diff --git a/packages/base-contract/README.md b/packages/base-contract/README.md new file mode 100644 index 000000000..ffb0d0190 --- /dev/null +++ b/packages/base-contract/README.md @@ -0,0 +1,53 @@ +## @0xproject/utils + +Utils to be shared across 0x projects and packages + +## Installation + +```bash +yarn add @0xproject/utils +``` + +## Usage + +```javascript +import { addressUtils, bigNumberConfigs, classUtils, intervalUtils, promisify } from '@0xproject/utils'; +``` + +## Contributing + +We strongly recommend that the community help us make improvements and determine the future direction of the protocol. To report bugs within this package, please create an issue in this repository. + +Please read our [contribution guidelines](../../CONTRIBUTING.md) before getting started. + +### Install Dependencies + +If you don't have yarn workspaces enabled (Yarn < v1.0) - enable them: + +```bash +yarn config set workspaces-experimental true +``` + +Then install dependencies + +```bash +yarn install +``` + +### Build + +```bash +yarn build +``` + +or + +```bash +yarn build:watch +``` + +### Lint + +```bash +yarn lint +``` diff --git a/packages/base-contract/package.json b/packages/base-contract/package.json new file mode 100644 index 000000000..6edbed161 --- /dev/null +++ b/packages/base-contract/package.json @@ -0,0 +1,39 @@ +{ + "name": "@0xproject/base-contract", + "version": "0.0.1", + "description": "0x Base TS contract", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "scripts": { + "build:watch": "tsc -w", + "build": "tsc", + "clean": "shx rm -rf lib", + "lint": "tslint --project . 'src/**/*.ts'" + }, + "license": "Apache-2.0", + "repository": { + "type": "git", + "url": "https://github.com/0xProject/0x.js.git" + }, + "bugs": { + "url": "https://github.com/0xProject/0x.js/issues" + }, + "homepage": "https://github.com/0xProject/0x.js/packages/base-contract/README.md", + "devDependencies": { + "@0xproject/tslint-config": "^0.4.9", + "@types/lodash": "^4.14.86", + "npm-run-all": "^4.1.2", + "shx": "^0.2.2", + "tslint": "5.8.0", + "typescript": "2.7.1", + "ethers-typescript-typings": "^0.0.1", + "web3-typescript-typings": "^0.9.11" + }, + "dependencies": { + "@0xproject/types": "^0.2.3", + "@0xproject/web3-wrapper": "^0.1.14", + "ethers-contracts": "^2.2.1", + "lodash": "^4.17.4", + "web3": "^0.20.0" + } +} diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts new file mode 100644 index 000000000..cc1e16a13 --- /dev/null +++ b/packages/base-contract/src/index.ts @@ -0,0 +1,68 @@ +import { TxData, TxDataPayable } from '@0xproject/types'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as ethersContracts from 'ethers-contracts'; +import * as _ from 'lodash'; +import * as Web3 from 'web3'; + +export class BaseContract { + protected _ethersInterface: ethersContracts.Interface; + protected _web3Wrapper: Web3Wrapper; + public abi: Web3.ContractAbi; + public address: string; + protected static _transformABIData( + abis: Web3.DataItem[], + values: any[], + transformation: (type: string, value: any) => any, + ): any { + return _.map(values, (value: any, i: number) => + BaseContract._transformTypedData(abis[i].type, value, transformation), + ); + } + protected static _lowercaseAddress(type: string, value: string): string { + return type === 'address' ? value.toLowerCase() : value; + } + protected static _bigNumberToString(type: string, value: string): string { + return _.isObject(value) && (value as any).isBigNumber ? value.toString() : value; + } + private static _transformTypedData( + type: string, + values: any, + transformation: (type: string, value: any) => any, + ): any { + const trailingArrayRegex = /\[\d*\]$/; + if (type.match(trailingArrayRegex)) { + const arrayItemType = type.replace(trailingArrayRegex, ''); + return _.map(values, value => this._transformTypedData(arrayItemType, value, transformation)); + } else { + return transformation(type, values); + } + } + protected async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>( + txData: T, + estimateGasAsync?: (txData: T) => Promise<number>, + ): Promise<TxData> { + // Gas amount sourced with the following priorities: + // 1. Optional param passed in to public method call + // 2. Global config passed in at library instantiation + // 3. Gas estimate calculation + safety margin + const removeUndefinedProperties = _.pickBy; + const txDataWithDefaults = { + to: this.address, + ...removeUndefinedProperties(this._web3Wrapper.getContractDefaults()), + ...removeUndefinedProperties(txData as any), + // HACK: TS can't prove that T is spreadable. + // Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged + }; + if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) { + const estimatedGas = await estimateGasAsync(txData); + txDataWithDefaults.gas = estimatedGas; + } + return txDataWithDefaults; + } + constructor(web3Wrapper: Web3Wrapper, abi: Web3.ContractAbi, address: string) { + this._web3Wrapper = web3Wrapper; + this.abi = abi; + this.address = address; + this._ethersInterface = new ethersContracts.Interface(abi); + } +} diff --git a/packages/base-contract/tsconfig.json b/packages/base-contract/tsconfig.json new file mode 100644 index 000000000..8114d99cd --- /dev/null +++ b/packages/base-contract/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig", + "compilerOptions": { + "outDir": "lib" + }, + "include": [ + "./src/**/*", + "../../node_modules/web3-typescript-typings/index.d.ts", + "../../node_modules/ethers-typescript-typings/index.d.ts" + ] +} diff --git a/packages/base-contract/tslint.json b/packages/base-contract/tslint.json new file mode 100644 index 000000000..ffaefe83a --- /dev/null +++ b/packages/base-contract/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": ["@0xproject/tslint-config"] +} |