From fa21e47f64ff2c5a114e9f1a9ebbe46e78903212 Mon Sep 17 00:00:00 2001 From: Richard Meissner Date: Thu, 19 Oct 2017 17:34:11 +0200 Subject: [PATCH] WA-36: Fixed bug when multiple function have same name (#40) --- .../src/main/kotlin/pm/gnosis/AbiParser.kt | 50 +++++++++++-------- build.gradle | 4 +- sample/app/abi/test.json | 17 +++++++ 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/bivrost-abi-parser/src/main/kotlin/pm/gnosis/AbiParser.kt b/bivrost-abi-parser/src/main/kotlin/pm/gnosis/AbiParser.kt index 80b85fd..6d70652 100644 --- a/bivrost-abi-parser/src/main/kotlin/pm/gnosis/AbiParser.kt +++ b/bivrost-abi-parser/src/main/kotlin/pm/gnosis/AbiParser.kt @@ -99,6 +99,7 @@ class AbiParser { build.writeTo(output) } } + companion object { const val VARIABLE_NAME_ITEM_DECODER = "itemDecoder" } @@ -208,27 +209,34 @@ class AbiParser { } private fun generateFunctionObjects(context: GeneratorContext) = - context.root.abi.filter { it.type == "function" }.map { functionJson -> - val functionObject = TypeSpec.objectBuilder(functionJson.name.capitalize()) - - //Add method id - val methodId = "${functionJson.name}${generateMethodSignature(functionJson.inputs)}".generateSolidityMethodId() - functionObject.addProperty(PropertySpec.builder("METHOD_ID", String::class, KModifier.CONST).initializer("\"$methodId\"").build()) - functionObject.addFun(generateFunctionEncoder(functionJson, context)) - if (functionJson.outputs.isNotEmpty()) { - val returnHolder = generateParameterHolder("Return", functionJson.outputs, context) - functionObject.addFun(generateParameterDecoder("decode", functionJson.outputs, returnHolder.name!!, context)) - functionObject.addType(returnHolder) - } - - if (functionJson.inputs.isNotEmpty()) { - val argumentsHolder = generateParameterHolder("Arguments", functionJson.inputs, context) - functionObject.addFun(generateParameterDecoder("decodeArguments", functionJson.inputs, argumentsHolder.name!!, context)) - functionObject.addType(argumentsHolder) - } - - functionObject.build() - }.toList() + context.root.abi + .filter { it.type == "function" } + .groupBy { it.name } + .flatMap { (_, value) -> value.map { Pair(it, value.size > 1) } } + .map { (functionJson, useMethodId) -> + + //Add method id + val methodId = "${functionJson.name}${generateMethodSignature(functionJson.inputs)}".generateSolidityMethodId() + val baseName = functionJson.name.capitalize() + val name = if (useMethodId) "${baseName}_$methodId" else baseName + val functionObject = TypeSpec.objectBuilder(name) + + functionObject.addProperty(PropertySpec.builder("METHOD_ID", String::class, KModifier.CONST).initializer("\"$methodId\"").build()) + functionObject.addFun(generateFunctionEncoder(functionJson, context)) + if (functionJson.outputs.isNotEmpty()) { + val returnHolder = generateParameterHolder("Return", functionJson.outputs, context) + functionObject.addFun(generateParameterDecoder("decode", functionJson.outputs, returnHolder.name!!, context)) + functionObject.addType(returnHolder) + } + + if (functionJson.inputs.isNotEmpty()) { + val argumentsHolder = generateParameterHolder("Arguments", functionJson.inputs, context) + functionObject.addFun(generateParameterDecoder("decodeArguments", functionJson.inputs, argumentsHolder.name!!, context)) + functionObject.addType(argumentsHolder) + } + + functionObject.build() + }.toList() private fun generateMethodSignature(parameters: List): String = "(${parameters.joinToString(",") { diff --git a/build.gradle b/build.gradle index 0d55bae..2c16cc4 100644 --- a/build.gradle +++ b/build.gradle @@ -2,11 +2,11 @@ buildscript { ext.versions = [ 'minSdk' : 14, 'compileSdk' : 26, - 'buildTools' : '26.0.1', + 'buildTools' : '26.0.2', 'kotlin' : '1.1.4-3', - 'androidPlugin': '3.0.0-beta6', + 'androidPlugin': '3.0.0-beta7', ] ext.deps = [ diff --git a/sample/app/abi/test.json b/sample/app/abi/test.json index 01f068f..4aac884 100644 --- a/sample/app/abi/test.json +++ b/sample/app/abi/test.json @@ -74,6 +74,23 @@ "payable": false, "type": "function" }, + { + "constant": false, + "inputs": [ + { + "name": "owner", + "type": "address" + }, + { + "name": "reason", + "type": "string" + } + ], + "name": "removeOwner", + "outputs": [], + "payable": false, + "type": "function" + }, { "constant": false, "inputs": [