Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module dependency issue, getLatestBlockhash implementation. #101

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"object": {
"pins": [
{
"package": "secp256k1",
"repositoryURL": "https://github.com/Boilertalk/secp256k1.swift.git",
"state": {
"branch": null,
"revision": "cd187c632fb812fd93711a9f7e644adb7e5f97f0",
"version": "0.1.7"
"identity" : "secp256k1.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/GigaBitcoin/secp256k1.swift",
"state" : {
"revision" : "9683e8e311c76d8114cd308b697dad2f9fc58fed",
"version" : "0.17.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
],
dependencies: [
// Main depedencies
.package(url: "https://github.com/Boilertalk/secp256k1.swift.git", from: "0.1.0"),
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift", from: "0.1.0"),
.package(url: "https://github.com/bitmark-inc/tweetnacl-swiftwrap.git", from: "1.0.2"),
.package(url: "https://github.com/bigearsenal/task-retrying-swift.git", from: "2.0.0"),

Expand Down
4 changes: 2 additions & 2 deletions Sources/SolanaSwift/APIClient/APIClient+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public extension SolanaAPIClient {
try await getMinimumBalanceForRentExemption(dataLength: span, commitment: "recent")
}

func getRecentBlockhash() async throws -> String {
try await getRecentBlockhash(commitment: nil)
func getLatestBlockhash() async throws -> String {
try await getLatestBlockhash(commitment: nil)
}

func observeSignatureStatus(signature: String) -> AsyncStream<PendingTransactionStatus> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public class JSONRPCAPIClient: SolanaAPIClient {
)
}

public func getRecentBlockhash(commitment: Commitment? = nil) async throws -> String {
let result: Rpc<Fee> = try await get(method: "getRecentBlockhash",
public func getLatestBlockhash(commitment: Commitment? = nil) async throws -> String {
let result: Rpc<Fee> = try await get(method: "getLatestBlockhash",
params: [RequestConfiguration(commitment: commitment)])
guard let blockhash = result.value.blockhash else {
throw APIClientError.blockhashNotFound
Expand Down
9 changes: 4 additions & 5 deletions Sources/SolanaSwift/APIClient/SolanaAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,21 @@ 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 block hash from the ledger
/// - Parameters:
/// - commitment: (optional) Commitment
/// - Throws: APIClientError
/// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#getrecentblockhash
/// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#getlatestblockhash
///
func getRecentBlockhash(commitment: Commitment?) async throws -> String
func getLatestBlockhash(commitment: Commitment?) async throws -> String

/// Returns signatures for confirmed transactions that include the given address in their accountKeys list.
/// Returns signatures backwards in time from the provided signature or most recent confirmed block
/// - Parameters:
/// - address: account address as base-58 encoded string
/// - configs: (optional) Configuration object
/// - Throws: APIClientError
/// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#getrecentblockhash
/// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#getsignaturesforaddress
///
func getSignaturesForAddress(address: String, configs: RequestConfiguration?) async throws -> [SignatureInfo]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class BlockchainClient: SolanaBlockchainClient {
}
let expectedFee = try feeCalculator.calculateNetworkFee(transaction: transaction)

let blockhash = try await apiClient.getRecentBlockhash()
let blockhash = try await apiClient.getLatestBlockhash()
transaction.recentBlockhash = blockhash

// if any signers, sign
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public extension SolanaBlockchainClient {
retryDelay: 1,
timeoutInSeconds: 60
) {
let recentBlockhash = try await self.apiClient.getRecentBlockhash()
let latestBlockhash = try await self.apiClient.getLatestBlockhash()
let serializedTransaction = try self.signAndSerialize(
preparedTransaction: preparedTransaction,
recentBlockhash: recentBlockhash
recentBlockhash: latestBlockhash
)
return try await self.apiClient.sendTransaction(
transaction: serializedTransaction,
Expand All @@ -67,10 +67,10 @@ public extension SolanaBlockchainClient {
func simulateTransaction(
preparedTransaction: PreparedTransaction
) async throws -> SimulationResult {
let recentBlockhash = try await apiClient.getRecentBlockhash()
let latestBlockhash = try await apiClient.getLatestBlockhash()
let serializedTransaction = try signAndSerialize(
preparedTransaction: preparedTransaction,
recentBlockhash: recentBlockhash
recentBlockhash: latestBlockhash
)
return try await apiClient.simulateTransaction(
transaction: serializedTransaction, configs: RequestConfiguration(
Expand Down
8 changes: 4 additions & 4 deletions Tests/SolanaSwiftUnitTests/APIClient/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ class APIClientTests: XCTestCase {
XCTAssertEqual(result, 890_880)
}

func testGetRecentBlockhash() async throws {
let mock = NetworkManagerMock(NetworkManagerMockJSON["getRecentBlockhash"]!)
func testGetLatestBlockhash() async throws {
let mock = NetworkManagerMock(NetworkManagerMockJSON["getLatestBlockhash"]!)
let apiClient = JSONRPCAPIClient(endpoint: endpoint, networkManager: mock)
let result = try! await apiClient.getRecentBlockhash(commitment: nil)
let result = try! await apiClient.getLatestBlockhash(commitment: nil)
XCTAssertNotNil(result)
XCTAssertEqual(result, "63ionHTAM94KaSujUCg23hfg7TLharchq5BYXdLGqia1")
}
Expand Down Expand Up @@ -375,7 +375,7 @@ private var NetworkManagerMockJSON = [
"getEpochInfo": "{\"jsonrpc\":\"2.0\",\"result\":{\"absoluteSlot\":131686768,\"blockHeight\":119443373,\"epoch\":304,\"slotIndex\":358768,\"slotsInEpoch\":432000,\"transactionCount\":71271072342},\"id\":\"AE699DFA-84E8-495C-8B06-F30DDFA6C56D\"}\n",
"getFees": "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"slot\":131770081},\"value\":{\"blockhash\":\"7jvToPQ4ASj3xohjM117tMqmtppQDaWVADZyaLFnytFr\",\"feeCalculator\":{\"lamportsPerSignature\":5000},\"lastValidBlockHeight\":119512694,\"lastValidSlot\":131770381}},\"id\":\"3FF1AACE-812A-4106-8C34-6EF66237673C\"}\n",
"getMinimumBalanceForRentExemption": "{\"jsonrpc\":\"2.0\",\"result\":890880,\"id\":\"25423C5F-2FF3-4134-8CB3-9090BFCB2CE3\"}\n",
"getRecentBlockhash": "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"slot\":131780453},\"value\":{\"blockhash\":\"63ionHTAM94KaSujUCg23hfg7TLharchq5BYXdLGqia1\",\"feeCalculator\":{\"lamportsPerSignature\":5000}}},\"id\":\"21D61199-F235-4CC9-9BE6-06745D3AC69E\"}\n",
"getLatestBlockhash": "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"slot\":131780453},\"value\":{\"blockhash\":\"63ionHTAM94KaSujUCg23hfg7TLharchq5BYXdLGqia1\"}},\"id\":\"21D61199-F235-4CC9-9BE6-06745D3AC69E\"}\n",
"getMultipleAccounts": "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"slot\":132420615},\"value\":[{\"data\":[\"APoAh5MDAAAAAAKLjuya35R64GfrOPbupmMcxJ1pmaH2fciYq9DxSQ88FioLlNul6FnDNF06/RKhMFBVI8fFQKRYcqukjYZitosKxZBjjg9hLR2AsDm2e/itloPtlrPeVDPIVdnO4+dmM2JiSZHdhsj7+Fn94OTNte9elt1ek0p487C2fLrFA9CvUPerjZvfP97EqlF9OXbPSzaGJzdmfWhk4jRnThsg5scAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAObFpMVhxY3CRrzEcywhYTa4a4SsovPp4wKPRTbTJVtzAfQBZAAAAABDU47UFrGnHMTsb0EaE1TBoVQGvCIHKJ4/EvpK3zvIfwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACsWQY44PYS0dgLA5tnv4rZaD7Zaz3lQzyFXZzuPnZjMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\",\"base64\"],\"executable\":false,\"lamports\":1345194,\"owner\":\"617jbWo616ggkDxvW1Le8pV38XLbVSyWY8ae6QUmGBAU\",\"rentEpoch\":306}]},\"id\":\"D5BCACBB-3CE7-44D6-8F66-C57470A90440\"}\n",
"getMultipleMintDatas": "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"slot\":132426903},\"value\":[{\"data\":[\"AQAAABzjWe1aAS4E+hQrnHUaHF6Hz9CgFhuchf/TG3jN/Nj2dbn7oe7/EAAGAQEAAAAqnl7btTwEZ5CY/3sSZRcUQ0/AjFYqmjuGEQXmctQicw==\",\"base64\"],\"executable\":false,\"lamports\":122356825965,\"owner\":\"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\",\"rentEpoch\":306},{\"data\":[\"AQAAAAXqnPFs5BGY8aSZN8iMNwqU1K//ibW6y470XmMku3j3J0UE6/G2BgAGAQEAAAAF6pzxbOQRmPGkmTfIjDcKlNSv/4m1usuO9F5jJLt49w==\",\"base64\"],\"executable\":false,\"lamports\":23879870146,\"owner\":\"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\",\"rentEpoch\":306}]},\"id\":\"65766E4D-F678-489A-943B-8D70B5C6F1ED\"}\n",
"simulateTransaction": "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"slot\":132581497},\"value\":{\"accounts\":null,\"err\":\"AccountNotFound\",\"logs\":[],\"unitsConsumed\":0}},\"id\":\"3916EC69-2924-40E7-8843-8CBA8C6DB14C\"}\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class BlockchainClientWithNativeSOLTests: XCTestCase {
feePayer: account.publicKey
)

let recentBlockhash = try await apiClient.getRecentBlockhash()
let latestBlockhash = try await apiClient.getLatestBlockhash()
let serializedTransaction = try blockchain.signAndSerialize(
preparedTransaction: tx,
recentBlockhash: recentBlockhash
recentBlockhash: latestBlockhash
)

XCTAssertEqual(tx.expectedFee, .init(transaction: 5000, accountBalances: 0))
Expand All @@ -57,10 +57,10 @@ class BlockchainClientWithNativeSOLTests: XCTestCase {
feePayer: account.publicKey
)

let recentBlockhash = try await apiClient.getRecentBlockhash()
let latestBlockhash = try await apiClient.getLatestBlockhash()
let serializedTransaction = try blockchain.signAndSerialize(
preparedTransaction: tx,
recentBlockhash: recentBlockhash
recentBlockhash: latestBlockhash
)

XCTAssertEqual(tx.expectedFee, .init(transaction: 5000, accountBalances: 0))
Expand Down Expand Up @@ -184,7 +184,7 @@ private class MockAPIClient: SolanaAPIClient {
)
}

func getRecentBlockhash(commitment _: Commitment?) async throws -> String {
func getLatestBlockhash(commitment _: Commitment?) async throws -> String {
switch testCase {
case "testPrepareSendingNativeSOL()":
return "DSfeYUm7WDw1YnKodR361rg8sUzUCGdat9V7fSKPFgzq"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ final class BlockchainClientWithTokenProgramTests: XCTestCase {

XCTAssertEqual(tx.expectedFee, expectedFee)

let recentBlockhash = try await apiClient.getRecentBlockhash()
let latestBlockhash = try await apiClient.getLatestBlockhash()
let serializedTransaction = try blockchainClient.signAndSerialize(
preparedTransaction: tx,
recentBlockhash: recentBlockhash
recentBlockhash: latestBlockhash
)
XCTAssertEqual(serializedTransaction, expectedSerializedTransaction)
}
Expand Down Expand Up @@ -221,7 +221,7 @@ private class MockAPIClient: SolanaAPIClient {
)
}

func getRecentBlockhash(commitment _: Commitment?) async throws -> String {
func getLatestBlockhash(commitment _: Commitment?) async throws -> String {
switch testCase {
case "testPrepareSendingNativeSOL()":
return "DSfeYUm7WDw1YnKodR361rg8sUzUCGdat9V7fSKPFgzq"
Expand Down