-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssets.swift
More file actions
112 lines (100 loc) · 3.68 KB
/
Copy pathAssets.swift
File metadata and controls
112 lines (100 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Assets.swift
// Cardano
//
// Created by hellc.
// Copyright © 2020-2026 KXP. All rights reserved.
// Licensed under the MIT License.
//
import Foundation
import CCardano
/// Represents a collection of assets (native tokens) within a single policy of the Cardano blockchain.
///
/// ### Example
/// ```swift
/// let assets = try Assets()
/// try assets.add(assetName: "KXP", amount: 1000)
/// ```
public class Assets {
/// Opaque pointer to the underlying Rust assets object.
internal let pointer: RPtr
/// Initializes an empty assets collection.
/// - Throws: CardanoError if initialization fails.
public init() throws {
self.pointer = try CSL.callRPtr { csl_bridge_assets_new($0, $1) }
}
deinit {
var p = pointer
csl_bridge_rptr_free(&p)
}
/// Returns the number of assets in the collection.
public func len() throws -> Int {
Int(try CSL.call { csl_bridge_assets_len(pointer, $0, $1) })
}
/// Adds a token to the collection.
/// - Parameters:
/// - assetName: The name of the asset (string).
/// - amount: The quantity of the asset to add.
/// - Throws: CardanoError if addition fails.
public func add(assetName: String, amount: UInt64) throws {
let nameData = Data(assetName.utf8)
let namePtr = try nameData.withUnsafeBytes { ptr in
try CSL.callRPtr { csl_bridge_asset_name_new(ptr.bindMemory(to: UInt8.self).baseAddress!, uintptr_t(nameData.count), $0, $1) }
}
let amountPtr = try CSL.callRPtr { csl_bridge_big_num_from_str(String(amount), $0, $1) }
_ = try CSL.callRPtr { csl_bridge_assets_insert(pointer, namePtr, amountPtr, $0, $1) }
}
}
/// Represents multiple asset collections, grouped by their respective policy IDs.
///
/// ### Example
/// ```swift
/// let multiAsset = try MultiAsset()
/// let assets = try Assets()
/// try assets.add(assetName: "KXP", amount: 1000)
/// try multiAsset.insert(policyId: "6b8d3c96...", assets: assets)
/// ```
public class MultiAsset {
/// Opaque pointer to the underlying Rust MultiAsset object.
internal let pointer: RPtr
/// Initializes an empty multi-asset container.
/// - Throws: CardanoError if initialization fails.
public init() throws {
self.pointer = try CSL.callRPtr { csl_bridge_multi_asset_new($0, $1) }
}
deinit {
var p = pointer
csl_bridge_rptr_free(&p)
}
/// Returns the number of policies in the multi-asset collection.
public func len() throws -> Int {
Int(try CSL.call { csl_bridge_multi_asset_len(pointer, $0, $1) })
}
/// Inserts a set of assets for a given policy ID.
/// - Parameters:
/// - policyId: The hexadecimal string of the policy ID (Script Hash).
/// - assets: The `Assets` collection to associate with this policy.
/// - Throws: CardanoError if insertion fails.
public func insert(policyId: String, assets: Assets) throws {
let policyPtr = try CSL.callRPtr { csl_bridge_script_hash_from_hex(policyId, $0, $1) }
_ = try CSL.callRPtr { csl_bridge_multi_asset_insert(pointer, policyPtr, assets.pointer, $0, $1) }
var p = policyPtr
csl_bridge_rptr_free(&p)
}
}
extension Data {
/// Internal helper to initialize Data from a hexadecimal string.
init(hex: String) {
self.init()
var hex = hex
if hex.hasPrefix("0x") { hex = String(hex.dropFirst(2)) }
var tempHex = hex
while tempHex.count > 0 {
let sub = String(tempHex.prefix(2))
tempHex = String(tempHex.dropFirst(2))
if let b = UInt8(sub, radix: 16) {
self.append(b)
}
}
}
}