Skip to content

Commit d891e38

Browse files
committed
Merge branch 'master' of github.com:nerzh/ton-sdk-swift-smc
2 parents 2ce3379 + 9de9ab9 commit d891e38

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

README.md

+52-41
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ Swift library for interaction with TON (The Open Network) smart contract
1313

1414
Install ton-sdk-swift-smc:
1515

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")),`
1717

1818
## Example
1919

2020
```swift
2121
import TonSdkSwiftSmc
22+
import BigInt
23+
import SwiftExtensionsPack
2224
import XCTest
2325

2426
final class TTests: XCTestCase {
@@ -27,62 +29,68 @@ final class TTests: XCTestCase {
2729
let publicKey: String = "..."
2830
let secretKey: String = "..."
2931
let apiKey: String = "..."
30-
32+
33+
34+
35+
/// HighloadWalletV2
3136
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())
3439
print("hash", try wallet.code.hash())
35-
let address = wallet.address.toString(type: .base64)
40+
let address: String = wallet.address.toString(type: .base64)
3641
print(address)
37-
print("transfer > 0.1 TON to this address:", address)
42+
print("transfer > 0.05 TON to this address:", address)
3843
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"
4045
while true {
4146
if !isInit { break }
4247
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)
4550
print("balance: \(balance)", coins.coinsValue)
4651
if coins.coinsValue >= 0.05 {
4752
break
4853
}
4954
}
5055
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] = [
5459
HighloadWalletTransfer(destination: wallet.address, bounce: false, value: Coins(0.0001), mode: 3, body: comment),
5560
HighloadWalletTransfer(destination: wallet.address, bounce: false, value: Coins(0.000101), mode: 3, body: comment2),
5661
]
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()
5964
let out = try await api.jsonRpc().send(boc: boc)
6065
print("result:", out.result?.toJSON() ?? "", "error:", out.error ?? "")
6166
}
62-
67+
68+
69+
70+
/// WalletV3
6371
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())
6674
print("hash", try wallet.code.hash())
67-
let address = wallet.address.toString(type: .base64)
75+
let address: String = wallet.address.toString(type: .base64)
6876
print(address)
69-
print("transfer > 0.1 TON to this address:", address)
77+
print("transfer > 0.05 TON to this address:", address)
7078
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"
7280
while true {
7381
if !isInit { break }
7482
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)
7785
print("balance: \(balance)", coins.coinsValue)
7886
if coins.coinsValue >= 0.05 {
7987
break
8088
}
8189
}
8290
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] = [
8694
WalletV3Transfer(destination: wallet.address, bounce: false, value: Coins(0.0001), mode: 3, body: comment),
8795
WalletV3Transfer(destination: wallet.address, bounce: false, value: Coins(0.000101), mode: 3, body: comment2),
8896
]
@@ -97,41 +105,44 @@ final class TTests: XCTestCase {
97105
seqno = currentSeqno
98106
}
99107

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()
102110
let out = try await api.jsonRpc().send(boc: boc)
103111
print("result:", out.result?.toJSON() ?? "", "error:", out.error ?? "")
104112
}
105-
113+
114+
115+
116+
/// WalletV4
106117
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())
109120
print("hash", try wallet.code.hash())
110-
let address = wallet.address.toString(type: .base64)
121+
let address: String = wallet.address.toString(type: .base64)
111122
print(address)
112-
print("transfer > 0.1 TON to this address:", address)
123+
print("transfer > 0.05 TON to this address:", address)
113124
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"
115126
while true {
116127
if !isInit { break }
117128
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)
120131
print("balance: \(balance)", coins.coinsValue)
121132
if coins.coinsValue >= 0.05 {
122133
break
123134
}
124135
}
125136
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] = [
129140
WalletV4Transfer(destination: wallet.address, bounce: false, value: Coins(0.0001), mode: 3, body: comment),
130141
WalletV4Transfer(destination: wallet.address, bounce: false, value: Coins(0.000101), mode: 3, body: comment2),
131142
]
132143

133144
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() ?? [:]
135146
if
136147
((wallet_info["exit_code"] as? Int) ?? -1) == 0,
137148
let seqnoStr = (wallet_info["stack"] as? [[String]])?.first?.last,
@@ -140,8 +151,8 @@ final class TTests: XCTestCase {
140151
seqno = currentSeqno
141152
}
142153

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()
145156
let out = try await api.jsonRpc().send(boc: boc)
146157
print("result:", out.result?.toJSON() ?? "", "error:", out.error ?? "")
147158
}

0 commit comments

Comments
 (0)