Kotlin SDK Quickstart
此内容尚不支持你的语言。
This guide will walk you through the process of setting up Kaptos, fetching data, and sending a transaction on the Aptos blockchain.
- 
Install the SDK Kaptos is available for both multiplatform and single-platform development. Artifacts are published at Sonatype Maven Central and can be added to your project using Gradle as shown below: Multiplatform DevelopmentSection titled “Multiplatform Development”In your build.gradle.ktsfile, and in yourcommonMainsource set block, add as follows:kotlin {sourceSets {commonMain.dependencies {implementation("xyz.mcxross.kaptos:kaptos:<version>")}}}Single-platform DevelopmentSection titled “Single-platform Development”Depending on your target platform, Kaptos provides different artifacts in the form of kaptos-jvm,kaptos-android,kaptos-iosArm64, andkaptos-js. For example, to add the JVM artifact to your project, add the following dependency:dependencies {implementation("xyz.mcxross.kaptos:kaptos-jvm:<version>")}To add the Android artifact, use: dependencies {implementation("xyz.mcxross.kaptos:kaptos-android:<version>")}
- 
Set up the Aptos client You can use the Aptosobject to handle everything that requires a connection to the Aptos network.val aptos = Aptos()If you want to pass in a custom configuration, you can do so by passing in a AptosConfig object that takes in an AptosSettings object. The AptosSettings object allows you to specify the network you want to connect to, the fullnode URL, and other settings. val settings = AptosSettings(network = Network.MAINNET, clientConfig = ClientConfig(maxRetries = 10))val aptosConfig = AptosConfig(settings = settings)val aptos = Aptos(aptosConfig)
- 
Fetch data from on-chain Once you have an Aptosobject, you can use it to fetch data from the Aptos blockchain. For example, you can fetch the ledger information like so:val ledgerInfo = aptos.getLedgerInfo()
- 
Send Transactions To interact with the ledger and change its state, you must send transactions. To do this, you need an existing account. You can create an account by generating a new account key pair and funding the account on-chain. Once you have an account, you can sign transactions to demonstrate authority, allowing you to perform actions such as transferring tokens, triggering Move modules, or trading NFTs. Here’s how you can build a transaction to transfer APT: - 
Create an Account To create a new account, you first generate new credentials then fund the account. On devnet networks, you can fund an account programmatically by asking a “faucet” val aliceAccount = Account.generate()val bobAccount = Account.generate()On testnet you can mint at the mint page. 
- 
Build the Transaction val txn = aptos.buildTransaction.simple(sender = aliceAccount.accountAddress,data = entryFunctionData {function = "0x1::coin::transfer"typeArguments = typeArguments {+TypeTagStruct("0x1::aptos_coin::AptosCoin")}functionArguments = functionArguments {+bobAccount.accountAddress+U64(SEND_AMOUNT)}},)
- 
Sign the Transaction Once you have built a transaction, you can sign it using the signmethod.val aliceAuthenticator = aptos.sign(sender = aliceAccount,transaction = txn,)
- 
Submit the Transaction Finally, you can submit the transaction to the network using the submitTransaction.simplemethod.val committedTransaction = aptos.submitTransaction.simple(transaction = signedTransaction,senderAuthenticator = aliceAuthenticator,)
- 
Wait for the Transaction to Execute Then you can wait for the transaction to be executed by using the waitForTransactionmethod.val executedTransaction = aptos.waitForTransaction(HexInput.fromString(committedTransaction.expect("Transaction failed").hash))
 
-