@@ -13,12 +13,14 @@ Swift library for interaction with TON (The Open Network) smart contract
13
13
14
14
Install ton-sdk-swift-smc:
15
15
16
- - ` .package(url: "https://github.com/nerzh/ton-sdk-swift-smc", .upToNextMajor(from: "0 .0.1 ")), `
16
+ - ` .package(url: "https://github.com/nerzh/ton-sdk-swift-smc", .upToNextMajor(from: "1 .0.0 ")), `
17
17
18
18
## Example
19
19
20
20
``` swift
21
21
import TonSdkSwiftSmc
22
+ import BigInt
23
+ import SwiftExtensionsPack
22
24
import XCTest
23
25
24
26
final class TTests : XCTestCase {
@@ -27,62 +29,68 @@ final class TTests: XCTestCase {
27
29
let publicKey: String = " ..."
28
30
let secretKey: String = " ..."
29
31
let apiKey: String = " ..."
30
-
32
+
33
+
34
+
35
+ /// HighloadWalletV2
31
36
func testHighloadWalletV2 () async throws {
32
- let api = ToncenterApi (apiKey : apiKey, protocol : .https )
33
- let wallet = try HighloadWalletV2 (publicKey : publicKey.hexToBytes ())
37
+ let api: ToncenterApi = . init (apiKey : apiKey, protocol : .https )
38
+ let wallet: HighloadWalletV2 = try . init (publicKey : publicKey.hexToBytes ())
34
39
print (" hash" , try wallet.code .hash ())
35
- let address = wallet.address .toString (type : .base64 )
40
+ let address: String = wallet.address .toString (type : .base64 )
36
41
print (address)
37
- print (" transfer > 0.1 TON to this address:" , address)
42
+ print (" transfer > 0.05 TON to this address:" , address)
38
43
print (" awaiting deposit ..." )
39
- let isInit = try await api.jsonRpc ().getAddressInformation (address : address).result ? .state .lowercased () != " active"
44
+ let isInit: Bool = try await api.jsonRpc ().getAddressInformation (address : address).result ? .state .lowercased () != " active"
40
45
while true {
41
46
if ! isInit { break }
42
47
try await Task.sleep (nanoseconds : 1_000_000_000 )
43
- let balance = BigInt ((try await api.jsonRpc ().getAddressBalance (address : address).result ? .toJSON ().replace (#" \""# , " " ) ?? " 0" )) ?? 0
44
- let coins = Coins (nanoValue : balance)
48
+ let balance: BigInt = . init ((try await api.jsonRpc ().getAddressBalance (address : address).result ? .toJSON ().replace (#" \""# , " " ) ?? " 0" )) ?? 0
49
+ let coins: Coins = . init (nanoValue : balance)
45
50
print (" balance: \( balance ) " , coins.coinsValue )
46
51
if coins.coinsValue >= 0.05 {
47
52
break
48
53
}
49
54
}
50
55
print (" got deposit, initializing transfer to itself..." )
51
- let comment = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction" ).cell ()
52
- let comment2 = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction 2" ).cell ()
53
- let transfers = [
56
+ let comment: Cell = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction" ).cell ()
57
+ let comment2: Cell = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction 2" ).cell ()
58
+ let transfers: [HighloadWalletTransfer] = [
54
59
HighloadWalletTransfer (destination : wallet.address , bounce : false , value : Coins (0.0001 ), mode : 3 , body : comment),
55
60
HighloadWalletTransfer (destination : wallet.address , bounce : false , value : Coins (0.000101 ), mode : 3 , body : comment2),
56
61
]
57
- let transfer = try wallet.buildTransfer (transfers : transfers, secret32Byte : secretKey.hexToBytes (), isInit : isInit)
58
- let boc = try Boc.serialize (root : [transfer.cell ()]).toBase64 ()
62
+ let transfer: Message = try wallet.buildTransfer (transfers : transfers, secret32Byte : secretKey.hexToBytes (), isInit : isInit)
63
+ let boc: String = try Boc.serialize (root : [transfer.cell ()]).toBase64 ()
59
64
let out = try await api.jsonRpc ().send (boc : boc)
60
65
print (" result:" , out.result ? .toJSON () ?? " " , " error:" , out.error ?? " " )
61
66
}
62
-
67
+
68
+
69
+
70
+ /// WalletV3
63
71
func testWalletV3 () async throws {
64
- let api = ToncenterApi (apiKey : apiKey, protocol : .https )
65
- let wallet = try WalletV3 (pubkey : publicKey.hexToBytes ())
72
+ let api: ToncenterApi = . init (apiKey : apiKey, protocol : .https )
73
+ let wallet: WalletV3 = try . init (pubkey : publicKey.hexToBytes ())
66
74
print (" hash" , try wallet.code .hash ())
67
- let address = wallet.address .toString (type : .base64 )
75
+ let address: String = wallet.address .toString (type : .base64 )
68
76
print (address)
69
- print (" transfer > 0.1 TON to this address:" , address)
77
+ print (" transfer > 0.05 TON to this address:" , address)
70
78
print (" awaiting deposit ..." )
71
- let isInit = try await api.jsonRpc ().getAddressInformation (address : address).result ? .state .lowercased () != " active"
79
+ let isInit: Bool = try await api.jsonRpc ().getAddressInformation (address : address).result ? .state .lowercased () != " active"
72
80
while true {
73
81
if ! isInit { break }
74
82
try await Task.sleep (nanoseconds : 1_000_000_000 )
75
- let balance = BigInt ((try await api.jsonRpc ().getAddressBalance (address : address).result ? .toJSON ().replace (#" \""# , " " ) ?? " 0" )) ?? 0
76
- let coins = Coins (nanoValue : balance)
83
+ let balance: BigInt = . init ((try await api.jsonRpc ().getAddressBalance (address : address).result ? .toJSON ().replace (#" \""# , " " ) ?? " 0" )) ?? 0
84
+ let coins: Coins = . init (nanoValue : balance)
77
85
print (" balance: \( balance ) " , coins.coinsValue )
78
86
if coins.coinsValue >= 0.05 {
79
87
break
80
88
}
81
89
}
82
90
print (" got deposit, initializing transfer to itself..." )
83
- let comment = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction" ).cell ()
84
- let comment2 = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction 2" ).cell ()
85
- let transfers = [
91
+ let comment: Cell = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction" ).cell ()
92
+ let comment2: Cell = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction 2" ).cell ()
93
+ let transfers: [WalletV3Transfer] = [
86
94
WalletV3Transfer (destination : wallet.address , bounce : false , value : Coins (0.0001 ), mode : 3 , body : comment),
87
95
WalletV3Transfer (destination : wallet.address , bounce : false , value : Coins (0.000101 ), mode : 3 , body : comment2),
88
96
]
@@ -97,41 +105,44 @@ final class TTests: XCTestCase {
97
105
seqno = currentSeqno
98
106
}
99
107
100
- let transfer = try wallet.buildTransfer (transfers : transfers, seqno : seqno, privateKey : secretKey.hexToBytes (), isInit : isInit)
101
- let boc = try Boc.serialize (root : [transfer.cell ()]).toBase64 ()
108
+ let transfer: Message = try wallet.buildTransfer (transfers : transfers, seqno : seqno, privateKey : secretKey.hexToBytes (), isInit : isInit)
109
+ let boc: String = try Boc.serialize (root : [transfer.cell ()]).toBase64 ()
102
110
let out = try await api.jsonRpc ().send (boc : boc)
103
111
print (" result:" , out.result ? .toJSON () ?? " " , " error:" , out.error ?? " " )
104
112
}
105
-
113
+
114
+
115
+
116
+ /// WalletV4
106
117
func testWalletV4 () async throws {
107
- let api = ToncenterApi (apiKey : apiKey, protocol : .https )
108
- let wallet = try WalletV4 (pubkey : publicKey.hexToBytes ())
118
+ let api: ToncenterApi = . init (apiKey : apiKey, protocol : .https )
119
+ let wallet: WalletV4 = try . init (pubkey : publicKey.hexToBytes ())
109
120
print (" hash" , try wallet.code .hash ())
110
- let address = wallet.address .toString (type : .base64 )
121
+ let address: String = wallet.address .toString (type : .base64 )
111
122
print (address)
112
- print (" transfer > 0.1 TON to this address:" , address)
123
+ print (" transfer > 0.05 TON to this address:" , address)
113
124
print (" awaiting deposit ..." )
114
- let isInit = try await api.jsonRpc ().getAddressInformation (address : address).result ? .state .lowercased () != " active"
125
+ let isInit: Bool = try await api.jsonRpc ().getAddressInformation (address : address).result ? .state .lowercased () != " active"
115
126
while true {
116
127
if ! isInit { break }
117
128
try await Task.sleep (nanoseconds : 1_000_000_000 )
118
- let balance = BigInt ((try await api.jsonRpc ().getAddressBalance (address : address).result ? .toJSON ().replace (#" \""# , " " ) ?? " 0" )) ?? 0
119
- let coins = Coins (nanoValue : balance)
129
+ let balance: BigInt = . init ((try await api.jsonRpc ().getAddressBalance (address : address).result ? .toJSON ().replace (#" \""# , " " ) ?? " 0" )) ?? 0
130
+ let coins: Coins = . init (nanoValue : balance)
120
131
print (" balance: \( balance ) " , coins.coinsValue )
121
132
if coins.coinsValue >= 0.05 {
122
133
break
123
134
}
124
135
}
125
136
print (" got deposit, initializing transfer to itself..." )
126
- let comment = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction" ).cell ()
127
- let comment2 = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction 2" ).cell ()
128
- let transfers = [
137
+ let comment: Cell = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction" ).cell ()
138
+ let comment2: Cell = try CellBuilder ().storeUInt (0 , 32 ).storeString (" My first transaction 2" ).cell ()
139
+ let transfers: [WalletV4Transfer] = [
129
140
WalletV4Transfer (destination : wallet.address , bounce : false , value : Coins (0.0001 ), mode : 3 , body : comment),
130
141
WalletV4Transfer (destination : wallet.address , bounce : false , value : Coins (0.000101 ), mode : 3 , body : comment2),
131
142
]
132
143
133
144
var seqno: UInt32 = 0
134
- let wallet_info = try await api.jsonRpc ().runGetMethod (address : address, method : " seqno" , stack : []).result ? .toDictionary () ?? [: ]
145
+ let wallet_info: [ String : String ] = try await api.jsonRpc ().runGetMethod (address : address, method : " seqno" , stack : []).result ? .toDictionary () ?? [: ]
135
146
if
136
147
((wallet_info[" exit_code" ] as? Int ) ?? -1 ) == 0 ,
137
148
let seqnoStr = (wallet_info[" stack" ] as? [[String ]])? .first ? .last ,
@@ -140,8 +151,8 @@ final class TTests: XCTestCase {
140
151
seqno = currentSeqno
141
152
}
142
153
143
- let transfer = try wallet.buildTransfer (transfers : transfers, seqno : seqno, privateKey : secretKey.hexToBytes (), isInit : isInit)
144
- let boc = try Boc.serialize (root : [transfer.cell ()]).toBase64 ()
154
+ let transfer: Message = try wallet.buildTransfer (transfers : transfers, seqno : seqno, privateKey : secretKey.hexToBytes (), isInit : isInit)
155
+ let boc: String = try Boc.serialize (root : [transfer.cell ()]).toBase64 ()
145
156
let out = try await api.jsonRpc ().send (boc : boc)
146
157
print (" result:" , out.result ? .toJSON () ?? " " , " error:" , out.error ?? " " )
147
158
}
0 commit comments