From fbd97551ad69dd365bce50f5d5bbc53912ed5df2 Mon Sep 17 00:00:00 2001
From: Sajal Bansal <77548932+sajal-madmax@users.noreply.github.com>
Date: Mon, 2 Sep 2024 18:17:42 +0530
Subject: [PATCH 1/6] retrieve an account balance
---
wallet_app/android/.idea/other.xml | 66 +++++++++++
wallet_app/android/app/build.gradle.kts | 2 +
.../android/app/src/main/AndroidManifest.xml | 3 +
.../walletapp/AccountBalanceActivity.kt | 105 ++++++++++++++++++
.../com/example/walletapp/MainActivity.kt | 20 ++++
wallet_app/android/gradle/libs.versions.toml | 2 +
6 files changed, 198 insertions(+)
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/AccountBalanceActivity.kt
diff --git a/wallet_app/android/.idea/other.xml b/wallet_app/android/.idea/other.xml
index 4604c446..94c96f63 100644
--- a/wallet_app/android/.idea/other.xml
+++ b/wallet_app/android/.idea/other.xml
@@ -69,6 +69,28 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -91,6 +113,17 @@
+
+
+
+
+
+
+
+
+
+
+
@@ -102,6 +135,17 @@
+
+
+
+
+
+
+
+
+
+
+
@@ -146,6 +190,17 @@
+
+
+
+
+
+
+
+
+
+
+
@@ -235,6 +290,17 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/wallet_app/android/app/build.gradle.kts b/wallet_app/android/app/build.gradle.kts
index 5b88153a..232f331b 100644
--- a/wallet_app/android/app/build.gradle.kts
+++ b/wallet_app/android/app/build.gradle.kts
@@ -79,6 +79,8 @@ dependencies {
implementation(libs.androidx.hilt.navigation.fragment)
implementation (libs.androidx.hilt.navigation.compose.v100alpha03)
+ implementation (libs.core)
+
implementation(libs.androidx.ui.tooling.preview)
debugImplementation(libs.androidx.ui.tooling)
diff --git a/wallet_app/android/app/src/main/AndroidManifest.xml b/wallet_app/android/app/src/main/AndroidManifest.xml
index e6da76a6..80309731 100644
--- a/wallet_app/android/app/src/main/AndroidManifest.xml
+++ b/wallet_app/android/app/src/main/AndroidManifest.xml
@@ -32,6 +32,9 @@
+
diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/AccountBalanceActivity.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/AccountBalanceActivity.kt
new file mode 100644
index 00000000..8f3170d8
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/AccountBalanceActivity.kt
@@ -0,0 +1,105 @@
+package com.example.walletapp
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.activity.enableEdgeToEdge
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.material.Surface
+import androidx.compose.material.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.text.font.Font
+import androidx.compose.ui.text.font.FontFamily
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.core.graphics.toColorInt
+import androidx.core.view.WindowCompat
+import org.web3j.protocol.Web3j
+import org.web3j.protocol.http.HttpService
+import org.web3j.utils.Convert
+import java.math.BigDecimal
+import com.example.walletapp.ui.theme.WalletappTheme
+
+class AccountBalanceActivity : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ enableEdgeToEdge()
+ WindowCompat.setDecorFitsSystemWindows(window, true)
+ setContent {
+ WalletappTheme {
+ Surface(modifier = Modifier.fillMaxSize()) {
+ AccountBalanceScreenView(
+ )
+ }
+ }
+ }
+ }
+
+ @Composable
+ fun AccountBalanceScreenView(){
+
+ val rpcUrl = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID" // Replace with your RPC URL
+ val accountAddress = "0xYourHardcodedAccountAddress" // account Address
+
+
+ // Call the balance retrieval function
+ val balance = getAccountBalance(rpcUrl, accountAddress)?.toPlainString() ?: "0.0"
+
+ Column(modifier = Modifier
+ .fillMaxSize()
+ .background(Color("#0C0C4F".toColorInt()))
+ .padding(16.dp),
+ horizontalAlignment = Alignment.CenterHorizontally,
+ verticalArrangement = Arrangement.Center){
+ Spacer(modifier = Modifier.height(40.dp))
+ Text(
+ text = "Your Account Balance",
+ fontFamily = FontFamily(Font(R.font.publicsans_regular)),
+ color = Color.White,
+ fontSize = 14.sp
+ )
+ Spacer(modifier = Modifier.height(8.dp))
+ Text(
+ text = "$$balance",
+ fontFamily = FontFamily(Font(R.font.inter_regular)),
+ color = Color.White,
+ fontSize = 28.sp,
+ modifier = Modifier
+ .align(Alignment.CenterHorizontally)
+ .padding(top = 70.dp)
+ )
+ Spacer(modifier = Modifier.height(8.dp))
+ Text(
+ text = accountAddress,
+ fontFamily = FontFamily(Font(R.font.inter_regular)),
+ color = Color.White,
+ fontSize = 16.sp,
+ modifier = Modifier.align(Alignment.CenterHorizontally)
+ )
+
+
+
+ }
+ }
+
+ fun getAccountBalance(rpcUrl: String, accountAddress: String): BigDecimal? {
+ val web3j = Web3j.build(HttpService(rpcUrl))
+ return try {
+ val ethGetBalance = web3j.ethGetBalance(accountAddress, org.web3j.protocol.core.DefaultBlockParameterName.LATEST).send()
+ val balanceInWei = ethGetBalance.balance
+ Convert.fromWei(balanceInWei.toString(), Convert.Unit.ETHER)
+ } catch (e: Exception) {
+ e.printStackTrace()
+ null
+ }
+ }
+}
\ No newline at end of file
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 85132061..662d7009 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
@@ -43,6 +43,10 @@ import androidx.compose.ui.unit.sp
import androidx.core.graphics.toColorInt
import androidx.core.view.WindowCompat
import com.example.walletapp.ui.theme.WalletappTheme
+import org.web3j.protocol.Web3j
+import org.web3j.protocol.http.HttpService
+import org.web3j.utils.Convert
+import java.math.BigDecimal
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@@ -65,6 +69,7 @@ class MainActivity : ComponentActivity() {
fun Wallet( modifier: Modifier) {
val context = (LocalContext.current as Activity)
+
Column(
modifier = Modifier
.fillMaxSize()
@@ -111,6 +116,20 @@ fun Wallet( modifier: Modifier) {
.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 = "Get Account Balance", fontFamily = FontFamily(Font(R.font.publicsans_bold)),
+ color = Color.White,
+ fontSize = 14.sp)
+ }
+
Spacer(modifier = Modifier.weight(1f))
Row(
@@ -190,3 +209,4 @@ fun WalletCard(icon: Painter,amount:String,exchange:Double,type:String){
}
}
}
+
diff --git a/wallet_app/android/gradle/libs.versions.toml b/wallet_app/android/gradle/libs.versions.toml
index de282c12..b8c80f01 100644
--- a/wallet_app/android/gradle/libs.versions.toml
+++ b/wallet_app/android/gradle/libs.versions.toml
@@ -2,6 +2,7 @@
activityCompose = "1.9.1"
agp = "8.5.2"
converterGson = "2.9.0"
+core = "4.8.7"
hiltAndroid = "2.51"
hiltNavigationCompose = "1.2.0"
hiltNavigationComposeVersion = "1.2.0"
@@ -33,6 +34,7 @@ androidx-runtime-livedata = { module = "androidx.compose.runtime:runtime-livedat
androidx-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
androidx-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
converter-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "converterGson" }
+core = { module = "org.web3j:core", version.ref = "core" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
From 3fc62141d864a6596af0307d77af7bc19ee106d0 Mon Sep 17 00:00:00 2001
From: Sajal Bansal <77548932+sajal-madmax@users.noreply.github.com>
Date: Sun, 8 Sep 2024 01:13:10 +0530
Subject: [PATCH 2/6] retrieving an account balance
---
wallet_app/android/app/build.gradle.kts | 12 ++
.../android/app/src/main/AndroidManifest.xml | 5 +-
.../walletapp/AccountBalanceActivity.kt | 105 ------------
.../com/example/walletapp/MainActivity.kt | 22 +++
.../com/example/walletapp/WalletActivity.kt | 1 +
.../data/datasource/RetrofitInstance.kt | 17 ++
.../data/datasource/StarknetApiService.kt | 14 ++
.../walletapp/data/model/StarknetResponse.kt | 8 +
.../data/repository/StarknetCallRequest.kt | 8 +
.../data/repository/StarknetRepository.kt | 26 +++
.../ui/activity/AccountBalanceActivity.kt | 155 ++++++++++++++++++
.../ui/activity/StarknetViewModel.kt | 45 +++++
.../ui/activity/StarknetViewModelFactory.kt | 15 ++
wallet_app/android/build.gradle.kts | 2 +
wallet_app/android/gradle/libs.versions.toml | 4 +
15 files changed, 333 insertions(+), 106 deletions(-)
delete mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/AccountBalanceActivity.kt
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/RetrofitInstance.kt
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/StarknetApiService.kt
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/model/StarknetResponse.kt
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetCallRequest.kt
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetRepository.kt
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/AccountBalanceActivity.kt
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModel.kt
create mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModelFactory.kt
diff --git a/wallet_app/android/app/build.gradle.kts b/wallet_app/android/app/build.gradle.kts
index 232f331b..f0b74905 100644
--- a/wallet_app/android/app/build.gradle.kts
+++ b/wallet_app/android/app/build.gradle.kts
@@ -1,6 +1,8 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
+ id("com.google.dagger.hilt.android")
+ id("kotlin-kapt")
}
android {
@@ -93,6 +95,16 @@ 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/AndroidManifest.xml b/wallet_app/android/app/src/main/AndroidManifest.xml
index ce5f1264..31403cd6 100644
--- a/wallet_app/android/app/src/main/AndroidManifest.xml
+++ b/wallet_app/android/app/src/main/AndroidManifest.xml
@@ -2,6 +2,9 @@
+
+
+
-
diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/AccountBalanceActivity.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/AccountBalanceActivity.kt
deleted file mode 100644
index 8f3170d8..00000000
--- a/wallet_app/android/app/src/main/java/com/example/walletapp/AccountBalanceActivity.kt
+++ /dev/null
@@ -1,105 +0,0 @@
-package com.example.walletapp
-
-import android.os.Bundle
-import androidx.activity.ComponentActivity
-import androidx.activity.compose.setContent
-import androidx.activity.enableEdgeToEdge
-import androidx.compose.foundation.background
-import androidx.compose.foundation.layout.Arrangement
-import androidx.compose.foundation.layout.Column
-import androidx.compose.foundation.layout.Spacer
-import androidx.compose.foundation.layout.fillMaxSize
-import androidx.compose.foundation.layout.height
-import androidx.compose.foundation.layout.padding
-import androidx.compose.material.Surface
-import androidx.compose.material.Text
-import androidx.compose.runtime.Composable
-import androidx.compose.ui.Alignment
-import androidx.compose.ui.Modifier
-import androidx.compose.ui.graphics.Color
-import androidx.compose.ui.text.font.Font
-import androidx.compose.ui.text.font.FontFamily
-import androidx.compose.ui.unit.dp
-import androidx.compose.ui.unit.sp
-import androidx.core.graphics.toColorInt
-import androidx.core.view.WindowCompat
-import org.web3j.protocol.Web3j
-import org.web3j.protocol.http.HttpService
-import org.web3j.utils.Convert
-import java.math.BigDecimal
-import com.example.walletapp.ui.theme.WalletappTheme
-
-class AccountBalanceActivity : ComponentActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- enableEdgeToEdge()
- WindowCompat.setDecorFitsSystemWindows(window, true)
- setContent {
- WalletappTheme {
- Surface(modifier = Modifier.fillMaxSize()) {
- AccountBalanceScreenView(
- )
- }
- }
- }
- }
-
- @Composable
- fun AccountBalanceScreenView(){
-
- val rpcUrl = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID" // Replace with your RPC URL
- val accountAddress = "0xYourHardcodedAccountAddress" // account Address
-
-
- // Call the balance retrieval function
- val balance = getAccountBalance(rpcUrl, accountAddress)?.toPlainString() ?: "0.0"
-
- Column(modifier = Modifier
- .fillMaxSize()
- .background(Color("#0C0C4F".toColorInt()))
- .padding(16.dp),
- horizontalAlignment = Alignment.CenterHorizontally,
- verticalArrangement = Arrangement.Center){
- Spacer(modifier = Modifier.height(40.dp))
- Text(
- text = "Your Account Balance",
- fontFamily = FontFamily(Font(R.font.publicsans_regular)),
- color = Color.White,
- fontSize = 14.sp
- )
- Spacer(modifier = Modifier.height(8.dp))
- Text(
- text = "$$balance",
- fontFamily = FontFamily(Font(R.font.inter_regular)),
- color = Color.White,
- fontSize = 28.sp,
- modifier = Modifier
- .align(Alignment.CenterHorizontally)
- .padding(top = 70.dp)
- )
- Spacer(modifier = Modifier.height(8.dp))
- Text(
- text = accountAddress,
- fontFamily = FontFamily(Font(R.font.inter_regular)),
- color = Color.White,
- fontSize = 16.sp,
- modifier = Modifier.align(Alignment.CenterHorizontally)
- )
-
-
-
- }
- }
-
- fun getAccountBalance(rpcUrl: String, accountAddress: String): BigDecimal? {
- val web3j = Web3j.build(HttpService(rpcUrl))
- return try {
- val ethGetBalance = web3j.ethGetBalance(accountAddress, org.web3j.protocol.core.DefaultBlockParameterName.LATEST).send()
- val balanceInWei = ethGetBalance.balance
- Convert.fromWei(balanceInWei.toString(), Convert.Unit.ETHER)
- } catch (e: Exception) {
- e.printStackTrace()
- null
- }
- }
-}
\ No newline at end of file
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 f993e3ce..d9b25a74 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
@@ -38,6 +38,9 @@ import androidx.compose.ui.unit.sp
import androidx.core.graphics.toColorInt
import androidx.core.view.WindowCompat
import com.example.walletapp.ui.theme.WalletappTheme
+import dagger.hilt.android.AndroidEntryPoint
+import dagger.hilt.android.HiltAndroidApp
+
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@@ -127,6 +130,25 @@ fun CreateAccount( modifier: Modifier) {
fontSize = 17.sp
)
}
+ Spacer(modifier = Modifier.height(10.dp))
+
+
+ Button(
+ onClick = { val i = Intent(context, WalletActivity::class.java)
+ context.startActivity(i) },
+ colors = ButtonDefaults.buttonColors(backgroundColor = Color("#EC796B".toColorInt())),
+ shape = RoundedCornerShape(10.dp),
+ modifier = Modifier
+ .fillMaxWidth()
+ .height(49.dp)
+ ) {
+ Text(
+ text = "My Starknet Wallet",
+ fontFamily = FontFamily(Font(R.font.inter_regular)),
+ color = Color.White,
+ fontSize = 17.sp
+ )
+ }
}
Spacer(modifier = Modifier.height(15.dp))
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 d7f9449c..bd6199ad 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
@@ -35,6 +35,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.graphics.toColorInt
import androidx.core.view.WindowCompat
+import com.example.walletapp.ui.activity.AccountBalanceActivity
import com.example.walletapp.ui.theme.WalletappTheme
class WalletActivity : ComponentActivity() {
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
new file mode 100644
index 00000000..fa4ce814
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/RetrofitInstance.kt
@@ -0,0 +1,17 @@
+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
new file mode 100644
index 00000000..77b9c670
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/StarknetApiService.kt
@@ -0,0 +1,14 @@
+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
new file mode 100644
index 00000000..4de494d3
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/data/model/StarknetResponse.kt
@@ -0,0 +1,8 @@
+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
new file mode 100644
index 00000000..0160b2f9
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetCallRequest.kt
@@ -0,0 +1,8 @@
+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
new file mode 100644
index 00000000..cb7ac07c
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetRepository.kt
@@ -0,0 +1,26 @@
+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
new file mode 100644
index 00000000..f073fffa
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/AccountBalanceActivity.kt
@@ -0,0 +1,155 @@
+package com.example.walletapp.ui.activity
+
+import android.app.Activity
+import android.os.Bundle
+import android.util.Log
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.activity.enableEdgeToEdge
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.width
+import androidx.compose.material.Button
+import androidx.compose.material.MaterialTheme
+import androidx.compose.material.Surface
+import androidx.compose.material.Text
+import androidx.compose.material.TextField
+import androidx.compose.material.TextFieldDefaults
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.livedata.observeAsState
+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.graphics.Color
+import androidx.compose.ui.graphics.Color.Companion.Transparent
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.dp
+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.ui.theme.WalletappTheme
+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
+ )
+ }
+ }
+ }
+ }
+
+ @Composable
+ fun AccountBalanceScreenView(viewModelFactory: StarknetViewModelFactory){
+ val context = (LocalContext.current as Activity)
+ val viewModel: StarknetViewModel = viewModel(factory = viewModelFactory)
+
+ val contractAddress by remember { mutableStateOf("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7") }
+ var accountAddress by remember { mutableStateOf("0x02dc260794e4c2eeae87b1403a88385a72c18a5844d220b88117b2965a8cf3a5") }
+
+ val balance by viewModel.balanceLiveData.observeAsState("...")
+ val error by viewModel.errorLiveData.observeAsState("")
+
+ Column(modifier = Modifier
+ .fillMaxSize()
+ .background(Color("#0C0C4F".toColorInt()))
+ .padding(16.dp),
+ horizontalAlignment = Alignment.CenterHorizontally,
+ verticalArrangement = Arrangement.Center)
+ {
+ TextField(
+ value = accountAddress,
+ onValueChange = { accountAddress = it },
+ label = { Text("Account Address",
+ fontSize = 16.sp,
+ color = Color.White,
+ modifier=Modifier.padding(10.dp),
+ fontWeight = FontWeight.Bold) },
+ modifier=Modifier.padding(15.dp),
+ colors = TextFieldDefaults.textFieldColors(
+ textColor = Color("#ffffff".toColorInt()),
+ disabledTextColor = Transparent,
+ backgroundColor = Transparent,
+ focusedIndicatorColor = Transparent,
+ unfocusedIndicatorColor = Transparent,
+ disabledIndicatorColor = Transparent
+ )
+
+ )
+
+ Spacer(modifier = Modifier.height(16.dp))
+ Row(){
+ Button(onClick = {
+ viewModel.fetchAccountBalance(contractAddress, accountAddress)
+ }) {
+ Text("Get Balance")
+ }
+ Spacer(modifier = Modifier.width(10.dp))
+ Button(onClick = {
+ context.finish()
+ }) {
+ Text("Back to home")
+ }
+
+ }
+
+ 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)
+ }
+
+
+ }
+ }
+
+
+ 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
+ }
+ }
+
+}
\ 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
new file mode 100644
index 00000000..a5621b72
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModel.kt
@@ -0,0 +1,45 @@
+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
new file mode 100644
index 00000000..6a2f02c4
--- /dev/null
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModelFactory.kt
@@ -0,0 +1,15 @@
+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")
+ }
+}
diff --git a/wallet_app/android/build.gradle.kts b/wallet_app/android/build.gradle.kts
index f74b04bf..be658d4c 100644
--- a/wallet_app/android/build.gradle.kts
+++ b/wallet_app/android/build.gradle.kts
@@ -2,4 +2,6 @@
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
+
+ id("com.google.dagger.hilt.android") version "2.50" apply false
}
\ No newline at end of file
diff --git a/wallet_app/android/gradle/libs.versions.toml b/wallet_app/android/gradle/libs.versions.toml
index b8c80f01..a6fa483c 100644
--- a/wallet_app/android/gradle/libs.versions.toml
+++ b/wallet_app/android/gradle/libs.versions.toml
@@ -12,6 +12,8 @@ junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
appcompat = "1.7.0"
+kotlinxCoroutinesAndroid = "1.7.3"
+kotlinxCoroutinesCore = "1.7.3"
lifecycleViewmodelCompose = "2.8.4"
loggingInterceptor = "4.11.0"
material = "1.12.0"
@@ -40,6 +42,8 @@ junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
+kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinxCoroutinesAndroid" }
+kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinxCoroutinesCore" }
logging-interceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "loggingInterceptor" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
From b5f9d71f4b219d0fa889f1f4b42ee911cea8e0b6 Mon Sep 17 00:00:00 2001
From: Sajal Bansal <77548932+sajal-madmax@users.noreply.github.com>
Date: Fri, 13 Sep 2024 00:41:07 +0530
Subject: [PATCH 3/6] starknet sdk integrated
---
wallet_app/android/app/build.gradle.kts | 30 +-
.../com/example/walletapp/AccountPassword.kt | 14 +-
.../com/example/walletapp/AddTokenActivity.kt | 1 -
.../com/example/walletapp/MainActivity.kt | 1 -
.../walletapp/RecoveryPhraseActivity.kt | 2 +-
.../com/example/walletapp/SendActivity.kt | 2 +-
.../com/example/walletapp/WalletActivity.kt | 266 +++++++++---------
.../data/datasource/RetrofitInstance.kt | 17 --
.../data/datasource/StarknetApiService.kt | 14 -
.../walletapp/data/model/StarknetResponse.kt | 8 -
.../data/repository/StarknetCallRequest.kt | 8 -
.../data/repository/StarknetRepository.kt | 26 --
.../ui/activity/AccountBalanceActivity.kt | 109 ++++---
.../ui/activity/StarknetViewModel.kt | 45 ---
.../ui/activity/StarknetViewModelFactory.kt | 15 -
15 files changed, 221 insertions(+), 337 deletions(-)
delete mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/RetrofitInstance.kt
delete mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/StarknetApiService.kt
delete mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/model/StarknetResponse.kt
delete mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetCallRequest.kt
delete mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/StarknetRepository.kt
delete mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModel.kt
delete mode 100644 wallet_app/android/app/src/main/java/com/example/walletapp/ui/activity/StarknetViewModelFactory.kt
diff --git a/wallet_app/android/app/build.gradle.kts b/wallet_app/android/app/build.gradle.kts
index f0b74905..bbff2e01 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 7affb26a..fab2d7c6 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 48776674..64b435d0 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 d9b25a74..d9be4f62 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 4ecef975..f403227b 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 e91ac7b6..9a205691 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 bd6199ad..1404f5a3 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 fa4ce814..00000000
--- 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 77b9c670..00000000
--- 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 4de494d3..00000000
--- 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 0160b2f9..00000000
--- 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 cb7ac07c..00000000
--- 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 f073fffa..8b1fe502 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 a5621b72..00000000
--- 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 6a2f02c4..00000000
--- 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")
- }
-}
From 66ae86671308312d4a477669923bdab0578369ba Mon Sep 17 00:00:00 2001
From: Sajal Bansal <77548932+sajal-madmax@users.noreply.github.com>
Date: Fri, 13 Sep 2024 00:41:43 +0530
Subject: [PATCH 4/6] starknet sdk
---
wallet_app/android/build.gradle.kts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/wallet_app/android/build.gradle.kts b/wallet_app/android/build.gradle.kts
index be658d4c..4952695e 100644
--- a/wallet_app/android/build.gradle.kts
+++ b/wallet_app/android/build.gradle.kts
@@ -3,5 +3,5 @@ plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
- id("com.google.dagger.hilt.android") version "2.50" apply false
+ id("org.jmailen.kotlinter") version "3.15.0"
}
\ No newline at end of file
From a9b4934337ae445791c82cf4c1750e9086258e17 Mon Sep 17 00:00:00 2001
From: Sajal Bansal <77548932+sajal-madmax@users.noreply.github.com>
Date: Sat, 21 Sep 2024 22:21:47 +0530
Subject: [PATCH 5/6] variables loading from local.properties
---
wallet_app/android/app/build.gradle.kts | 10 ++++------
.../main/java/com/example/walletapp/StarknetClient.kt | 8 ++++++--
2 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/wallet_app/android/app/build.gradle.kts b/wallet_app/android/app/build.gradle.kts
index 28b6accd..337adb89 100644
--- a/wallet_app/android/app/build.gradle.kts
+++ b/wallet_app/android/app/build.gradle.kts
@@ -25,15 +25,13 @@ android {
targetSdk = 34
versionCode = 1
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\"")
+ buildConfigField("String", "DEMO_RPC_URL", "\"https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/rFAP8fkTAz9TmYw8_V5Fyzxi-WSoQdhk\"")
+ buildConfigField("String", "ACCOUNT_ADDRESS", "\"${properties.getProperty("ACCOUNT_ADDRESS")}\"")
+ buildConfigField("String", "PRIVATE_KEY", "\"${properties.getProperty("KEY_NAME")}\"")
}
buildTypes {
@@ -97,7 +95,7 @@ dependencies {
implementation(libs.hilt.android)
implementation(libs.androidx.hilt.navigation.fragment)
implementation (libs.androidx.hilt.navigation.compose.v100alpha03)
- implementation("com.swmansion.starknet:starknet:0.12.2")
+
implementation(libs.androidx.ui.tooling.preview)
debugImplementation(libs.androidx.ui.tooling)
diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/StarknetClient.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/StarknetClient.kt
index 0cbf35aa..9a759781 100644
--- a/wallet_app/android/app/src/main/java/com/example/walletapp/StarknetClient.kt
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/StarknetClient.kt
@@ -1,3 +1,4 @@
+import com.example.walletapp.BuildConfig
import com.swmansion.starknet.account.StandardAccount
import com.swmansion.starknet.data.types.Call
import com.swmansion.starknet.data.types.Felt
@@ -12,12 +13,15 @@ const val ETH_ERC20_ADDRESS = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741
class StarknetClient(private val rpcUrl: String) {
private val provider = JsonRpcProvider(rpcUrl)
+ private val privateKey=BuildConfig.PRIVATE_KEY
+ private val accountAddress=BuildConfig.ACCOUNT_ADDRESS
suspend fun deployAccount() {
+
// Predefined values for account creation
- val privateKey = Felt.fromHex("0x2bbf4f9fd0bbb2e60b0316c1fe0b76cf7a4d0198bd493ced9b8df2a3a24d68a") // TODO(#80): Load from local.properties
- val accountAddress = Felt.fromHex("0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca") // TODO(#80): Load from local.properties
+ val privateKey = Felt.fromHex(privateKey) // TODO(#80): Load from local.properties
+ val accountAddress = Felt.fromHex(accountAddress) // TODO(#80): Load from local.properties
val signer = StarkCurveSigner(privateKey)
val chainId = provider.getChainId().sendAsync().await()
From 4a9f20207e3b422bb01a34a740d4f1e12cec9dd1 Mon Sep 17 00:00:00 2001
From: Sajal Bansal <77548932+sajal-madmax@users.noreply.github.com>
Date: Sun, 22 Sep 2024 19:56:02 +0530
Subject: [PATCH 6/6] changes done and loaded actual balance and address
---
wallet_app/android/app/build.gradle.kts | 2 +-
.../com/example/walletapp/MainActivity.kt | 2 +-
.../com/example/walletapp/StarknetClient.kt | 4 +-
.../com/example/walletapp/WalletActivity.kt | 58 ++++++++++++++-----
.../ui/activity/AccountBalanceActivity.kt | 2 +-
5 files changed, 50 insertions(+), 18 deletions(-)
diff --git a/wallet_app/android/app/build.gradle.kts b/wallet_app/android/app/build.gradle.kts
index 337adb89..c955a0f3 100644
--- a/wallet_app/android/app/build.gradle.kts
+++ b/wallet_app/android/app/build.gradle.kts
@@ -29,7 +29,7 @@ android {
- buildConfigField("String", "DEMO_RPC_URL", "\"https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/rFAP8fkTAz9TmYw8_V5Fyzxi-WSoQdhk\"")
+ buildConfigField("String", "RPC_URL", "\"${properties.getProperty("RPC_URL")}\"")
buildConfigField("String", "ACCOUNT_ADDRESS", "\"${properties.getProperty("ACCOUNT_ADDRESS")}\"")
buildConfigField("String", "PRIVATE_KEY", "\"${properties.getProperty("KEY_NAME")}\"")
}
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 7c5c499d..15ce801d 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
@@ -71,7 +71,7 @@ fun StarknetLogo (modifier: Modifier = Modifier) {
fun CreateAccount( modifier: Modifier) {
val context = (LocalContext.current as Activity)
val scope = rememberCoroutineScope()
- val starknetClient = StarknetClient(BuildConfig.DEMO_RPC_URL)
+ val starknetClient = StarknetClient(BuildConfig.RPC_URL)
Column(
modifier = Modifier
diff --git a/wallet_app/android/app/src/main/java/com/example/walletapp/StarknetClient.kt b/wallet_app/android/app/src/main/java/com/example/walletapp/StarknetClient.kt
index 9a759781..efe38aea 100644
--- a/wallet_app/android/app/src/main/java/com/example/walletapp/StarknetClient.kt
+++ b/wallet_app/android/app/src/main/java/com/example/walletapp/StarknetClient.kt
@@ -20,8 +20,8 @@ class StarknetClient(private val rpcUrl: String) {
// Predefined values for account creation
- val privateKey = Felt.fromHex(privateKey) // TODO(#80): Load from local.properties
- val accountAddress = Felt.fromHex(accountAddress) // TODO(#80): Load from local.properties
+ val privateKey = Felt.fromHex(privateKey)
+ val accountAddress = Felt.fromHex(accountAddress)
val signer = StarkCurveSigner(privateKey)
val chainId = provider.getChainId().sendAsync().await()
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 d1807170..74d6621c 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
@@ -1,8 +1,10 @@
package com.example.walletapp
+import StarknetClient
import android.app.Activity
import android.content.Intent
import android.os.Bundle
+import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
@@ -54,6 +56,12 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
+import com.swmansion.starknet.data.types.Felt
+import com.swmansion.starknet.provider.exceptions.RpcRequestFailedException
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import java.math.BigDecimal
+import java.text.DecimalFormat
class WalletActivity : ComponentActivity() {
@@ -80,6 +88,24 @@ class WalletActivity : ComponentActivity() {
val networkList = listOf("Starknet Mainnet", "Test Networks")
var selectedNetworkIndex by remember { mutableStateOf(0) }
val context = (LocalContext.current as Activity)
+ val address=BuildConfig.ACCOUNT_ADDRESS
+ val accountAddress = Felt.fromHex(address)
+ val starknetClient = StarknetClient(BuildConfig.RPC_URL)
+ var balance by remember { mutableStateOf("") }
+
+ LaunchedEffect (Unit){
+ try {
+ // Get the balance of the account
+ val getBalance = starknetClient.getEthBalance(accountAddress)
+ withContext(Dispatchers.Main) {
+ balance = starknetClient.weiToEther(getBalance).toDoubleWithTwoDecimal()
+ }
+ } catch (e: RpcRequestFailedException) {
+ withContext(Dispatchers.Main) { Toast.makeText(context, "${e.code}: ${e.message}", Toast.LENGTH_LONG).show() }
+ } catch (e: Exception) {
+ withContext(Dispatchers.Main) { Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() }
+ }
+ }
Column(
modifier = Modifier
@@ -104,29 +130,29 @@ class WalletActivity : ComponentActivity() {
Text(
- text = "$11,625.48", // TODO(#82): load actual balance
- fontFamily = FontFamily(Font(R.font.inter_regular)),
+ text ="$11,625.48",
+ fontFamily = FontFamily(Font(R.font.publicsans_bold)),
color = Color.White,
- fontSize = 28.sp,
+ fontSize = 24.sp,
modifier = Modifier
.align(Alignment.CenterHorizontally)
.padding(top = 70.dp)
)
Text(
- text = "0xfoo...123", // TODO(#82): load actual address
+ text = address.take(10) + ".....",
fontFamily = FontFamily(Font(R.font.inter_regular)),
color = Color.White,
- fontSize = 16.sp,
+ fontSize = 14.sp,
modifier = Modifier.align(Alignment.CenterHorizontally)
)
Spacer(modifier = Modifier.height(32.dp))
- // TODO(#82): load actual balance
+
WalletCard(
icon = painterResource(id = R.drawable.ic_ethereum),
amount = "$11,625.7",
- exchange = 4.44,
+ exchange = balance,
type = "ETH"
)
@@ -134,7 +160,7 @@ class WalletActivity : ComponentActivity() {
WalletCard(
icon = painterResource(id = R.drawable.token2),
amount = "$1.78",
- exchange = 4.44,
+ exchange ="4.44",
type = "STRK"
)
@@ -194,9 +220,14 @@ class WalletActivity : ComponentActivity() {
Spacer(modifier = Modifier.height(15.dp))
}
}
+// Function to format BigDecimal to Double with 2 decimal places
+fun BigDecimal.toDoubleWithTwoDecimal(): String {
+ val decimalFormat = DecimalFormat("#.00")
+ return decimalFormat.format(this.toDouble())
+}
@Composable
- fun WalletCard(icon: Painter, amount: String, exchange: Double, type: String) {
+ fun WalletCard(icon: Painter, amount: String, exchange: String, type: String) {
Card(
backgroundColor = Color(0xFF1E1E96),
modifier = Modifier
@@ -223,16 +254,17 @@ class WalletActivity : ComponentActivity() {
)
Row {
Text(
- text = exchange.toString(),
- fontFamily = FontFamily(Font(R.font.inter_regular)),
+ text = exchange,
+ fontFamily = FontFamily(Font(R.font.publicsans_bold)),
color = Color.White,
- fontSize = 10.sp
+ fontSize = 14.sp
)
+ Spacer(modifier=Modifier.width(2.dp))
Text(
text = type,
fontFamily = FontFamily(Font(R.font.publicsans_bold)),
color = Color.White,
- fontSize = 10.sp
+ fontSize = 12.sp
)
}
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 a60050a0..dc960f1e 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
@@ -81,7 +81,7 @@ class AccountBalanceActivity : ComponentActivity() {
val scope = CoroutineScope(Dispatchers.IO)
- val starknetClient = StarknetClient(BuildConfig.DEMO_RPC_URL)
+ val starknetClient = StarknetClient(BuildConfig.RPC_URL)
Column(modifier = Modifier