Skip to content

Commit

Permalink
update samples...
Browse files Browse the repository at this point in the history
  • Loading branch information
astinz committed Apr 27, 2024
1 parent 7903999 commit c910b69
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ class MainActivity : ComponentActivity() {
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
var created by remember { mutableStateOf(false) }
var keyDetails by remember { mutableStateOf<KeyDetails?>(null) }

if (!created) {
Wallet(Modifier.padding()) {
Wallet(
Modifier.padding(),
{
val details = SuiCommons.derive.importKey(it)
keyDetails = details
created = true
},
) {
val details = SuiCommons.derive.newKey()
keyDetails = details
created = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package xyz.mcxross.ksui.android.controller

import android.util.Log
import xyz.mcxross.ksui.client.EndPoint
import xyz.mcxross.ksui.client.suiHttpClient
import xyz.mcxross.ksui.extension.asObjectReference
import xyz.mcxross.ksui.model.Argument
import xyz.mcxross.ksui.model.IntentMessage
import xyz.mcxross.ksui.model.IntentType
import xyz.mcxross.ksui.model.SuiAddress
import xyz.mcxross.ksui.model.TransactionDataComposer
import xyz.mcxross.ksui.model.programmableTx
import xyz.mcxross.ksui.util.bcs
import xyz.mcxross.ksui.util.fullTxBlockResponseOptions
import xyz.mcxross.ksui.util.inputs
import xyz.mcxross.sc.SuiCommons

suspend fun send(sk: String, from: SuiAddress, toSuiAddress: SuiAddress) {
val suiRpcClient = suiHttpClient {
endpoint = EndPoint.DEVNET
agentName = "KSUI/0.0.1"
maxRetries = 10
}

val paymentObject = suiRpcClient.getCoins(SuiAddress(from.pubKey), limit = 5)

Log.d("MainActivity", paymentObject.data.map { it.asObjectReference() }.toString())

val gasPrice = suiRpcClient.getReferenceGasPrice()

Log.d("MainActivity", "Reference Gas Price: $gasPrice")

val pt = programmableTx {
command {
val splitCoins = splitCoins {
coin = Argument.GasCoin
into = inputs(1_000.toULong())
}

/*transferObjects {
objects = inputs(splitCoins)
to = input(toSuiAddress.pubKey)
}*/
}
}

val txData =
TransactionDataComposer.programmable(
from,
listOf(paymentObject.data[0].asObjectReference()),
pt,
5_000_000UL,
gasPrice.cost.toULong(),
)

val intentMessage = IntentMessage(IntentType.SUI_TX.intent(), txData)

val sig = SuiCommons.utils.suiSign(bcs(intentMessage), sk)

Log.d("MainActivity", "Signature: $sig")

Log.d("MainActivity", "Sending transaction")

val result =
suiRpcClient.executeTransactionBlock(
SuiCommons.encode.encodeBase64(bcs(intentMessage)),
listOf(sig),
fullTxBlockResponseOptions(),
)

Log.d("MainActivity", "Transaction result: $result")
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import java.util.*
import kotlinx.coroutines.launch
import xyz.mcxross.ksui.android.controller.send
import xyz.mcxross.ksui.client.EndPoint
import xyz.mcxross.ksui.client.suiHttpClient
import xyz.mcxross.ksui.model.SuiAddress
Expand All @@ -49,7 +50,7 @@ fun Account(context: Context, keyDetails: KeyDetails?) {

val prepSending = remember { mutableStateOf(false) }

val toAddress = remember { mutableStateOf("") }
val recipient = remember { mutableStateOf("") }

LaunchedEffect(key1 = updateTrigger.intValue) {
balance.longValue = suiHttpClient.getBalance(SuiAddress(keyDetails?.address ?: "")).totalBalance
Expand Down Expand Up @@ -100,23 +101,43 @@ fun Account(context: Context, keyDetails: KeyDetails?) {
.clickable { isBlurred.value = !isBlurred.value },
)

if (!isBlurred.value) {
Button(
onClick = { clipboardManager.setText(AnnotatedString(keyDetails?.phrase ?: "")) },
modifier = Modifier.padding(5.dp),
) {
Text("Copy Phrase")
}
}

if (prepSending.value) {
TextField(
value = toAddress.value,
onValueChange = { toAddress.value = it },
label = { Text("Address") },
value = recipient.value,
onValueChange = { recipient.value = it },
label = { Text("Recipient Address") },
modifier = Modifier.fillMaxWidth().padding(5.dp),
)
}

// Don't show send btn if user has no balance
if (balance.longValue > 0) {
Button(
onClick = { prepSending.value = !prepSending.value },
onClick = {
if (prepSending.value && recipient.value.isNotEmpty()) {
coroutineScope.launch {
send(
keyDetails?.sk ?: "",
SuiAddress(keyDetails?.address ?: ""),
SuiAddress(recipient.value),
)
}
}
prepSending.value = !prepSending.value
},
modifier = Modifier.fillMaxWidth().padding(5.dp),
shape = RoundedCornerShape(15.dp),
) {
if (prepSending.value) {
if (prepSending.value && recipient.value.isEmpty()) {
Text("Cancel")
} else {
Text("Send")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
package xyz.mcxross.ksui.android.ui.view

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun Wallet(modifier: Modifier = Modifier, onNewWallet: () -> Unit) {
fun Wallet(
modifier: Modifier = Modifier,
onImportWallet: (String) -> Unit,
onNewWallet: () -> Unit,
) {
var importing by remember { mutableStateOf(false) }
var importString by remember { mutableStateOf("") }
Box(modifier = modifier, contentAlignment = Alignment.Center) {
Button(onClick = onNewWallet) { Text("Create Wallet") }
Column {
if (!importing) {
Button(modifier = Modifier.padding(8.dp).fillMaxWidth(), onClick = onNewWallet) {
Text("Create Wallet")
}
} else {
TextField(
modifier = Modifier.fillMaxWidth(),
value = importString,
label = { Text("Enter Phrase or Private Key") },
onValueChange = { importString = it },
)
}
Button(
modifier = Modifier.padding(8.dp).fillMaxWidth(),
onClick = {
if (importing) {
onImportWallet(importString)
}
importing = true
},
) {
Text("Import Wallet")
}
}
}
}
2 changes: 1 addition & 1 deletion sample/jvm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {
mavenCentral()
}

kotlin { jvmToolchain(11) }
kotlin { jvmToolchain(17) }

dependencies {
implementation(project(":ksui"))
Expand Down

0 comments on commit c910b69

Please sign in to comment.