Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzh committed Jun 25, 2023
1 parent 360535c commit c5767f9
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 69 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Swift Client for TVM SDK (toncoin, everscale, venom, gosh)

<! --
<p align="center">
<a href="https://github.com/venom-blockchain/developer-program">
<img src="https://github.com/raw/venom-blockchain/developer-program/main/vf-dev-program.png" alt="Logo" width="366.8" height="146.4">
</a>
</p>
-->

[![SPM](https://img.shields.io/badge/swift-package%20manager-green)](https://swift.org/package-manager/)
[![SPM](https://img.shields.io/badge/SDK%20VERSION-1.42.1-orange)](https://github.com/tonlabs/ever-sdk)
[![SPM](https://img.shields.io/badge/SDK%20VERSION-1.43.3-orange)](https://github.com/tonlabs/ever-sdk)

Swift is a strongly typed language that has long been used not only for iOS development. Apple is actively promoting it to new platforms and today it can be used for almost any task. Thanks to this, this implementation provides the work of TVM (toncoin, everscale, venom, gosh) SDK on many platforms at once, including the native one for mobile phones. Let me remind you that swift can also be built for android.

Expand Down
15 changes: 11 additions & 4 deletions Sources/EverscaleClientSwift/Abi/AbiTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public struct TSDKAbi: Codable {
}

public struct TSDKFunctionHeader: Codable {
/// Message expiration time in seconds. If not specified - calculated automatically from message_expiration_timeout(), try_index and message_expiration_timeout_grow_factor() (if ABI includes `expire` header).
/// Message expiration timestamp (UNIX time) in seconds.
/// If not specified - calculated automatically from message_expiration_timeout(),try_index and message_expiration_timeout_grow_factor() (if ABI includes `expire` header).
public var expire: UInt32?
/// Message creation time in milliseconds.
/// If not specified, `now` is used (if ABI includes `time` header).
Expand Down Expand Up @@ -103,8 +104,12 @@ public struct TSDKCallSet: Codable {
}

public struct TSDKDeploySet: Codable {
/// Content of TVC file encoded in `base64`.
public var tvc: String
/// Content of TVC file encoded in `base64`. For compatibility reason this field can contain an encoded `StateInit`.
public var tvc: String?
/// Contract code BOC encoded with base64.
public var code: String?
/// State init BOC encoded with base64.
public var state_init: String?
/// Target workchain for destination address.
/// Default is `0`.
public var workchain_id: Int32?
Expand All @@ -117,8 +122,10 @@ public struct TSDKDeploySet: Codable {
/// 3. Public key, provided by Signer.
public var initial_pubkey: String?

public init(tvc: String, workchain_id: Int32? = nil, initial_data: AnyValue? = nil, initial_pubkey: String? = nil) {
public init(tvc: String? = nil, code: String? = nil, state_init: String? = nil, workchain_id: Int32? = nil, initial_data: AnyValue? = nil, initial_pubkey: String? = nil) {
self.tvc = tvc
self.code = code
self.state_init = state_init
self.workchain_id = workchain_id
self.initial_data = initial_data
self.initial_pubkey = initial_pubkey
Expand Down
67 changes: 51 additions & 16 deletions Sources/EverscaleClientSwift/Boc/Boc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@ public final class TSDKBocModule {
self.binding = binding
}

/// Decodes tvc according to the tvc spec. Read more about tvc structure here https://github.com/tonlabs/ever-struct/blob/main/src/scheme/mod.rs#L30
public func decode_tvc(_ payload: TSDKParamsOfDecodeTvc, _ handler: @escaping (TSDKBindingResponse<TSDKResultOfDecodeTvc, TSDKClientError>) throws -> Void
) throws {
let method: String = "decode_tvc"
try binding.requestLibraryAsync(methodName(module, method), payload) { (requestId, params, responseType, finished) in
var response: TSDKBindingResponse<TSDKResultOfDecodeTvc, TSDKClientError> = .init()
response.update(requestId, params, responseType, finished)
try handler(response)
}
}

/// Decodes tvc according to the tvc spec. Read more about tvc structure here https://github.com/tonlabs/ever-struct/blob/main/src/scheme/mod.rs#L30
@available(iOS 13, *)
@available(macOS 12, *)
public func decode_tvc(_ payload: TSDKParamsOfDecodeTvc) async throws -> TSDKResultOfDecodeTvc {
try await withCheckedThrowingContinuation { continuation in
do {
let method: String = "decode_tvc"
try binding.requestLibraryAsyncAwait(methodName(module, method), payload) { (requestId, params, responseType, finished) in
var response: TSDKBindingResponse<TSDKResultOfDecodeTvc, TSDKClientError> = .init()
response.update(requestId, params, responseType, finished)
if let error = response.error {
continuation.resume(throwing: error)
} else if let result = response.result {
continuation.resume(returning: result)
} else {
continuation.resume(throwing: TSDKClientError("Nothing for return"))
}
}
} catch {
continuation.resume(throwing: error)
}
}
}

/// Parses message boc into a JSON
/// JSON structure is compatible with GraphQL API message object
public func parse_message(_ payload: TSDKParamsOfParse, _ handler: @escaping (TSDKBindingResponse<TSDKResultOfParse, TSDKClientError>) throws -> Void
Expand Down Expand Up @@ -544,26 +579,26 @@ public final class TSDKBocModule {
}
}

/// Decodes tvc into code, data, libraries and special options.
public func decode_tvc(_ payload: TSDKParamsOfDecodeTvc, _ handler: @escaping (TSDKBindingResponse<TSDKResultOfDecodeTvc, TSDKClientError>) throws -> Void
/// Decodes contract's initial state into code, data, libraries and special options.
public func decode_state_init(_ payload: TSDKParamsOfDecodeStateInit, _ handler: @escaping (TSDKBindingResponse<TSDKResultOfDecodeStateInit, TSDKClientError>) throws -> Void
) throws {
let method: String = "decode_tvc"
let method: String = "decode_state_init"
try binding.requestLibraryAsync(methodName(module, method), payload) { (requestId, params, responseType, finished) in
var response: TSDKBindingResponse<TSDKResultOfDecodeTvc, TSDKClientError> = .init()
var response: TSDKBindingResponse<TSDKResultOfDecodeStateInit, TSDKClientError> = .init()
response.update(requestId, params, responseType, finished)
try handler(response)
}
}

/// Decodes tvc into code, data, libraries and special options.
/// Decodes contract's initial state into code, data, libraries and special options.
@available(iOS 13, *)
@available(macOS 12, *)
public func decode_tvc(_ payload: TSDKParamsOfDecodeTvc) async throws -> TSDKResultOfDecodeTvc {
public func decode_state_init(_ payload: TSDKParamsOfDecodeStateInit) async throws -> TSDKResultOfDecodeStateInit {
try await withCheckedThrowingContinuation { continuation in
do {
let method: String = "decode_tvc"
let method: String = "decode_state_init"
try binding.requestLibraryAsyncAwait(methodName(module, method), payload) { (requestId, params, responseType, finished) in
var response: TSDKBindingResponse<TSDKResultOfDecodeTvc, TSDKClientError> = .init()
var response: TSDKBindingResponse<TSDKResultOfDecodeStateInit, TSDKClientError> = .init()
response.update(requestId, params, responseType, finished)
if let error = response.error {
continuation.resume(throwing: error)
Expand All @@ -579,26 +614,26 @@ public final class TSDKBocModule {
}
}

/// Encodes tvc from code, data, libraries ans special options (see input params)
public func encode_tvc(_ payload: TSDKParamsOfEncodeTvc, _ handler: @escaping (TSDKBindingResponse<TSDKResultOfEncodeTvc, TSDKClientError>) throws -> Void
/// Encodes initial contract state from code, data, libraries ans special options (see input params)
public func encode_state_init(_ payload: TSDKParamsOfEncodeStateInit, _ handler: @escaping (TSDKBindingResponse<TSDKResultOfEncodeStateInit, TSDKClientError>) throws -> Void
) throws {
let method: String = "encode_tvc"
let method: String = "encode_state_init"
try binding.requestLibraryAsync(methodName(module, method), payload) { (requestId, params, responseType, finished) in
var response: TSDKBindingResponse<TSDKResultOfEncodeTvc, TSDKClientError> = .init()
var response: TSDKBindingResponse<TSDKResultOfEncodeStateInit, TSDKClientError> = .init()
response.update(requestId, params, responseType, finished)
try handler(response)
}
}

/// Encodes tvc from code, data, libraries ans special options (see input params)
/// Encodes initial contract state from code, data, libraries ans special options (see input params)
@available(iOS 13, *)
@available(macOS 12, *)
public func encode_tvc(_ payload: TSDKParamsOfEncodeTvc) async throws -> TSDKResultOfEncodeTvc {
public func encode_state_init(_ payload: TSDKParamsOfEncodeStateInit) async throws -> TSDKResultOfEncodeStateInit {
try await withCheckedThrowingContinuation { continuation in
do {
let method: String = "encode_tvc"
let method: String = "encode_state_init"
try binding.requestLibraryAsyncAwait(methodName(module, method), payload) { (requestId, params, responseType, finished) in
var response: TSDKBindingResponse<TSDKResultOfEncodeTvc, TSDKClientError> = .init()
var response: TSDKBindingResponse<TSDKResultOfEncodeStateInit, TSDKClientError> = .init()
response.update(requestId, params, responseType, finished)
if let error = response.error {
continuation.resume(throwing: error)
Expand Down
136 changes: 89 additions & 47 deletions Sources/EverscaleClientSwift/Boc/BocTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ public enum TSDKBocCacheTypeEnumTypes: String, Codable {
case Unpinned = "Unpinned"
}

public enum TSDKBuilderOpEnumTypes: String, Codable {
case Integer = "Integer"
case BitString = "BitString"
case Cell = "Cell"
case CellBoc = "CellBoc"
case Address = "Address"
}

public enum TSDKTvcEnumTypes: String, Codable {
case V1 = "V1"
}

public enum TSDKBocErrorCode: Int, Codable {
case InvalidBoc = 201
case SerializationError = 202
Expand All @@ -17,14 +29,6 @@ public enum TSDKBocErrorCode: Int, Codable {
case InvalidBocRef = 207
}

public enum TSDKBuilderOpEnumTypes: String, Codable {
case Integer = "Integer"
case BitString = "BitString"
case Cell = "Cell"
case CellBoc = "CellBoc"
case Address = "Address"
}

public struct TSDKBocCacheType: Codable {
public var type: TSDKBocCacheTypeEnumTypes
public var pin: String?
Expand All @@ -35,6 +39,71 @@ public struct TSDKBocCacheType: Codable {
}
}

public struct TSDKBuilderOp: Codable {
public var type: TSDKBuilderOpEnumTypes
/// Bit size of the value.
public var size: UInt32?
/// Value: - `Number` containing integer number.
/// e.g. `123`, `-123`. - Decimal string. e.g. `"123"`, `"-123"`.
/// - `0x` prefixed hexadecimal string.
/// e.g `0x123`, `0X123`, `-0x123`.
public var value: AnyValue?
/// Nested cell builder.
public var builder: [TSDKBuilderOp]?
/// Nested cell BOC encoded with `base64` or BOC cache key.
public var boc: String?
/// Address in a common `workchain:account` or base64 format.
public var address: String?

public init(type: TSDKBuilderOpEnumTypes, size: UInt32? = nil, value: AnyValue? = nil, builder: [TSDKBuilderOp]? = nil, boc: String? = nil, address: String? = nil) {
self.type = type
self.size = size
self.value = value
self.builder = builder
self.boc = boc
self.address = address
}
}

/// Cell builder operation.
public struct TSDKTvc: Codable {
public var type: TSDKTvcEnumTypes
public var value: TSDKTvcV1?

public init(type: TSDKTvcEnumTypes, value: TSDKTvcV1? = nil) {
self.type = type
self.value = value
}
}

public struct TSDKTvcV1: Codable {
public var code: String?
public var description: String?

public init(code: String? = nil, description: String? = nil) {
self.code = code
self.description = description
}
}

public struct TSDKParamsOfDecodeTvc: Codable {
/// Contract TVC BOC encoded as base64 or BOC handle
public var tvc: String

public init(tvc: String) {
self.tvc = tvc
}
}

public struct TSDKResultOfDecodeTvc: Codable {
/// Decoded TVC
public var tvc: TSDKTvc

public init(tvc: TSDKTvc) {
self.tvc = tvc
}
}

public struct TSDKParamsOfParse: Codable {
/// BOC encoded as base64
public var boc: String
Expand Down Expand Up @@ -192,33 +261,6 @@ public struct TSDKParamsOfBocCacheUnpin: Codable {
}
}

public struct TSDKBuilderOp: Codable {
public var type: TSDKBuilderOpEnumTypes
/// Bit size of the value.
public var size: UInt32?
/// Value: - `Number` containing integer number.
/// e.g. `123`, `-123`. - Decimal string. e.g. `"123"`, `"-123"`.
/// - `0x` prefixed hexadecimal string.
/// e.g `0x123`, `0X123`, `-0x123`.
public var value: AnyValue?
/// Nested cell builder.
public var builder: [TSDKBuilderOp]?
/// Nested cell BOC encoded with `base64` or BOC cache key.
public var boc: String?
/// Address in a common `workchain:account` or base64 format.
public var address: String?

public init(type: TSDKBuilderOpEnumTypes, size: UInt32? = nil, value: AnyValue? = nil, builder: [TSDKBuilderOp]? = nil, boc: String? = nil, address: String? = nil) {
self.type = type
self.size = size
self.value = value
self.builder = builder
self.boc = boc
self.address = address
}
}

/// Cell builder operation.
public struct TSDKParamsOfEncodeBoc: Codable {
/// Cell builder operations.
public var builder: [TSDKBuilderOp]
Expand Down Expand Up @@ -288,19 +330,19 @@ public struct TSDKResultOfSetCodeSalt: Codable {
}
}

public struct TSDKParamsOfDecodeTvc: Codable {
/// Contract TVC image BOC encoded as base64 or BOC handle
public var tvc: String
public struct TSDKParamsOfDecodeStateInit: Codable {
/// Contract StateInit image BOC encoded as base64 or BOC handle
public var state_init: String
/// Cache type to put the result. The BOC itself returned if no cache type provided.
public var boc_cache: TSDKBocCacheType?

public init(tvc: String, boc_cache: TSDKBocCacheType? = nil) {
self.tvc = tvc
public init(state_init: String, boc_cache: TSDKBocCacheType? = nil) {
self.state_init = state_init
self.boc_cache = boc_cache
}
}

public struct TSDKResultOfDecodeTvc: Codable {
public struct TSDKResultOfDecodeStateInit: Codable {
/// Contract code BOC encoded as base64 or BOC handle
public var code: String?
/// Contract code hash
Expand Down Expand Up @@ -341,7 +383,7 @@ public struct TSDKResultOfDecodeTvc: Codable {
}
}

public struct TSDKParamsOfEncodeTvc: Codable {
public struct TSDKParamsOfEncodeStateInit: Codable {
/// Contract code BOC encoded as base64 or BOC handle
public var code: String?
/// Contract data BOC encoded as base64 or BOC handle
Expand Down Expand Up @@ -370,12 +412,12 @@ public struct TSDKParamsOfEncodeTvc: Codable {
}
}

public struct TSDKResultOfEncodeTvc: Codable {
/// Contract TVC image BOC encoded as base64 or BOC handle of boc_cache parameter was specified
public var tvc: String
public struct TSDKResultOfEncodeStateInit: Codable {
/// Contract StateInit image BOC encoded as base64 or BOC handle of boc_cache parameter was specified
public var state_init: String

public init(tvc: String) {
self.tvc = tvc
public init(state_init: String) {
self.state_init = state_init
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/EverscaleClientSwift/Client/ClientTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public struct TSDKClientError: Codable, LocalizedError {
public var failureReason: String? { self.message }
public var recoverySuggestion: String? { self.message }
public var helpAnchor: String? { self.message }
public var data: AnyValue = ([:] as! [String: Any]).toAnyValue()
public var data: AnyValue = [String: Any]().toAnyValue()

public init(_ error: Error) {
self.code = 0
Expand Down
1 change: 1 addition & 0 deletions Sources/EverscaleClientSwift/Tvm/TvmTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum TSDKTvmErrorCode: Int, Codable {
case InvalidAccountBoc = 412
case InvalidMessageType = 413
case ContractExecutionError = 414
case AccountIsSuspended = 415
}

public enum TSDKAccountForExecutorEnumTypes: String, Codable {
Expand Down

0 comments on commit c5767f9

Please sign in to comment.