Skip to content

Commit dc0d85b

Browse files
committed
restoreWeb3+BrowserFunctions before proper refactor to remove reliance on the old json initializers in Transaction and TransactionOptions
1 parent 482589d commit dc0d85b

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,20 @@ extension web3.BrowserFunctions {
7171
guard let publicKey = SECP256K1.recoverPublicKey(hash: hash, signature: signatureData) else {return nil}
7272
return Web3.Utils.publicToAddressString(publicKey)
7373
}
74-
74+
75+
76+
public func sendTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> [String:Any]? {
77+
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
78+
guard let options = TransactionOptions.fromJSON(transactionJSON) else {return nil}
79+
var transactionOptions = TransactionOptions()
80+
transactionOptions.from = options.from
81+
transactionOptions.to = options.to
82+
transactionOptions.value = options.value != nil ? options.value! : BigUInt(0)
83+
transactionOptions.gasLimit = options.gasLimit != nil ? options.gasLimit! : .automatic
84+
transactionOptions.gasPrice = options.gasPrice != nil ? options.gasPrice! : .automatic
85+
return self.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
86+
}
87+
7588
public func sendTransaction(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> [String:Any]? {
7689
do {
7790
let result = try self.web3.eth.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
@@ -80,7 +93,19 @@ extension web3.BrowserFunctions {
8093
return nil
8194
}
8295
}
83-
96+
97+
public func estimateGas(_ transactionJSON: [String: Any]) -> BigUInt? {
98+
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
99+
guard let options = TransactionOptions.fromJSON(transactionJSON) else {return nil}
100+
var transactionOptions = TransactionOptions()
101+
transactionOptions.from = options.from
102+
transactionOptions.to = options.to
103+
transactionOptions.value = options.value != nil ? options.value! : BigUInt(0)
104+
transactionOptions.gasLimit = .automatic
105+
transactionOptions.gasPrice = options.gasPrice != nil ? options.gasPrice! : .automatic
106+
return self.estimateGas(transaction, transactionOptions: transactionOptions)
107+
}
108+
84109
public func estimateGas(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions) -> BigUInt? {
85110
do {
86111
let result = try self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
@@ -89,6 +114,16 @@ extension web3.BrowserFunctions {
89114
return nil
90115
}
91116
}
117+
118+
public func prepareTxForApproval(_ transactionJSON: [String: Any]) -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
119+
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return (nil, nil)}
120+
guard let options = TransactionOptions.fromJSON(transactionJSON) else {return (nil, nil)}
121+
do {
122+
return try self.prepareTxForApproval(transaction, options: options)
123+
} catch {
124+
return (nil, nil)
125+
}
126+
}
92127

93128
public func prepareTxForApproval(_ trans: EthereumTransaction, options opts: TransactionOptions) throws -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
94129
do {
@@ -107,7 +142,24 @@ extension web3.BrowserFunctions {
107142
return (nil, nil)
108143
}
109144
}
110-
145+
146+
public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> String? {
147+
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
148+
guard let options = TransactionOptions.fromJSON(transactionJSON) else {return nil}
149+
var transactionOptions = TransactionOptions()
150+
transactionOptions.from = options.from
151+
transactionOptions.to = options.to
152+
transactionOptions.value = options.value != nil ? options.value! : BigUInt(0)
153+
transactionOptions.gasLimit = options.gasLimit != nil ? options.gasLimit! : .automatic
154+
transactionOptions.gasPrice = options.gasPrice != nil ? options.gasPrice! : .automatic
155+
if let nonceString = transactionJSON["nonce"] as? String, let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) {
156+
transactionOptions.nonce = .manual(nonce)
157+
} else {
158+
transactionOptions.nonce = .pending
159+
}
160+
return self.signTransaction(transaction, transactionOptions: transactionOptions, password: password)
161+
}
162+
111163
public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> String? {
112164
do {
113165
var transaction = trans

0 commit comments

Comments
 (0)