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.
- 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
Add the Ksui
dependency to the common sourceSet
implementation("xyz.mcxross.ksui:ksui:<$ksui_version>")
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>")
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.
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)
To get started, create an instance of the Sui client. This single step also automatically configures a default,
globally-accessible client that can be used by top-level functions like ptb
.
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.
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"))
To write to the chain, you build a Programmable Transaction Block (PTB). The SDK provides an expressive DSL that makes this
process simple and intuitive. The top level ptb
function automatically uses the default client you initialized to resolve object
details, so you don't need to pass it explicitly.
For example, to construct a PTB that splits a coin and sends it to another address:
// Assuming you have an account object
val alice = Account.import("suipri...8cpv0g")
// Create a programmable transaction
val ptb = ptb {
val coins = splitCoins {
coin = Argument.GasCoin
into = listOf(pure(100_000_000UL))
}
transferObjects {
objects = coins
to = address("0xbf...cde")
}
}
// Sign and execute txn
val txn = sui.signAndExecuteTransactionBlock(alice, ptb)
For more information, please see the documentation.
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 |
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.
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.