aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/interface/ABI.cpp
blob: aefb34afa848489e8ef27e351e62cad5e92d8555 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
        This file is part of solidity.

        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.

        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.

        You should have received a copy of the GNU General Public License
        along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * Utilities to handle the Contract ABI (https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI)
 */

#include <libsolidity/interface/ABI.h>
#include <libsolidity/ast/AST.h>

using namespace std;
using namespace dev;
using namespace dev::solidity;

Json::Value ABI::generate(ContractDefinition const& _contractDef)
{
    Json::Value abi(Json::arrayValue);

    for (auto it: _contractDef.interfaceFunctions())
    {
        auto externalFunctionType = it.second->interfaceFunctionType();
        solAssert(!!externalFunctionType, "");
        Json::Value method;
        method["type"] = "function";
        method["name"] = it.second->declaration().name();
        // TODO: deprecate constant in a future release
        method["constant"] = externalFunctionType->stateMutability() == StateMutability::Pure || it.second->stateMutability() == StateMutability::View;
        method["payable"] = externalFunctionType->isPayable();
        method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
        method["inputs"] = formatTypeList(
            externalFunctionType->parameterNames(),
            externalFunctionType->parameterTypes(),
            _contractDef.isLibrary()
        );
        method["outputs"] = formatTypeList(
            externalFunctionType->returnParameterNames(),
            externalFunctionType->returnParameterTypes(),
            _contractDef.isLibrary()
        );
        abi.append(method);
    }
    if (_contractDef.constructor())
    {
        auto externalFunctionType = FunctionType(*_contractDef.constructor(), false).interfaceFunctionType();
        solAssert(!!externalFunctionType, "");
        Json::Value method;
        method["type"] = "constructor";
        method["payable"] = externalFunctionType->isPayable();
        method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
        method["inputs"] = formatTypeList(
            externalFunctionType->parameterNames(),
            externalFunctionType->parameterTypes(),
            _contractDef.isLibrary()
        );
        abi.append(method);
    }
    if (_contractDef.fallbackFunction())
    {
        auto externalFunctionType = FunctionType(*_contractDef.fallbackFunction(), false).interfaceFunctionType();
        solAssert(!!externalFunctionType, "");
        Json::Value method;
        method["type"] = "fallback";
        method["payable"] = externalFunctionType->isPayable();
        method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
        abi.append(method);
    }
    for (auto const& it: _contractDef.interfaceEvents())
    {
        Json::Value event;
        event["type"] = "event";
        event["name"] = it->name();
        event["anonymous"] = it->isAnonymous();
        Json::Value params(Json::arrayValue);
        for (auto const& p: it->parameters())
        {
            auto type = p->annotation().type->interfaceType(false);
            solAssert(type, "");
            Json::Value input;
            auto param = formatType(p->name(), *type, false);
            param["indexed"] = p->isIndexed();
            params.append(param);
        }
        event["inputs"] = params;
        abi.append(event);
    }

    return abi;
}

Json::Value ABI::formatTypeList(
    vector<string> const& _names,
    vector<TypePointer> const& _types,
    bool _forLibrary
)
{
    Json::Value params(Json::arrayValue);
    solAssert(_names.size() == _types.size(), "Names and types vector size does not match");
    for (unsigned i = 0; i < _names.size(); ++i)
    {
        solAssert(_types[i], "");
        params.append(formatType(_names[i], *_types[i], _forLibrary));
    }
    return params;
}

Json::Value ABI::formatType(string const& _name, Type const& _type, bool _forLibrary)
{
    Json::Value ret;
    ret["name"] = _name;
    string suffix = (_forLibrary && _type.dataStoredIn(DataLocation::Storage)) ? " storage" : "";
    if (_type.isValueType() || (_forLibrary && _type.dataStoredIn(DataLocation::Storage)))
        ret["type"] = _type.canonicalName() + suffix;
    else if (ArrayType const* arrayType = dynamic_cast<ArrayType const*>(&_type))
    {
        if (arrayType->isByteArray())
            ret["type"] = _type.canonicalName() + suffix;
        else
        {
            string suffix;
            if (arrayType->isDynamicallySized())
                suffix = "[]";
            else
                suffix = string("[") + arrayType->length().str() + "]";
            solAssert(arrayType->baseType(), "");
            Json::Value subtype = formatType("", *arrayType->baseType(), _forLibrary);
            if (subtype.isMember("components"))
            {
                ret["type"] = subtype["type"].asString() + suffix;
                ret["components"] = subtype["components"];
            }
            else
                ret["type"] = subtype["type"].asString() + suffix;
        }
    }
    else if (StructType const* structType = dynamic_cast<StructType const*>(&_type))
    {
        ret["type"] = "tuple";
        ret["components"] = Json::arrayValue;
        for (auto const& member: structType->members(nullptr))
        {
            solAssert(member.type, "");
            auto t = member.type->interfaceType(_forLibrary);
            solAssert(t, "");
            ret["components"].append(formatType(member.name, *t, _forLibrary));
        }
    }
    else
        solAssert(false, "Invalid type.");
    return ret;
}