Skip to content

Commit ca06466

Browse files
committed
value as optional parameter
1 parent 2035f08 commit ca06466

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

Example/web3swiftBrowser/web3swiftBrowser/ViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class ViewController: BrowserViewController {
2222
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
2323
])
2424

25-
webView.load(URLRequest(url: URL(string: "https://app.compound.finance")!))
25+
26+
let urlToOpen = "https://app.compound.finance"
27+
webView.load(URLRequest(url: URL(string: urlToOpen)!))
2628

2729
do {
2830
let userDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

Sources/web3swift/Transaction/EthereumTransaction.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ public struct EthereumTransaction: CustomStringConvertible {
1414
public var nonce: BigUInt
1515
public var gasPrice: BigUInt = BigUInt(0)
1616
public var gasLimit: BigUInt = BigUInt(0)
17+
// The destination address of the message, left undefined for a contract-creation transaction.
1718
public var to: EthereumAddress
18-
public var value: BigUInt
19+
// (optional) The value transferred for the transaction in wei, also the endowment if it’s a contract-creation transaction.
20+
public var value: BigUInt?
1921
public var data: Data
2022
public var v: BigUInt = BigUInt(1)
2123
public var r: BigUInt = BigUInt(0)
@@ -87,8 +89,8 @@ public struct EthereumTransaction: CustomStringConvertible {
8789
toReturn = toReturn + "Nonce: " + String(self.nonce) + "\n"
8890
toReturn = toReturn + "Gas price: " + String(self.gasPrice) + "\n"
8991
toReturn = toReturn + "Gas limit: " + String(describing: self.gasLimit) + "\n"
90-
toReturn = toReturn + "To: " + self.to.address + "\n"
91-
toReturn = toReturn + "Value: " + String(self.value) + "\n"
92+
toReturn = toReturn + "To: " + self.to.address + "\n"
93+
toReturn = toReturn + "Value: " + String(self.value ?? "nil") + "\n"
9294
toReturn = toReturn + "Data: " + self.data.toHexString().addHexPrefix().lowercased() + "\n"
9395
toReturn = toReturn + "v: " + String(self.v) + "\n"
9496
toReturn = toReturn + "r: " + String(self.r) + "\n"
@@ -193,7 +195,7 @@ public struct EthereumTransaction: CustomStringConvertible {
193195
params.gas = gasEncoding?.toHexString().addHexPrefix().stripLeadingZeroes()
194196
let gasPriceEncoding = self.gasPrice.abiEncode(bits: 256)
195197
params.gasPrice = gasPriceEncoding?.toHexString().addHexPrefix().stripLeadingZeroes()
196-
let valueEncoding = self.value.abiEncode(bits: 256)
198+
let valueEncoding = self.value?.abiEncode(bits: 256)
197199
params.value = valueEncoding?.toHexString().addHexPrefix().stripLeadingZeroes()
198200
if (self.data != Data()) {
199201
params.data = self.data.toHexString().addHexPrefix()
@@ -310,8 +312,11 @@ public extension EthereumTransaction {
310312
self.gasLimit = BigUInt(UInt64(21000))
311313
}
312314
}
313-
314-
self.value = merged.value!
315+
316+
if let value = merged.value {
317+
self.value = value
318+
}
319+
315320
self.to = to
316321
self.data = data
317322
}

Sources/web3swift/Web3/Web3+Options.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public protocol TransactionOptionsInheritable {
1616
public struct TransactionOptions {
1717
/// Sets the transaction destination. It can either be a contract address or a private key controlled wallet address.
1818
///
19-
/// Usually should never be nil.
19+
/// Usually should never be nil, left undefined for a contract-creation transaction.
2020
public var to: EthereumAddress? = nil
2121
/// Sets from what account a transaction should be sent. Used only internally as the sender of Ethereum transaction
2222
/// is determined purely from the transaction signature. Indicates to the Ethereum node or to the local keystore what private key
@@ -40,6 +40,7 @@ public struct TransactionOptions {
4040
}
4141
public var gasPrice: GasPricePolicy?
4242

43+
/// The value transferred for the transaction in wei, also the endowment if it’s a contract-creation transaction.
4344
public var value: BigUInt? = nil
4445

4546
public enum NoncePolicy {

0 commit comments

Comments
 (0)