I already has some working code samples for a dummy smart contract. No doubt you can adapt them fairly easily to your case.
Here is my dummy smart contract:
pragma solidity ^0.4.18;
contract AdditionContract {
uint public state = 0;
function add(uint value1, uint value2) public {
state = value1 + value2;
}
function getState() public constant returns (uint) {
return state;
}
}
and some python code to parse the abi and compute your function selectors (compile your smart contract with truffle to create the abi, or with solcjs)
### parse your abi from truffle
import json
truffleFile = json.load(open('/path/to/your/truffle/workspace' + '/build/contracts/AdditionContract.json'))
abi = truffleFile['abi']
functionSelector = []
for fields in abi:
if(fields['type'] == 'function'):
functionName = fields['name']
print(functionName)
functionInputs = []
for inputs in fields['inputs']:
functionInputs.append(inputs['type'])
print(inputs)
functionSelector.append([functionName, functionInputs])
print(functionSelector)
### prepare the data field of the transaction
# function selector and argument encoding
# https://solidity.readthedocs.io/en/develop/abi-spec.html#function-selector-and-argument-encoding
import web3 # Release 4.0.0-beta.9
w3 = web3.Web3()
value1, value2 = 10, 32 # your input value here
function = 'add(uint256,uint256)'
methodId = w3.sha3(text=function)[0:4].hex()
param1 = (value1).to_bytes(32, byteorder='big').hex()
param2 = (value2).to_bytes(32, byteorder='big').hex()
data = '0x' + methodId + param1 + param2
print(data)
### example of transaction dict
transaction_dict = {'from':myAddress,
'to':myContractAddress,
'chainId':CHAINID,
'gasPrice':myGasPrice,
'nonce':myNonce,
'data':data}
terminal prints :
getState
add
{'name': 'value1', 'type': 'uint256'}
{'name': 'value2', 'type': 'uint256'}
state
[['getState', []], ['add', ['uint256', 'uint256']], ['state', []]]
0x771602f7000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000020
if you want rock solid parsing I suggest that you give a look at the web3py source code (or web3js).
UPDATE
I don't know how much you know about python so I give you the way to build "add(uint256,uint256)" from the code above. But please also spend some time trying as this is fairly straightforward...
for functions in functionSelector:
functionName = functions[0]
functionType = functions[1]
s = functionName + '('
for i in range(len(functionType)):
s += functionType[i]
if(i+1 < len(functionType)):
s += ','
s += ')'
print(s)
prints:
getState()
add(uint256,uint256)
state()