Skip to content

Swift Binding for Everscale, Venom SDK. Library for interacting with Everscale, Venom blockchain.

License

Notifications You must be signed in to change notification settings

nerzh/everscale-client-swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift Client for Free Ton SDK

SPM

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 TonSDK on many platforms at once, including the native one for mobile phones. Let me remind you that swift can also be built for android.

OS Result
MacOS
Linux
iOS
Windows Soon

Setup TONSDK For Linux and MacOS

Install sdk with bash script

  1. create folder for TONSDK, for example mkdir ./TONSDK
  2. cd ./TONSDK
  3. bash path_to_this_library/scripts/install_tonsdk.sh

Manual install sdk

  1. Install Rust to your OS

  2. git clone https://github.com/tonlabs/TON-SDK

  3. cd ./TON-SDK

  4. cargo update

  5. cargo build --release

  6. copy or create symlink of dynamic library
    macOS :
    ./TON-SDK/target/release/libton_client.dylib
    to
    /usr/local/lib/libton_client.dylib

    Linux :
    ./TON-SDK/target/release/libton_client.so
    to
    /usr/lib/libton_client.so

  7. Create pkgConfig file :

macOS :
/usr/local/lib/pkgconfig/libton_client.pc

prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: ton_client
Description: ton_client
Version: 1.0.0
Cflags: -I${includedir}
Libs: -L${libdir} -lton_client

Linux:
/usr/lib/pkgconfig/libton_client.pc

prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: ton_client
Description: ton_client
Version: 1.0.0
Cflags: -I${includedir}
Libs: -L${libdir} -lton_client
  1. Copy or create symlink of file
    /TON-SDK/ton_client/client/tonclient.h
    to
    MacOS:
    /usr/local/include/tonclient.h
    Linux:
    /usr/include/tonclient.h

Setup TONSDK For iOS

  1. The Cargo.toml for ton_client must notify cargo to create static and dynamic C libraries for our code
[lib]
name = "ton_client"
crate-type = ["cdylib", "staticlib"]
  1. cargo install cargo-lipo
  2. rustup target add aarch64-apple-ios x86_64-apple-ios
cd ./TON-SDK
cargo lipo --release
  1. In xcode File > Add files to "Name Your Project" navigate to ./TON-SDK/ton_client/tonclient.h
  2. Create bridge. In xcode File > New > File, select Header File, set name for example Tonclient-Bridging-Header.h and add this code:
#ifndef Tonclient_Bridging_Header_h
#define Tonclient_Bridging_Header_h

#include <stdbool.h>
#import "tonclient.h"

#endif
  1. Add path to search for headers ( path to tonclient.h )
  2. Add path to search for libraries ( path to libton_client.a )
  3. Build ...

Usage

All requests are async.

import TonClientSwift

var config: TSDKClientConfig = .init()
config.network = TSDKNetworkConfig(server_address: "https://net.ton.dev")
let client: TSDKClientModule = .init(config: config)

// Crypto
client.crypto.factorize(TSDKParamsOfFactorize(composite: "17ED48941A08F981")) { (response) in
    print(response.result?.factors)
}

// Boc
let payload: TSDKParamsOfParse = .init(bocEncodedBase64: "te6ccgEBAQEAWAAAq2n+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE/zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzSsG8DgAAAAAjuOu9NAL7BxYpA")
client.boc.parse_message(payload) { (response) in
    if let result = response.result, let parsed: [String: Any] = result.parsed.toDictionary() {
        print(parsed["id"])
        print(parsed["src"])
        print(parsed["dst"])
    }
}

Errors

client.crypto.factorize(TSDKParamsOfFactorize(composite: "17ED48941A08F981")) { (response) in
    if let error = response.error {
        print(error.data.toJSON())
        print(error.code)
    }
}

Tests

If you use Xcode for Test

Please, set custom working directory to project folder for your xcode scheme. This is necessary for the relative path "./" to the project folders to work. You may change it with the xcode edit scheme menu.

Or inside file path_to_this_library/.swiftpm/xcode/xcshareddata/xcschemes/TonClientSwift.xcscheme set to tag "LaunchAction" absolute path to this library with options:
useCustomWorkingDirectory = "YES"
customWorkingDirectory = "/path_to_ton_sdk"

Tests

  1. inside root directory of ton-client-swift create .env.debug file with NET TON DEV
server_address=https://net.ton.dev
giver_address=0:653b9a6452c7a982c6dc92b2da9eba832ade1c467699ebb3b43dca6d77b780dd
giver_abi_name=Giver
giver_function=grant

Optional: Install locale NodeSE for tests if you needed:

  • launch docker
  • install nodejs to your OS
  • npm install -g ton-dev-cli
  • ton start
    by default nodeSE will start on localhost:80
    NODE SE
server_address=http:https://localhost:80
giver_address=0:841288ed3b55d9cdafa806807f02a0ae0c169aa5edfe88a789a6482429756a94
giver_abi_name=GiverNodeSE
giver_function=sendGrams
giver_amount=10000000000
  1. Run Tests
    MacOS:
    Run Tests inside Xcode
    Linux:
    swift test --generate-linuxmain
    swift test --enable-test-discovery