From f3ed7cd5998c400cc710423b84ecefd4170642c7 Mon Sep 17 00:00:00 2001 From: Franciszek Job <54181625+franciszekjob@users.noreply.github.com> Date: Fri, 24 May 2024 13:09:20 +0200 Subject: [PATCH] Add public `init` for `StarknetFeeEstimate` (#198) --- Sources/Starknet/Data/Responses.swift | 18 ++++++++++++++++++ .../Crypto/FeeEstimateTests.swift | 15 +++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Sources/Starknet/Data/Responses.swift b/Sources/Starknet/Data/Responses.swift index 16ed6966c..d1d3084f7 100644 --- a/Sources/Starknet/Data/Responses.swift +++ b/Sources/Starknet/Data/Responses.swift @@ -24,6 +24,24 @@ public struct StarknetFeeEstimate: Decodable, Equatable { case overallFee = "overall_fee" case feeUnit = "unit" } + + public init(gasConsumed: Felt, gasPrice: Felt, dataGasConsumed: Felt, dataGasPrice: Felt, overallFee: Felt, feeUnit: StarknetPriceUnit) { + self.gasConsumed = gasConsumed + self.gasPrice = gasPrice + self.dataGasConsumed = dataGasConsumed + self.dataGasPrice = dataGasPrice + self.overallFee = overallFee + self.feeUnit = feeUnit + } + + public init?(gasConsumed: Felt, gasPrice: Felt, dataGasConsumed: Felt, dataGasPrice: Felt, feeUnit: StarknetPriceUnit) { + self.gasConsumed = gasConsumed + self.gasPrice = gasPrice + self.dataGasConsumed = dataGasConsumed + self.dataGasPrice = dataGasPrice + self.overallFee = Felt(gasPrice.value * gasConsumed.value + dataGasPrice.value * dataGasConsumed.value)! + self.feeUnit = feeUnit + } } public struct StarknetDeployAccountResponse: Decodable, Equatable { diff --git a/Tests/StarknetTests/Crypto/FeeEstimateTests.swift b/Tests/StarknetTests/Crypto/FeeEstimateTests.swift index 08ec99fdf..c107c2dc5 100644 --- a/Tests/StarknetTests/Crypto/FeeEstimateTests.swift +++ b/Tests/StarknetTests/Crypto/FeeEstimateTests.swift @@ -40,4 +40,19 @@ final class FeeEstimateTests: XCTestCase { XCTAssertEqual(estimated, $2) } } + + func testEstimateFeeOverallFeeCalculation() { + let cases: [(StarknetFeeEstimate, Felt)] = + [ + (StarknetFeeEstimate(gasConsumed: 1, gasPrice: 2138, dataGasConsumed: 10, dataGasPrice: 1, feeUnit: .wei)!, 2148), + (StarknetFeeEstimate(gasConsumed: 10, gasPrice: 1000, dataGasConsumed: 10, dataGasPrice: 1, feeUnit: .wei)!, 10010), + (StarknetFeeEstimate(gasConsumed: 10, gasPrice: 0, dataGasConsumed: 10, dataGasPrice: 1, feeUnit: .wei)!, 10), + (StarknetFeeEstimate(gasConsumed: 10, gasPrice: 2000, dataGasConsumed: 10, dataGasPrice: 1, feeUnit: .wei)!, 20010), + ] + + cases.forEach { + let calculatedOverallFee = $0.overallFee + XCTAssertEqual(calculatedOverallFee, $1) + } + } }