Skip to content

Commit

Permalink
fix: get fee by getFeeForMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
zanyfly committed Dec 12, 2024
1 parent d0a2d61 commit 0090cd6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Sources/SolanaSwift/APIClient/SolanaAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public protocol SolanaAPIClient {
///
func getFees(commitment: Commitment?) async throws -> Fee

// getFees is Deprecated
func getFeeForMessage(message: String, commitment: Commitment?) async throws -> FeeForMessage

/// Returns minimum balance required to make account rent exempt
/// - Parameters:
/// - dataLength: account data length
Expand Down
14 changes: 8 additions & 6 deletions Sources/SolanaSwift/BlockchainClient/BlockchainClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ public class BlockchainClient: SolanaBlockchainClient {
) async throws -> PreparedTransaction {
// form transaction
var transaction = Transaction(instructions: instructions, recentBlockhash: nil, feePayer: feePayer)

var blockhash = try await apiClient.getRecentBlockhash()
transaction.recentBlockhash = blockhash

let feeCalculator: FeeCalculator
if let fc = fc {
feeCalculator = fc
} else {
let lps = try? await apiClient.getFees(commitment: nil).feeCalculator?.lamportsPerSignature
let message = try transaction.serializeMessage().base64EncodedString()
let feeForMessage = try await apiClient.getFeeForMessage(message: message, commitment: nil)
let minRentExemption = try await apiClient.getMinimumBalanceForRentExemption(span: 165)
let lamportsPerSignature = lps ?? 5000
let lamportsPerSignature = feeForMessage ?? 5000
feeCalculator = DefaultFeeCalculator(
lamportsPerSignature: lamportsPerSignature,
minRentExemption: minRentExemption
)
}
let blockhash = try await apiClient.getRecentBlockhash()
blockhash = try await apiClient.getRecentBlockhash()
transaction.recentBlockhash = blockhash

let expectedFee = try feeCalculator.calculateNetworkFee(transaction: transaction)

// if any signers, sign
if !signers.isEmpty {
try transaction.sign(signers: signers)
Expand Down
4 changes: 1 addition & 3 deletions Sources/SolanaSwift/Models/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ public struct Fee: Decodable {
public let lastValidSlot: UInt64?
}

public struct FeeForMessage: Decodable {
public let value: UInt64?
}
public typealias FeeForMessage = UInt64?

public struct FeeCalculatorResponse: Decodable {
public let lamportsPerSignature: Lamports
Expand Down

0 comments on commit 0090cd6

Please sign in to comment.