Skip to content

Commit

Permalink
Add public init for StarknetFeeEstimate (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
franciszekjob authored May 24, 2024
1 parent 9f84360 commit f3ed7cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/Starknet/Data/Responses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions Tests/StarknetTests/Crypto/FeeEstimateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit f3ed7cd

Please sign in to comment.