Skip to content

Commit

Permalink
[ADD] Implement starknet_estimateMessageFee method #80 (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
dioKaratzas committed Jul 18, 2023
1 parent a7218ea commit 9fd942a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ enum JsonRpcMethod: String, Encodable {
case getTransactionByBlockIdAndIndex = "starknet_getTransactionByBlockIdAndIndex"
case getTransactionReceipt = "starknet_getTransactionReceipt"
case simulateTransaction = "starknet_simulateTransaction"
case estimateMessageFee = "starknet_estimateMessageFee"
}
12 changes: 12 additions & 0 deletions Sources/Starknet/Providers/StarknetProvider/JsonRpcParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ struct EstimateFeeParams: Encodable {
}
}

struct EstimateMessageFeeParams: Encodable {
let message: StarknetCall
let senderAddress: Felt
let blockId: StarknetBlockId

enum CodingKeys: String, CodingKey {
case message
case senderAddress = "sender_address"
case blockId = "block_id"
}
}

struct AddDeployAccountTransactionParams: Encodable {
let deployAccountTransaction: StarknetSequencerDeployAccountTransaction

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public class StarknetProvider: StarknetProviderProtocol {
return result
}

public func estimateMessageFee(_ message: StarknetCall, senderAddress: Felt, at blockId: StarknetBlockId) async throws -> StarknetFeeEstimate {
let params = EstimateMessageFeeParams(message: message, senderAddress: senderAddress, blockId: blockId)

let result = try await makeRequest(method: .estimateMessageFee, params: params, receive: StarknetFeeEstimate.self)

return result
}

public func estimateFee(for transactions: [any StarknetSequencerTransaction], at blockId: StarknetBlockId) async throws -> [StarknetFeeEstimate] {
let params = EstimateFeeParams(request: transactions, blockId: blockId)

Expand Down
19 changes: 19 additions & 0 deletions Sources/Starknet/Providers/StarknetProviderProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public protocol StarknetProviderProtocol {
/// - Returns: Array of fee estimates
func estimateFee(for transactions: [any StarknetSequencerTransaction], at blockId: StarknetBlockId) async throws -> [StarknetFeeEstimate]

/// Estimate the L2 fee of a message sent on L1
///
/// - Parameters:
/// - message: the message's parameters
/// - senderAddress: the L1 address of the sender
/// - blockId: hash, numer, or tag of a block for which the estimation should be made.
/// - Returns: the fee estimation
func estimateMessageFee(_ message: StarknetCall, senderAddress: Felt, at blockId: StarknetBlockId) async throws -> StarknetFeeEstimate

/// Invoke a function.
///
/// Invoke a function in deployed contract.
Expand Down Expand Up @@ -147,6 +156,16 @@ public extension StarknetProviderProtocol {
try await estimateFee(for: transaction, at: defaultBlockId)
}

/// Estimate the L2 fee of a message sent on L1
///
/// - Parameters:
/// - message: the message's parameters
/// - senderAddress: the L1 address of the sender
/// - Returns: the fee estimation
func estimateMessageFee(_ message: StarknetCall, senderAddress: Felt) async throws -> StarknetFeeEstimate {
try await estimateMessageFee(message, senderAddress: senderAddress, at: defaultBlockId)
}

/// Get nonce of given starknet contract in the pending block.
///
/// - Parameters
Expand Down

0 comments on commit 9fd942a

Please sign in to comment.