Skip to content

Commit

Permalink
Add getLatestBlockhash API method as replacement for getRecentBlockhash
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Manzer committed Nov 22, 2024
1 parent 57d3c1f commit 21d35b3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Sources/SolanaSwift/APIClient/APIClient+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public extension SolanaAPIClient {
try await getMinimumBalanceForRentExemption(dataLength: span, commitment: "recent")
}

@available(*, deprecated, message: "Use getLatestBlockhash instead")
func getRecentBlockhash() async throws -> String {
try await getRecentBlockhash(commitment: nil)
try await getLatestBlockhash(commitment: nil, minContextSlot: nil).blockhash
}

func observeSignatureStatus(signature: String) -> AsyncStream<PendingTransactionStatus> {
Expand Down
20 changes: 14 additions & 6 deletions Sources/SolanaSwift/APIClient/Networking/JSONRPCAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,21 @@ public class JSONRPCAPIClient: SolanaAPIClient {
)
}

@available(*, deprecated, message: "Use getLatestBlockhash instead")
public func getRecentBlockhash(commitment: Commitment? = nil) async throws -> String {
let result: Rpc<Fee> = try await get(method: "getRecentBlockhash",
params: [RequestConfiguration(commitment: commitment)])
guard let blockhash = result.value.blockhash else {
throw APIClientError.blockhashNotFound
}
return blockhash
try await getLatestBlockhash(commitment: commitment, minContextSlot: nil).blockhash
}

public func getLatestBlockhash(commitment: Commitment?, minContextSlot: UInt64?) async throws -> LatestBlockhashResponse {
let config = RequestConfiguration(
commitment: commitment,
minContextSlot: minContextSlot
)
let result: Rpc<LatestBlockhashResponse> = try await get(
method: "getLatestBlockhash",
params: [config]
)
return result.value
}

public func getSignatureStatuses(signatures: [String],
Expand Down
14 changes: 8 additions & 6 deletions Sources/SolanaSwift/APIClient/SolanaAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,15 @@ public protocol SolanaAPIClient {
///
func observeSignatureStatus(signature: String, timeout: Int, delay: Int) -> AsyncStream<PendingTransactionStatus>

/// Returns a recent block hash from the ledger, and a fee schedule that can be used to compute the cost of
/// submitting a transaction using it.
/// Returns the latest blockhash and last valid block height
/// - Parameters:
/// - commitment: (optional) Commitment
/// - Throws: APIClientError
/// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#getrecentblockhash
///
/// - commitment: Optional commitment level
/// - minContextSlot: The minimum slot that the request can be evaluated at
/// - Returns: Latest blockhash response containing the blockhash and lastValidBlockHeight
/// - SeeAlso: https://solana.com/docs/rpc/http/getlatestblockhash
func getLatestBlockhash(commitment: Commitment?, minContextSlot: UInt64?) async throws -> LatestBlockhashResponse

@available(*, deprecated, message: "Use getLatestBlockhash instead")
func getRecentBlockhash(commitment: Commitment?) async throws -> String

/// Returns signatures for confirmed transactions that include the given address in their accountKeys list.
Expand Down
5 changes: 5 additions & 0 deletions Sources/SolanaSwift/Models/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public struct LargestAccount: Decodable {
public let address: String
}

public struct LatestBlockhashResponse: Decodable {
public let blockhash: String
public let lastValidBlockHeight: UInt64
}

public struct ProgramAccounts<T: BufferLayout>: Decodable {
public let accounts: [ProgramAccount<T>]
public init(from decoder: Decoder) throws {
Expand Down
7 changes: 5 additions & 2 deletions Sources/SolanaSwift/Models/RequestModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public struct RequestConfiguration: Encodable {
public let skipPreflight: Bool?
public let preflightCommitment: Commitment?
public let searchTransactionHistory: Bool?
public let minContextSlot: UInt64?
public let replaceRecentBlockhash: Bool?

public init?(
Expand All @@ -94,6 +95,7 @@ public struct RequestConfiguration: Encodable {
skipPreflight: Bool? = nil,
preflightCommitment: Commitment? = nil,
searchTransactionHistory: Bool? = nil,
minContextSlot: UInt64? = nil,
replaceRecentBlockhash: Bool? = nil
) {
if commitment == nil,
Expand All @@ -106,8 +108,8 @@ public struct RequestConfiguration: Encodable {
skipPreflight == nil,
preflightCommitment == nil,
searchTransactionHistory == nil,
replaceRecentBlockhash == nil
{
minContextSlot == nil,
replaceRecentBlockhash == nil {
return nil
}

Expand All @@ -121,6 +123,7 @@ public struct RequestConfiguration: Encodable {
self.skipPreflight = skipPreflight
self.preflightCommitment = preflightCommitment
self.searchTransactionHistory = searchTransactionHistory
self.minContextSlot = minContextSlot
self.replaceRecentBlockhash = replaceRecentBlockhash
}
}
Expand Down

0 comments on commit 21d35b3

Please sign in to comment.