json and solidity ABI generted for events

This commit is contained in:
arkpar 2015-01-31 14:41:11 +01:00
parent 6c8b5cabdc
commit d6d7c9219b
3 changed files with 53 additions and 9 deletions

17
AST.cpp
View File

@ -152,6 +152,23 @@ void ContractDefinition::checkIllegalOverrides() const
} }
} }
std::vector<ASTPointer<EventDefinition>> const& ContractDefinition::getInterfaceEvents() const
{
if (!m_interfaceEvents)
{
set<string> eventsSeen;
m_interfaceEvents.reset(new std::vector<ASTPointer<EventDefinition>>());
for (ContractDefinition const* contract: getLinearizedBaseContracts())
for (ASTPointer<EventDefinition> const& e: contract->getEvents())
if (eventsSeen.count(e->getName()) == 0)
{
eventsSeen.insert(e->getName());
m_interfaceEvents->push_back(e);
}
}
return *m_interfaceEvents;
}
vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getInterfaceFunctionList() const vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getInterfaceFunctionList() const
{ {
if (!m_interfaceFunctionList) if (!m_interfaceFunctionList)

2
AST.h
View File

@ -222,6 +222,7 @@ public:
std::vector<ASTPointer<ModifierDefinition>> const& getFunctionModifiers() const { return m_functionModifiers; } std::vector<ASTPointer<ModifierDefinition>> const& getFunctionModifiers() const { return m_functionModifiers; }
std::vector<ASTPointer<FunctionDefinition>> const& getDefinedFunctions() const { return m_definedFunctions; } std::vector<ASTPointer<FunctionDefinition>> const& getDefinedFunctions() const { return m_definedFunctions; }
std::vector<ASTPointer<EventDefinition>> const& getEvents() const { return m_events; } std::vector<ASTPointer<EventDefinition>> const& getEvents() const { return m_events; }
std::vector<ASTPointer<EventDefinition>> const& getInterfaceEvents() const;
virtual TypePointer getType(ContractDefinition const* m_currentContract) const override; virtual TypePointer getType(ContractDefinition const* m_currentContract) const override;
@ -257,6 +258,7 @@ private:
std::vector<ContractDefinition const*> m_linearizedBaseContracts; std::vector<ContractDefinition const*> m_linearizedBaseContracts;
mutable std::unique_ptr<std::vector<std::pair<FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList; mutable std::unique_ptr<std::vector<std::pair<FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList;
mutable std::unique_ptr<std::vector<ASTPointer<EventDefinition>>> m_interfaceEvents;
}; };
class InheritanceSpecifier: public ASTNode class InheritanceSpecifier: public ASTNode

View File

@ -37,16 +37,11 @@ std::unique_ptr<std::string> InterfaceHandler::getDocumentation(ContractDefiniti
std::unique_ptr<std::string> InterfaceHandler::getABIInterface(ContractDefinition const& _contractDef) std::unique_ptr<std::string> InterfaceHandler::getABIInterface(ContractDefinition const& _contractDef)
{ {
Json::Value methods(Json::arrayValue); Json::Value abi(Json::arrayValue);
for (auto const& it: _contractDef.getInterfaceFunctions()) for (auto const& it: _contractDef.getInterfaceFunctions())
{ {
Json::Value method; Json::Value method;
Json::Value inputs(Json::arrayValue); auto populateParameters = [](vector<string> const& _paramNames, vector<string> const& _paramTypes)
Json::Value outputs(Json::arrayValue);
auto populateParameters = [](vector<string> const& _paramNames,
vector<string> const& _paramTypes)
{ {
Json::Value params(Json::arrayValue); Json::Value params(Json::arrayValue);
solAssert(_paramNames.size() == _paramTypes.size(), "Names and types vector size does not match"); solAssert(_paramNames.size() == _paramTypes.size(), "Names and types vector size does not match");
@ -66,9 +61,28 @@ std::unique_ptr<std::string> InterfaceHandler::getABIInterface(ContractDefinitio
it.second->getParameterTypeNames()); it.second->getParameterTypeNames());
method["outputs"] = populateParameters(it.second->getReturnParameterNames(), method["outputs"] = populateParameters(it.second->getReturnParameterNames(),
it.second->getReturnParameterTypeNames()); it.second->getReturnParameterTypeNames());
methods.append(method); abi.append(method);
} }
return std::unique_ptr<std::string>(new std::string(m_writer.write(methods)));
for (auto const& it: _contractDef.getInterfaceEvents())
{
Json::Value event;
event["type"] = "event";
event["name"] = it->getName();
Json::Value params(Json::arrayValue);
for (auto const& p: it->getParameters())
{
Json::Value input;
input["name"] = p->getName();
input["type"] = p->getType()->toString();
input["indexed"] = p->isIndexed();
params.append(input);
}
event["inputs"] = params;
abi.append(event);
}
return std::unique_ptr<std::string>(new std::string(m_writer.write(abi)));
} }
unique_ptr<string> InterfaceHandler::getABISolidityInterface(ContractDefinition const& _contractDef) unique_ptr<string> InterfaceHandler::getABISolidityInterface(ContractDefinition const& _contractDef)
@ -94,6 +108,17 @@ unique_ptr<string> InterfaceHandler::getABISolidityInterface(ContractDefinition
ret.pop_back(); ret.pop_back();
ret += "{}"; ret += "{}";
} }
for (auto const& it: _contractDef.getInterfaceEvents())
{
std::string params;
for (auto const& p: it->getParameters())
params += (params.empty() ? "(" : ",") + p->getType()->toString() + (p->isIndexed() ? " indexed " : " ") + p->getName();
if (!params.empty())
params += ")";
ret += "event " + it->getName() + params + ";";
}
return unique_ptr<string>(new string(ret + "}")); return unique_ptr<string>(new string(ret + "}"));
} }