Skip to content

mcxross/ksui

Repository files navigation

Ksui - Multiplatform SDK for Sui

Ksui, /keɪˈsuːiː/ (pronounced as "kay-soo-ee"), is a Kotlin Multiplatform (KMP) SDK for integrating with the Sui blockchain.

It is designed to be a type-safe, client-configurable, and multiplatform SDK that can be used across different platforms such as Android, iOS, JS, and JVM. It is built on top of the KMM toolchain and is designed to be extensible and easy to use.

Kotlin Version Docs Publish Maven Central Snapshot License

badge-android badge-ios badge-js badge-jvm badge-linux badge-macos badge-windows

Table of contents

Features

  • Multiplatform (Android, iOS, JS, JVM)
  • Type-safe intuitive API
  • Client Configurable (Retries, Timeout, etc)
  • Asynchronous client
  • Coroutine based
  • Expressive DSL for PTB construction
  • Plus everything else you would expect from a Sui SDK

Quick Start

Installation

Multiplatform

Add the Ksui dependency to the common sourceSet

implementation("xyz.mcxross.ksui:ksui:<$ksui_version>")

Platform specific (Android, JS, Native, JVM)

Add the Ksui dependency to the Project's dependency block

Generic:

implementation("xyz.mcxross.ksui:<ksui-[platform]>:<$ksui_version>")

For example for Android and JS

Android:

implementation("xyz.mcxross.ksui:ksui-android:<$ksui_version>")

Account Management

Generating a new account

To generate a new Sui account, simply call the static create method on the Account class as shown below:

val yourAccount = Account.create()

This generates a new account with a random mnemonic, public key, and address.

Importing an existing account

From a private key

There are a couple of ways to import an existing account. One way is to import an account from a private key as a private key object:

val privateKey = PrivateKey.fromEncoded("suipri...8cpv0g")

val yourAccount = Account.import(privateKey)

or as a string:

val yourAccount = Account.import("suipri...8cpv0g")

Note

Ksui adheres to the standard Sui private key format of encoding the private key in Bech32 format as proposed in SIP-15.

You can also import an account from a mnemonic:

val mnemonic = "abandon salad ..."
val yourAccount = Account.import(mnemonic)

Initialization

First, you need to create a new instance of the Sui RPC HTTP Client as shown below:

val sui = Sui()

In case you want to configure the client, you can do so as shown below:

val config = SuiConfig(settings = SuiSettings(network = Network.MAINNET))
val sui = Sui(config)

Now you can use the sui instance to interact with the Sui chain for reading and writing.

Reading from the chain

Once you have initialized the client, you can use it to read from the chain. For example, to get the balance of an address:

val balance = sui.getBalance(AccountAddress("0x4afc81d797fd02bd7e923389677352eb592d55a00b65067fa582c05f62b4788b"))

Writing to the chain (PTBs)

To write to the chain, you need to create a Transaction and sign it with the private key of the sender. A transaction, among its metadata, is made up of PTBs which are a chain of commands that are executed by the chain. For example, to construct a PTB that splits a coin and sends it to another address, you can do so as shown below:

// Assuming you have an account object
val alice = Account.import("suipri...8cpv0g")

// Create a programmable transaction
val ptb = programmableTx {
    command {
        // Split the coin
        val splitCoins = splitCoins {
            coin = Argument.GasCoin
            into = inputs(1_000_000UL)
        }

        // Send the split coin to the receiver
        transferObjects {
            objects = inputs(splitCoins)
            to = input(alice.address)
        }
    }
}

// Sign and execute txn
val txn = sui.signAndExecuteTransactionBlock(alice, ptb)

For more information, please see the documentation.

What's included

File/Folder Description
lib Library implementation folder. It contains the code for Ksui that can be used across multiple platforms
sample Samples on how to use the exported APIs

Contribution

All contributions to Ksui are welcome. Before opening a PR, please submit an issue detailing the bug or feature. When opening a PR, please ensure that your contribution builds on the KMM toolchain, has been linted with ktfmt <GOOGLE (INTERNAL)>, and contains tests when applicable. For more information, please see the contribution guidelines.

License

Copyright 2024 McXross

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.