Skip to content

Commit

Permalink
Merge pull request #52 from OpenZesame/increase_default_gasLimit
Browse files Browse the repository at this point in the history
Increase min gasLimit to 50 for payments
  • Loading branch information
Sajjon authored May 30, 2021
2 parents aa0b8a7 + 597d839 commit 6c6b2ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Sources/Zesame/Models/Manual/Payment/GasLimit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import Foundation

public typealias GasLimit = Int
public extension GasLimit {
static var defaultGasLimit: GasLimit {
return 1
static var defaultGasLimit: Self = Self.minimum

static var minimum: Self {
return 50
}
}
20 changes: 19 additions & 1 deletion Sources/Zesame/Models/Manual/Payment/Payment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,34 @@ public struct Payment {
gasLimit: GasLimit = .defaultGasLimit,
gasPrice: GasPrice,
nonce: Nonce = 0
) {
) throws {
guard gasLimit >= GasLimit.minimum else { throw Error.gasLimitTooLow(got: gasLimit, butMinIs: GasLimit.minimum) }
self.recipient = recipient
self.amount = amount
self.gasLimit = gasLimit
self.gasPrice = gasPrice
self.nonce = nonce.increasedByOne()
}

enum Error: Swift.Error {
case gasLimitTooLow(got: GasLimit, butMinIs: GasLimit)
}

}

public extension Payment {


static func withMinimumGasLimit(
to recipient: LegacyAddress,
amount: ZilAmount,
gasPrice: GasPrice,
nonce: Nonce = 0
) -> Self {
try! .init(to: recipient, amount: amount, gasLimit: GasLimit.minimum, gasPrice: gasPrice, nonce: nonce)
}


static func estimatedTotalTransactionFee(gasPrice: GasPrice, gasLimit: GasLimit = .defaultGasLimit) throws -> Qa {
return Qa(qa: Qa.Magnitude(gasLimit) * gasPrice.qa)
}
Expand Down
9 changes: 4 additions & 5 deletions Tests/ZesameTests/TransactionSigningTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class TransactionSigningTests: XCTestCase {
let publicKey = PublicKey(privateKey: privateKey)

let unsignedTx = Transaction(
payment: Payment(
payment: Payment.withMinimumGasLimit(
to: try! LegacyAddress(string: "9Ca91EB535Fb92Fda5094110FDaEB752eDb9B039"),
amount: 15,
gasLimit: 1,
gasPrice: GasPrice.min,
nonce: Nonce(3)
),
Expand Down Expand Up @@ -73,10 +72,10 @@ private let expectedTransactionJSONString = """
"pubKey" : "034AE47910D58B9BDE819C3CFFA8DE4441955508DB00AA2540DB8E6BF6E99ABC1B",
"data" : null,
"code" : null,
"signature" : "1727048B9F200D07E6E2D8EFF6BA3E54BF491589CCB55D872E242505B72E6CA74E022AC954DF21420AF8737C89A7E9048B75B531F6865B78B1E2A864169ED64D",
"gasLimit" : "1",
"signature" : "379AA79C8A38A51B3D20232F89D8CF06B7CB2C52D5B42F96280BCD5EE3B1E848E9778C3E0AFE7164F3A913B6E31B88AC226D3816638B03D9718BBDC6F21909DD",
"gasLimit" : "50",
"version" : 65537,
"gasPrice" : "1000000000",
"gasPrice" : "100000000000",
"nonce" : 4
}
"""

0 comments on commit 6c6b2ac

Please sign in to comment.