@@ -10,26 +10,31 @@ import Foundation
10
10
import BigInt
11
11
import Core
12
12
13
+ /// Standard representation of a smart contract.
13
14
public protocol ContractProtocol {
15
+ /// Address of the referenced smart contract. Can be set later, e.g. if the contract is deploying and address is not yet known.
14
16
var address : EthereumAddress ? { get set }
15
17
16
18
/// All ABI elements like: events, functions, constructors and errors.
17
19
var abi : [ ABI . Element ] { get }
18
20
19
- /// Functions filtered from `abi`.
20
- /// Contains methods mapped to function name, like `getData`,
21
- /// name with input parameters `getData(bytes32)` and 4 bytes signature `0xffffffff` (expected to be lowercased).
21
+ /// Functions filtered from ``abi``.
22
+ /// Functions are mapped to:
23
+ /// - name, like `getData` that is defined in ``ABI/Element/Function/name``;
24
+ /// - name with input parameters that is a combination of ``ABI/Element/Function/name`` and
25
+ /// ``ABI/Element/Function/inputs``, e.g. `getData(bytes32)`;
26
+ /// - and 4 bytes signature `0xffffffff` (expected to be lowercased).
22
27
/// The mapping by name (e.g. `getData`) is the one most likely expected to return arrays with
23
28
/// more than one entry due to the fact that solidity allows method overloading.
24
29
var methods : [ String : [ ABI . Element . Function ] ] { get }
25
30
26
- /// All entries from `methods`.
31
+ /// All values from `` methods` `.
27
32
var allMethods : [ ABI . Element . Function ] { get }
28
33
29
- /// Events filtered from `abi`.
34
+ /// Events filtered from `` abi`` and mapped to their unchanged ``ABI/Element/Event/name` `.
30
35
var events : [ String : ABI . Element . Event ] { get }
31
36
32
- /// All events from `events`.
37
+ /// All values from `` events` `.
33
38
var allEvents : [ ABI . Element . Event ] { get }
34
39
35
40
/// Errors filtered from ``abi`` and mapped to their unchanged ``ABI/Element/EthError/name``.
@@ -50,7 +55,64 @@ public protocol ContractProtocol {
50
55
/// new Solidity keywords, types etc. that are not yet supported, etc.
51
56
init ( _ abiString: String , at: EthereumAddress ? ) throws
52
57
53
- /// Creates transaction to deploy a smart contract.
58
+ /// Creates a smart contract deployment transaction.
59
+ ///
60
+ /// ## How to
61
+ /// To create a smart contract deployment transaction there is only one requirement - `bytecode`.
62
+ /// That is the compiled smart contract that is ready to be executed by EVM, or eWASM if that is a Serenity.
63
+ /// Creating a transaction is as simple as:
64
+ ///
65
+ /// ```swift
66
+ /// contractInstance.deploy(bytecode: smartContractBytecode)
67
+ /// ```
68
+ ///
69
+ /// One of the default implementations of `ContractProtocol` is ``EthereumContract``.
70
+ /// ```swift
71
+ /// let contract = EthereumContract(abi: [])
72
+ /// contract.deploy(bytecode: smartContractBytecode)
73
+ /// ```
74
+ ///
75
+ /// ### Setting constructor arguments
76
+ /// Some smart contracts expect input arguments for a constructor that is called on contract deployment.
77
+ /// To set these input arguments you must provide `constructor` and `parameters`.
78
+ /// Constructor can be statically created if you know upfront what are the input arguments and their exact order:
79
+ ///
80
+ /// ```swift
81
+ /// let inputArgsTypes: [ABI.Element.InOut] = [.init(name: "firstArgument", type: ABI.Element.ParameterType.string),
82
+ /// .init(name: "secondArgument", type: ABI.Element.ParameterType.uint(bits: 256))]
83
+ /// let constructor = ABI.Element.Constructor(inputs: inputArgsTypes, constant: false, payable: payable)
84
+ /// let constructorArguments = ["This is the array of constructor arguments", 10_000] as [AnyObject]
85
+ ///
86
+ /// contract.deploy(bytecode: smartContractBytecode,
87
+ /// constructor: constructor,
88
+ /// parameters: constructorArguments)
89
+ /// ```
90
+ ///
91
+ /// Alternatively, if you have ABI string that holds meta data about the constructor you can use it instead of creating constructor manually.
92
+ /// But you must make sure the arguments for constructor call are of expected type in and correct order.
93
+ /// Example of ABI string can be found in ``Web3/Utils/erc20ABI``.
94
+ ///
95
+ /// ```swift
96
+ /// let contract = EthereumContract(abiString)
97
+ /// let constructorArguments = ["This is the array of constructor arguments", 10_000] as [AnyObject]
98
+ ///
99
+ /// contract.deploy(bytecode: smartContractBytecode,
100
+ /// constructor: contract.constructor,
101
+ /// parameters: constructorArguments)
102
+ /// ```
103
+ ///
104
+ /// ⚠️ If you pass in only constructor or only parameters - it will have no effect on the final transaction object.
105
+ /// Also, you have an option to set any extra bytes at the end of ``EthereumTransaction/data`` attribute.
106
+ /// Alternatively you can encode constructor parameters outside of the deploy function and only set `extraData` to pass in these
107
+ /// parameters:
108
+ ///
109
+ /// ```swift
110
+ /// // `encodeParameters` call returns `Data?`. Check it for nullability before calling `deploy`
111
+ /// // function to create `EthereumTransaction`.
112
+ /// let encodedConstructorArguments = someConstructor.encodeParameters(arrayOfInputArguments)
113
+ /// constructor.deploy(bytecode: smartContractBytecode, extraData: encodedConstructorArguments)
114
+ /// ```
115
+ ///
54
116
/// - Parameters:
55
117
/// - bytecode: bytecode to deploy.
56
118
/// - constructor: constructor of the smart contract bytecode is related to. Used to encode `parameters`.
@@ -104,11 +166,19 @@ public protocol ContractProtocol {
104
166
func decodeInputData( _ data: Data ) -> [ String : Any ] ?
105
167
106
168
/// Attempts to parse given event based on the data from `allEvents`, or in other words based on the given smart contract ABI.
107
- func parseEvent( _ eventLog: EventLog ) -> ( eventName: String ? , eventData: [ String : Any ] ? )
169
+ func parseEvent( _ eventLog: EventLog ) -> ( eventName: String ? , eventData: [ String : Any ] ? )
108
170
171
+ /// Tests for probable presence of an event with `eventName` in a given bloom filter.
172
+ /// - Parameters:
173
+ /// - eventName: event name like `ValueReceived`.
174
+ /// - bloom: bloom filter.
175
+ /// - Returns: `true` if event is possibly present, `false` if definitely not present and `nil` if event with given name
176
+ /// is not part of the ``EthereumContract/abi``.
109
177
func testBloomForEventPresence( eventName: String , bloom: EthereumBloomFilter ) -> Bool ?
110
178
}
111
179
180
+ // MARK: - Overloaded ContractProtocol's functions
181
+
112
182
extension ContractProtocol {
113
183
114
184
/// Overloading of ``ContractProtocol/deploy(bytecode:constructor:parameters:extraData:)`` to allow
@@ -119,10 +189,10 @@ extension ContractProtocol {
119
189
constructor: ABI . Element . Constructor ? = nil ,
120
190
parameters: [ AnyObject ] ? = nil ,
121
191
extraData: Data ? = nil ) -> EthereumTransaction ? {
122
- return deploy ( bytecode: bytecode,
123
- constructor: constructor,
124
- parameters: parameters,
125
- extraData: extraData)
192
+ deploy ( bytecode: bytecode,
193
+ constructor: constructor,
194
+ parameters: parameters,
195
+ extraData: extraData)
126
196
}
127
197
128
198
/// Overloading of ``ContractProtocol/method(_:parameters:extraData:)`` to allow
@@ -132,7 +202,7 @@ extension ContractProtocol {
132
202
func method( _ method: String = " fallback " ,
133
203
parameters: [ AnyObject ] ? = nil ,
134
204
extraData: Data ? = nil ) -> EthereumTransaction ? {
135
- return self . method ( method, parameters: parameters ?? [ ] , extraData: extraData)
205
+ self . method ( method, parameters: parameters ?? [ ] , extraData: extraData)
136
206
}
137
207
138
208
func decodeInputData( _ data: Data ) -> [ String : Any ] ? {
0 commit comments