Skip to content

Commit

Permalink
Add batchRequests method
Browse files Browse the repository at this point in the history
  • Loading branch information
franciszekjob committed May 8, 2024
1 parent 0adb281 commit f9df81b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Sources/Starknet/Providers/StarknetProvider/StarknetProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class StarknetProvider: StarknetProviderProtocol {
}

private func buildRequest<U: Decodable, P: Encodable>(method: JsonRpcMethod, params: P) -> HttpRequest<U, P> {
let rpcPayload = JsonRpcPayload<P>(method: method, params: params)
let config = HttpNetworkProvider.Configuration(
url: url,
method: "POST",
Expand All @@ -44,7 +43,22 @@ public class StarknetProvider: StarknetProviderProtocol {
(header: "Accept", value: "application/json"),
]
)
return HttpRequest<U, P>(rpcPayload: rpcPayload, config: config, networkProvider: networkProvider)
return HttpRequest<U, P>(method: method, params: params, config: config, networkProvider: networkProvider)
}

public func batchRequests<U: Decodable, P: Encodable>(requests: [HttpRequest<U, P>]) -> HttpBatchRequest<U, P> {
let rpcPayloads = requests.map { request in
JsonRpcPayload(method: request.method, params: request.params)
}
let config = HttpNetworkProvider.Configuration(
url: url,
method: "POST",
params: [
(header: "Content-Type", value: "application/json"),
(header: "Accept", value: "application/json"),
]
)
return HttpBatchRequest<U, P>(rpcPayloads: rpcPayloads, config: config, networkProvider: networkProvider)
}

public func specVersion() -> HttpRequest<String, EmptyParams> {
Expand Down
5 changes: 5 additions & 0 deletions Sources/Starknet/Providers/StarknetProviderProtocol.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation

// TODO: Update deocumentation comments

/// Provider used to interact with the StakNet blockchain.
public protocol StarknetProviderProtocol {
/// Get the version of the Starknet JSON-RPC specification being used by the node.
Expand Down Expand Up @@ -138,6 +140,9 @@ public protocol StarknetProviderProtocol {
///
/// - Returns: array of simulated transactions
func simulateTransactions(_ transactions: [any StarknetExecutableTransaction], at blockId: StarknetBlockId, simulationFlags: Set<StarknetSimulationFlag>) -> HttpRequest<[StarknetSimulatedTransaction], SimulateTransactionsParams>

// TODO: Add documentation comments
func batchRequests<U: Decodable, P: Encodable>(requests: [HttpRequest<U, P>]) -> HttpBatchRequest<U, P>
}

let defaultBlockId = StarknetBlockId.tag(.pending)
Expand Down

0 comments on commit f9df81b

Please sign in to comment.