Skip to content

Commit

Permalink
WA-36: Fixed bug when multiple function have same name (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmeissner authored and fmrsabino committed Oct 19, 2017
1 parent 493e6a0 commit fa21e47
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
50 changes: 29 additions & 21 deletions bivrost-abi-parser/src/main/kotlin/pm/gnosis/AbiParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class AbiParser {
build.writeTo(output)
}
}

companion object {
const val VARIABLE_NAME_ITEM_DECODER = "itemDecoder"
}
Expand Down Expand Up @@ -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<ParameterJson>): String =
"(${parameters.joinToString(",") {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
17 changes: 17 additions & 0 deletions sample/app/abi/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit fa21e47

Please sign in to comment.