Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated code with resolved conflicts #73

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions wallet_app/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -79,6 +81,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)

Expand All @@ -91,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)
Expand Down
8 changes: 8 additions & 0 deletions wallet_app/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />


<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down Expand Up @@ -32,6 +35,11 @@
<activity android:name=".AddTokenActivity"
android:exported="true"
android:theme="@style/Theme.Walletapp"/>

<activity android:name=".ui.activity.AccountBalanceActivity"
android:exported="true"
android:theme="@style/Theme.Walletapp"/>

<activity android:name=".AccountPasswordActivity"
android:exported="true"
android:theme="@style/Theme.Walletapp"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down Expand Up @@ -127,11 +130,29 @@ 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))
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -114,6 +115,20 @@ class WalletActivity : ComponentActivity() {
.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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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<StarknetResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.walletapp.data.model

data class StarknetResponse(
val jsonrpc: String,
val id: Int,
val result: List<String>
)

Original file line number Diff line number Diff line change
@@ -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<Any>
)
Original file line number Diff line number Diff line change
@@ -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<StarknetResponse> {
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)
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}

}
Loading
Loading