Skip to content

Commit

Permalink
Merge pull request #123 from matter-labs/fix/removeDeprecated
Browse files Browse the repository at this point in the history
Fix/remove deprecated
  • Loading branch information
TonioMacaronio authored Mar 6, 2019
2 parents e86c10b + 095a51c commit ac752c2
Show file tree
Hide file tree
Showing 37 changed files with 276 additions and 1,727 deletions.
262 changes: 30 additions & 232 deletions web3swift.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion web3swift/Contract/Classes/EthereumContract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import EthereumAddress
import EthereumABI

public struct EthereumContract:ContractProtocol {
public var options: Web3Options? = Web3Options.defaultOptions()
public var transactionOptions: TransactionOptions? = TransactionOptions.defaultOptions
public var address: EthereumAddress? = nil

Expand Down
24 changes: 3 additions & 21 deletions web3swift/HookedFunctions/Classes/Web3+BrowserFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,7 @@ extension web3.BrowserFunctions {

public func sendTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> [String:Any]? {
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
guard let options = Web3Options.fromJSON(transactionJSON) else {return nil}
var transactionOptions = TransactionOptions()
transactionOptions.from = options.from
transactionOptions.to = options.to
transactionOptions.value = options.value
transactionOptions.gasLimit = options.gasLimit != nil ? .limited(options.gasLimit!) : .automatic
transactionOptions.gasPrice = options.gasPrice != nil ? .manual(options.gasPrice!) : .automatic
guard let transactionOptions = TransactionOptions.fromJSON(transactionJSON) else {return nil}
return self.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
}

Expand All @@ -89,13 +83,7 @@ extension web3.BrowserFunctions {

public func estimateGas(_ transactionJSON: [String: Any]) -> BigUInt? {
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
guard let options = Web3Options.fromJSON(transactionJSON) else {return nil}
var transactionOptions = TransactionOptions()
transactionOptions.from = options.from
transactionOptions.to = options.to
transactionOptions.value = options.value
transactionOptions.gasLimit = .automatic
transactionOptions.gasPrice = options.gasPrice != nil ? .manual(options.gasPrice!) : .automatic
guard let transactionOptions = TransactionOptions.fromJSON(transactionJSON) else {return nil}
return self.estimateGas(transaction, transactionOptions: transactionOptions)
}

Expand Down Expand Up @@ -134,13 +122,7 @@ extension web3.BrowserFunctions {

public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> String? {
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
guard let options = Web3Options.fromJSON(transactionJSON) else {return nil}
var transactionOptions = TransactionOptions()
transactionOptions.from = options.from
transactionOptions.to = options.to
transactionOptions.value = options.value
transactionOptions.gasLimit = options.gasLimit != nil ? .limited(options.gasLimit!) : .automatic
transactionOptions.gasPrice = options.gasPrice != nil ? .manual(options.gasPrice!) : .automatic
guard var transactionOptions = TransactionOptions.fromJSON(transactionJSON) else {return nil}
if let nonceString = transactionJSON["nonce"] as? String, let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) {
transactionOptions.nonce = .manual(nonce)
} else {
Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC1155/Web3+ERC1155.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ protocol IERC1155Metadata {

public class ERC1155: IERC1155 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _tokenId: BigUInt? = nil
private var _hasReadProperties: Bool = false

Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC1376/Web3+ERC1376.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ protocol IERC1376: IERC20 {

public class ERC1376: IERC1376 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
private var _decimals: UInt8? = nil
Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC1400/Web3+ERC1400.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ protocol IERC1400: IERC20 {
// can be imperatively read and saved
public class ERC1400: IERC1400 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
private var _decimals: UInt8? = nil
Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC1410/Web3+ERC1410.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ protocol IERC1410: IERC20 {

public class ERC1410: IERC1410 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
private var _decimals: UInt8? = nil
Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC1594/Web3+ERC1594.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ protocol IERC1594: IERC20 {

public class ERC1594: IERC1594 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
private var _decimals: UInt8? = nil
Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC1633/Web3+ERC1633.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ protocol IERC1633: IERC20, IERC165 {

public class ERC1633: IERC1633 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
private var _decimals: UInt8? = nil
Expand Down
2 changes: 0 additions & 2 deletions web3swift/PrecompiledContracts/ERC1643/Web3+ERC1643.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ protocol IERC1643: IERC20 {
}

public class ERC1643: IERC1643 {
@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
Expand Down
2 changes: 0 additions & 2 deletions web3swift/PrecompiledContracts/ERC1644/Web3+ERC1644.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ protocol IERC1644: IERC20 {
}

public class ERC1644: IERC1644 {
@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC20/Web3+ERC20.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ protocol IERC20 {
// can be imperatively read and saved
public class ERC20: IERC20 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
private var _decimals: UInt8? = nil
Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC721/Web3+ERC721.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ protocol IERC721Enumerable {
// can be imperatively read and saved
public class ERC721: IERC721 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _tokenId: BigUInt? = nil
private var _hasReadProperties: Bool = false

Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC721x/Web3+ERC721x.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ protocol IERC721x: IERC721, IERC721Metadata, IERC721Enumerable {

public class ERC721x: IERC721x {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _tokenId: BigUInt? = nil
private var _hasReadProperties: Bool = false

Expand Down
3 changes: 0 additions & 3 deletions web3swift/PrecompiledContracts/ERC888/Web3+ERC888.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ protocol IERC888 {

public class ERC888: IERC888 {

@available(*, deprecated, renamed: "transactionOptions")
public var options: Web3Options = .init()

private var _name: String? = nil
private var _symbol: String? = nil
private var _decimals: UInt8? = nil
Expand Down
107 changes: 107 additions & 0 deletions web3swift/Transaction/Classes/EthereumTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,110 @@ public struct EthereumTransaction: CustomStringConvertible {
return request
}
}

extension EthereumTransaction {
public init(to: EthereumAddress, data: Data, options: TransactionOptions) {
let defaults = TransactionOptions.defaultOptions
let merged = defaults.merge(options)
self.nonce = BigUInt(0)

if let gP = merged.gasPrice {
switch gP {
case .manual(let value):
self.gasPrice = value
default:
self.gasPrice = BigUInt("5000000000")!
}
}

if let gL = merged.gasLimit {
switch gL {
case .manual(let value):
self.gasLimit = value
default:
self.gasLimit = BigUInt(UInt64(21000))
}
}

self.value = merged.value!
self.to = to
self.data = data
}

public func mergedWithOptions(_ options: TransactionOptions) -> EthereumTransaction {
var tx = self;

if let gP = options.gasPrice {
switch gP {
case .manual(let value):
tx.gasPrice = value
default:
tx.gasPrice = BigUInt("5000000000")!
}
}

if let gL = options.gasLimit {
switch gL {
case .manual(let value):
tx.gasLimit = value
default:
tx.gasLimit = BigUInt(UInt64(21000))
}
}

if options.value != nil {
tx.value = options.value!
}
if options.to != nil {
tx.to = options.to!
}
return tx
}

static func fromJSON(_ json: [String: Any]) -> EthereumTransaction? {
guard let options = TransactionOptions.fromJSON(json) else {return nil}
guard let toString = json["to"] as? String else {return nil}
var to: EthereumAddress
if toString == "0x" || toString == "0x0" {
to = EthereumAddress.contractDeploymentAddress()
} else {
guard let ethAddr = EthereumAddress(toString) else {return nil}
to = ethAddr
}
// if (!to.isValid) {
// return nil
// }
var dataString = json["data"] as? String
if (dataString == nil) {
dataString = json["input"] as? String
}
guard dataString != nil, let data = Data.fromHex(dataString!) else {return nil}
var transaction = EthereumTransaction(to: to, data: data, options: options)
if let nonceString = json["nonce"] as? String {
guard let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) else {return nil}
transaction.nonce = nonce
}
if let vString = json["v"] as? String {
guard let v = BigUInt(vString.stripHexPrefix(), radix: 16) else {return nil}
transaction.v = v
}
if let rString = json["r"] as? String {
guard let r = BigUInt(rString.stripHexPrefix(), radix: 16) else {return nil}
transaction.r = r
}
if let sString = json["s"] as? String {
guard let s = BigUInt(sString.stripHexPrefix(), radix: 16) else {return nil}
transaction.s = s
}
if let valueString = json["value"] as? String {
guard let value = BigUInt(valueString.stripHexPrefix(), radix: 16) else {return nil}
transaction.value = value
}
let inferedChainID = transaction.inferedChainID
if (transaction.inferedChainID != nil && transaction.v >= BigUInt(37)) {
transaction.chainID = inferedChainID
}
return transaction
}

}
28 changes: 0 additions & 28 deletions web3swift/Web3/Classes/Web3+Contract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ extension web3 {
var web3 : web3
public var transactionOptions: TransactionOptions? = nil

@available(*, deprecated, message: "Use transactioinOptions instead")
public var options: Web3Options? = nil

/// Initialize the bound contract instance by supplying the Web3 provider bound object, ABI, Ethereum address and some default
/// options for further function calls. By default the contract inherits options from the web3 object. Additionally supplied "options"
/// do override inherited ones.
Expand All @@ -45,31 +42,6 @@ extension web3 {
self.transactionOptions = mergedOptions
}


@available(*, deprecated, message: "Use init with transactionOptions instead")
public init?(web3 web3Instance:web3, abiString: String, at: EthereumAddress? = nil, options: Web3Options?, abiVersion: Int = 2) {
self.web3 = web3Instance
self.options = web3.options
switch abiVersion {
case 1:
print("ABIv1 bound contract is now deprecated")
return nil
case 2:
guard let c = EthereumContract(abiString, at: at) else {return nil}
contract = c
default:
return nil
}
var mergedOptions = Web3Options.merge(self.options, with: options)
if at != nil {
contract.address = at
mergedOptions?.to = at
} else if let addr = mergedOptions?.to {
contract.address = addr
}
self.options = mergedOptions
}

/// Deploys a constact instance using the previously provided (at initialization) ABI, some bytecode, constructor parameters and options.
/// If extraData is supplied it is appended to encoded bytecode and constructor parameters.
///
Expand Down
Loading

0 comments on commit ac752c2

Please sign in to comment.