aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/abi_utils.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-04-11 18:00:30 +0800
committerFabio Berger <me@fabioberger.com>2018-04-11 18:00:30 +0800
commit29dc22e208080fa8ff0871b98b530a2deb251a73 (patch)
treedb372a34e85ac8caf4a3f3ed9d7591452df9cb1a /packages/utils/src/abi_utils.ts
parent6f72fed8b5b37fac5096413b363b533e0a29f7b5 (diff)
parentc44f9e56ada898ffd0d0e57aa228006977fb238c (diff)
downloaddexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.gz
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.bz2
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.lz
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.xz
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.zst
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.zip
Merge branch 'development' into removeMigrateStep
* development: Fix lint error Fix documentation links in some READMEs Fix relative link Add step to publishing that upload staging doc jsons, deploys staging website, opens every docs page and asks the dev to confirm that each one renders properly before publishing Fix web3Wrapper build command Add top-level `yarn lerna:stage_docs` to upload docJsons to the staging S3 bucket for all packages with a docs page Added a detailed description of `renameOverloadedMethods` (special thanks to @fabioberger). Updated Javascript styles in the Abi-Gen and Utils packages, around support for function overloading. Updated deployer to accept a list of contract directories as input. Contract directories are namespaced to a void clashes. Also in this commit is a fix for overloading contract functions. Refactor publish script to have it's main execution body be lean and discrete steps # Conflicts: # packages/contracts/package.json # packages/deployer/package.json
Diffstat (limited to 'packages/utils/src/abi_utils.ts')
-rw-r--r--packages/utils/src/abi_utils.ts71
1 files changed, 71 insertions, 0 deletions
diff --git a/packages/utils/src/abi_utils.ts b/packages/utils/src/abi_utils.ts
new file mode 100644
index 000000000..c4533d42e
--- /dev/null
+++ b/packages/utils/src/abi_utils.ts
@@ -0,0 +1,71 @@
+import { AbiDefinition, AbiType, ConstructorAbi, ContractAbi, DataItem, MethodAbi } from '@0xproject/types';
+import * as _ from 'lodash';
+
+export const abiUtils = {
+ parseFunctionParam(param: DataItem): string {
+ if (param.type === 'tuple') {
+ // Parse out tuple types into {type_1, type_2, ..., type_N}
+ const tupleComponents = param.components;
+ const paramString = _.map(tupleComponents, component => this.parseFunctionParam(component));
+ const tupleParamString = `{${paramString}}`;
+ return tupleParamString;
+ }
+ return param.type;
+ },
+ getFunctionSignature(methodAbi: MethodAbi): string {
+ const functionName = methodAbi.name;
+ const parameterTypeList = _.map(methodAbi.inputs, (param: DataItem) => this.parseFunctionParam(param));
+ const functionSignature = `${functionName}(${parameterTypeList})`;
+ return functionSignature;
+ },
+ /**
+ * Solidity supports function overloading whereas TypeScript does not.
+ * See: https://solidity.readthedocs.io/en/v0.4.21/contracts.html?highlight=overload#function-overloading
+ * In order to support overloaded functions, we suffix overloaded function names with an index.
+ * This index should be deterministic, regardless of function ordering within the smart contract. To do so,
+ * we assign indexes based on the alphabetical order of function signatures.
+ *
+ * E.g
+ * ['f(uint)', 'f(uint,byte32)']
+ * Should always be renamed to:
+ * ['f1(uint)', 'f2(uint,byte32)']
+ * Regardless of the order in which these these overloaded functions are declared within the contract ABI.
+ */
+ renameOverloadedMethods(inputContractAbi: ContractAbi): ContractAbi {
+ const contractAbi = _.cloneDeep(inputContractAbi);
+ const methodAbis = contractAbi.filter((abi: AbiDefinition) => abi.type === AbiType.Function) as MethodAbi[];
+ // Sort method Abis into alphabetical order, by function signature
+ const methodAbisOrdered = _.sortBy(methodAbis, [
+ (methodAbi: MethodAbi) => {
+ const functionSignature = this.getFunctionSignature(methodAbi);
+ return functionSignature;
+ },
+ ]);
+ // Group method Abis by name (overloaded methods will be grouped together, in alphabetical order)
+ const methodAbisByName: { [key: string]: MethodAbi[] } = {};
+ _.each(methodAbisOrdered, methodAbi => {
+ (methodAbisByName[methodAbi.name] || (methodAbisByName[methodAbi.name] = [])).push(methodAbi);
+ });
+ // Rename overloaded methods to overloadedMethodName1, overloadedMethodName2, ...
+ _.each(methodAbisByName, methodAbisWithSameName => {
+ _.each(methodAbisWithSameName, (methodAbi, i: number) => {
+ if (methodAbisWithSameName.length > 1) {
+ const overloadedMethodId = i + 1;
+ const sanitizedMethodName = `${methodAbi.name}${overloadedMethodId}`;
+ const indexOfExistingAbiWithSanitizedMethodNameIfExists = _.findIndex(
+ methodAbis,
+ currentMethodAbi => currentMethodAbi.name === sanitizedMethodName,
+ );
+ if (indexOfExistingAbiWithSanitizedMethodNameIfExists >= 0) {
+ const methodName = methodAbi.name;
+ throw new Error(
+ `Failed to rename overloaded method '${methodName}' to '${sanitizedMethodName}'. A method with this name already exists.`,
+ );
+ }
+ methodAbi.name = sanitizedMethodName;
+ }
+ });
+ });
+ return contractAbi;
+ },
+};