1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include "ParamBase.h"
10
#include "Parameters.h"
11
#include "Bytes.h"
12

13
#include "../../uint256.h"
14
#include "../../Hash.h"
15

16
#include <string>
17

18
namespace TW::Ethereum::ABI {
19

20
/// Non-generic version of Function, templated version is impossible to pass around to and back over C interface
21
/// (void* looses the temaplate parameters).
22 1
class Function {
23
public:
24
    std::string name;
25
    ParamSet _inParams;
26
    ParamSet _outParams;
27

28 1
    Function(std::string name) : name(std::move(name)) {}
29 1
    Function(std::string name, const std::vector<std::shared_ptr<ParamBase>>& inParams)
30 1
        : name(std::move(name)), _inParams(ParamSet(inParams)) {}
31 1
    virtual ~Function() {}
32
    /// Add an input parameter. Returns the index of the parameter.
33
    int addInParam(std::shared_ptr<ParamBase> param) {
34
        return _inParams.addParam(param);
35
    }
36
    /// Add an output parameter. Returns the index of the parameter.
37 1
    int addOutParam(std::shared_ptr<ParamBase> param) {
38 1
        return _outParams.addParam(param);
39
    }
40
    /// Add an input or output parameter. Returns the index of the parameter.
41 1
    int addParam(std::shared_ptr<ParamBase> param, bool isOutput = false) {
42 1
        return isOutput ? _outParams.addParam(param) : _inParams.addParam(param);
43
    }
44
    /// Get an input parameter.
45 1
    bool getInParam(int paramIndex, std::shared_ptr<ParamBase>& param_out) {
46 1
        return _inParams.getParam(paramIndex, param_out);
47
    }
48
    /// Get an output parameter.
49 1
    bool getOutParam(int paramIndex, std::shared_ptr<ParamBase>& param_out) {
50 1
        return _outParams.getParam(paramIndex, param_out);
51
    }
52
    /// Get an input or output parameter.
53 1
    bool getParam(int paramIndex, std::shared_ptr<ParamBase>& param_out, bool isOutput = false) {
54 1
        return isOutput ? _outParams.getParam(paramIndex, param_out) : _inParams.getParam(paramIndex, param_out);
55
    }
56
    /// Return the function type signature, of the form "baz(int32,uint256)"
57 1
    std::string getType() const {
58 1
        return name + _inParams.getType();
59
    }
60

61
    /// Return the 4-byte function signature
62
    Data getSignature() const;
63

64
    virtual void encode(Data& data) const;
65

66
    /// Decode binary, fill output parameters
67
    bool decodeOutput(const Data& encoded, size_t& offset_inout);
68
    /// Decode binary, fill input parameters
69
    bool decodeInput(const Data& encoded, size_t& offset_inout);
70
};
71

72
inline void encode(const Function& func, Data& data) {
73
    func.encode(data);
74
}
75

76
} // namespace TW::Ethereum::ABI
77

78
/// Wrapper for C interface.
79 1
struct TWEthereumAbiFunction {
80
    TW::Ethereum::ABI::Function impl;
81
};

Read our documentation on viewing source code .

Loading