Skip to content

Kotlin SDK

Kaptos is a Kotlin Multiplatform SDK for interacting with the Aptos blockchain across various platforms. It offers a consistent API for data requests, transaction submissions, and more, facilitating cross-platform app development with shared business logic. The SDK includes asynchronous Aptos clients for smooth blockchain interactions.

Kaptos also provides platform-specific SDKs for JVM, Android, iOS, JS, Linux, macOS, and Windows.

Android BadgeiOS BadgetvOS BadgewatchOS BadgeNodeJS BadgeJVM BadgeLinux BadgemacOS BadgeWindows Badge
Maven Central

Static Badge

  • Type-safe: The SDK is fully type-safe and provides a rich set of types for all operations.
  • Expressive: Kaptos provides a simple and expressive DSL-style API for building transactions.
  • Multiplatform: Write cross-platform applications with shared business logic.
  • Consistent API: All operations bare a uniform and consistent API across all platforms.
  • BCS Support: The SDK defaults to BCS for serialization and deserialization of transactions.
  • Asynchronous: All blockchain operations are asynchronous.
  • Configurable: The SDK provides highly configurable clients for all platforms.
commonMain.dependencies {
implementation("xyz.mcxross.kaptos:kaptos:<version>")
}

Below is an example of how you can perform a transaction using the Kotlin SDK. The snippet demonstrates how to build a transaction to transfer APT. We then sign and submit the transaction to the blockchain in a single step.

APTTransfer.kt
58 collapsed lines
/*
* 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.
*/
package xyz.mcxross.kaptos.sample
import xyz.mcxross.kaptos.Aptos
import xyz.mcxross.kaptos.account.Account
import xyz.mcxross.kaptos.model.*
import xyz.mcxross.kaptos.util.runBlocking
const val FUNDING_AMOUNT = 100_000_000L
const val SEND_AMOUNT_APT = 0.5f
const val UNIT_CONVERSION = 100_000_000
const val SEND_AMOUNT_UNITS = (SEND_AMOUNT_APT * UNIT_CONVERSION)
const val SEND_AMOUNT = 1_000_000UL
/**
* This example demonstrates how to transfer APT from one account to another.
*
* Each run generates and creates new accounts on-chain using faucet funding. After funding, the APT
* balance of each account is printed; if funding fails, an error is thrown.
*
* Next, a transaction is constructed to send 0.5 APT from Alice to Bob. The transaction is then
* signed and submitted using the one-step `signAndSubmitTransaction` method. We wait for the
* transaction to complete and print the updated balances of Alice and Bob. If the transaction
* fails, an error is thrown.
*/
fun main() = runBlocking {
val aptos = Aptos(AptosConfig(AptosSettings(network = Network.TESTNET)))
println("Generating Alice and Bob's accounts")
val alice = Account.generate()
val bob = Account.generate()
aptos.fundAccount(alice.accountAddress, FUNDING_AMOUNT).expect("Failed to fund Alice's account")
aptos.fundAccount(bob.accountAddress, FUNDING_AMOUNT).expect("Failed to fund Bob's account")
println("Created accounts on chain")
println("Alice's balance: ${aptos.getAccountAPTAmount(alice.accountAddress)}")
println("Bob's balance: ${aptos.getAccountAPTAmount(bob.accountAddress)}")
println("=============================================")
println(
"Building transaction to send ${SEND_AMOUNT / 100_000_000u} APT to Bob: ${bob.accountAddress}"
)
val txn =
aptos.buildTransaction.simple(
sender = alice.accountAddress,
data =
entryFunctionData {
function = "0x1::coin::transfer"
typeArguments = typeArguments { +TypeTagStruct("0x1::aptos_coin::AptosCoin") }
functionArguments = functionArguments {
+bob.accountAddress
+U64(SEND_AMOUNT_UNITS.toULong())
}
},
)
21 collapsed lines
// Sign and submit the transaction
val commitedTransaction = aptos.signAndSubmitTransaction(alice, txn)
val executedTransaction =
aptos.waitForTransaction(
HexInput.fromString(commitedTransaction.expect("Transaction failed").hash)
)
println(
"Transaction wait response: $executedTransaction\n============================================="
)
val aliceNewBalance =
aptos.getAccountAPTAmount(alice.accountAddress).expect("Alice's account does not exist")
val bobNewBalance =
aptos.getAccountAPTAmount(bob.accountAddress).expect("Bob's account does not exist")
println("Alice's new balance: $aliceNewBalance")
println("Bob's new balance: $bobNewBalance")
}

The SDK also provides pre-built methods for common transaction operations. For example, you can use the transferCoinTransaction method to generate a transfer transaction between two accounts as shown below:

val txn = aptos.transferCoinTransaction(
alice.accountAddress,
bob.accountAddress,
SEND_AMOUNT
)

You can then sign and submit this transaction.

For more examples on how and what you can do with the Kotlin SDK, check out the following: