2017-05-10 09:54:23 +00:00
|
|
|
/*
|
2019-02-13 15:56:46 +00:00
|
|
|
This file is part of solidity.
|
2017-05-10 09:54:23 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2017-05-10 09:54:23 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2017-05-10 09:54:23 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-10 09:54:23 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-05-10 09:54:23 +00:00
|
|
|
/**
|
2020-11-18 14:20:34 +00:00
|
|
|
* Utilities to handle the Contract ABI (https://docs.soliditylang.org/en/develop/abi-spec.html)
|
2017-05-10 09:54:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/interface/ABI.h>
|
2018-12-17 18:24:42 +00:00
|
|
|
|
2017-05-10 09:54:23 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::frontend;
|
2017-05-10 09:54:23 +00:00
|
|
|
|
2019-03-05 13:55:04 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool anyDataStoredInStorage(TypePointers const& _pointers)
|
|
|
|
{
|
2021-03-22 16:12:05 +00:00
|
|
|
for (Type const* pointer: _pointers)
|
2019-03-05 13:55:04 +00:00
|
|
|
if (pointer->dataStoredIn(DataLocation::Storage))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 09:54:23 +00:00
|
|
|
Json::Value ABI::generate(ContractDefinition const& _contractDef)
|
|
|
|
{
|
2019-09-03 15:55:00 +00:00
|
|
|
auto compare = [](Json::Value const& _a, Json::Value const& _b) -> bool {
|
|
|
|
return make_tuple(_a["type"], _a["name"]) < make_tuple(_b["type"], _b["name"]);
|
|
|
|
};
|
|
|
|
multiset<Json::Value, decltype(compare)> abi(compare);
|
2017-05-10 09:54:23 +00:00
|
|
|
|
|
|
|
for (auto it: _contractDef.interfaceFunctions())
|
|
|
|
{
|
2019-07-02 14:04:52 +00:00
|
|
|
if (_contractDef.isLibrary() && (
|
|
|
|
it.second->stateMutability() > StateMutability::View ||
|
|
|
|
anyDataStoredInStorage(it.second->parameterTypes() + it.second->returnParameterTypes())
|
|
|
|
))
|
2019-03-05 13:55:04 +00:00
|
|
|
continue;
|
|
|
|
|
2019-07-02 14:04:52 +00:00
|
|
|
FunctionType const* externalFunctionType = it.second->interfaceFunctionType();
|
2017-08-25 09:39:53 +00:00
|
|
|
solAssert(!!externalFunctionType, "");
|
2021-09-15 18:09:03 +00:00
|
|
|
Json::Value method{Json::objectValue};
|
2017-05-10 09:54:23 +00:00
|
|
|
method["type"] = "function";
|
|
|
|
method["name"] = it.second->declaration().name();
|
2017-08-25 09:39:53 +00:00
|
|
|
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
|
2017-05-10 09:54:23 +00:00
|
|
|
method["inputs"] = formatTypeList(
|
|
|
|
externalFunctionType->parameterNames(),
|
|
|
|
externalFunctionType->parameterTypes(),
|
2019-07-02 14:04:52 +00:00
|
|
|
it.second->parameterTypes(),
|
2017-05-10 09:54:23 +00:00
|
|
|
_contractDef.isLibrary()
|
|
|
|
);
|
|
|
|
method["outputs"] = formatTypeList(
|
|
|
|
externalFunctionType->returnParameterNames(),
|
|
|
|
externalFunctionType->returnParameterTypes(),
|
2019-07-02 14:04:52 +00:00
|
|
|
it.second->returnParameterTypes(),
|
2017-05-10 09:54:23 +00:00
|
|
|
_contractDef.isLibrary()
|
|
|
|
);
|
2019-09-03 15:55:00 +00:00
|
|
|
abi.emplace(std::move(method));
|
2017-05-10 09:54:23 +00:00
|
|
|
}
|
2020-05-07 03:33:08 +00:00
|
|
|
FunctionDefinition const* constructor = _contractDef.constructor();
|
2020-06-10 16:19:42 +00:00
|
|
|
if (constructor && !_contractDef.abstract())
|
2017-05-10 09:54:23 +00:00
|
|
|
{
|
2020-05-07 03:33:08 +00:00
|
|
|
FunctionType constrType(*constructor);
|
2019-07-02 14:04:52 +00:00
|
|
|
FunctionType const* externalFunctionType = constrType.interfaceFunctionType();
|
2017-08-25 09:39:53 +00:00
|
|
|
solAssert(!!externalFunctionType, "");
|
2021-09-15 18:09:03 +00:00
|
|
|
Json::Value method{Json::objectValue};
|
2017-05-10 09:54:23 +00:00
|
|
|
method["type"] = "constructor";
|
2017-08-25 09:39:53 +00:00
|
|
|
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
|
2017-05-10 09:54:23 +00:00
|
|
|
method["inputs"] = formatTypeList(
|
2017-08-25 09:39:53 +00:00
|
|
|
externalFunctionType->parameterNames(),
|
|
|
|
externalFunctionType->parameterTypes(),
|
2019-07-02 14:04:52 +00:00
|
|
|
constrType.parameterTypes(),
|
2017-05-10 09:54:23 +00:00
|
|
|
_contractDef.isLibrary()
|
|
|
|
);
|
2019-09-03 15:55:00 +00:00
|
|
|
abi.emplace(std::move(method));
|
2017-05-10 09:54:23 +00:00
|
|
|
}
|
2019-09-09 16:22:02 +00:00
|
|
|
for (auto const* fallbackOrReceive: {_contractDef.fallbackFunction(), _contractDef.receiveFunction()})
|
|
|
|
if (fallbackOrReceive)
|
|
|
|
{
|
2020-01-07 10:42:37 +00:00
|
|
|
auto const* externalFunctionType = FunctionType(*fallbackOrReceive).interfaceFunctionType();
|
2019-09-09 16:22:02 +00:00
|
|
|
solAssert(!!externalFunctionType, "");
|
2021-09-15 18:09:03 +00:00
|
|
|
Json::Value method{Json::objectValue};
|
2019-09-09 16:22:02 +00:00
|
|
|
method["type"] = TokenTraits::toString(fallbackOrReceive->kind());
|
|
|
|
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
|
|
|
|
abi.emplace(std::move(method));
|
|
|
|
}
|
2021-02-23 13:13:52 +00:00
|
|
|
for (auto const& it: _contractDef.interfaceEvents())
|
2017-05-10 09:54:23 +00:00
|
|
|
{
|
2021-09-15 18:09:03 +00:00
|
|
|
Json::Value event{Json::objectValue};
|
2017-05-10 09:54:23 +00:00
|
|
|
event["type"] = "event";
|
|
|
|
event["name"] = it->name();
|
|
|
|
event["anonymous"] = it->isAnonymous();
|
2021-09-15 18:09:03 +00:00
|
|
|
Json::Value params{Json::arrayValue};
|
2017-05-10 09:54:23 +00:00
|
|
|
for (auto const& p: it->parameters())
|
|
|
|
{
|
2019-07-02 14:04:52 +00:00
|
|
|
Type const* type = p->annotation().type->interfaceType(false);
|
|
|
|
solAssert(type, "");
|
|
|
|
auto param = formatType(p->name(), *type, *p->annotation().type, false);
|
2017-01-24 11:44:49 +00:00
|
|
|
param["indexed"] = p->isIndexed();
|
2019-07-02 14:04:52 +00:00
|
|
|
params.append(std::move(param));
|
2017-05-10 09:54:23 +00:00
|
|
|
}
|
2019-07-02 14:04:52 +00:00
|
|
|
event["inputs"] = std::move(params);
|
2019-09-03 15:55:00 +00:00
|
|
|
abi.emplace(std::move(event));
|
2017-05-10 09:54:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 11:34:58 +00:00
|
|
|
for (ErrorDefinition const* error: _contractDef.interfaceErrors())
|
|
|
|
{
|
2021-09-15 18:09:03 +00:00
|
|
|
Json::Value errorJson{Json::objectValue};
|
2021-02-02 11:34:58 +00:00
|
|
|
errorJson["type"] = "error";
|
|
|
|
errorJson["name"] = error->name();
|
|
|
|
errorJson["inputs"] = Json::arrayValue;
|
|
|
|
for (auto const& p: error->parameters())
|
|
|
|
{
|
|
|
|
Type const* type = p->annotation().type->interfaceType(false);
|
|
|
|
solAssert(type, "");
|
|
|
|
errorJson["inputs"].append(
|
|
|
|
formatType(p->name(), *type, *p->annotation().type, false)
|
|
|
|
);
|
|
|
|
}
|
2022-08-23 17:28:45 +00:00
|
|
|
abi.emplace(std::move(errorJson));
|
2021-02-02 11:34:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 15:55:00 +00:00
|
|
|
Json::Value abiJson{Json::arrayValue};
|
|
|
|
for (auto& f: abi)
|
|
|
|
abiJson.append(std::move(f));
|
|
|
|
return abiJson;
|
2017-05-10 09:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value ABI::formatTypeList(
|
|
|
|
vector<string> const& _names,
|
2021-03-22 16:12:05 +00:00
|
|
|
vector<Type const*> const& _encodingTypes,
|
|
|
|
vector<Type const*> const& _solidityTypes,
|
2017-05-10 09:54:23 +00:00
|
|
|
bool _forLibrary
|
|
|
|
)
|
|
|
|
{
|
2021-09-15 18:09:03 +00:00
|
|
|
Json::Value params{Json::arrayValue};
|
2019-07-02 14:04:52 +00:00
|
|
|
solAssert(_names.size() == _encodingTypes.size(), "Names and types vector size does not match");
|
|
|
|
solAssert(_names.size() == _solidityTypes.size(), "");
|
2017-05-10 09:54:23 +00:00
|
|
|
for (unsigned i = 0; i < _names.size(); ++i)
|
|
|
|
{
|
2019-07-02 14:04:52 +00:00
|
|
|
solAssert(_encodingTypes[i], "");
|
|
|
|
params.append(formatType(_names[i], *_encodingTypes[i], *_solidityTypes[i], _forLibrary));
|
2017-05-10 09:54:23 +00:00
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
2017-01-24 11:44:49 +00:00
|
|
|
|
2019-07-02 14:04:52 +00:00
|
|
|
Json::Value ABI::formatType(
|
|
|
|
string const& _name,
|
|
|
|
Type const& _encodingType,
|
|
|
|
Type const& _solidityType,
|
|
|
|
bool _forLibrary
|
|
|
|
)
|
2017-01-24 11:44:49 +00:00
|
|
|
{
|
2021-09-15 18:09:03 +00:00
|
|
|
Json::Value ret{Json::objectValue};
|
2017-01-24 11:44:49 +00:00
|
|
|
ret["name"] = _name;
|
2019-07-02 14:04:52 +00:00
|
|
|
ret["internalType"] = _solidityType.toString(true);
|
|
|
|
string suffix = (_forLibrary && _encodingType.dataStoredIn(DataLocation::Storage)) ? " storage" : "";
|
|
|
|
if (_encodingType.isValueType() || (_forLibrary && _encodingType.dataStoredIn(DataLocation::Storage)))
|
|
|
|
ret["type"] = _encodingType.canonicalName() + suffix;
|
|
|
|
else if (ArrayType const* arrayType = dynamic_cast<ArrayType const*>(&_encodingType))
|
2017-01-24 11:44:49 +00:00
|
|
|
{
|
2022-01-28 17:57:05 +00:00
|
|
|
if (arrayType->isByteArrayOrString())
|
2019-07-02 14:04:52 +00:00
|
|
|
ret["type"] = _encodingType.canonicalName() + suffix;
|
2017-01-24 11:44:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
string suffix;
|
|
|
|
if (arrayType->isDynamicallySized())
|
|
|
|
suffix = "[]";
|
|
|
|
else
|
|
|
|
suffix = string("[") + arrayType->length().str() + "]";
|
|
|
|
solAssert(arrayType->baseType(), "");
|
2019-07-02 14:04:52 +00:00
|
|
|
Json::Value subtype = formatType(
|
|
|
|
"",
|
|
|
|
*arrayType->baseType(),
|
|
|
|
*dynamic_cast<ArrayType const&>(_solidityType).baseType(),
|
|
|
|
_forLibrary
|
|
|
|
);
|
2017-06-30 08:22:35 +00:00
|
|
|
if (subtype.isMember("components"))
|
2017-01-24 11:44:49 +00:00
|
|
|
{
|
2017-06-30 08:22:35 +00:00
|
|
|
ret["type"] = subtype["type"].asString() + suffix;
|
|
|
|
ret["components"] = subtype["components"];
|
2017-01-24 11:44:49 +00:00
|
|
|
}
|
2017-06-30 08:22:35 +00:00
|
|
|
else
|
|
|
|
ret["type"] = subtype["type"].asString() + suffix;
|
2017-01-24 11:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 14:04:52 +00:00
|
|
|
else if (StructType const* structType = dynamic_cast<StructType const*>(&_encodingType))
|
2017-01-24 11:44:49 +00:00
|
|
|
{
|
2017-09-01 11:37:40 +00:00
|
|
|
ret["type"] = "tuple";
|
2017-06-30 08:22:35 +00:00
|
|
|
ret["components"] = Json::arrayValue;
|
2017-01-24 11:44:49 +00:00
|
|
|
for (auto const& member: structType->members(nullptr))
|
|
|
|
{
|
|
|
|
solAssert(member.type, "");
|
2019-07-02 14:04:52 +00:00
|
|
|
Type const* t = member.type->interfaceType(_forLibrary);
|
|
|
|
solAssert(t, "");
|
|
|
|
ret["components"].append(formatType(member.name, *t, *member.type, _forLibrary));
|
2017-01-24 11:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(false, "Invalid type.");
|
|
|
|
return ret;
|
|
|
|
}
|