Skip to content

Commit 3aaaf48

Browse files
Contract protocol methods now returning Data instead of CodableTransaction,
because it overhead to return whole transaction struct when it responsible for only filling data in it.
1 parent 64da360 commit 3aaaf48

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

Sources/Core/Contract/ContractProtocol.swift

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,19 @@ public protocol ContractProtocol {
110110
/// new Solidity keywords, types etc. that are not yet supported, etc.
111111
init(_ abiString: String, at: EthereumAddress?) throws
112112

113-
/// Creates a smart contract deployment transaction.
113+
/// Prepare transaction data for smart contract deployment transaction.
114114
///
115115
/// - Parameters:
116116
/// - bytecode: bytecode to deploy.
117117
/// - constructor: constructor of the smart contract bytecode is related to. Used to encode `parameters`.
118118
/// - parameters: parameters for `constructor`.
119119
/// - extraData: any extra data. It can be encoded input arguments for a constuctor but then you should set `constructor` and
120120
/// `parameters` to be `nil`.
121-
/// - Returns: transaction if given `parameters` were successfully encoded in a `constructor`. If any or both are `nil`
122-
/// then no encoding will take place and a transaction with `bytecode + extraData` will be returned.
121+
/// - Returns: Encoded data for a given parameters, which is should be assigned to ``CodableTransaction.data`` property
123122
func deploy(bytecode: Data,
124123
constructor: ABI.Element.Constructor?,
125124
parameters: [AnyObject]?,
126-
extraData: Data?) -> CodableTransaction?
125+
extraData: Data?) -> Data?
127126

128127
/// Creates function call transaction with data set as `method` encoded with given `parameters`.
129128
/// The `method` must be part of the ABI used to init this contract.
@@ -187,7 +186,7 @@ extension ContractProtocol {
187186
func deploy(_ bytecode: Data,
188187
constructor: ABI.Element.Constructor? = nil,
189188
parameters: [AnyObject]? = nil,
190-
extraData: Data? = nil) -> CodableTransaction? {
189+
extraData: Data? = nil) -> Data? {
191190
deploy(bytecode: bytecode,
192191
constructor: constructor,
193192
parameters: parameters,
@@ -200,7 +199,7 @@ extension ContractProtocol {
200199
/// See ``ContractProtocol/method(_:parameters:extraData:)`` for details.
201200
func method(_ method: String = "fallback",
202201
parameters: [AnyObject]? = nil,
203-
extraData: Data? = nil) -> CodableTransaction? {
202+
extraData: Data? = nil) -> Data? {
204203
self.method(method, parameters: parameters ?? [], extraData: extraData)
205204
}
206205

@@ -219,15 +218,14 @@ extension DefaultContractProtocol {
219218
public func deploy(bytecode: Data,
220219
constructor: ABI.Element.Constructor?,
221220
parameters: [AnyObject]?,
222-
extraData: Data?) -> CodableTransaction? {
221+
extraData: Data?) -> Data? {
223222
var fullData = bytecode
224223

225224
if let constructor = constructor,
226225
let parameters = parameters,
227226
!parameters.isEmpty {
228227
guard constructor.inputs.count == parameters.count,
229-
let encodedData = constructor.encodeParameters(parameters)
230-
else {
228+
let encodedData = constructor.encodeParameters(parameters) else {
231229
NSLog("Constructor encoding will fail as the number of input arguments doesn't match the number of given arguments.")
232230
return nil
233231
}
@@ -239,12 +237,7 @@ extension DefaultContractProtocol {
239237
}
240238

241239
// MARK: Writing Data flow
242-
return CodableTransaction(to: .contractDeploymentAddress(),
243-
value: BigUInt(0),
244-
data: fullData
245-
// FIXME: Return parameters
246-
// parameters: CodableTransaction()
247-
)
240+
return fullData
248241
}
249242

250243
/// Call given contract method with given parameters

Sources/web3swift/Web3/Web3+Contract.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,20 @@ extension web3 {
5757
extraData: Data? = nil,
5858
transactionOptions: CodableTransaction? = nil) -> WriteOperation? {
5959
// MARK: Writing Data flow
60-
guard var tx = self.contract.deploy(bytecode: bytecode,
60+
guard let data = self.contract.deploy(bytecode: bytecode,
6161
constructor: constructor,
6262
parameters: parameters,
6363
extraData: extraData)
6464
else { return nil }
6565

6666
if let network = self.web3.provider.network {
67-
tx.chainID = network.chainID
67+
transaction.chainID = network.chainID
6868
}
69+
70+
transaction.value = 0
71+
transaction.data = data
72+
transaction.to = .contractDeploymentAddress()
73+
6974
return WriteOperation(transaction: self.transaction,
7075
web3: self.web3,
7176
contract: self.contract)

0 commit comments

Comments
 (0)