diff --git a/wallet_app/android/app/build.gradle.kts b/wallet_app/android/app/build.gradle.kts index f0b7490..bbff2e0 100644 --- a/wallet_app/android/app/build.gradle.kts +++ b/wallet_app/android/app/build.gradle.kts @@ -1,8 +1,10 @@ +import java.util.Properties + plugins { alias(libs.plugins.android.application) alias(libs.plugins.jetbrains.kotlin.android) - id("com.google.dagger.hilt.android") id("kotlin-kapt") + id("org.jmailen.kotlinter") } android { @@ -15,6 +17,9 @@ android { } defaultConfig { + val properties = Properties() + properties.load(project.rootProject.file("local.properties").inputStream()) + applicationId = "com.example.walletapp" minSdk = 24 targetSdk = 34 @@ -22,9 +27,18 @@ android { versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + + + + buildConfigField("String", "DEMO_RPC_URL", "\"https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/${properties.getProperty("KEY_NAME")}\"") + buildConfigField("String", "DEMO_ACCOUNT_ADDRESS", "\"0x02dc260794e4c2eeae87b1403a88385a72c18a5844d220b88117b2965a8cf3a5\"") + buildConfigField("String", "DEMO_PRIVATE_KEY", "\"rFAP8fkTAz9TmYw8_V5Fyzxi-WSoQdhk\"") + buildConfigField("String", "DEMO_RECIPIENT_ACCOUNT_ADDRESS", "\"0xc1c7db92d22ef773de96f8bde8e56c85\"") } buildTypes { + + release { isMinifyEnabled = false proguardFiles( @@ -49,7 +63,6 @@ android { } } } - dependencies { implementation(libs.androidx.core.ktx) @@ -80,8 +93,7 @@ dependencies { implementation(libs.hilt.android) implementation(libs.androidx.hilt.navigation.fragment) implementation (libs.androidx.hilt.navigation.compose.v100alpha03) - - implementation (libs.core) + implementation("com.swmansion.starknet:starknet:0.12.2") implementation(libs.androidx.ui.tooling.preview) debugImplementation(libs.androidx.ui.tooling) @@ -95,16 +107,6 @@ dependencies { implementation(libs.androidx.navigation.compose) - implementation (libs.retrofit) - implementation(libs.converter.gson) - implementation (libs.kotlinx.coroutines.core) - implementation (libs.kotlinx.coroutines.android) - - implementation("com.google.dagger:hilt-android:2.50") - kapt("com.google.dagger:hilt-android-compiler:2.50") - kapt("androidx.hilt:hilt-compiler:1.0.0") - implementation("androidx.hilt:hilt-navigation-fragment:1.0.0") - implementation("androidx.hilt:hilt-navigation-compose:1.0.0-alpha03") implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/AccountPassword.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/AccountPassword.kt index 7affb26..fab2d7c 100644 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/AccountPassword.kt +++ b/wallet_app/android/app/src/main/java/com/example/walletapp/AccountPassword.kt @@ -18,15 +18,15 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.material.IconButton -import androidx.compose.material.Button -import androidx.compose.material.ButtonDefaults -import androidx.compose.material.TextField +import androidx.compose.material3.IconButton +import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.TextField import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.unit.dp -import androidx.compose.material.Surface -import androidx.compose.material.Text +import androidx.compose.material3.Surface +import androidx.compose.material3.Text import androidx.compose.material.icons.Icons import androidx.compose.material.icons.sharp.ArrowBack import androidx.compose.runtime.Composable @@ -157,7 +157,7 @@ class AccountPasswordActivity : ComponentActivity() { Button( onClick = { val i = Intent(context, RecoveryPhraseActivity::class.java) context.startActivity(i) }, - colors = ButtonDefaults.buttonColors(backgroundColor = Color("#1B1B76".toColorInt())), + colors = ButtonDefaults.buttonColors(Color("#1B1B76".toColorInt())), modifier = Modifier .fillMaxWidth() .height(49.dp) diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/AddTokenActivity.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/AddTokenActivity.kt index 4877667..64b435d 100644 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/AddTokenActivity.kt +++ b/wallet_app/android/app/src/main/java/com/example/walletapp/AddTokenActivity.kt @@ -160,4 +160,3 @@ fun AddTokenScreenView(modifier: Modifier) { } } - diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/MainActivity.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/MainActivity.kt index d9b25a7..d9be4f6 100644 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/MainActivity.kt +++ b/wallet_app/android/app/src/main/java/com/example/walletapp/MainActivity.kt @@ -155,4 +155,3 @@ fun CreateAccount( modifier: Modifier) { } } - diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/RecoveryPhraseActivity.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/RecoveryPhraseActivity.kt index 4ecef97..f403227 100644 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/RecoveryPhraseActivity.kt +++ b/wallet_app/android/app/src/main/java/com/example/walletapp/RecoveryPhraseActivity.kt @@ -301,4 +301,4 @@ fun CustomDialog(onDismiss: () -> Unit) { .width(500.dp) .padding(8.dp) ) -} +} \ No newline at end of file diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/SendActivity.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/SendActivity.kt index e91ac7b..9a20569 100644 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/SendActivity.kt +++ b/wallet_app/android/app/src/main/java/com/example/walletapp/SendActivity.kt @@ -144,4 +144,4 @@ class SendActivity : ComponentActivity() { } } -} +} \ No newline at end of file diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/WalletActivity.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/WalletActivity.kt index bd6199a..1404f5a 100644 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/WalletActivity.kt +++ b/wallet_app/android/app/src/main/java/com/example/walletapp/WalletActivity.kt @@ -56,170 +56,162 @@ class WalletActivity : ComponentActivity() { } - @Composable - fun Wallet(modifier: Modifier) { - val context = (LocalContext.current as Activity) - Column( +@Composable +fun Wallet(modifier: Modifier) { + val context = (LocalContext.current as Activity) + Column( + modifier = Modifier + .fillMaxSize() + .background(Color("#0C0C4F".toColorInt())) + .padding(20.dp) + ) { + Text( + text = "$11,625.48", + fontFamily = FontFamily(Font(R.font.inter_regular)), + color = Color.White, + fontSize = 28.sp, modifier = Modifier - .fillMaxSize() - .background(Color("#0C0C4F".toColorInt())) - .padding(20.dp) + .align(Alignment.CenterHorizontally) + .padding(top = 70.dp) + ) + Text( + text = "0xfoo...123", + fontFamily = FontFamily(Font(R.font.inter_regular)), + color = Color.White, + fontSize = 16.sp, + modifier = Modifier.align(Alignment.CenterHorizontally) + ) + + Spacer(modifier = Modifier.height(32.dp)) + + WalletCard( + icon = painterResource(id = R.drawable.ic_ethereum), + amount = "$11,625.7", + exchange = 4.44, + type = "ETH" + ) + + WalletCard( + icon = painterResource(id = R.drawable.token2), + amount = "$1.78", + exchange = 4.44, + type = "STRK" + ) + + Spacer(modifier = Modifier.height(32.dp)) + + Text( + text = "+ New Token", + fontFamily = FontFamily(Font(R.font.publicsans_bold)), + color = Color.White, + fontSize = 14.sp, + modifier = Modifier + .clickable { + val intent = Intent(context, AddTokenActivity::class.java) + context.startActivity(intent) + } + .background(Color.Transparent) + .padding(10.dp) + .align(Alignment.CenterHorizontally) + ) + + Button( + onClick = {val intent = Intent(context, AccountBalanceActivity::class.java) + context.startActivity(intent)}, + colors = ButtonDefaults.buttonColors(backgroundColor = Color("#1B1B76".toColorInt())), + shape = RoundedCornerShape(15.dp), + modifier = Modifier.background(Color.Transparent) + .padding(10.dp) + .align(Alignment.CenterHorizontally) ) { - Text( - text = "$11,625.48", - fontFamily = FontFamily(Font(R.font.inter_regular)), - color = Color.White, - fontSize = 28.sp, - modifier = Modifier - .align(Alignment.CenterHorizontally) - .padding(top = 70.dp) - ) - Text( - text = "0xfoo...123", - fontFamily = FontFamily(Font(R.font.inter_regular)), + Text(text = "Get Account Balance", fontFamily = FontFamily(Font(R.font.publicsans_bold)), color = Color.White, - fontSize = 16.sp, - modifier = Modifier.align(Alignment.CenterHorizontally) - ) - - Spacer(modifier = Modifier.height(32.dp)) - - WalletCard( - icon = painterResource(id = R.drawable.ic_ethereum), - amount = "$11,625.7", - exchange = 4.44, - type = "ETH" - ) - - WalletCard( - icon = painterResource(id = R.drawable.token2), - amount = "$1.78", - exchange = 4.44, - type = "STRK" - ) - - Spacer(modifier = Modifier.height(32.dp)) + fontSize = 14.sp) + } - Text( - text = "+ New Token", - fontFamily = FontFamily(Font(R.font.publicsans_bold)), - color = Color.White, - fontSize = 14.sp, - modifier = Modifier - .clickable { - val intent = Intent(context, AddTokenActivity::class.java) - context.startActivity(intent) - } - .background(Color.Transparent) - .padding(10.dp) - .align(Alignment.CenterHorizontally) - ) + Spacer(modifier = Modifier.weight(1f)) + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceEvenly + ) { Button( - onClick = {val intent = Intent(context, AccountBalanceActivity::class.java) - context.startActivity(intent)}, + onClick = { + val i = Intent(context, ReceiverActivity::class.java) + context.startActivity(i) + }, colors = ButtonDefaults.buttonColors(backgroundColor = Color("#1B1B76".toColorInt())), shape = RoundedCornerShape(15.dp), - modifier = Modifier.background(Color.Transparent) - .padding(10.dp) - .align(Alignment.CenterHorizontally) ) { - Text(text = "Get Account Balance", fontFamily = FontFamily(Font(R.font.publicsans_bold)), + Text( + text = "Receive", fontFamily = FontFamily(Font(R.font.publicsans_bold)), color = Color.White, - fontSize = 14.sp) + fontSize = 14.sp + ) } + Button( + onClick = { + val i = Intent(context, SendActivity::class.java) + context.startActivity(i) + }, - - Spacer(modifier = Modifier.weight(1f)) - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceEvenly + colors = ButtonDefaults.buttonColors(backgroundColor = Color("#1B1B76".toColorInt())), + shape = RoundedCornerShape(15.dp), ) { - Button( - onClick = { - val i = Intent(context, ReceiverActivity::class.java) - context.startActivity(i) - }, - colors = ButtonDefaults.buttonColors(backgroundColor = Color("#1B1B76".toColorInt())), - shape = RoundedCornerShape(15.dp), - ) { - Text( - text = "Receive", fontFamily = FontFamily(Font(R.font.publicsans_bold)), - color = Color.White, - fontSize = 14.sp - ) - } - Button( - onClick = { - val i = Intent(context, SendActivity::class.java) - context.startActivity(i) - }, - - colors = ButtonDefaults.buttonColors(backgroundColor = Color("#1B1B76".toColorInt())), - shape = RoundedCornerShape(15.dp), - ) { - Text( - text = "Send", fontFamily = FontFamily(Font(R.font.publicsans_bold)), - color = Color.White, - fontSize = 14.sp - ) - } + Text( + text = "Send", fontFamily = FontFamily(Font(R.font.publicsans_bold)), + color = Color.White, + fontSize = 14.sp + ) } - Spacer(modifier = Modifier.height(15.dp)) } + Spacer(modifier = Modifier.height(15.dp)) } +} - @Composable - fun WalletCard(icon: Painter, amount: String, exchange: Double, type: String) { - Card( - backgroundColor = Color(0xFF1E1E96), +@Composable +fun WalletCard(icon: Painter, amount: String, exchange: Double, type: String) { + Card( + backgroundColor = Color(0xFF1E1E96), + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 8.dp) + ) { + Row( + verticalAlignment = Alignment.CenterVertically, modifier = Modifier + .padding(16.dp) .fillMaxWidth() - .padding(vertical = 8.dp) ) { - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier - .padding(16.dp) - .fillMaxWidth() - ) { - Image( - painter = icon, // replace with your Ethereum icon - contentDescription = null, + Image( + painter = icon, // replace with your Ethereum icon + contentDescription = null, + ) + Spacer(modifier = Modifier.weight(1f)) + Column(modifier = Modifier, horizontalAlignment = Alignment.End) { + Text( + text = amount, + fontFamily = FontFamily(Font(R.font.inter_regular)), + color = Color.White, + fontSize = 18.sp ) - Spacer(modifier = Modifier.weight(1f)) - Column(modifier = Modifier, horizontalAlignment = Alignment.End) { + Row { Text( - text = amount, + text = exchange.toString(), fontFamily = FontFamily(Font(R.font.inter_regular)), color = Color.White, - fontSize = 18.sp + fontSize = 10.sp + ) + Text( + text = type, + fontFamily = FontFamily(Font(R.font.publicsans_bold)), + color = Color.White, + fontSize = 10.sp ) - Row { - Text( - text = exchange.toString(), - fontFamily = FontFamily(Font(R.font.inter_regular)), - color = Color.White, - fontSize = 10.sp - ) - Text( - text = type, - fontFamily = FontFamily(Font(R.font.publicsans_bold)), - color = Color.White, - fontSize = 10.sp - ) - - } } + } } } - - - - - - - - +} \ No newline at end of file diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/RetrofitInstance.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/RetrofitInstance.kt deleted file mode 100644 index fa4ce81..0000000 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/RetrofitInstance.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.example.walletapp.data.datasource - -import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory - -object RetrofitInstance { - // Replace this with the actual StarkNet RPC URL - private const val BASE_URL = "https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/" - - val api: StarknetApiService by lazy { - Retrofit.Builder() - .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create()) - .build() - .create(StarknetApiService::class.java) - } -} diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/StarknetApiService.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/StarknetApiService.kt deleted file mode 100644 index 77b9c67..0000000 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/StarknetApiService.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.example.walletapp.data.datasource - -import com.example.walletapp.data.model.StarknetResponse -import com.example.walletapp.data.repository.StarknetCallRequest -import retrofit2.Call -import retrofit2.http.Body -import retrofit2.http.POST - -interface StarknetApiService { - - - @POST("rFAP8fkTAz9TmYw8_V5Fyzxi-WSoQdhk") // rpc end point - fun getBalance( @Body request: StarknetCallRequest): Call -} \ No newline at end of file diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/data/model/StarknetResponse.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/data/model/StarknetResponse.kt deleted file mode 100644 index 4de494d..0000000 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/data/model/StarknetResponse.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.walletapp.data.model - -data class StarknetResponse( - val jsonrpc: String, - val id: Int, - val result: List -) - diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetCallRequest.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetCallRequest.kt deleted file mode 100644 index 0160b2f..0000000 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetCallRequest.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.walletapp.data.repository - -data class StarknetCallRequest( - val id: Int = 1, - val jsonrpc: String = "2.0", - val method: String = "starknet_call", - val params: List -) \ No newline at end of file diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetRepository.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetRepository.kt deleted file mode 100644 index cb7ac07..0000000 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetRepository.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.walletapp.data.repository - -import android.util.Log -import com.example.walletapp.data.datasource.RetrofitInstance -import com.example.walletapp.data.model.StarknetResponse -import retrofit2.Call - -class StarknetRepository() { - fun getAccountBalance( - contractAddress: String, - accountAddress: String - ): Call { - val calldata = listOf(accountAddress) - val params = listOf( - mapOf( - "contract_address" to "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", - "calldata" to listOf("0x02dc260794e4c2eeae87b1403a88385a72c18a5844d220b88117b2965a8cf3a5"), - "entry_point_selector" to "0x2e4263afad30923c891518314c3c95dbe830a16874e8abc5777a9a20b54c76e" - ), - "latest" - ) - val request = StarknetCallRequest(params = params) - - return RetrofitInstance.api.getBalance(request) - } -} diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/AccountBalanceActivity.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/AccountBalanceActivity.kt index f073fff..8b1fe50 100644 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/AccountBalanceActivity.kt +++ b/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/AccountBalanceActivity.kt @@ -3,6 +3,7 @@ package com.example.walletapp.ui.activity import android.app.Activity import android.os.Bundle import android.util.Log +import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge @@ -38,42 +39,47 @@ import androidx.compose.ui.unit.sp import androidx.core.graphics.toColorInt import androidx.core.view.WindowCompat import androidx.lifecycle.viewmodel.compose.viewModel -import com.example.walletapp.data.repository.StarknetRepository +import com.example.walletapp.BuildConfig import com.example.walletapp.ui.theme.WalletappTheme +import com.swmansion.starknet.data.types.Call +import com.swmansion.starknet.data.types.Felt +import com.swmansion.starknet.data.types.Uint256 +import com.swmansion.starknet.provider.exceptions.RpcRequestFailedException +import com.swmansion.starknet.provider.rpc.JsonRpcProvider +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.future.await +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.math.BigDecimal import java.math.RoundingMode class AccountBalanceActivity : ComponentActivity() { - private lateinit var viewModelFactory: StarknetViewModelFactory override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() - // Create the repository and factory - val repository = StarknetRepository() // Initialize your repository here - viewModelFactory = StarknetViewModelFactory(repository) WindowCompat.setDecorFitsSystemWindows(window, true) setContent { WalletappTheme { Surface(modifier = Modifier.fillMaxSize()) { - AccountBalanceScreenView(viewModelFactory = viewModelFactory - ) + AccountBalanceScreenView() } } } } @Composable - fun AccountBalanceScreenView(viewModelFactory: StarknetViewModelFactory){ - val context = (LocalContext.current as Activity) - val viewModel: StarknetViewModel = viewModel(factory = viewModelFactory) + fun AccountBalanceScreenView(){ + val context = (LocalContext.current as Activity) - val contractAddress by remember { mutableStateOf("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7") } - var accountAddress by remember { mutableStateOf("0x02dc260794e4c2eeae87b1403a88385a72c18a5844d220b88117b2965a8cf3a5") } + var accountAddress by remember { mutableStateOf("0x02dc260794e4c2eeae87b1403a88385a72c18a5844d220b88117b2965a8cf3a5") } + + var balance by remember { mutableStateOf("")} + + val scope = CoroutineScope(Dispatchers.IO) - val balance by viewModel.balanceLiveData.observeAsState("...") - val error by viewModel.errorLiveData.observeAsState("") Column(modifier = Modifier .fillMaxSize() @@ -105,7 +111,22 @@ class AccountBalanceActivity : ComponentActivity() { Spacer(modifier = Modifier.height(16.dp)) Row(){ Button(onClick = { - viewModel.fetchAccountBalance(contractAddress, accountAddress) + + scope.launch { + // Catch any errors and display message in the UI + try { + val accountAddress2 = Felt.fromHex( accountAddress) + + // Get the balance of the account + val balancefinal = getBalance(accountAddress2) + Log.d("balance","${balancefinal}") + withContext(Dispatchers.Main) { balance= "${balancefinal.value} wei" } + } catch (e: RpcRequestFailedException) { + withContext(Dispatchers.Main) { Toast.makeText(applicationContext, "${e.code}: ${e.message}", Toast.LENGTH_LONG).show() } + } catch (e: Exception) { + withContext(Dispatchers.Main) { Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show() } + } + } }) { Text("Get Balance") } @@ -119,37 +140,41 @@ class AccountBalanceActivity : ComponentActivity() { } Spacer(modifier = Modifier.height(20.dp)) - - if (error.isNotEmpty()) { - Text("Error: $error",color= MaterialTheme.colors.error,fontSize = 20.sp,fontWeight = FontWeight.Bold) - } else { - Text("Balance : ${ConvertHexToBalance(balance)}",color= Color.White,fontSize = 20.sp,fontWeight = FontWeight.Bold) - } + Text("Balance : ${(balance)}",color= Color.White,fontSize = 20.sp,fontWeight = FontWeight.Bold) } } - - - fun ConvertHexToBalance(hex: String): String { - return try { - // Remove "0x" prefix if present - val cleanedHex = hex.removePrefix("0x") - - // Convert hex string to BigInteger - val balanceBigInt = cleanedHex.toBigInteger(16) - - // If StarkNet uses a base unit similar to Ethereum's wei, you might need to adjust the balance - // For this example, we'll assume the balance is in the smallest unit and needs to be converted - - // Convert balance to a decimal format (adjust the divisor based on StarkNet's unit) - val balanceDecimal = BigDecimal(balanceBigInt) - .divide(BigDecimal("1000000000000000000"), 18, RoundingMode.HALF_UP) - - balanceDecimal.stripTrailingZeros().toPlainString() // Return balance without scientific notation - } catch (e: Exception) { - "Invalid Balance" // Handle invalid hex strings or other exceptions - } + private val provider = JsonRpcProvider( + url = BuildConfig.DEMO_RPC_URL, + ) + + private suspend fun getBalance(accountAddress: Felt): Uint256 { + val erc20ContractAddress = Felt.fromHex("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7") + + // Create a call to Starknet ERC-20 ETH contract + val call = Call( + contractAddress = erc20ContractAddress, + entrypoint = "balanceOf", // entrypoint can be passed both as a string name and Felt value + calldata = listOf(accountAddress), // calldata is List, so we wrap accountAddress in listOf() + ) + + // Create a Request object which has to be executed in synchronous or asynchronous way + val request = provider.callContract(call) + + // Execute a Request. This operation returns JVM CompletableFuture + val future = request.sendAsync() + + // Await the completion of the future without blocking the main thread + // this comes from kotlinx-coroutines-jdk8 + // The result of the future is a List which represents the output values of the balanceOf function + val response = future.await() + + // Output value's type is UInt256 and is represented by two Felt values + return Uint256( + low = response[0], + high = response[1], + ) } } \ No newline at end of file diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModel.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModel.kt deleted file mode 100644 index a5621b7..0000000 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModel.kt +++ /dev/null @@ -1,45 +0,0 @@ -package com.example.walletapp.ui.activity - -import android.util.Log -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel -import androidx.lifecycle.viewModelScope -import com.example.walletapp.data.model.StarknetResponse -import com.example.walletapp.data.repository.StarknetRepository -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response - - -class StarknetViewModel(private val repository: StarknetRepository) : ViewModel() { - - val balanceLiveData = MutableLiveData() - val errorLiveData = MutableLiveData() - - // Function to trigger API call to fetch balance - fun fetchAccountBalance(contractAddress: String, accountAddress: String) { - viewModelScope.launch(Dispatchers.IO) { - repository.getAccountBalance(contractAddress, accountAddress) - .enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - if (response.isSuccessful) { - balanceLiveData.postValue(response.body()?.result?.get(0) ?: "0") - - } else { - errorLiveData.postValue("Error fetching balance") - } - } - - override fun onFailure(call: Call, t: Throwable) { - errorLiveData.postValue(t.message) - } - }) - } - } -} - diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModelFactory.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModelFactory.kt deleted file mode 100644 index 6a2f02c..0000000 --- a/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModelFactory.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.example.walletapp.ui.activity - -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.example.walletapp.data.repository.StarknetRepository - -class StarknetViewModelFactory(private val repository: StarknetRepository) : ViewModelProvider.Factory { - override fun create(modelClass: Class): T { - if (modelClass.isAssignableFrom(StarknetViewModel::class.java)) { - @Suppress("UNCHECKED_CAST") - return StarknetViewModel(repository) as T - } - throw IllegalArgumentException("Unknown ViewModel class") - } -}