|
| 1 | +import Foundation |
| 2 | + |
| 3 | +#if SWIFT_PACKAGE |
| 4 | + import LDKHeaders |
| 5 | +#endif |
| 6 | + |
| 7 | + |
| 8 | +/// |
| 9 | +public typealias Amount = Bindings.Amount |
| 10 | + |
| 11 | +extension Bindings { |
| 12 | + |
| 13 | + /// The minimum amount required for an item in an [`Offer`], denominated in either bitcoin or |
| 14 | + /// another currency. |
| 15 | + public class Amount: NativeTypeWrapper { |
| 16 | + |
| 17 | + |
| 18 | + /// Set to false to suppress an individual type's deinit log statements. |
| 19 | + /// Only applicable when log threshold is set to `.Debug`. |
| 20 | + public static var enableDeinitLogging = true |
| 21 | + |
| 22 | + /// Set to true to suspend the freeing of this type's associated Rust memory. |
| 23 | + /// Should only ever be used for debugging purposes, and will likely be |
| 24 | + /// deprecated soon. |
| 25 | + public static var suspendFreedom = false |
| 26 | + |
| 27 | + private static var instanceCounter: UInt = 0 |
| 28 | + internal let instanceNumber: UInt |
| 29 | + |
| 30 | + internal var cType: LDKAmount? |
| 31 | + |
| 32 | + internal init(cType: LDKAmount, instantiationContext: String) { |
| 33 | + Self.instanceCounter += 1 |
| 34 | + self.instanceNumber = Self.instanceCounter |
| 35 | + self.cType = cType |
| 36 | + |
| 37 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 38 | + } |
| 39 | + |
| 40 | + internal init(cType: LDKAmount, instantiationContext: String, anchor: NativeTypeWrapper) { |
| 41 | + Self.instanceCounter += 1 |
| 42 | + self.instanceNumber = Self.instanceCounter |
| 43 | + self.cType = cType |
| 44 | + |
| 45 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 46 | + self.dangling = true |
| 47 | + try! self.addAnchor(anchor: anchor) |
| 48 | + } |
| 49 | + |
| 50 | + internal init(cType: LDKAmount, instantiationContext: String, anchor: NativeTypeWrapper, dangle: Bool = false) { |
| 51 | + Self.instanceCounter += 1 |
| 52 | + self.instanceNumber = Self.instanceCounter |
| 53 | + self.cType = cType |
| 54 | + |
| 55 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 56 | + self.dangling = dangle |
| 57 | + try! self.addAnchor(anchor: anchor) |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + public enum AmountType { |
| 62 | + |
| 63 | + /// An amount of bitcoin. |
| 64 | + case Bitcoin |
| 65 | + |
| 66 | + /// An amount of currency specified using ISO 4712. |
| 67 | + case Currency |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public func getValueType() -> AmountType { |
| 72 | + switch self.cType!.tag { |
| 73 | + case LDKAmount_Bitcoin: |
| 74 | + return .Bitcoin |
| 75 | + |
| 76 | + case LDKAmount_Currency: |
| 77 | + return .Currency |
| 78 | + |
| 79 | + default: |
| 80 | + Bindings.print("Error: Invalid value type for Amount! Aborting.", severity: .ERROR) |
| 81 | + abort() |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + /// Frees any resources used by the Amount |
| 88 | + internal func free() { |
| 89 | + // native call variable prep |
| 90 | + |
| 91 | + |
| 92 | + // native method call |
| 93 | + let nativeCallResult = Amount_free(self.cType!) |
| 94 | + |
| 95 | + // cleanup |
| 96 | + |
| 97 | + |
| 98 | + // return value (do some wrapping) |
| 99 | + let returnValue = nativeCallResult |
| 100 | + |
| 101 | + |
| 102 | + return returnValue |
| 103 | + } |
| 104 | + |
| 105 | + /// Creates a copy of the Amount |
| 106 | + internal func clone() -> Amount { |
| 107 | + // native call variable prep |
| 108 | + |
| 109 | + |
| 110 | + // native method call |
| 111 | + let nativeCallResult = |
| 112 | + withUnsafePointer(to: self.cType!) { (origPointer: UnsafePointer<LDKAmount>) in |
| 113 | + Amount_clone(origPointer) |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + // cleanup |
| 118 | + |
| 119 | + |
| 120 | + // return value (do some wrapping) |
| 121 | + let returnValue = Amount( |
| 122 | + cType: nativeCallResult, instantiationContext: "Amount.swift::\(#function):\(#line)") |
| 123 | + |
| 124 | + |
| 125 | + return returnValue |
| 126 | + } |
| 127 | + |
| 128 | + /// Utility method to constructs a new Bitcoin-variant Amount |
| 129 | + public class func initWithBitcoin(amountMsats: UInt64) -> Amount { |
| 130 | + // native call variable prep |
| 131 | + |
| 132 | + |
| 133 | + // native method call |
| 134 | + let nativeCallResult = Amount_bitcoin(amountMsats) |
| 135 | + |
| 136 | + // cleanup |
| 137 | + |
| 138 | + |
| 139 | + // return value (do some wrapping) |
| 140 | + let returnValue = Amount( |
| 141 | + cType: nativeCallResult, instantiationContext: "Amount.swift::\(#function):\(#line)") |
| 142 | + |
| 143 | + |
| 144 | + return returnValue |
| 145 | + } |
| 146 | + |
| 147 | + /// Utility method to constructs a new Currency-variant Amount |
| 148 | + public class func initWithCurrency(iso4217Code: [UInt8], amount: UInt64) -> Amount { |
| 149 | + // native call variable prep |
| 150 | + |
| 151 | + let iso4217CodePrimitiveWrapper = ThreeBytes( |
| 152 | + value: iso4217Code, instantiationContext: "Amount.swift::\(#function):\(#line)") |
| 153 | + |
| 154 | + |
| 155 | + // native method call |
| 156 | + let nativeCallResult = Amount_currency(iso4217CodePrimitiveWrapper.cType!, amount) |
| 157 | + |
| 158 | + // cleanup |
| 159 | + |
| 160 | + // for elided types, we need this |
| 161 | + iso4217CodePrimitiveWrapper.noOpRetain() |
| 162 | + |
| 163 | + |
| 164 | + // return value (do some wrapping) |
| 165 | + let returnValue = Amount( |
| 166 | + cType: nativeCallResult, instantiationContext: "Amount.swift::\(#function):\(#line)") |
| 167 | + |
| 168 | + |
| 169 | + return returnValue |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + public func getValueAsBitcoin() -> Bitcoin? { |
| 174 | + if self.cType?.tag != LDKAmount_Bitcoin { |
| 175 | + return nil |
| 176 | + } |
| 177 | + |
| 178 | + return Amount_LDKBitcoin_Body( |
| 179 | + cType: self.cType!.bitcoin, instantiationContext: "Amount.swift::\(#function):\(#line)", anchor: self) |
| 180 | + } |
| 181 | + |
| 182 | + public func getValueAsCurrency() -> Currency? { |
| 183 | + if self.cType?.tag != LDKAmount_Currency { |
| 184 | + return nil |
| 185 | + } |
| 186 | + |
| 187 | + return Amount_LDKCurrency_Body( |
| 188 | + cType: self.cType!.currency, instantiationContext: "Amount.swift::\(#function):\(#line)", anchor: self) |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + internal func danglingClone() -> Amount { |
| 193 | + let dangledClone = self.clone() |
| 194 | + dangledClone.dangling = true |
| 195 | + return dangledClone |
| 196 | + } |
| 197 | + |
| 198 | + deinit { |
| 199 | + if Bindings.suspendFreedom || Self.suspendFreedom { |
| 200 | + return |
| 201 | + } |
| 202 | + |
| 203 | + if !self.dangling { |
| 204 | + if Self.enableDeinitLogging { |
| 205 | + Bindings.print("Freeing Amount \(self.instanceNumber). (Origin: \(self.instantiationContext))") |
| 206 | + } |
| 207 | + |
| 208 | + self.free() |
| 209 | + } else if Self.enableDeinitLogging { |
| 210 | + Bindings.print( |
| 211 | + "Not freeing Amount \(self.instanceNumber) due to dangle. (Origin: \(self.instantiationContext))") |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + |
| 216 | + /// |
| 217 | + internal typealias Amount_LDKBitcoin_Body = Bitcoin |
| 218 | + |
| 219 | + |
| 220 | + /// |
| 221 | + public class Bitcoin: NativeTypeWrapper { |
| 222 | + |
| 223 | + |
| 224 | + /// Set to false to suppress an individual type's deinit log statements. |
| 225 | + /// Only applicable when log threshold is set to `.Debug`. |
| 226 | + public static var enableDeinitLogging = true |
| 227 | + |
| 228 | + /// Set to true to suspend the freeing of this type's associated Rust memory. |
| 229 | + /// Should only ever be used for debugging purposes, and will likely be |
| 230 | + /// deprecated soon. |
| 231 | + public static var suspendFreedom = false |
| 232 | + |
| 233 | + private static var instanceCounter: UInt = 0 |
| 234 | + internal let instanceNumber: UInt |
| 235 | + |
| 236 | + internal var cType: LDKAmount_LDKBitcoin_Body? |
| 237 | + |
| 238 | + internal init(cType: LDKAmount_LDKBitcoin_Body, instantiationContext: String) { |
| 239 | + Self.instanceCounter += 1 |
| 240 | + self.instanceNumber = Self.instanceCounter |
| 241 | + self.cType = cType |
| 242 | + |
| 243 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 244 | + } |
| 245 | + |
| 246 | + internal init(cType: LDKAmount_LDKBitcoin_Body, instantiationContext: String, anchor: NativeTypeWrapper) { |
| 247 | + Self.instanceCounter += 1 |
| 248 | + self.instanceNumber = Self.instanceCounter |
| 249 | + self.cType = cType |
| 250 | + |
| 251 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 252 | + self.dangling = true |
| 253 | + try! self.addAnchor(anchor: anchor) |
| 254 | + } |
| 255 | + |
| 256 | + internal init( |
| 257 | + cType: LDKAmount_LDKBitcoin_Body, instantiationContext: String, anchor: NativeTypeWrapper, |
| 258 | + dangle: Bool = false |
| 259 | + ) { |
| 260 | + Self.instanceCounter += 1 |
| 261 | + self.instanceNumber = Self.instanceCounter |
| 262 | + self.cType = cType |
| 263 | + |
| 264 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 265 | + self.dangling = dangle |
| 266 | + try! self.addAnchor(anchor: anchor) |
| 267 | + } |
| 268 | + |
| 269 | + |
| 270 | + /// The amount in millisatoshi. |
| 271 | + public func getAmountMsats() -> UInt64 { |
| 272 | + // return value (do some wrapping) |
| 273 | + let returnValue = self.cType!.amount_msats |
| 274 | + |
| 275 | + return returnValue |
| 276 | + } |
| 277 | + |
| 278 | + |
| 279 | + } |
| 280 | + |
| 281 | + |
| 282 | + /// |
| 283 | + internal typealias Amount_LDKCurrency_Body = Currency |
| 284 | + |
| 285 | + |
| 286 | + /// |
| 287 | + public class Currency: NativeTypeWrapper { |
| 288 | + |
| 289 | + |
| 290 | + /// Set to false to suppress an individual type's deinit log statements. |
| 291 | + /// Only applicable when log threshold is set to `.Debug`. |
| 292 | + public static var enableDeinitLogging = true |
| 293 | + |
| 294 | + /// Set to true to suspend the freeing of this type's associated Rust memory. |
| 295 | + /// Should only ever be used for debugging purposes, and will likely be |
| 296 | + /// deprecated soon. |
| 297 | + public static var suspendFreedom = false |
| 298 | + |
| 299 | + private static var instanceCounter: UInt = 0 |
| 300 | + internal let instanceNumber: UInt |
| 301 | + |
| 302 | + internal var cType: LDKAmount_LDKCurrency_Body? |
| 303 | + |
| 304 | + internal init(cType: LDKAmount_LDKCurrency_Body, instantiationContext: String) { |
| 305 | + Self.instanceCounter += 1 |
| 306 | + self.instanceNumber = Self.instanceCounter |
| 307 | + self.cType = cType |
| 308 | + |
| 309 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 310 | + } |
| 311 | + |
| 312 | + internal init(cType: LDKAmount_LDKCurrency_Body, instantiationContext: String, anchor: NativeTypeWrapper) { |
| 313 | + Self.instanceCounter += 1 |
| 314 | + self.instanceNumber = Self.instanceCounter |
| 315 | + self.cType = cType |
| 316 | + |
| 317 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 318 | + self.dangling = true |
| 319 | + try! self.addAnchor(anchor: anchor) |
| 320 | + } |
| 321 | + |
| 322 | + internal init( |
| 323 | + cType: LDKAmount_LDKCurrency_Body, instantiationContext: String, anchor: NativeTypeWrapper, |
| 324 | + dangle: Bool = false |
| 325 | + ) { |
| 326 | + Self.instanceCounter += 1 |
| 327 | + self.instanceNumber = Self.instanceCounter |
| 328 | + self.cType = cType |
| 329 | + |
| 330 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 331 | + self.dangling = dangle |
| 332 | + try! self.addAnchor(anchor: anchor) |
| 333 | + } |
| 334 | + |
| 335 | + |
| 336 | + /// The currency that the amount is denominated in. |
| 337 | + public func getIso4217Code() -> [UInt8] { |
| 338 | + // return value (do some wrapping) |
| 339 | + let returnValue = ThreeBytes( |
| 340 | + cType: self.cType!.iso4217_code, instantiationContext: "Amount.swift::\(#function):\(#line)", |
| 341 | + anchor: self |
| 342 | + ) |
| 343 | + .getValue() |
| 344 | + |
| 345 | + return returnValue |
| 346 | + } |
| 347 | + |
| 348 | + /// The amount in the currency unit adjusted by the ISO 4712 exponent (e.g., USD cents). |
| 349 | + public func getAmount() -> UInt64 { |
| 350 | + // return value (do some wrapping) |
| 351 | + let returnValue = self.cType!.amount |
| 352 | + |
| 353 | + return returnValue |
| 354 | + } |
| 355 | + |
| 356 | + |
| 357 | + } |
| 358 | + |
| 359 | + |
| 360 | + } |
| 361 | + |
| 362 | +} |
0 commit comments