aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base-contract/src
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-03-28 17:05:36 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-04-02 17:34:29 +0800
commit4d9029bb0e3b215efdf165704c80d3bacef0e85a (patch)
tree8f699dcd0a7b5fba3c31c065a62d640407bba8c3 /packages/base-contract/src
parentbc49dde4d5d24b9a8b01459bde061f9b23fb5898 (diff)
downloaddexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.gz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.bz2
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.lz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.xz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.zst
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.zip
Add metacoin example project
Diffstat (limited to 'packages/base-contract/src')
-rw-r--r--packages/base-contract/src/index.ts29
-rw-r--r--packages/base-contract/src/utils.ts25
2 files changed, 35 insertions, 19 deletions
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts
index 961da8842..c8cbd7886 100644
--- a/packages/base-contract/src/index.ts
+++ b/packages/base-contract/src/index.ts
@@ -1,40 +1,31 @@
import { ContractAbi, DataItem, TxData, TxDataPayable } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as ethersContracts from 'ethers-contracts';
import * as _ from 'lodash';
+import { formatABIDataItem } from './utils';
+
export class BaseContract {
protected _ethersInterface: ethersContracts.Interface;
protected _web3Wrapper: Web3Wrapper;
public abi: ContractAbi;
public address: string;
- protected static _transformABIData(
+ protected static _formatABIDataItemList(
abis: DataItem[],
values: any[],
- transformation: (type: string, value: any) => any,
+ formatter: (type: string, value: any) => any,
): any {
- return _.map(values, (value: any, i: number) =>
- BaseContract._transformTypedData(abis[i].type, value, transformation),
- );
+ return _.map(values, (value: any, i: number) => formatABIDataItem(abis[i], value, formatter));
}
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;
+ protected static _bigNumberToString(type: string, value: any): any {
+ return _.isObject(value) && value.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 static _bnToBigNumber(type: string, value: any): any {
+ return _.isObject(value) && value._bn ? new BigNumber(value.toString()) : value;
}
protected async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>(
txData: T,
diff --git a/packages/base-contract/src/utils.ts b/packages/base-contract/src/utils.ts
new file mode 100644
index 000000000..4b86bb1ad
--- /dev/null
+++ b/packages/base-contract/src/utils.ts
@@ -0,0 +1,25 @@
+import { DataItem } from '@0xproject/types';
+import * as _ from 'lodash';
+
+// tslint:disable-next-line:completed-docs
+export function formatABIDataItem(abi: DataItem, value: any, formatter: (type: string, value: any) => any): any {
+ const trailingArrayRegex = /\[\d*\]$/;
+ if (abi.type.match(trailingArrayRegex)) {
+ const arrayItemType = abi.type.replace(trailingArrayRegex, '');
+ return _.map(value, val => {
+ const arrayItemAbi = {
+ ...abi,
+ type: arrayItemType,
+ };
+ return formatABIDataItem(arrayItemAbi, val, formatter);
+ });
+ } else if (abi.type === 'tuple') {
+ const formattedTuple: { [componentName: string]: DataItem } = {};
+ _.forEach(abi.components, componentABI => {
+ formattedTuple[componentABI.name] = formatABIDataItem(componentABI, value[componentABI.name], formatter);
+ });
+ return formattedTuple;
+ } else {
+ return formatter(abi.type, value);
+ }
+}