|
| 1 | +import BigInt |
| 2 | +import Foundation |
| 3 | + |
| 4 | +/// Estimates the transaction tip by taking the median of all V3 transaction tips in the latest block. |
| 5 | +/// |
| 6 | +/// - Parameter provider: The provider used to interact with Starknet. |
| 7 | +/// - Returns: The estimated median tip. |
| 8 | +/// - Throws: An error if the RPC call fails or no transactions are found. |
| 9 | +public func estimateTip(provider: StarknetProviderProtocol) async throws -> UInt64AsHex { |
| 10 | + try await estimateTip(provider: provider, blockId: .tag(.latest)) |
| 11 | +} |
| 12 | + |
| 13 | +/// Estimates the transaction tip by taking the median of all V3 transaction tips in the latest block. |
| 14 | +/// |
| 15 | +/// - Parameters: |
| 16 | +/// - provider: The provider used to interact with Starknet. |
| 17 | +/// - blockHash: The block hash to estimate tip for. |
| 18 | +/// - Returns: The estimated median tip. |
| 19 | +/// - Throws: An error if the RPC call fails or no transactions are found. |
| 20 | +public func estimateTip(provider: StarknetProviderProtocol, blockHash: Felt) async throws -> UInt64AsHex { |
| 21 | + try await estimateTip(provider: provider, blockId: .hash(blockHash)) |
| 22 | +} |
| 23 | + |
| 24 | +/// Estimates the transaction tip by taking the median of all V3 transaction tips in the latest block. |
| 25 | +/// |
| 26 | +/// - Parameters: |
| 27 | +/// - provider: The provider used to interact with Starknet. |
| 28 | +/// - blockNumber: The block number to estimate tip for. |
| 29 | +/// - Returns: The estimated median tip. |
| 30 | +/// - Throws: An error if the RPC call fails or no transactions are found. |
| 31 | +public func estimateTip(provider: StarknetProviderProtocol, blockNumber: Int) async throws -> UInt64AsHex { |
| 32 | + try await estimateTip(provider: provider, blockId: .number(blockNumber)) |
| 33 | +} |
| 34 | + |
| 35 | +/// Estimates the transaction tip by taking the median of all V3 transaction tips in the latest block. |
| 36 | +/// |
| 37 | +/// - Parameters: |
| 38 | +/// - provider: The provider used to interact with Starknet. |
| 39 | +/// - blockTag: The block tag to estimate tip for. |
| 40 | +/// - Returns: The estimated median tip. |
| 41 | +/// - Throws: An error if the RPC call fails or no transactions are found. |
| 42 | +public func estimateTip(provider: StarknetProviderProtocol, blockTag: StarknetBlockId.BlockTag) async throws -> UInt64AsHex { |
| 43 | + try await estimateTip(provider: provider, blockId: .tag(blockTag)) |
| 44 | +} |
| 45 | + |
| 46 | +/// Estimates the transaction tip by taking the median of all V3 transaction tips in the specified block. |
| 47 | +/// |
| 48 | +/// - Parameters: |
| 49 | +/// - provider: The provider used to interact with Starknet. |
| 50 | +/// - blockId: The block identifier to estimate the tip for (hash, number, or tag). |
| 51 | +/// - Returns: The estimated median tip. |
| 52 | +/// - Throws: An error if the RPC call fails or no transactions are found. |
| 53 | +private func estimateTip(provider: StarknetProviderProtocol, blockId: StarknetBlockId) async throws -> UInt64AsHex { |
| 54 | + let request = RequestBuilder.getBlockWithTxs(blockId) |
| 55 | + let blockWithTxs = try await provider.send(request: request) |
| 56 | + |
| 57 | + let transactions: [TransactionWrapper] = switch blockWithTxs { |
| 58 | + case let .processed(block): block.transactions |
| 59 | + case let .preConfirmed(block): block.transactions |
| 60 | + } |
| 61 | + |
| 62 | + let tips = transactions.compactMap { transactionWrapper in |
| 63 | + switch transactionWrapper { |
| 64 | + case let .invokeV3(invokeV3): invokeV3.tip.value |
| 65 | + case let .deployAccountV3(deployAccountV3): deployAccountV3.tip.value |
| 66 | + case let .declareV3(declareV3): declareV3.tip.value |
| 67 | + default: nil |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if tips.isEmpty { |
| 72 | + return UInt64AsHex.zero |
| 73 | + } |
| 74 | + |
| 75 | + let sortedTips = tips.sorted() |
| 76 | + let count = sortedTips.count |
| 77 | + |
| 78 | + let median = if count % 2 == 1 { |
| 79 | + sortedTips[count / 2] |
| 80 | + } else { |
| 81 | + (sortedTips[count / 2 - 1] + sortedTips[count / 2]) / 2 |
| 82 | + } |
| 83 | + |
| 84 | + if let median = median.toUInt64AsHex() { |
| 85 | + return median |
| 86 | + } else { |
| 87 | + fatalError("Failed to convert BigUInt to UInt64AsHex") |
| 88 | + } |
| 89 | +} |
0 commit comments