From 82e5126ecbca1ee9e9d7e99f745fc06717ac8f17 Mon Sep 17 00:00:00 2001 From: Simon McLoughlin Date: Wed, 3 Apr 2024 15:01:25 +0100 Subject: [PATCH] allow operations to decode gas, or storage independently --- .../Models/Operation/Operation.swift | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/Sources/KukaiCoreSwift/Models/Operation/Operation.swift b/Sources/KukaiCoreSwift/Models/Operation/Operation.swift index f01c5cbd..700ba5c6 100644 --- a/Sources/KukaiCoreSwift/Models/Operation/Operation.swift +++ b/Sources/KukaiCoreSwift/Models/Operation/Operation.swift @@ -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