Skip to content

Commit

Permalink
allow operations to decode gas, or storage independently
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcl committed Apr 3, 2024
1 parent 0dfd474 commit 82e5126
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions Sources/KukaiCoreSwift/Models/Operation/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,13 @@ public class Operation: Codable {
source = try container.decodeIfPresent(String.self, forKey: .source)
counter = try container.decodeIfPresent(String.self, forKey: .counter)

// When coming from Wallet Connect, operations may include suggested gas, storage or fee
// Each one is optional, and will be used later on as
let storage = Int(try container.decodeIfPresent(String.self, forKey: .storageLimit) ?? "0") ?? 0
let gas = Int(try container.decodeIfPresent(String.self, forKey: .gasLimit) ?? "0") ?? 0
let feeString = try container.decodeIfPresent(String.self, forKey: .fee) ?? "0"

// When we encode, we encode all properties. But when coming from beacon it will sometimes supply a suggestion for gas and storage, but will not include a fee
// When we have all, just encode
if let storageInt = Int(try container.decodeIfPresent(String.self, forKey: .storageLimit) ?? ""),
let gasInt = Int(try container.decodeIfPresent(String.self, forKey: .gasLimit) ?? ""),
let feeString = try container.decodeIfPresent(String.self, forKey: .fee) {
operationFees = OperationFees(transactionFee: XTZAmount(fromRpcAmount: feeString) ?? XTZAmount.zero(), gasLimit: gasInt, storageLimit: storageInt)
}

// When we have gas and storage suggestions, encode those and compute a fee
else if let storageInt = Int(try container.decodeIfPresent(String.self, forKey: .storageLimit) ?? ""),
let gasInt = Int(try container.decodeIfPresent(String.self, forKey: .gasLimit) ?? "") {

let fee = XTZAmount.zero() // Fee will need to be computed outside. Requires network calls to see current constants and forging the JSON. Can't be done here
operationFees = OperationFees(transactionFee: fee, gasLimit: gasInt, storageLimit: storageInt)
}
operationFees = OperationFees(transactionFee: XTZAmount(fromRpcAmount: feeString) ?? XTZAmount.zero(), gasLimit: gas, storageLimit: storage)

} else {
source = nil
Expand Down

0 comments on commit 82e5126

Please sign in to comment.