forked from yenom/BitcoinKit
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add FeeCalculator and FeeCalculatorTests
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// FeeCalculator.swift | ||
// | ||
// Copyright © 2019 BitcoinKit developers | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct FeeCalculator { | ||
/// Calclate minimum utxo amount that will be accepted by full nodes | ||
/// Check here : https://github.com/Bitcoin-ABC/bitcoin-abc/blob/1da1ddd10d3a52de49fcf3b399917cfbc28ae8d6/src/test/transaction_tests.cpp#L641 | ||
static func calculateDust(feePerByte: UInt64) -> UInt64 { | ||
return 3 * 182 * feePerByte | ||
} | ||
|
||
/// Calculate fee | ||
// txin(P2PKH) : 148 bytes | ||
// txout(P2PKH) : 34 bytes | ||
// cf. txin(P2SH) : cannot be decided | ||
// cf. txout(P2SH) : 32 bytes | ||
// cf. txout(OP_RETURN + String) : Roundup([#Characters]/32) + [#Characters] + 11 bytes | ||
static func calculateFee(inputs: UInt64, outputs: UInt64, feePerByte: UInt64) -> UInt64 { | ||
guard inputs > 0 else { | ||
return 0 | ||
} | ||
let txByteSize: UInt64 = (inputs * 148) + (outputs * 34) + 10 | ||
return txByteSize * feePerByte | ||
} | ||
|
||
/// Calculate fee per single input (Not including other inputs, outputs and tx of itself) | ||
static func calculateSingleInputFee(feePerByte: UInt64) -> UInt64 { | ||
return 148 * feePerByte | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// FeeCalculatorTests.swift | ||
// | ||
// Copyright © 2019 BitcoinKit developers | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import XCTest | ||
@testable import BitcoinKit | ||
|
||
final class FeeCalculatorTests: XCTestCase { | ||
func testCalculateDust() { | ||
XCTAssertEqual(FeeCalculator.calculateDust(feePerByte: 1), 546) | ||
XCTAssertEqual(FeeCalculator.calculateDust(feePerByte: 2), 1092) | ||
XCTAssertEqual(FeeCalculator.calculateDust(feePerByte: 123), 67158) | ||
|
||
} | ||
|
||
func testCalculateSingleInputFee() { | ||
XCTAssertEqual(FeeCalculator.calculateSingleInputFee(feePerByte: 1), 148) | ||
XCTAssertEqual(FeeCalculator.calculateSingleInputFee(feePerByte: 2), 296) | ||
XCTAssertEqual(FeeCalculator.calculateSingleInputFee(feePerByte: 123), 18_204) | ||
|
||
} | ||
|
||
func testCalculateFee() { | ||
XCTAssertEqual(FeeCalculator.calculateFee(inputs: 0, outputs: 0, feePerByte: 1), 0) | ||
XCTAssertEqual(FeeCalculator.calculateFee(inputs: 1, outputs: 0, feePerByte: 1), 158) | ||
XCTAssertEqual(FeeCalculator.calculateFee(inputs: 0, outputs: 1, feePerByte: 1), 0) | ||
XCTAssertEqual(FeeCalculator.calculateFee(inputs: 1, outputs: 1, feePerByte: 1), 192) | ||
XCTAssertEqual(FeeCalculator.calculateFee(inputs: 10, outputs: 20, feePerByte: 1), 2170) | ||
XCTAssertEqual(FeeCalculator.calculateFee(inputs: 10, outputs: 2, feePerByte: 1), 1558) | ||
XCTAssertEqual(FeeCalculator.calculateFee(inputs: 10, outputs: 2, feePerByte: 123), 191_634) | ||
} | ||
} | ||
|