Skip to content

Commit 2cdc1be

Browse files
committed
Add policies to web3 provider. Add resolver as prop of read transaction. Update functions accordingly
1 parent 3a93aa4 commit 2cdc1be

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

Sources/Core/Structure/Web3ProviderProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
public protocol Web3Provider {
1111
var network: Networks? {get set}
1212
var attachedKeystoreManager: KeystoreManager? {get set}
13+
var policies: Policies {get set}
1314
var url: URL {get}
1415
var session: URLSession {get}
1516
}

Sources/web3swift/Operations/ReadTransaction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class ReadOperation {
1616
public var method: String
1717
public var data: Data? { transaction.data }
1818

19+
var resolver: PolicyResolver
1920
var web3: Web3
2021

2122
// FIXME: Rewrite this to CodableTransaction
@@ -30,12 +31,11 @@ public class ReadOperation {
3031
if let network = self.web3.provider.network {
3132
self.transaction.chainID = network.chainID
3233
}
34+
self.resolver = PolicyResolver(provider: web3.provider)
3335
}
3436

3537
// TODO: Remove type erasing here, some broad wide protocol should be added instead
3638
public func callContractMethod() async throws -> [String: Any] {
37-
// TODO: might change to be dependency
38-
let resolver = PolicyResolver(provider: web3.provider)
3939
try await resolver.resolveAll(for: &transaction)
4040
// MARK: Read data from ABI flow
4141
// FIXME: This should be dropped, and after `execute()` call, just to decode raw data.

Sources/web3swift/Operations/WriteOperation.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import Core
1111
public class WriteOperation: ReadOperation {
1212

1313
// FIXME: Rewrite this to CodableTransaction
14-
public func writeToChain(password: String, policies: Policies = .auto) async throws -> TransactionSendingResult {
15-
// TODO: might change to be dependency
16-
let resolver = PolicyResolver(provider: web3.provider)
17-
try await resolver.resolveAll(for: &transaction)
14+
/// Sends (raw) transaction for write operation.
15+
/// - Parameters:
16+
/// - password: Password for private key.
17+
/// - policies: Custom policies for how to resolve (optional). Default are policies on Web3Provider instance.
18+
public func writeToChain(password: String, policies: Policies? = nil) async throws -> TransactionSendingResult {
19+
let policiesOrUserDefaults = policies ?? web3.provider.policies
20+
try await resolver.resolveAll(for: &transaction, with: policiesOrUserDefaults)
1821
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
1922
do {
2023
try Web3Signer.signTX(transaction: &transaction,

Sources/web3swift/Web3/Web3+HttpProvider.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Core
1111
public class Web3HttpProvider: Web3Provider {
1212
public var url: URL
1313
public var network: Networks?
14+
public var policies: Policies = .auto
1415
public var attachedKeystoreManager: KeystoreManager?
1516
public var session: URLSession = {() -> URLSession in
1617
let config = URLSessionConfiguration.default

0 commit comments

Comments
 (0)